123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import { defineStore } from 'pinia';
- import { clone, cloneDeep } from 'lodash-es';
- import { getBaseInfo, getUserInfo } from '@/api/user';
- import { smsLogin, passwordLogin, logout } from '@/api/common'
- import { closeAuthModal } from '@/hooks/useModal'
- // 默认用户信息
- const defaultBaseInfo = {
- avatar: '', // 头像
- nickname: '', // 昵称
- gender: 0, // 性别
- mobile: '', // 手机号
- point: 0, // 积分
- };
- // 默认账户信息
- const defaultAccountInfo = {
- accessToken: '',
- expiresTime: '',
- openid: '',
- refreshToken: '',
- userId: ''
- }
- const tabUrl = [
- 'pages/index/position',
- 'pages/index/communicate',
- 'pages/index/my'
- ]
- export const userStore = defineStore({
- id: 'user',
- state: () => ({
- baseInfo: {}, // 用户信息
- userInfo: {},
- isLogin: !!uni.getStorageSync('token'), // 登录状态
- lastUpdateTime: 0, // 上次更新时间
- accountInfo: cloneDeep(defaultAccountInfo), // 账号信息
- }),
- actions: {
- // 登录
- async handleSmsLogin (query, type, route) {
- const api = type ? smsLogin : passwordLogin
- const { data, code } = await api(query)
- if (code === 0) {
- uni.showToast({
- title: '登录成功'
- })
- }
- this.accountInfo = data
- this.getInfo()
- this.getUserInfo()
- closeAuthModal()
- // 登录成功后的跳转地址
- // if (tabUrl.includes(route)) {
- // uni.switchTab({
- // url: '/' + route
- // })
- // } else {
- // uni.navigateTo({
- // url: '/' + route
- // })
- // }
- // uni.switchTab({
- // url: '/pages/index/position'
- // })
- },
- // 获取人才信息
- async getInfo() {
- const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
- if (code !== 0) {
- return;
- }
- this.baseInfo = data;
- return Promise.resolve(data);
- },
- // 获取用户信息
- async getUserInfo() {
- const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
- if (code !== 0) {
- return;
- }
- this.userInfo = data;
- return Promise.resolve(data);
- },
- // 设置 token
- setToken(token = '', refreshToken = '') {
- if (token === '') {
- this.isLogin = false;
- uni.removeStorageSync('token');
- uni.removeStorageSync('refresh-token');
- } else {
- this.isLogin = true;
- uni.setStorageSync('token', token);
- uni.setStorageSync('refresh-token', refreshToken);
- this.loginAfter();
- }
- return this.isLogin;
- },
- // 更新用户相关信息 (手动限流,5 秒之内不刷新)
- async updateUserData() {
- if (!this.isLogin) {
- this.resetUserData();
- return;
- }
- // 防抖,5 秒之内不刷新
- const nowTime = new Date().getTime();
- if (this.lastUpdateTime + 5000 > nowTime) {
- return;
- }
- this.lastUpdateTime = nowTime;
- // 获取最新信息
- return this.baseInfo;
- },
- // 重置用户默认数据
- resetUserData() {
- // 清空 token
- this.setToken();
- // 清空用户相关的缓存
- this.baseInfo = clone(defaultBaseInfo);
- this.userInfo = {}
- this.accountInfo = cloneDeep(defaultAccountInfo);
- },
- // 登录后,加载各种信息
- async loginAfter() {
- await this.updateUserData();
- },
- // 登出系统
- async handleLogout() {
- await logout()
- this.resetUserData();
- return !this.isLogin;
- },
- },
- persist: {
- // enabled: true,
- // strategies: [
- // {
- // key: 'user-store'
- // }
- // ]
- storage: {
- setItem(key, value) {
- uni.setStorageSync(key, value)
- },
- getItem(key) {
- return uni.getStorageSync(key)
- },
- },
- }
- })
|