feat(登录): 登录优化

This commit is contained in:
feige996 2025-05-28 00:33:41 +08:00
parent b4316befdd
commit cc56472da6
4 changed files with 16 additions and 34 deletions

View File

@ -4,14 +4,8 @@
export type IUserInfoVo = { export type IUserInfoVo = {
id: number id: number
username: string username: string
name: string
sex: string
email: string
phone: string
avatar: string avatar: string
createTime: string token: string
roles: string[]
permissions: string[]
} }
/** /**

View File

@ -187,8 +187,6 @@ const handleAccountLogin = async () => {
} }
// //
await userStore.login(loginForm.value) await userStore.login(loginForm.value)
//
await userStore.getUserInfo()
// //
const targetUrl = redirectRoute.value || '/pages/index/index' const targetUrl = redirectRoute.value || '/pages/index/index'
if (isTableBar(targetUrl)) { if (isTableBar(targetUrl)) {
@ -212,8 +210,6 @@ const handleWechatLogin = async () => {
} }
// //
await userStore.wxLogin() await userStore.wxLogin()
//
await userStore.getUserInfo()
// //
const targetUrl = redirectRoute.value || '/pages/index/index' const targetUrl = redirectRoute.value || '/pages/index/index'
if (isTableBar(targetUrl)) { if (isTableBar(targetUrl)) {

View File

@ -8,16 +8,17 @@
<template> <template>
<view class="profile-container"> <view class="profile-container">
{{ JSON.stringify(userStore.userInfo) }}
<!-- 用户信息区域 --> <!-- 用户信息区域 -->
<view class="user-info-section"> <view class="user-info-section">
<!-- #ifdef MP-WEIXIN --> <!-- #ifdef MP-WEIXIN -->
<button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"> <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
<wd-img :src="userInfo.avatar" width="80px" height="80px" radius="50%"></wd-img> <wd-img :src="userStore.userInfo.avatar" width="80px" height="80px" radius="50%"></wd-img>
</button> </button>
<!-- #endif --> <!-- #endif -->
<!-- #ifndef MP-WEIXIN --> <!-- #ifndef MP-WEIXIN -->
<view class="avatar-wrapper" @click="run"> <view class="avatar-wrapper" @click="run">
<wd-img :src="userInfo.avatar" width="100%" height="100%" radius="50%"></wd-img> <wd-img :src="userStore.userInfo.avatar" width="100%" height="100%" radius="50%"></wd-img>
</view> </view>
<!-- #endif --> <!-- #endif -->
<view class="user-details"> <view class="user-details">
@ -26,13 +27,13 @@
type="nickname" type="nickname"
class="weui-input" class="weui-input"
placeholder="请输入昵称" placeholder="请输入昵称"
v-model="userInfo.username" v-model="userStore.userInfo.username"
/> />
<!-- #endif --> <!-- #endif -->
<!-- #ifndef MP-WEIXIN --> <!-- #ifndef MP-WEIXIN -->
<view class="username">{{ userInfo.username }}</view> <view class="username">{{ userStore.userInfo.username }}</view>
<!-- #endif --> <!-- #endif -->
<view class="user-id">ID: {{ userInfo.id }}</view> <view class="user-id">ID: {{ userStore.userInfo.id }}</view>
</view> </view>
</view> </view>
@ -98,7 +99,7 @@ const hasLogin = ref(false)
onShow((options) => { onShow((options) => {
hasLogin.value = !!uni.getStorageSync('token') hasLogin.value = !!uni.getStorageSync('token')
console.log('个人中心onShow', options) console.log('个人中心onShow', hasLogin.value, options)
hasLogin.value && useUserStore().getUserInfo() hasLogin.value && useUserStore().getUserInfo()
}) })
@ -150,8 +151,6 @@ const getUserInfo = (e: any) => {
} }
// #endif // #endif
//
const { userInfo } = storeToRefs(useUserStore())
// //
const handleProfileInfo = () => { const handleProfileInfo = () => {
uni.navigateTo({ url: `/pages/mine/info/index` }) uni.navigateTo({ url: `/pages/mine/info/index` })

View File

@ -8,20 +8,14 @@ import {
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref } from 'vue' import { ref } from 'vue'
import { toast } from '@/utils/toast' import { toast } from '@/utils/toast'
import { IUserInfoVo, IUserLogin } from '@/api/login.typings' import { IUserInfoVo } from '@/api/login.typings'
// 初始化状态 // 初始化状态
const userInfoState: IUserInfoVo = { const userInfoState: IUserInfoVo = {
id: 0, id: 0,
username: '', username: '',
name: '',
sex: '',
email: '',
phone: '',
avatar: '/static/images/default-avatar.png', avatar: '/static/images/default-avatar.png',
createTime: '', token: '',
roles: [],
permissions: [],
} }
export const useUserStore = defineStore( export const useUserStore = defineStore(
@ -60,9 +54,7 @@ export const useUserStore = defineStore(
const res = await _login(credentials) const res = await _login(credentials)
console.log('登录信息', res) console.log('登录信息', res)
toast.success('登录成功') toast.success('登录成功')
const userInfo = res.data getUserInfo()
uni.setStorageSync('userInfo', userInfo)
uni.setStorageSync('token', userInfo.token)
return res return res
} }
/** /**
@ -70,7 +62,10 @@ export const useUserStore = defineStore(
*/ */
const getUserInfo = async () => { const getUserInfo = async () => {
const res = await _getUserInfo() const res = await _getUserInfo()
setUserInfo(res.data) const userInfo = res.data
setUserInfo(userInfo)
uni.setStorageSync('userInfo', userInfo)
uni.setStorageSync('token', userInfo.token)
// TODO 这里可以增加获取用户路由的方法 根据用户的角色动态生成路由 // TODO 这里可以增加获取用户路由的方法 根据用户的角色动态生成路由
return res return res
} }
@ -90,9 +85,7 @@ export const useUserStore = defineStore(
console.log('微信登录code', data) console.log('微信登录code', data)
const res = await _wxLogin(data) const res = await _wxLogin(data)
const userInfo = res.data getUserInfo()
uni.setStorageSync('userInfo', userInfo)
uni.setStorageSync('token', userInfo.token)
return res return res
} }