user.js 6.0 KB

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