diff --git a/common/api.js b/common/api.js index e69de29..16bd5bc 100644 --- a/common/api.js +++ b/common/api.js @@ -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 认证 + } + }); +}; + diff --git a/pages/index/index.vue b/pages/index/index.vue index 5fba5fc..07a8a3b 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -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 => {