58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
// @/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;
|
||
* }
|
||
* }
|
||
*/ |