| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 | 
							- import { defineStore } from 'pinia';
 
- import { clone, cloneDeep } from 'lodash-es';
 
- import { getUserInfo } from '@/api/user';
 
- import { smsLogin, passwordLogin, logout } from '@/api/common'
 
- // 默认用户信息
 
- const defaultUserInfo = {
 
-   avatar: '', // 头像
 
-   nickname: '', // 昵称
 
-   gender: 0, // 性别
 
-   mobile: '', // 手机号
 
-   point: 0, // 积分
 
- };
 
- // 默认账户信息
 
- const defaultAccountInfo = {
 
-   accessToken: '',
 
-   expiresTime: '',
 
-   openid: '',
 
-   refreshToken: '',
 
-   userId: ''
 
- }
 
- const tabUrl = [
 
-   'pages/index/index',
 
-   'pages/index/position',
 
-   'pages/index/communicate',
 
-   'pages/index/my'
 
- ]
 
- export const userStore = defineStore({
 
-   id: 'user',
 
-   state: () => ({
 
-     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()
 
-       // 登录成功后的跳转地址
 
-       // 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 getUserInfo({ userId: 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;
 
-       // 获取最新信息
 
-       // await this.getInfo();
 
-       // this.getWallet();
 
-       return this.userInfo;
 
-     },
 
-     // 重置用户默认数据
 
-     resetUserData() {
 
-       // 清空 token
 
-       this.setToken();
 
-       // 清空用户相关的缓存
 
-       this.userInfo = clone(defaultUserInfo);
 
-       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)
 
-       },
 
-     },
 
-   }
 
- })
 
 
  |