| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | import { defineStore } from 'pinia';import { getBaseInfo, getUserInfo, getUserAccountInfo } from '@/api/user';import { smsLogin, passwordLogin, weChatLogin, logout, userRegister, shareUserRegister } from '@/api/common'import { closeAuthModal, showAuthModal } from '@/hooks/useModal'import { timesTampChange } from '@/utils/date'import { getBaseInfoDictOfName } from '@/utils/getText'// 默认账户信息const defaultAccountInfo = {  accessToken: '',  expiresTime: '',  openid: '',  refreshToken: '',  userId: ''}export const userStore = defineStore('user', {  state: () => {    const userLocal = uni.getStorageSync('user')    const userInfo = userLocal ? JSON.parse(userLocal) : {}    return {      phone: null,      baseInfo: userInfo.baseInfo ?? {}, // 用户信息      userInfo: userInfo.userInfo ?? {},      isLogin: !!uni.getStorageSync('token'), // 登录状态      refreshToken: uni.getStorageSync('refresh-token'), // 用户切换      lastUpdateTime: 0, // 上次更新时间      accountInfo: { ...defaultAccountInfo }, // 账号信息      userAccountInfo: userInfo.userAccountInfo ?? {}, // 账户信息    }  },  actions: {    setLogin (val) {      this.isLogin = val    },    // 登录    async handleSmsLogin (query, index = 0) {      this.phone = query.phone      const apiList = [smsLogin, passwordLogin]       const { data, code } = await apiList[index](query)      if (code === 0) {        uni.showToast({          title: '登录成功'        })      }      this.accountInfo = data      this.getUserInfos()      this.getAccountInfo()      closeAuthModal()    },    async handleRegister (query) {      this.phone = query.phone      const { data, code } = await userRegister(query)      if (code === 0) {        uni.showToast({          title: '手机号验证成功'        })      }      this.accountInfo = data      const res = await this.getInfo()      this.getUserInfos()      return Promise.resolve(res);    },    // 获取用户信息    async getUserInfos() {      const { code, data } = await getUserInfo();      if (code !== 0) {        return;      }      this.userInfo = data;      return Promise.resolve(data);    },    // 获取账户信息    async getAccountInfo () {      const { code, data } = await getUserAccountInfo();      if (code !== 0) {        return;      }      this.userAccountInfo = data;      return Promise.resolve(data);    },    // 设置 token    setToken(token = '', refreshToken = '') {      if (token === '') {        this.isLogin = false;        this.refreshToken = ''        uni.removeStorageSync('token');        uni.removeStorageSync('refresh-token');      } else {        this.isLogin = true;        uni.setStorageSync('token', token);        this.refreshToken = refreshToken        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;    },    // 重置用户默认数据    resetUserData() {      // 清空 token      this.setToken();      // 清空用户相关的缓存      this.userInfo = {}      this.phone = ''      this.userAccountInfo = {}      this.accountInfo = { ...defaultAccountInfo };    },    // 登录后,加载各种信息    async loginAfter() {      await this.updateUserData();    },    // 登出系统    async handleLogout() {      await logout(uni.getStorageSync('token'))      this.resetUserData();      return !this.isLogin;    },    // 字典对应中文    async getFieldText (data) {      if (!data || !Object.keys(data).length) return {}      if (data.birthday && data.birthday !== 0) {        data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期      }      if (data.firstWorkTime && data.firstWorkTime !== 0) {        data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间      }      if (data.areaId && data.areaId !== 0) {        await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text        await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text      }      if (data.eduType && data.eduType !== 0) {        await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历      }      if (data.expType && data.expType !== 0) {        await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验      }      if (data.sex && data.sex !== 0) {        await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别      }      if (data.jobType && data.jobType !== 0) {        await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型      }      if (data.jobStatus && data.jobStatus !== 0) {        await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 	求职状态      }      if (data.maritalStatus && data.maritalStatus !== 0) {        await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 	婚姻状况      }      return data    }  },  persist: {    storage: {      setItem(key, value) {        uni.setStorageSync(key, value)      },      getItem(key) {        return uni.getStorageSync(key)      },    },  }})
 |