Files
huihuiSquare/uniapp-avatar/src/store/user.js
T

25 lines
831 B
JavaScript

import { defineStore } from 'pinia'
export const useUserStore = defineStore('user', {
state: () => ({ token: '', userId: '', nickname: '', avatarUrl: '', initialized: false }),
getters: { isLogin: state => Boolean(state.token) },
actions: {
init() {
this.$patch({
token: uni.getStorageSync('hh_token') || '',
userId: uni.getStorageSync('hh_userId') || '',
nickname: uni.getStorageSync('hh_nickname') || '',
avatarUrl: uni.getStorageSync('hh_avatarUrl') || '',
initialized: true
})
},
applySession(session) {
this.$patch(session)
uni.setStorageSync('hh_token', this.token)
uni.setStorageSync('hh_userId', this.userId)
uni.setStorageSync('hh_nickname', this.nickname)
uni.setStorageSync('hh_avatarUrl', this.avatarUrl)
}
}
})