user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { wechatAuthLogin, mobileLogin, getAccessToken } from '@/services/api/auth.js'
  2. import { fetchMessageCount } from '@/services/api/notice.js'
  3. import { wxLogin } from '@/common/auth.js'
  4. import { setStorage, getStorage } from '@/common/storage.js'
  5. import { objAssign } from '@/common/utils.js'
  6. function initUserState() {
  7. const state = {
  8. // 用户信息
  9. headImgUrl: '',
  10. mobile: '',
  11. nickName: '',
  12. openId: '',
  13. userId: 0,
  14. userIdentity: -1, // 用户类型
  15. inviteUserId: '', // 分享者用户ID
  16. accessToken: '', // token
  17. hasLogin: false, // 用户是否登录
  18. notice_count: 0, // 消息通知数量
  19. }
  20. const userInfo = getStorage('USER_INFO')
  21. if (userInfo) {
  22. Object.assign(state, userInfo)
  23. }
  24. const accessToken = getStorage('ACCESS_TOKEN')
  25. if (accessToken) {
  26. state.accessToken = accessToken
  27. }
  28. return state
  29. }
  30. const state = initUserState()
  31. console.log(state)
  32. const mutations = {
  33. SET_USER_INFO: (state, userInfo) => {
  34. state.hasLogin = true
  35. objAssign(state, userInfo)
  36. },
  37. SET_INVITE_USER_ID: (state, id) => {
  38. state.inviteUserId = id
  39. },
  40. SET_ACCESS_TOKEN: (state, token) => {
  41. state.accessToken = token
  42. },
  43. SET_LOGINOUT: (state, logout) => {
  44. state.hasLogin = logout
  45. },
  46. SET_NOTICE_COUNT: (state, count) => {
  47. state.notice_count = count
  48. },
  49. SET_TABBAR_BADGE: (state, count) => {
  50. if (count >= 100) {
  51. return uni.setTabBarBadge({ index: 2, text: '99+' })
  52. }
  53. if (count > 0) {
  54. return uni.setTabBarBadge({ index: 2, text: count.toString() })
  55. }
  56. uni.removeTabBarBadge({ index: 2 })
  57. }
  58. }
  59. const actions = {
  60. // 微信自动登录
  61. async wxAutoLogin({ commit, state, dispatch }) {
  62. try {
  63. const code = await wxLogin() // 获取微信code
  64. const res = await wechatAuthLogin({ code }) // 微信自动登录
  65. const data = JSON.parse(res.data)
  66. commit('SET_USER_INFO', data)
  67. setStorage('USER_INFO', data)
  68. dispatch('getAccessToken') // 获取token
  69. commit('SET_LOGINOUT', true)
  70. } catch (e) {
  71. commit('SET_LOGINOUT', false)
  72. console.log(e)
  73. }
  74. },
  75. // 手机号注册登录
  76. async register({ commit }, resigterData) {
  77. try {
  78. const res = await mobileLogin(resigterData)
  79. const data = JSON.parse(res.data)
  80. commit('SET_USER_INFO', data)
  81. setStorage('USER_INFO', data)
  82. commit('SET_LOGINOUT', true)
  83. } catch (e) {
  84. commit('SET_LOGINOUT', false)
  85. console.log(e)
  86. }
  87. },
  88. // 获取accessToken
  89. async getAccessToken({ commit }) {
  90. try {
  91. const res = await getAccessToken()
  92. commit('SET_ACCESS_TOKEN', res.data)
  93. setStorage('ACCESS_TOKEN', res.data)
  94. commit('SET_LOGINOUT', true)
  95. } catch (e) {
  96. commit('SET_LOGINOUT', false)
  97. console.log(e)
  98. }
  99. },
  100. // 短信消息统计
  101. async updateNoticeNum({ commit }) { // 更新通知消息数量
  102. try{
  103. const commonId = getStorage('USER_INFO').userId || 0
  104. const { data } = await fetchMessageCount({ commonId })
  105. commit('SET_NOTICE_COUNT', data.count)
  106. commit('SET_TABBAR_BADGE', data.count)
  107. }catch(error){
  108. console.log(error)
  109. }
  110. },
  111. }
  112. export default {
  113. namespaced: true,
  114. state,
  115. mutations,
  116. actions
  117. }