feat(uniapp): 集成 Pinia 状态管理并实现用户认证流程

This commit is contained in:
augushong
2026-02-01 12:01:37 +08:00
parent ae6b3f1b67
commit 1a88ff286e
14 changed files with 242 additions and 5228 deletions

View File

@@ -0,0 +1,4 @@
import { createPinia } from 'pinia'
export const pinia = createPinia()

View File

@@ -0,0 +1,29 @@
import { defineStore } from 'pinia'
const TOKEN_KEY = 'ULTHON_ADMIN_TOKEN'
export const useUserStore = defineStore('user', {
state: () => ({
token: '',
inited: false,
}),
getters: {
isLoggedIn: (state) => !!state.token,
},
actions: {
init() {
if (this.inited) return
this.token = uni.getStorageSync(TOKEN_KEY) || ''
this.inited = true
},
setToken(token) {
this.token = token || ''
uni.setStorageSync(TOKEN_KEY, this.token)
},
logout() {
this.token = ''
uni.removeStorageSync(TOKEN_KEY)
},
},
})