Ver Fonte

feat: 持久化stores

王家程 há 10 meses atrás
pai
commit
52fac55317

+ 3 - 1
src/models/userTypes.ts

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-05 17:14:01
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-12 17:08:38
+ * @LastEditTime: 2024-06-25 15:08:20
  * @Description:
  */
 export interface UserState {
@@ -10,6 +10,8 @@ export interface UserState {
   isInstall: boolean
   token: string
   userInfo: Employees
+  storageLoginInfo: ILogin
+  storageUserId: string
 }
 
 export interface ILogin {

+ 3 - 3
src/pages/login/index.vue

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-06 14:51:25
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-18 10:21:00
+ * @LastEditTime: 2024-06-25 15:28:12
  * @Description: 
 -->
 <template>
@@ -58,9 +58,9 @@
   const userStore = useUserStore()
   const formRef = ref(null)
   const formState = ref({
-    entCode: '',
+    entCode: '460101',
     account: '18889757900',
-    password: undefined,
+    password: '',
   })
   const isChecks = ref([])
   const rules = ref({

+ 2 - 2
src/pages/splash/index.vue

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-05 15:04:50
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-17 14:37:55
+ * @LastEditTime: 2024-06-25 15:12:29
  * @Description: 
 -->
 <template>
@@ -31,7 +31,7 @@
   defineOptions({ name: 'Splash' })
 
   const userStore = useUserStore()
-  const isInstall = computed(() => userStore.getIsInstall)
+  const isInstall = computed(() => userStore.isInstall)
   const list = ref([guide1, guide2, guide3])
 
   const onTapSwiper = (index: number) => {

+ 20 - 3
src/stores/index.ts

@@ -2,15 +2,32 @@
  * @Author: wjc
  * @Date: 2024-06-05 11:23:46
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-05 11:26:42
+ * @LastEditTime: 2024-06-25 15:02:42
  * @Description:
  */
 import type { App } from 'vue'
 import * as Pinia from 'pinia'
-import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
+import { createPersistedState } from 'pinia-plugin-persistedstate'
 
 const store = Pinia.createPinia()
-store.use(piniaPluginPersistedstate)
+store.use(createStorage())
+
+/**
+ * 使用 uniapp 的持久化 api
+ * @returns
+ */
+function createStorage() {
+  return createPersistedState({
+    storage: {
+      getItem(key: string) {
+        return uni.getStorageSync(key)
+      },
+      setItem(key: string, value: any) {
+        uni.setStorageSync(key, value)
+      },
+    },
+  })
+}
 
 export function setupStores(app: App<Element>) {
   app.use(store)

+ 11 - 16
src/stores/modules/userStore.ts

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-05 17:13:30
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-17 15:11:47
+ * @LastEditTime: 2024-06-25 15:15:56
  * @Description:
  */
 import { defineStore } from 'pinia'
@@ -17,16 +17,14 @@ export const useUserStore = defineStore('user', {
       isInstall: false,
       token: '',
       userInfo: new Employees({}),
+      storageLoginInfo: undefined,
+      storageUserId: '',
     }
   },
-  getters: {
-    getIsInstall(state) {
-      return state.isInstall || uni.getStorageSync('isInstall')
-    },
-    getIsPrivacyShowed(state) {
-      return state.isPrivacyShowed || uni.getStorageSync('isPrivacyShowed')
-    },
+  persist: {
+    paths: ['isPrivacyShowed', 'isInstall', 'storageUserId', 'storageLoginInfo'],
   },
+  getters: {},
   actions: {
     loginAction(data: ILogin): Promise<LoginRes> {
       return new Promise((resolve, reject) => {
@@ -35,10 +33,8 @@ export const useUserStore = defineStore('user', {
             if (res && res.data) {
               this.isPrivacyShowed = true
               this.isInstall = true
-              uni.setStorageSync('isPrivacyShowed', this.isPrivacyShowed)
-              uni.setStorageSync('isInstall', this.isInstall)
-              uni.setStorageSync('storage-loginInfo', data)
-              uni.setStorageSync('storage-user-id', res.data.id)
+              this.storageLoginInfo = data
+              this.storageUserId = res.data.id
               resolve(res.data)
             }
           })
@@ -52,7 +48,7 @@ export const useUserStore = defineStore('user', {
         logout()
           .then((res) => {
             if (res && res.data) {
-              uni.clearStorageSync()
+              this.$reset()
               resolve(res.data)
             }
           })
@@ -63,9 +59,8 @@ export const useUserStore = defineStore('user', {
     },
     getUserInfoAction(): Promise<Employees> {
       return new Promise((resolve, reject) => {
-        const id = uni.getStorageSync('storage-user-id')
-        if (id) {
-          getUserInfo(id)
+        if (this.storageUserId) {
+          getUserInfo(this.storageUserId)
             .then((res) => {
               if (res && res.data) {
                 this.userInfo = res.data

+ 3 - 3
src/utils/request/index.ts

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-05 10:21:23
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-12 16:24:04
+ * @LastEditTime: 2024-06-25 15:12:03
  * @Description:
  */
 import type { App } from 'vue'
@@ -18,6 +18,7 @@ export type IRequestOptions = UniApp.RequestOptions & {
 const interceptor = {
   //  请求前拦截
   invoke(options: IRequestOptions) {
+    const userStore = useUserStore()
     // api 处理
     options.url = `${import.meta.env.VITE_APP_BASE_API}${options.url}`
     // 查询参数处理
@@ -31,7 +32,7 @@ const interceptor = {
     options.timeout = 100000
     // 请求头标识符
     const sysInfo = uni.getSystemInfoSync()
-    const storageLoginInfo: ILogin = uni.getStorageSync('storage-loginInfo')
+    const storageLoginInfo: ILogin = userStore.storageLoginInfo
     options.header = {
       ...options.header,
       version: sysInfo.appVersion, // 版本号
@@ -42,7 +43,6 @@ const interceptor = {
       options.header.entcode = storageLoginInfo.entCode
     }
     // token
-    const userStore = useUserStore()
     if (userStore.token) {
       options.header.Authorization = `Bearer ${userStore.token}`
     }