daShangDao_miniProgram/service/categoryService.js
2025-11-24 10:25:20 +08:00

58 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @/service/categoryService.js
/**
* 获取图书分类信息
* @param {string} cookies - cookies认证信息
* @returns {Promise<Array>} - 返回分类列表数据
*/
export async function getCategory(cookies) {
try {
// 如果没有传入cookies尝试从本地存储获取
if (!cookies) {
cookies = uni.getStorageSync('cookies');
console.log("从本地存储获取cookies:", cookies);
}
const response = await uni.request({
url: 'https://api.buzhiyushu.cn/api/kongfz/getCategory',
method: 'GET',
data: {
token: cookies
},
header: {
'Content-Type': 'application/json'
}
});
// uni.request返回的是一个数组第二个元素是响应对象
const [_, res] = response;
if (res.statusCode === 200) {
return res.data;
} else {
throw new Error(`获取分类信息失败: ${res.statusCode}`);
}
} catch (error) {
console.error('获取图书分类信息失败:', error);
uni.showToast({
title: error.message || '获取图书分类信息失败',
icon: 'none',
duration: 2500
});
return null;
}
}
/**
* 使用示例:
* import { getCategory } from '@/service/categoryService.js';
*
* // 在组件方法中调用
* async fetchCategories() {
* const token = uni.getStorageSync('token');
* const categories = await getCategory(token);
* if (categories) {
* this.categories = categories;
* }
* }
*/