范鹏 5129d3624a 1. 修复全局列表刷新bug
2. 所有loading添加蒙层不可点击穿透
3. 密码和电子钥匙详情页添加删除功能
2024-08-29 14:42:16 +08:00

78 lines
2.0 KiB
JavaScript

/**
* @description 用户信息数据持久化
*/
import { defineStore } from 'pinia'
import { getUserInfoRequest, loginRequest, phoneLoginRequest } from '@/api/user'
import { useLockStore } from '@/stores/lock'
export const useUserStore = defineStore('user', {
state() {
return {
// 用户信息
userInfo: {},
// 登录状态
isLogin: false
}
},
actions: {
updateUserInfo(data) {
this.userInfo = data
},
updateLoginStatus(status) {
this.isLogin = status
},
// 获取用户信息
async getUserInfo() {
const { code, data } = await getUserInfoRequest()
if(code === 0) {
this.updateUserInfo(data)
this.updateLoginStatus(true)
}
return code
},
async phoneLogin(params) {
const { iv, encryptedData } = params
const openid = uni.getStorageSync('openid')
const { code, data, message } = await phoneLoginRequest({ iv, encryptedData, openid })
if(code === 0) {
uni.setStorageSync('token', data.accessToken)
this.getUserInfo()
useLockStore().updateLockSearch({
...useLockStore().lockSearch,
pageNo: 1
})
useLockStore().getLockList(useLockStore().lockSearch)
return true
} else {
return false
}
},
async checkSession() {
return new Promise((resolve) => {
uni.checkSession({
success() {
resolve(true)
},
fail() {
uni.login({
provider: 'weixin',
success: async function (loginRes) {
const { code, data } = await loginRequest({
js_code: loginRes.code
})
if (code === 0) {
uni.setStorageSync('openid', data.openid)
}
resolve(false)
},
fail() {
resolve(false)
}
})
}
})
})
}
}
})