generated from ccsens_fe/uni-vue3-template
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.3 KiB
65 lines
1.3 KiB
import type { IUser } from '@/types/user'
|
|
import { defineStore } from 'pinia'
|
|
import { store } from '@/store'
|
|
|
|
interface UserState {
|
|
user: null | IUser // 用户信息
|
|
token: string
|
|
}
|
|
|
|
export const useUserStore = defineStore({
|
|
id: 'user',
|
|
state: (): UserState => ({
|
|
user: null,
|
|
token: '',
|
|
}),
|
|
|
|
getters: {},
|
|
|
|
actions: {
|
|
// 登录
|
|
async login(padNo: string) {
|
|
console.log('store: login')
|
|
|
|
try {
|
|
const res = await uni.$u.api.login(padNo)
|
|
console.log(res)
|
|
|
|
if (res?.length) {
|
|
uni.setStorageSync('user', JSON.stringify(res[0]))
|
|
this.user = res[0]
|
|
|
|
uni.$u.toast('登录成功')
|
|
|
|
// this.getStationInfo(this.user!.jczbh)
|
|
|
|
// setTimeout(() => {
|
|
// uni.switchTab({ url: '/pages/index/index' })
|
|
// }, 1500)
|
|
return
|
|
}
|
|
|
|
uni.removeStorageSync('user')
|
|
this.user = null
|
|
} catch (error: any) {
|
|
this.user = null
|
|
uni.removeStorageSync('user')
|
|
}
|
|
},
|
|
|
|
// set token
|
|
setToken(token: string) {
|
|
this.token = token
|
|
uni.setStorageSync(uni.$u.LOCAL_KEY.TOKEN, token)
|
|
},
|
|
|
|
setUser(data) {
|
|
this.user = data || null
|
|
},
|
|
},
|
|
})
|
|
|
|
// Need to be used outside the setup
|
|
export function useUserStoreWidthOut() {
|
|
return useUserStore(store)
|
|
}
|
|
|