128 lines
3.4 KiB
JavaScript
128 lines
3.4 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import userApi from '@/nx/api/user'
|
|
import { getSysParameterBizByName } from '@/nx/api/sys'
|
|
import md5 from 'md5'
|
|
import nx from '@/nx'
|
|
// 默认用户信息
|
|
const defaultUserInfo = {
|
|
avatar: '', // 头像
|
|
nickname: '', // 昵称
|
|
gender: 0, // 性别
|
|
mobile: '' // 手机号
|
|
}
|
|
|
|
const user = defineStore({
|
|
id: 'user',
|
|
state: () => ({
|
|
userInfo: uni.getStorageSync('userInfo') || { ...defaultUserInfo }, // 用户信息
|
|
token: uni.getStorageSync('token') || '',
|
|
isLogin: !!uni.getStorageSync('token'), // 登录状态
|
|
roleMenus: uni.getStorageSync('roleMenus') || [],
|
|
timeout: 10 * 60 * 1000,
|
|
lastRequestTime: null,
|
|
timeoutInterval: null
|
|
}),
|
|
|
|
actions: {
|
|
// 更新最后请求时间
|
|
updateLastRequestTime() {
|
|
this.lastRequestTime = Date.now()
|
|
},
|
|
// 检查是否超时
|
|
checkTimeout() {
|
|
if (!this.token || !this.lastRequestTime) return false
|
|
const currentTime = Date.now()
|
|
const elapsed = currentTime - this.lastRequestTime
|
|
if (this.timeout > 0 && elapsed > this.timeout) {
|
|
uni.showToast({
|
|
icon: 'none',
|
|
title: '登录超时,请重新登录'
|
|
})
|
|
this.logout()
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
startTimeoutChecker(interval = 60 * 1000) {
|
|
this.stopTimeoutChecker()
|
|
this.timeoutInterval = setInterval(() => {
|
|
this.checkTimeout()
|
|
}, interval)
|
|
},
|
|
stopTimeoutChecker() {
|
|
if (this.timeoutInterval) {
|
|
clearInterval(this.timeoutInterval)
|
|
this.timeoutInterval = null
|
|
}
|
|
},
|
|
async getDeviceTimeout() {
|
|
const result = await getSysParameterBizByName('lims_pda_timeout')
|
|
this.timeout = Number(result.dictValue) * 60 * 1000
|
|
},
|
|
async login(loginInfo) {
|
|
const info = await userApi.accountLogin(loginInfo)
|
|
if (info) {
|
|
this.setToken(info.accessToken)
|
|
await this.getUserInfo()
|
|
uni.showToast({
|
|
icon: 'success',
|
|
title: '登录成功'
|
|
})
|
|
// await this.getRoleMenus()
|
|
// await this.getDeviceTimeout()
|
|
// this.startTimeoutChecker()
|
|
uni.reLaunch({
|
|
url: '/pages/index/index'
|
|
})
|
|
}
|
|
},
|
|
async getUserInfo() {
|
|
const { user } = await userApi.getInfo()
|
|
if (user) {
|
|
this.userInfo = user
|
|
uni.setStorageSync('userInfo', this.userInfo)
|
|
}
|
|
},
|
|
async getRoleMenus() {
|
|
let params = {
|
|
parentNodeUrl: '/lims/mobile'
|
|
}
|
|
const result = await userApi.getRoleMenusByParentMenuKey(params)
|
|
if (result.length > 0) {
|
|
this.roleMenus = result.map(item => ({ url: item.url, name: item.name, icon: item.otherConf }))
|
|
uni.setStorageSync('roleMenus', this.roleMenus)
|
|
}
|
|
},
|
|
|
|
// 设置token
|
|
setToken(token = '') {
|
|
this.token = token
|
|
if (token === '') {
|
|
this.isLogin = false
|
|
uni.removeStorageSync('token')
|
|
} else {
|
|
this.isLogin = true
|
|
uni.setStorageSync('token', token)
|
|
}
|
|
return this.isLogin
|
|
},
|
|
|
|
// 登出
|
|
async logout() {
|
|
this.stopTimeoutChecker()
|
|
this.clearUserInfo()
|
|
uni.reLaunch({
|
|
url: '/pages/login/login'
|
|
})
|
|
},
|
|
clearUserInfo() {
|
|
this.userInfo = {}
|
|
this.roleMenus = []
|
|
uni.setStorageSync('roleMenus', [])
|
|
this.setToken()
|
|
}
|
|
}
|
|
})
|
|
|
|
export default user
|