user.js 7.4 KB

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