user.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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, showAuthModal } from '@/hooks/useModal'
  5. import { timesTampChange } from '@/utils/date'
  6. import { getBaseInfoDictOfName } from '@/utils/getText'
  7. import { updateEventList } from '@/utils/eventList'
  8. import { checkPersonBaseInfo } from '@/utils/check'
  9. // 默认用户信息
  10. const defaultBaseInfo = {
  11. avatar: '', // 头像
  12. nickname: '', // 昵称
  13. gender: 0, // 性别
  14. mobile: '', // 手机号
  15. point: 0, // 积分
  16. };
  17. // 默认账户信息
  18. const defaultAccountInfo = {
  19. accessToken: '',
  20. expiresTime: '',
  21. openid: '',
  22. refreshToken: '',
  23. userId: ''
  24. }
  25. export const userStore = defineStore('user', {
  26. state: () => {
  27. const userLocal = uni.getStorageSync('user')
  28. const userInfo = userLocal ? JSON.parse(userLocal) : {}
  29. return {
  30. phone: null,
  31. baseInfo: userInfo.baseInfo ?? {}, // 用户信息
  32. userInfo: userInfo.userInfo ?? {},
  33. isLogin: !!uni.getStorageSync('token'), // 登录状态
  34. refreshToken: uni.getStorageSync('refresh-token'), // 用户切换
  35. lastUpdateTime: 0, // 上次更新时间
  36. accountInfo: { ...defaultAccountInfo }, // 账号信息
  37. }
  38. },
  39. actions: {
  40. setLogin (val) {
  41. this.isLogin = val
  42. },
  43. // 登录
  44. async handleSmsLogin (query, index = 0) {
  45. this.phone = query.phone
  46. const apiList = [weChatLogin, smsLogin, passwordLogin]
  47. const { data, code } = await apiList[index](query)
  48. // 扫码登录不需要弹提示语
  49. if (code === 0 && !uni.getStorageSync('wxLoginCode')) {
  50. uni.showToast({
  51. title: '登录成功'
  52. })
  53. }
  54. this.accountInfo = data
  55. const res = await this.getInfo()
  56. this.getUserInfo()
  57. return Promise.resolve(res);
  58. },
  59. async handleRegister (query) {
  60. this.phone = query.phone
  61. const { data, code } = await userRegister(query)
  62. if (code === 0) {
  63. uni.showToast({
  64. title: '手机号验证成功'
  65. })
  66. }
  67. this.accountInfo = data
  68. const res = await this.getInfo()
  69. this.getUserInfo()
  70. return Promise.resolve(res);
  71. },
  72. // 扫码注册登录
  73. async handleShareUserRegister (query) {
  74. try {
  75. this.phone = query.phone
  76. const { data, code } = await shareUserRegister(query)
  77. if (code === 0) {
  78. uni.showToast({
  79. title: '登录成功'
  80. })
  81. this.accountInfo = data
  82. await this.getInfo()
  83. this.getUserInfo()
  84. }
  85. return Promise.resolve(data);
  86. } catch (err) {
  87. uni.showToast({
  88. icon: 'none',
  89. title: err.msg
  90. })
  91. }
  92. },
  93. // 校验是否完善人才必填信息
  94. checkPersonBaseInfoFun(data) {
  95. data = data || {}
  96. if (!data || !Object.keys(data).length || data.type === undefined || data.type === null) {
  97. showAuthModal('selectUserType')
  98. return
  99. }
  100. const necessaryInfoReady = checkPersonBaseInfo(data)
  101. data.necessaryInfoReady = necessaryInfoReady
  102. if (necessaryInfoReady) closeAuthModal()
  103. else showAuthModal('necessaryInfo')
  104. uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'fddeaddc47868b');
  105. },
  106. // 获取人才信息
  107. async getInfo() {
  108. const { code, data } = await getBaseInfo({ userId: this.accountInfo.userId });
  109. updateEventList() // 更新事件列表
  110. await this.checkPersonBaseInfoFun(data) // 校验是否完善人才必填信息
  111. if (code !== 0) {
  112. return;
  113. }
  114. if (!data) return
  115. const _data = await this.getFieldText(data)
  116. this.baseInfo = _data
  117. console.log(data, data.type === '1', '当前账号角色是否为学生')
  118. // 是学生则跳转学生专区
  119. if (data && data?.type === '1') {
  120. uni.navigateTo({
  121. url: '/pagesA/student/index'
  122. })
  123. }
  124. return Promise.resolve(data);
  125. },
  126. // 获取用户信息
  127. async getUserInfo() {
  128. const { code, data } = await getUserInfo({ id: this.accountInfo.userId });
  129. if (code !== 0) {
  130. return;
  131. }
  132. this.userInfo = data;
  133. return Promise.resolve(data);
  134. },
  135. // 设置 token
  136. setToken(token = '', refreshToken = '') {
  137. if (token === '') {
  138. this.isLogin = false;
  139. this.refreshToken = ''
  140. uni.removeStorageSync('token');
  141. uni.removeStorageSync('refresh-token');
  142. } else {
  143. this.isLogin = true;
  144. uni.setStorageSync('token', token);
  145. this.refreshToken = refreshToken
  146. uni.setStorageSync('refresh-token', refreshToken);
  147. this.loginAfter();
  148. }
  149. return this.isLogin;
  150. },
  151. // 更新用户相关信息 (手动限流,5 秒之内不刷新)
  152. async updateUserData() {
  153. if (!this.isLogin) {
  154. this.resetUserData();
  155. return;
  156. }
  157. // 防抖,5 秒之内不刷新
  158. const nowTime = new Date().getTime();
  159. if (this.lastUpdateTime + 5000 > nowTime) {
  160. return;
  161. }
  162. this.lastUpdateTime = nowTime;
  163. // 获取最新信息
  164. return this.baseInfo;
  165. },
  166. // 重置用户默认数据
  167. resetUserData() {
  168. // 清空 token
  169. this.setToken();
  170. // 清空用户相关的缓存
  171. this.baseInfo = { ...defaultBaseInfo };
  172. this.userInfo = {}
  173. this.accountInfo = { ...defaultAccountInfo };
  174. },
  175. // 登录后,加载各种信息
  176. async loginAfter() {
  177. await this.updateUserData();
  178. },
  179. // 登出系统
  180. async handleLogout() {
  181. await logout()
  182. this.resetUserData();
  183. return !this.isLogin;
  184. },
  185. // 字典对应中文
  186. async getFieldText (data) {
  187. if (!data || !Object.keys(data).length) return {}
  188. if (data.birthday && data.birthday !== 0) {
  189. data.birthdayText = timesTampChange(data.birthday, 'Y-M-D') // 出生日期
  190. }
  191. if (data.firstWorkTime && data.firstWorkTime !== 0) {
  192. data.firstWorkTimeText = timesTampChange(data.firstWorkTime, 'Y-M-D') // 首次工作时间
  193. }
  194. if (data.areaId && data.areaId !== 0) {
  195. await getBaseInfoDictOfName(0, data, data.areaId, 'areaName') // 现居住地text
  196. await getBaseInfoDictOfName(0, data, data.regId, 'regName') // 户籍地text
  197. }
  198. if (data.eduType && data.eduType !== 0) {
  199. await getBaseInfoDictOfName(1, data, data.eduType, 'eduTypeText') // 学历
  200. }
  201. if (data.expType && data.expType !== 0) {
  202. await getBaseInfoDictOfName(2, data, data.expType, 'expTypeText') // 工作经验
  203. }
  204. if (data.sex && data.sex !== 0) {
  205. await getBaseInfoDictOfName(3, data, data.sex, 'sexTypeText') // 性别
  206. }
  207. if (data.jobType && data.jobType !== 0) {
  208. await getBaseInfoDictOfName(4, data, data.jobType, 'jobTypeText') // 求职类型
  209. }
  210. if (data.jobStatus && data.jobStatus !== 0) {
  211. await getBaseInfoDictOfName(5, data, data.jobStatus, 'jobStatusText') // 求职状态
  212. }
  213. if (data.maritalStatus && data.maritalStatus !== 0) {
  214. await getBaseInfoDictOfName(6, data, data.maritalStatus, 'maritalText') // 婚姻状况
  215. }
  216. return data
  217. }
  218. },
  219. persist: {
  220. storage: {
  221. setItem(key, value) {
  222. uni.setStorageSync(key, value)
  223. },
  224. getItem(key) {
  225. return uni.getStorageSync(key)
  226. },
  227. },
  228. }
  229. })