user.js 6.2 KB

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