import { request, setToken, clearToken, clearAllCache, clearCache } from '@/utils/request.js' // ========== 登录相关API ========== /** * 微信小程序登录 * @param {object} data - 登录参数 * @param {string} data.code - 微信登录code * @param {string} [data.encryptedData] - 加密数据 * @param {string} [data.iv] - 加密向量 * @returns {Promise} 登录结果 */ export const login = (data) => { return request({ url: '/member/auth/miniapp/login', method: 'POST', data: data, needToken: false // 登录请求不需要token }).then(res => { // 登录成功,保存token if (res.data && res.data.token) { setToken(res.data.token) } return res }) } /** * 退出登录 * @returns {Promise} 退出结果 */ export const logout = () => { return request({ url: '/member/auth/logout', method: 'POST' }).then(res => { // 退出成功,清除token和缓存 clearToken() clearAllCache() return res }).catch(err => { // 即使请求失败,也清除本地token clearToken() clearAllCache() throw err }) } // ========== 签到相关API ========== /** * 获取签到二维码 * @param {boolean} [cache=true] - 是否启用缓存 * @returns {Promise} 二维码数据 */ export const getQRCode = (cache = true) => { return request({ url: '/checkIn/qrcode', method: 'GET', cache: cache, cacheTime: 5 * 60 * 1000 // 5分钟缓存 }) } /** * 扫码签到 * @param {string} qrContent - 二维码内容 * @returns {Promise} 签到结果 */ export const checkIn = (qrContent) => { return request({ url: '/checkIn/scan', method: 'POST', data: { qrContent } }) } // ========== 用户相关API ========== /** * 获取用户信息 * @param {boolean} [cache=true] - 是否启用缓存 * @returns {Promise} 用户信息 */ export const getUserInfo = (cache = true) => { return request({ url: '/member/info', method: 'GET', cache: cache, cacheTime: 30 * 60 * 1000 // 30分钟缓存 }) } /** * 更新用户信息 * @param {object} data - 用户信息 * @returns {Promise} 更新结果 */ export const updateUserInfo = (data) => { return request({ url: '/member/info', method: 'PUT', data: data }).then(res => { // 更新成功,清除用户信息缓存 const cacheKey = `GET_/member/info_{}` clearCache(cacheKey) return res }) } // ========== 课程相关API ========== /** * 获取推荐课程列表 * @param {boolean} [cache=true] - 是否启用缓存 * @returns {Promise} 课程列表 */ export const getRecommendCourses = (cache = true) => { return request({ url: '/course/recommend', method: 'GET', cache: cache, cacheTime: 10 * 60 * 1000 // 10分钟缓存 }) } /** * 获取课程详情 * @param {number} id - 课程ID * @param {boolean} [cache=true] - 是否启用缓存 * @returns {Promise} 课程详情 */ export const getCourseDetail = (id, cache = true) => { return request({ url: `/course/${id}`, method: 'GET', cache: cache, cacheTime: 15 * 60 * 1000 // 15分钟缓存 }) } export default { login, logout, getQRCode, checkIn, getUserInfo, updateUserInfo, getRecommendCourses, getCourseDetail }