user.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = [weChatLogin, smsLogin, passwordLogin]
  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. try {
  75. const { data, code } = await shareUserRegister(query)
  76. if (code === 0) {
  77. uni.showToast({
  78. title: '登录成功'
  79. })
  80. this.accountInfo = data
  81. this.getInfo()
  82. this.getUserInfo()
  83. closeAuthModal()
  84. }
  85. return Promise.resolve(data);
  86. } catch (err) {
  87. uni.showToast({
  88. icon: 'none',
  89. title: err.msg
  90. })
  91. }
  92. },
  93. // 获取人才信息
  94. async getInfo() {
  95. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  96. updateEventList() // 更新事件列表
  97. if (code !== 0) {
  98. return;
  99. }
  100. if (!data) return
  101. const _data = await this.getFieldText(data)
  102. this.baseInfo = _data
  103. return Promise.resolve(data);
  104. },
  105. // 获取用户信息
  106. async getUserInfo() {
  107. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  108. if (code !== 0) {
  109. return;
  110. }
  111. this.userInfo = data;
  112. return Promise.resolve(data);
  113. },
  114. // 设置 token
  115. setToken(token = '', refreshToken = '') {
  116. if (token === '') {
  117. this.isLogin = false;
  118. this.refreshToken = ''
  119. uni.removeStorageSync('token');
  120. uni.removeStorageSync('refresh-token');
  121. } else {
  122. this.isLogin = true;
  123. uni.setStorageSync('token', token);
  124. this.refreshToken = refreshToken
  125. uni.setStorageSync('refresh-token', refreshToken);
  126. this.loginAfter();
  127. }
  128. return this.isLogin;
  129. },
  130. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  131. async updateUserData() {
  132. if (!this.isLogin) {
  133. this.resetUserData();
  134. return;
  135. }
  136. // 防抖,5 秒之内不刷新
  137. const nowTime = new Date().getTime();
  138. if (this.lastUpdateTime + 5000 > nowTime) {
  139. return;
  140. }
  141. this.lastUpdateTime = nowTime;
  142. // 获取最新信息
  143. return this.baseInfo;
  144. },
  145. // 重置用户默认数据
  146. resetUserData() {
  147. // 清空 token
  148. this.setToken();
  149. // 清空用户相关的缓存
  150. this.baseInfo = { ...defaultBaseInfo };
  151. this.userInfo = {}
  152. this.accountInfo = { ...defaultAccountInfo };
  153. },
  154. // 登录后,加载各种信息
  155. async loginAfter() {
  156. await this.updateUserData();
  157. },
  158. // 登出系统
  159. async handleLogout() {
  160. await logout()
  161. this.resetUserData();
  162. return !this.isLogin;
  163. },
  164. // 字典对应中文
  165. async getFieldText (data) {
  166. if (data.birthday && data.birthday !== 0) {
  167. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  168. }
  169. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  170. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  171. }
  172. if (data.areaId && data.areaId !== 0) {
  173. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  174. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  175. }
  176. if (data.eduType && data.eduType !== 0) {
  177. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  178. }
  179. if (data.expType && data.expType !== 0) {
  180. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  181. }
  182. if (data.sex && data.sex !== 0) {
  183. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  184. }
  185. if (data.jobType && data.jobType !== 0) {
  186. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  187. }
  188. if (data.jobStatus && data.jobStatus !== 0) {
  189. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  190. }
  191. if (data.maritalStatus && data.maritalStatus !== 0) {
  192. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  193. }
  194. return data
  195. }
  196. },
  197. persist: {
  198. // enabled: true,
  199. // strategies: [
  200. // {
  201. // key: 'user-store'
  202. // }
  203. // ]
  204. storage: {
  205. setItem(key, value) {
  206. uni.setStorageSync(key, value)
  207. },
  208. getItem(key) {
  209. return uni.getStorageSync(key)
  210. },
  211. },
  212. }
  213. })