diff --git a/common/server/interface/user.js b/common/server/interface/user.js index fd1cde8..fcdc80a 100644 --- a/common/server/interface/user.js +++ b/common/server/interface/user.js @@ -27,6 +27,18 @@ export const ueWxPhoneNumberLogin = async (code, sessionAuthId) => { return null; } +/** + * 匿名登录 + * @returns + */ +export const anonymousLogin = async () => { + const res = await request.post("user/AnonymousLogin"); + if (res.code == 0) { + return res.data; + } + return null; +} + /** * 微信小程序匿名授权登录,手机号已授权过 * @param {*} code diff --git a/common/server/user.js b/common/server/user.js index 1aeafbb..d678335 100644 --- a/common/server/user.js +++ b/common/server/user.js @@ -1,25 +1,153 @@ -import { getUserInfo, editUserInfo } from '@/common/server/interface/user' +// 导入用户相关的接口函数 +import { getUserInfo, editUserInfo, anonymousLogin as anonymousLoginInterface } from '@/common/server/interface/user' import { ref } from 'vue' +import throttle from 'lodash/throttle'; -export var userInfo = ref(null); +// 用户信息响应式数据 +export const userInfo = ref(null); +// 创建节流函数,防止频繁调用用户信息加载接口(2秒内只执行一次) +const throttledLoadUserInfo = throttle(_loadUserInfo, 2000, { leading: true, trailing: false }); + +/** + * 清除用户相关存储 + */ +const clearUserStorage = () => { + userInfo.value = null; + uni.removeStorageSync('tokenInfo'); + uni.removeStorageSync('userInfo'); +}; + +/** + * 检查token是否有效 + * @param {string} token - token字符串 + * @returns {boolean} token是否有效 + */ +const isValidToken = (token) => { + return token && token !== null && token !== ''; +}; + +/** + * 加载用户信息(外部调用接口) + * 使用节流函数防止频繁调用 + */ export const loadUserInfo = async () => { - const res = await getUserInfo(); - if (res == null) { + console.log('loadUserInfo'); + throttledLoadUserInfo(); +}; - userInfo.value = null; - uni.removeStorageSync('tokenInfo'); - uni.removeStorageSync('userInfo'); - return; +/** + * 内部用户信息加载函数 + * 从服务器获取用户信息并更新本地状态 + */ +async function _loadUserInfo() { + console.log("_loadUserInfo"); + + try { + // 调用接口获取用户信息 + const res = await getUserInfo(); + + // 如果获取失败,清空用户信息和本地存储 + if (res == null) { + clearUserStorage(); + return; + } + + // 更新用户信息 + userInfo.value = res; + } catch (error) { + console.error('加载用户信息失败:', error); + clearUserStorage(); } - userInfo.value = res; - uni.setStorageSync('userInfo', res); } +/** + * 更新用户信息 + * @param {string} nickName - 昵称 + * @param {string} avatar - 头像 + * @param {string} sex - 性别 + * @param {string} birthday - 生日 + */ export const updateUserInfo = async (nickName, avatar, sex, birthday) => { - const res = await editUserInfo(nickName, avatar, sex, birthday); - if (!res) { + try { + // 调用接口更新用户信息 + const res = await editUserInfo(nickName, avatar, sex, birthday); + + // 如果更新失败,直接返回 + if (!res) { + return; + } + + // 更新成功后重新加载用户信息 + loadUserInfo(); + } catch (error) { + console.error('更新用户信息失败:', error); + } +}; + +/** + * 检查用户是否已登录 + * @returns {Promise} 是否已登录 + */ +export const isLogin = async () => { + // 如果用户信息已存在,直接返回true + if (userInfo.value !== null) { + return true; + } + + // 检查本地token + const tokenInfo = uni.getStorageSync('tokenInfo'); + + if (!isValidToken(tokenInfo)) { + return false; + } + + try { + // 尝试加载用户信息 + await _loadUserInfo(); + + // 如果加载后用户信息仍为空,清除存储并返回false + if (userInfo.value === null) { + clearUserStorage(); + return false; + } + + return true; + } catch (error) { + console.error('检查登录状态失败:', error); + clearUserStorage(); + return false; + } +}; + +/** + * 匿名登录 + * 检查本地是否有token,如果有则进行匿名登录 + */ +export const anonymousLogin = async () => { + // 从本地存储获取token信息 + const tokenInfo = uni.getStorageSync('tokenInfo'); + + // 如果没有token,直接返回 + if (!isValidToken(tokenInfo)) { return; } - loadUserInfo(); -} \ No newline at end of file + + try { + // 调用匿名登录接口 + const res = await anonymousLoginInterface(); + + if (res) { + // 登录成功,保存新的token并加载用户信息 + uni.setStorageSync("tokenInfo", res); + console.log("匿名登录成功", res); + loadUserInfo(); + } else { + // 登录失败,清除本地存储的token和用户信息 + clearUserStorage(); + } + } catch (error) { + console.error('匿名登录失败:', error); + clearUserStorage(); + } +}; \ No newline at end of file diff --git a/common/system/system.js b/common/system/system.js index 5b9ed52..c41173d 100644 --- a/common/system/system.js +++ b/common/system/system.js @@ -1,6 +1,7 @@ // import { getAdvertList } from "@/common/server/interface/advert"; import { preloadHomeData } from '@/common/server/index' import { preloadConfigData } from '@/common/server/config' +import { anonymousLogin } from '@/common/server/user' /** * 应用初始化函数,带有超时控制 * @param {number} timeoutMs - 超时时间(毫秒) @@ -11,6 +12,7 @@ export const appInit = async (timeoutMs = 5000) => { //获取首页关键数据 const homeTask = preloadHomeData(); const configTask = preloadConfigData(); + const anonymousTask = anonymousLogin(); // 2. 创建一个会在指定时间后拒绝的Promise用于超时控制 const timeoutPromise = new Promise((_, reject) => setTimeout(() => @@ -23,7 +25,7 @@ export const appInit = async (timeoutMs = 5000) => { // 哪个先完成(成功或失败)就采用哪个的结果 await Promise.race([ // 使用Promise.all包装是为了保持一致性 - Promise.all([homeTask,configTask]), + Promise.all([homeTask, configTask, anonymousTask]), timeoutPromise ]); // 4. 如果没有超时全部获取成功,返回true diff --git a/package.json b/package.json index c37e778..2d410a8 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "dependencies": { - "uview-plus": "^3.5.22", "crypto-js": "^4.2.0", "js-md5": "^0.8.3", "lodash": "^4.17.21", - "qs": "^6.14.0" + "qs": "^6.14.0", + "uview-plus": "^3.5.22" }, "devDependencies": { "sass": "^1.63.2", "unplugin-auto-import": "^19.3.0", "unplugin-vue-components": "^28.7.0" } -} \ No newline at end of file +}