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 = {
id: number
username: string
name: string
sex: string
email: string
phone: string
avatar: string
createTime: string
roles: string[]
permissions: string[]
token: string
}
/**

View File

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

View File

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

View File

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