范鹏 2fc7ad7ed7 1. 完成手机验证码修改账号密码功能
2. 完成更换账号绑定手机号功能
2024-08-27 10:12:56 +08:00

77 lines
1.9 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().getLockList({
pageNo: 1,
pageSize: 50
})
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)
}
})
}
})
})
}
}
})