user.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { defineStore } from 'pinia';
  2. import { clone, cloneDeep } from 'lodash-es';
  3. import { getBaseInfo, getUserInfo } from '@/api/user';
  4. import { smsLogin, passwordLogin, logout, userRegister } from '@/api/common'
  5. import { closeAuthModal } from '@/hooks/useModal'
  6. import { timesTampChange } from '@/utils/date'
  7. import { getBaseInfoDictOfName } from '@/utils/getText'
  8. // 默认用户信息
  9. const defaultBaseInfo = {
  10. avatar: '', // 头像
  11. nickname: '', // 昵称
  12. gender: 0, // 性别
  13. mobile: '', // 手机号
  14. point: 0, // 积分
  15. };
  16. // 默认账户信息
  17. const defaultAccountInfo = {
  18. accessToken: '',
  19. expiresTime: '',
  20. openid: '',
  21. refreshToken: '',
  22. userId: ''
  23. }
  24. const tabUrl = [
  25. 'pages/index/position',
  26. 'pages/index/communicate',
  27. 'pages/index/my'
  28. ]
  29. export const userStore = defineStore({
  30. id: 'user',
  31. state: () => {
  32. const userInfo = JSON.parse(uni.getStorageSync('user') ?? '{}')
  33. console.log('userInfo====',userInfo)
  34. return {
  35. baseInfo: userInfo ? userInfo.baseInfo : {}, // 用户信息
  36. userInfo: userInfo ? userInfo.userInfo : {},
  37. isLogin: !!uni.getStorageSync('token'), // 登录状态
  38. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  39. lastUpdateTime: 0, // 上次更新时间
  40. accountInfo: cloneDeep(defaultAccountInfo), // 账号信息
  41. }
  42. },
  43. actions: {
  44. setLogin (val) {
  45. this.isLogin = val
  46. },
  47. // 登录
  48. async handleSmsLogin (query, type, route) {
  49. const api = type ? smsLogin : passwordLogin
  50. const { data, code } = await api(query)
  51. if (code === 0) {
  52. uni.showToast({
  53. title: '登录成功'
  54. })
  55. }
  56. this.accountInfo = data
  57. this.getInfo()
  58. this.getUserInfo()
  59. closeAuthModal()
  60. // 登录成功后的跳转地址
  61. // if (tabUrl.includes(route)) {
  62. // uni.switchTab({
  63. // url: '/' + route
  64. // })
  65. // } else {
  66. // uni.navigateTo({
  67. // url: '/' + route
  68. // })
  69. // }
  70. // uni.switchTab({
  71. // url: '/pages/index/position'
  72. // })
  73. },
  74. async handleRegister (query) {
  75. const { data, code } = await userRegister(query)
  76. if (code === 0) {
  77. uni.showToast({
  78. title: '注册成功'
  79. })
  80. }
  81. this.accountInfo = data
  82. this.getInfo()
  83. this.getUserInfo()
  84. closeAuthModal()
  85. },
  86. // 获取人才信息
  87. async getInfo() {
  88. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  89. if (code !== 0) {
  90. return;
  91. }
  92. const _data = await this.getFieldText(data)
  93. this.baseInfo = _data
  94. return Promise.resolve(data);
  95. },
  96. // 获取用户信息
  97. async getUserInfo() {
  98. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  99. if (code !== 0) {
  100. return;
  101. }
  102. this.userInfo = data;
  103. return Promise.resolve(data);
  104. },
  105. // 设置 token
  106. setToken(token = '', refreshToken = '') {
  107. if (token === '') {
  108. this.isLogin = false;
  109. this.refreshToken = ''
  110. uni.removeStorageSync('token');
  111. uni.removeStorageSync('refresh-token');
  112. } else {
  113. this.isLogin = true;
  114. uni.setStorageSync('token', token);
  115. this.refreshToken = refreshToken
  116. uni.setStorageSync('refresh-token', refreshToken);
  117. this.loginAfter();
  118. }
  119. return this.isLogin;
  120. },
  121. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  122. async updateUserData() {
  123. if (!this.isLogin) {
  124. this.resetUserData();
  125. return;
  126. }
  127. // 防抖,5 秒之内不刷新
  128. const nowTime = new Date().getTime();
  129. if (this.lastUpdateTime + 5000 > nowTime) {
  130. return;
  131. }
  132. this.lastUpdateTime = nowTime;
  133. // 获取最新信息
  134. return this.baseInfo;
  135. },
  136. // 重置用户默认数据
  137. resetUserData() {
  138. // 清空 token
  139. this.setToken();
  140. // 清空用户相关的缓存
  141. this.baseInfo = clone(defaultBaseInfo);
  142. this.userInfo = {}
  143. this.accountInfo = cloneDeep(defaultAccountInfo);
  144. },
  145. // 登录后,加载各种信息
  146. async loginAfter() {
  147. await this.updateUserData();
  148. },
  149. // 登出系统
  150. async handleLogout() {
  151. await logout()
  152. this.resetUserData();
  153. return !this.isLogin;
  154. },
  155. // 字典对应中文
  156. async getFieldText (data) {
  157. if (data.birthday && data.birthday !== 0) {
  158. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  159. }
  160. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  161. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  162. }
  163. if (data.areaId && data.areaId !== 0) {
  164. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  165. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  166. }
  167. if (data.eduType && data.eduType !== 0) {
  168. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  169. }
  170. if (data.expType && data.expType !== 0) {
  171. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  172. }
  173. if (data.sex && data.sex !== 0) {
  174. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  175. }
  176. if (data.jobType && data.jobType !== 0) {
  177. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  178. }
  179. if (data.jobStatus && data.jobStatus !== 0) {
  180. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  181. }
  182. if (data.maritalStatus && data.maritalStatus !== 0) {
  183. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  184. }
  185. return data
  186. }
  187. },
  188. persist: {
  189. // enabled: true,
  190. // strategies: [
  191. // {
  192. // key: 'user-store'
  193. // }
  194. // ]
  195. storage: {
  196. setItem(key, value) {
  197. uni.setStorageSync(key, value)
  198. },
  199. getItem(key) {
  200. return uni.getStorageSync(key)
  201. },
  202. },
  203. }
  204. })