From b4316befdd27909ce965e16c44057f4b1ecfe98b Mon Sep 17 00:00:00 2001 From: feige996 <1020102647@qq.com> Date: Wed, 28 May 2025 00:16:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(auth):=20=E7=A7=BB=E9=99=A4token?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E9=80=BB=E8=BE=91=E5=B9=B6=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除auth.ts及相关token管理函数 - 修改登录接口和用户信息获取接口,不再依赖token - 使用uni-app存储替代cookie存储用户信息 - 重构微信登录流程,简化参数传递 - 更新用户头像默认路径为新增的default-avatar.png - 在个人中心页面增加登录状态判断和登录按钮 ``` 这个提交消息遵循了以下原则: 1. 使用refactor类型,因为这是对现有代码结构的重构 2. 添加了scope(auth)来明确这是认证相关的重构 3. 描述简明扼要地说明了主要变更 4. 在body中列出了主要变更点,没有重复描述 5. 使用中文并保持简洁,每个变更点用短句说明 6. 使用动词开头并保持一致的格式 --- src/api/login.ts | 11 ++-- src/pages/login/index.vue | 6 +- src/pages/mine/index.vue | 30 ++++++++-- src/static/images/default-avatar.png | Bin 0 -> 560 bytes src/store/user.ts | 26 ++++++--- src/utils/auth.ts | 81 --------------------------- src/utils/uploadFile.ts | 2 - 7 files changed, 49 insertions(+), 107 deletions(-) create mode 100644 src/static/images/default-avatar.png delete mode 100644 src/utils/auth.ts diff --git a/src/api/login.ts b/src/api/login.ts index 9a53a35..effdd94 100644 --- a/src/api/login.ts +++ b/src/api/login.ts @@ -30,8 +30,8 @@ export const login = (loginForm: ILoginForm) => { /** * 获取用户信息 */ -export const getUserInfo = (token: string) => { - return http.get('/user/info', { token }) +export const getUserInfo = () => { + return http.get('/user/info') } /** @@ -72,15 +72,12 @@ export const getWxCode = () => { /** * 微信登录参数 */ -export interface IWxLoginParams { - code: string -} /** * 微信登录 * @param params 微信登录参数,包含code * @returns Promise 包含登录结果 */ -export const wxLogin = (params: IWxLoginParams) => { - return http.post('/app/wx/login', {}, params) +export const wxLogin = (data: { code: string }) => { + return http.post('/user/wxLogin', data) } diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index 2f6436e..7610f20 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -127,7 +127,7 @@ import { ref } from 'vue' import { useUserStore } from '@/store/user' import { isMpWeixin } from '@/utils/platform' -import { getCode, getWxCode, ILoginForm } from '@/api/login' +import { getCode, ILoginForm } from '@/api/login' import { toast } from '@/utils/toast' import { isTableBar } from '@/utils/index' import { ICaptcha } from '@/api/login.typings' @@ -210,10 +210,8 @@ const handleWechatLogin = async () => { toast.error('请先阅读并同意用户协议和隐私政策') return } - // 获取微信小程序登录的code - const { code } = await getWxCode() // 微信登录 - await userStore.wxLogin({ code }) + await userStore.wxLogin() // 获取用户信息 await userStore.getUserInfo() // 跳转到首页或重定向页面 diff --git a/src/pages/mine/index.vue b/src/pages/mine/index.vue index f3e8084..881cb10 100644 --- a/src/pages/mine/index.vue +++ b/src/pages/mine/index.vue @@ -77,7 +77,8 @@ - 退出登录 + 退出登录 + 登录 @@ -90,10 +91,16 @@ import { uploadFileUrl, useUpload } from '@/utils/uploadFile' import { storeToRefs } from 'pinia' import { IUploadSuccessInfo } from '@/api/login.typings' +const userStore = useUserStore() + const toast = useToast() +const hasLogin = ref(false) + onShow((options) => { + hasLogin.value = !!uni.getStorageSync('token') console.log('个人中心onShow', options) - useUserStore().getUserInfo() + + hasLogin.value && useUserStore().getUserInfo() }) // #ifndef MP-WEIXIN // 上传头像 @@ -106,7 +113,21 @@ const { run } = useUpload( ) // #endif +// 微信小程序下登录 +const handleLogin = async () => { + // #ifdef MP-WEIXIN + + // 微信登录 + await userStore.wxLogin() + hasLogin.value = true + // #endif + // #ifndef MP-WEIXIN + uni.navigateTo({ url: '/pages/login/index' }) + // #endif +} + // #ifdef MP-WEIXIN + // 微信小程序下选择头像事件 const onChooseAvatar = (e: any) => { console.log('选择头像', e.detail) @@ -215,15 +236,16 @@ const handleLogout = () => { if (res.confirm) { // 清空用户信息 useUserStore().logout() + hasLogin.value = false // 执行退出登录逻辑 toast.success('退出登录成功') // #ifdef MP-WEIXIN // 微信小程序,去首页 - uni.reLaunch({ url: '/pages/index/index' }) + // uni.reLaunch({ url: '/pages/index/index' }) // #endif // #ifndef MP-WEIXIN // 非微信小程序,去登录页 - uni.reLaunch({ url: '/pages/login/index' }) + // uni.reLaunch({ url: '/pages/login/index' }) // #endif } }, diff --git a/src/static/images/default-avatar.png b/src/static/images/default-avatar.png new file mode 100644 index 0000000000000000000000000000000000000000..4eb587954368fc74189a34a26a96e356f0433429 GIT binary patch literal 560 zcmV-00?+-4P)*wd_wY9afva;LT-0tr1&CSm1>+QO_ zy5Qj9x3{=Gp>E`egj zB-!E$SHTP*L>1CoxM-Ugz(ZHGBg6Lz6ZRUkH!@e-47=O$R{}jTF1=$8Z(!84=r!WO zVJ<%8@h;nPuB*Ue=7Sc?zaYNO#qj0$K{FROdAxy*@svU9g8Neqf5{h5K|J>S(v1{l z1di3rJ>Z^cOuW_2(i7|r@S;oqOnXoD-lJx3)@Ucpo~xT{*J>x)-BSHLx${)-6XoVL z?L@Ofyl<~-CsIFJjoZAR^(5(KsntL85|d)y80Yk8Z}_jiN$Jw7s#Odx;{PDlDdLnX yP?aK1xlO1_`;F)-*O)H|f*=TjAP9mW?9K;~E^CkQH+8iD0000 { userInfo.value = { ...userInfoState } - removeToken() + uni.removeStorageSync('userInfo') + uni.removeStorageSync('token') } /** * 用户登录 @@ -59,14 +60,16 @@ export const useUserStore = defineStore( const res = await _login(credentials) console.log('登录信息', res) toast.success('登录成功') - setToken(res.data.token) + const userInfo = res.data + uni.setStorageSync('userInfo', userInfo) + uni.setStorageSync('token', userInfo.token) return res } /** * 获取用户信息 */ const getUserInfo = async () => { - const res = await _getUserInfo(getToken()) + const res = await _getUserInfo() setUserInfo(res.data) // TODO 这里可以增加获取用户路由的方法 根据用户的角色动态生成路由 return res @@ -80,11 +83,16 @@ export const useUserStore = defineStore( } /** * 微信登录 - * @param credentials 微信登录Code */ - const wxLogin = async (credentials: { code: string }) => { - const res = await _wxLogin(credentials) - setToken(res.data.token) + const wxLogin = async () => { + // 获取微信小程序登录的code + const data = await getWxCode() + console.log('微信登录code', data) + + const res = await _wxLogin(data) + const userInfo = res.data + uni.setStorageSync('userInfo', userInfo) + uni.setStorageSync('token', userInfo.token) return res } diff --git a/src/utils/auth.ts b/src/utils/auth.ts deleted file mode 100644 index 3d071e4..0000000 --- a/src/utils/auth.ts +++ /dev/null @@ -1,81 +0,0 @@ -import Cookie from 'js-cookie' -import { isMpWeixin } from './platform' -/** - * TokeKey的名字 - */ -const TokenKey: string = 'token' - -/** - * 获取tokenKeyName - * @returns tokenKeyName - */ -export const getTokenKey = (): string => { - return TokenKey -} - -/** - * 是否登录,即是否有token,不检查Token是否过期和是否有效 - * @returns 是否登录 - */ -export const isLogin = () => { - return !!getToken() -} - -/** - * 获取Token - * @returns 令牌 - */ -export const getToken = () => { - return getCookieMap(getTokenKey()) -} - -/** - * 设置Token - * @param token 令牌 - */ -export const setToken = (token: string) => { - setCookieMap(getTokenKey(), token) -} -/** - * 删除Token - */ -export const removeToken = () => { - removeCookieMap(getTokenKey()) -} - -/** - * 设置Cookie - * @param key Cookie的key - * @param value Cookie的value - */ -export const setCookieMap = (key: string, value: any) => { - if (isMpWeixin) { - uni.setStorageSync(key, value) - return - } - Cookie.set(key, value) -} - -/** - * 获取Cookie - * @param key Cookie的key - * @returns Cookie的value - */ -export const getCookieMap = (key: string) => { - if (isMpWeixin) { - return uni.getStorageSync(key) as T - } - return Cookie.get(key) as T -} - -/** - * 删除Cookie - * @param key Cookie的key - */ -export const removeCookieMap = (key: string) => { - if (isMpWeixin) { - uni.removeStorageSync(key) - return - } - Cookie.remove(key) -} diff --git a/src/utils/uploadFile.ts b/src/utils/uploadFile.ts index 594f24c..b415a08 100644 --- a/src/utils/uploadFile.ts +++ b/src/utils/uploadFile.ts @@ -1,4 +1,3 @@ -import { getToken, getTokenKey } from './auth' import { toast } from './toast' /** @@ -286,7 +285,6 @@ function uploadFile({ // #ifndef H5 'Content-Type': 'multipart/form-data', // #endif - [getTokenKey()]: getToken(), // 添加认证token }, // 确保文件名称合法 success: (uploadFileRes) => {