166 lines
4.4 KiB
JavaScript
166 lines
4.4 KiB
JavaScript
/**
|
|
* @description 用户信息数据持久化
|
|
*/
|
|
import { defineStore } from 'pinia'
|
|
import {
|
|
getUserInfoRequest,
|
|
getWebUrlRequest,
|
|
loginRequest,
|
|
phoneLoginRequest,
|
|
registerRequest
|
|
} from '@/api/user'
|
|
import { useLockStore } from '@/stores/lock'
|
|
import { setStorage, getStorage } from '@/utils/storage'
|
|
import { useNotificationStore } from '@/stores/notification'
|
|
import { useBasicStore } from '@/stores/basic'
|
|
|
|
export const useUserStore = defineStore('user', {
|
|
state() {
|
|
return {
|
|
// 用户信息
|
|
userInfo: {},
|
|
// 登录状态
|
|
isLogin: false,
|
|
webUrl: null
|
|
}
|
|
},
|
|
actions: {
|
|
updateUserInfo(data) {
|
|
this.userInfo = data
|
|
},
|
|
updateLoginStatus(status) {
|
|
this.isLogin = status
|
|
},
|
|
// 获取用户信息
|
|
async getUserInfo() {
|
|
const { code, data } = await getUserInfoRequest()
|
|
if (code === 0) {
|
|
setStorage('userInfo', data)
|
|
this.updateUserInfo(data)
|
|
this.updateLoginStatus(true)
|
|
}
|
|
return code
|
|
},
|
|
async register(params) {
|
|
const { account, password, receiverType, countryCode, verificationCode } = params
|
|
const $basic = useBasicStore()
|
|
const deviceInfo = await $basic.getDeviceInfo()
|
|
const info = {
|
|
deviceBrand: deviceInfo.deviceBrand,
|
|
deviceId: deviceInfo.deviceId,
|
|
deviceModel: deviceInfo.deviceModel,
|
|
model: deviceInfo.model,
|
|
system: deviceInfo.system
|
|
}
|
|
const { code, data, message } = await registerRequest({
|
|
receiverType,
|
|
countryCode,
|
|
account,
|
|
password,
|
|
verificationCode,
|
|
platId: 2,
|
|
deviceInfo: info
|
|
})
|
|
if (code === 0) {
|
|
setStorage('token', data.accessToken)
|
|
return true
|
|
}
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none'
|
|
})
|
|
return false
|
|
},
|
|
async passwordLogin(params) {
|
|
const { username, password, countryCode } = params
|
|
const $basic = useBasicStore()
|
|
const deviceInfo = await $basic.getDeviceInfo()
|
|
const info = {
|
|
deviceBrand: deviceInfo.deviceBrand,
|
|
deviceId: deviceInfo.deviceId,
|
|
deviceModel: deviceInfo.deviceModel,
|
|
model: deviceInfo.model,
|
|
system: deviceInfo.system
|
|
}
|
|
const { code, data, message } = await registerRequest({
|
|
username,
|
|
password,
|
|
countryCode,
|
|
loginType: 1,
|
|
platId: 2,
|
|
deviceInfo: info
|
|
})
|
|
if (code === 0) {
|
|
setStorage('token', data.accessToken)
|
|
return true
|
|
}
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none'
|
|
})
|
|
return false
|
|
},
|
|
async phoneLogin(params) {
|
|
const { iv, encryptedData, code: js_code } = params
|
|
const openid = await getStorage('openid')
|
|
if (!openid) {
|
|
return false
|
|
}
|
|
const { code, data } = await phoneLoginRequest({ iv, encryptedData, openid, js_code })
|
|
if (code === 0) {
|
|
setStorage('token', data.accessToken)
|
|
this.getUserInfo()
|
|
useLockStore().updateLockSearch({
|
|
...useLockStore().lockSearch,
|
|
pageNo: 1
|
|
})
|
|
useLockStore().getLockList(useLockStore().lockSearch)
|
|
useNotificationStore().updateNotificationSearch({
|
|
...useNotificationStore().notificationSearch,
|
|
pageNo: 1
|
|
})
|
|
useNotificationStore().getNotificationList(useNotificationStore().notificationSearch)
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
async checkSession() {
|
|
return new Promise(resolve => {
|
|
uni.checkSession({
|
|
success() {
|
|
resolve(true)
|
|
},
|
|
fail() {
|
|
uni.login({
|
|
provider: 'weixin',
|
|
async success(loginRes) {
|
|
const { code, data } = await loginRequest({
|
|
js_code: loginRes.code
|
|
})
|
|
if (code === 0) {
|
|
setStorage('openid', data.openid)
|
|
}
|
|
resolve(false)
|
|
},
|
|
fail() {
|
|
resolve(false)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
},
|
|
async getWebUrl() {
|
|
if (this.webUrl) {
|
|
return { code: 0, data: this.webUrl }
|
|
}
|
|
const { code, data, message } = await getWebUrlRequest()
|
|
if (code === 0) {
|
|
this.webUrl = data
|
|
return { code, data }
|
|
}
|
|
return { code, message }
|
|
}
|
|
}
|
|
})
|