user.js 6.1 KB

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