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 0000000..4eb5879 Binary files /dev/null and b/src/static/images/default-avatar.png differ diff --git a/src/store/user.ts b/src/store/user.ts index a0c606c..1a0630d 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -3,8 +3,8 @@ import { getUserInfo as _getUserInfo, wxLogin as _wxLogin, logout as _logout, + getWxCode, } from '@/api/login' -import { getToken, getTokenKey, removeToken, setToken } from '@/utils/auth' import { defineStore } from 'pinia' import { ref } from 'vue' import { toast } from '@/utils/toast' @@ -18,7 +18,7 @@ const userInfoState: IUserInfoVo = { sex: '', email: '', phone: '', - avatar: '/static/images/avatar.jpg', + avatar: '/static/images/default-avatar.png', createTime: '', roles: [], permissions: [], @@ -43,7 +43,8 @@ export const useUserStore = defineStore( // 删除用户信息 const removeUserInfo = () => { 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) => {