Parcourir la source

feat(登录): 登录优化

feige996 il y a 11 mois
Parent
commit
cc56472da6
4 fichiers modifiés avec 16 ajouts et 34 suppressions
  1. 1 7
      src/api/login.typings.ts
  2. 0 4
      src/pages/login/index.vue
  3. 7 8
      src/pages/mine/index.vue
  4. 8 15
      src/store/user.ts

+ 1 - 7
src/api/login.typings.ts

@@ -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
 }
 
 /**

+ 0 - 4
src/pages/login/index.vue

@@ -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)) {

+ 7 - 8
src/pages/mine/index.vue

@@ -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` })

+ 8 - 15
src/store/user.ts

@@ -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
     }