user.js 7.5 KB

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