2024-08-06 10:20:56 +08:00
|
|
|
/**
|
|
|
|
|
* @description 用户信息数据持久化
|
|
|
|
|
*/
|
|
|
|
|
import { defineStore } from 'pinia'
|
2024-08-26 17:33:53 +08:00
|
|
|
import { getUserInfoRequest, loginRequest, phoneLoginRequest } from '@/api/user'
|
2024-08-22 15:03:30 +08:00
|
|
|
import { useLockStore } from '@/stores/lock'
|
2024-09-19 11:50:23 +08:00
|
|
|
import { setStorage, getStorage } from '@/utils/storage'
|
2024-10-07 14:16:26 +08:00
|
|
|
import { useNotificationStore } from '@/stores/notification'
|
2024-08-06 10:20:56 +08:00
|
|
|
|
|
|
|
|
export const useUserStore = defineStore('user', {
|
|
|
|
|
state() {
|
|
|
|
|
return {
|
2024-08-21 16:20:11 +08:00
|
|
|
// 用户信息
|
|
|
|
|
userInfo: {},
|
|
|
|
|
// 登录状态
|
|
|
|
|
isLogin: false
|
2024-08-06 10:20:56 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
updateUserInfo(data) {
|
|
|
|
|
this.userInfo = data
|
2024-08-21 16:20:11 +08:00
|
|
|
},
|
|
|
|
|
updateLoginStatus(status) {
|
|
|
|
|
this.isLogin = status
|
|
|
|
|
},
|
2024-08-27 10:12:56 +08:00
|
|
|
// 获取用户信息
|
|
|
|
|
async getUserInfo() {
|
|
|
|
|
const { code, data } = await getUserInfoRequest()
|
2025-02-06 11:37:41 +08:00
|
|
|
if (code === 0) {
|
2024-09-19 11:50:23 +08:00
|
|
|
setStorage('userInfo', data)
|
2024-08-27 10:12:56 +08:00
|
|
|
this.updateUserInfo(data)
|
|
|
|
|
this.updateLoginStatus(true)
|
|
|
|
|
}
|
|
|
|
|
return code
|
|
|
|
|
},
|
|
|
|
|
async phoneLogin(params) {
|
|
|
|
|
const { iv, encryptedData } = params
|
2024-10-29 13:40:19 +08:00
|
|
|
const openid = await getStorage('openid')
|
2025-02-06 11:37:41 +08:00
|
|
|
if (!openid) {
|
2024-10-29 13:40:19 +08:00
|
|
|
return false
|
|
|
|
|
}
|
2024-10-07 14:16:26 +08:00
|
|
|
const { code, data } = await phoneLoginRequest({ iv, encryptedData, openid })
|
2025-02-06 11:37:41 +08:00
|
|
|
if (code === 0) {
|
2024-09-19 11:50:23 +08:00
|
|
|
setStorage('token', data.accessToken)
|
2024-08-27 10:12:56 +08:00
|
|
|
this.getUserInfo()
|
2024-08-29 14:42:16 +08:00
|
|
|
useLockStore().updateLockSearch({
|
|
|
|
|
...useLockStore().lockSearch,
|
|
|
|
|
pageNo: 1
|
2024-08-27 10:12:56 +08:00
|
|
|
})
|
2024-08-29 14:42:16 +08:00
|
|
|
useLockStore().getLockList(useLockStore().lockSearch)
|
2024-10-07 14:16:26 +08:00
|
|
|
useNotificationStore().updateNotificationSearch({
|
|
|
|
|
...useNotificationStore().notificationSearch,
|
|
|
|
|
pageNo: 1
|
|
|
|
|
})
|
|
|
|
|
useNotificationStore().getNotificationList(useNotificationStore().notificationSearch)
|
2024-08-27 10:12:56 +08:00
|
|
|
return true
|
|
|
|
|
}
|
2025-02-06 11:37:41 +08:00
|
|
|
return false
|
2024-08-27 10:12:56 +08:00
|
|
|
},
|
|
|
|
|
async checkSession() {
|
2025-02-06 11:37:41 +08:00
|
|
|
return new Promise(resolve => {
|
2024-08-26 17:33:53 +08:00
|
|
|
uni.checkSession({
|
2024-08-27 10:12:56 +08:00
|
|
|
success() {
|
|
|
|
|
resolve(true)
|
2024-08-26 17:33:53 +08:00
|
|
|
},
|
|
|
|
|
fail() {
|
|
|
|
|
uni.login({
|
|
|
|
|
provider: 'weixin',
|
2025-02-06 11:37:41 +08:00
|
|
|
async success(loginRes) {
|
2024-08-26 17:33:53 +08:00
|
|
|
const { code, data } = await loginRequest({
|
|
|
|
|
js_code: loginRes.code
|
|
|
|
|
})
|
|
|
|
|
if (code === 0) {
|
2024-09-19 11:50:23 +08:00
|
|
|
setStorage('openid', data.openid)
|
2024-08-26 17:33:53 +08:00
|
|
|
}
|
|
|
|
|
resolve(false)
|
|
|
|
|
},
|
|
|
|
|
fail() {
|
|
|
|
|
resolve(false)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-08-22 15:03:30 +08:00
|
|
|
})
|
2024-08-06 10:20:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|