This commit is contained in:
那些花儿
2025-04-30 13:34:37 +08:00
parent ad9a7bc7b8
commit baa6f7284a
12 changed files with 567 additions and 284 deletions
+86
View File
@@ -0,0 +1,86 @@
import {defineStore} from "pinia";
import {useRequest} from "@/utils/request";
const request = useRequest()
export const useUserStore = defineStore("user", {
state: () => ({
userInfo: uni.getStorageSync('userInfo') || null,
token: uni.getStorageSync('token')
}),
actions: {
setToken(token) {
this.token = token
uni.setStorageSync('token', token)
},
setUserInfo(userInfo) {
this.userInfo = userInfo
uni.setStorageSync('userInfo', userInfo)
},
setUserInfoKV(key, value) {
this.userInfo[key] = value
},
async smsLogin(loginForm) {
try {
const {data, code, msg} = await request.post('/client/auth/sms-login', loginForm)
if (code === 0) {
this.setToken(data)
} else {
throw new Error(msg)
}
} catch (e) {
console.log(e)
throw e
}
},
async getUserInfo() {
try {
const {data, code, msg} = await request.get('/client/auth/user-info')
if (code === 0) {
this.setUserInfo(data)
} else {
throw new Error(msg)
}
} catch (e) {
console.log(e)
throw e
}
},
async updateUserInfo(userInfo) {
try {
const {data, code, msg} = await request.post('/client/auth/update-user-info', userInfo)
if (code === 0) {
this.setUserInfo(data)
} else {
throw new Error(msg)
}
} catch (e) {
console.log(e)
throw e
}
},
async logout() {
try {
const {code, msg} = await request.post('/client/auth/logout')
if (code === 0) {
this.setToken(null)
this.setUserInfo(null)
await uni.reLaunch({
url: '/pages/login/index'
});
} else {
throw new Error(msg)
}
} catch (e) {
console.log(e)
throw e
}
}
}
})