user.js 6.3 KB

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