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.
45 lines
856 B
45 lines
856 B
/** |
|
* 用户状态管理 - MobX |
|
*/ |
|
import { makeAutoObservable } from 'mobx' |
|
import Taro from '@tarojs/taro' |
|
|
|
export interface UserInfo { |
|
id: string |
|
nickname: string |
|
avatar?: string |
|
phone?: string |
|
} |
|
|
|
class UserStore { |
|
userInfo: UserInfo | null = null |
|
isLoggedIn: boolean = false |
|
balance: number = 0 |
|
|
|
constructor() { |
|
makeAutoObservable(this) |
|
} |
|
|
|
setUserInfo = (userInfo: UserInfo | null) => { |
|
this.userInfo = userInfo |
|
this.isLoggedIn = !!userInfo |
|
|
|
if (userInfo) { |
|
Taro.setStorageSync('userInfo', userInfo) |
|
} else { |
|
Taro.removeStorageSync('userInfo') |
|
} |
|
} |
|
|
|
setBalance = (balance: number) => { |
|
this.balance = balance |
|
} |
|
|
|
logout = () => { |
|
this.setUserInfo(null) |
|
Taro.removeStorageSync('token') |
|
Taro.removeStorageSync('userInfo') |
|
} |
|
} |
|
|
|
export const userStore = new UserStore()
|
|
|