api封装

This commit is contained in:
WindowBird 2025-11-06 09:46:14 +08:00
parent 465b68fbef
commit 10950db757
2 changed files with 48 additions and 20 deletions

View File

@ -0,0 +1,41 @@
/**
* API 接口封装
* 统一管理所有 API 请求
*/
/**
* 获取用户信息
* @returns {Promise} 返回用户信息
*/
export const getUserInfo = () => {
return uni.$uv.http.get('/getInfo', {
custom: {
auth: true // 启用 token 认证
}
});
};
/**
* 获取仪表板简要信息
* @param {Object} params 请求参数
* @param {string} params.joinUserId 用户ID
* @param {string[]} params.keys 需要获取的数据键名数组
* @returns {Promise} 返回仪表板简要信息
*/
export const getDashboardBrief = ({ joinUserId, keys }) => {
// 构建查询参数字符串
const params = [`joinUserId=${joinUserId}`];
if (keys && Array.isArray(keys)) {
keys.forEach((key) => {
params.push(`keys=${encodeURIComponent(key)}`);
});
}
const queryString = params.join('&');
return uni.$uv.http.get(`dashboard/brief?${queryString}`, {
custom: {
auth: true // 启用 token 认证
}
});
};

View File

@ -45,6 +45,7 @@
import { ref, computed, watch, onMounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import { useUserStore } from '@/store/user';
import { getUserInfo, getDashboardBrief } from '@/common/api';
import FabPlus from '@/components/FabPlus.vue';
import AddEventModal from '@/components/AddEventModal.vue';
@ -126,22 +127,17 @@ const value=ref(0);
//
onMounted(() => {
// /getInfo auth: true token
const userStore = useUserStore();
console.log('当前 token:', userStore.token);
uni.$uv.http.get('/getInfo', {
custom: {
auth: true // token
}
}).then(res => {
//
getUserInfo().then(res => {
console.log('getInfo 接口返回:', res);
}).catch(err => {
console.error('getInfo 接口请求失败:', err);
});
// dashboard/brief
//
const keys = [
'taskStatus',
'taskTodayCompleted',
@ -158,18 +154,9 @@ onMounted(() => {
'customerMonthFollowCount'
];
const params = ['joinUserId=23'];
keys.forEach((key) => {
params.push(`keys=${encodeURIComponent(key)}`);
});
const queryString = params.join('&');
uni.$uv.http.get(`dashboard/brief?${queryString}`, {
custom: {
auth: true // token
}
getDashboardBrief({
joinUserId: '23',
keys
}).then(res => {
console.log('dashboard/brief 接口返回:', res);
}).catch(err => {