user.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { userService } from '@/services/index.js'
  2. import authorize from '@/common/authorize.js'
  3. import { msg as showMsg } from '@/common/util.js'
  4. import { switchTabTo } from '@/common/utilsTools.js'
  5. const state = {
  6. userId: '',
  7. userIdentity: '',
  8. userInfo: null,
  9. hasLogin: false,
  10. isWxAuthorize: false,
  11. inviteUserId: 0,
  12. }
  13. const mutations = {
  14. // 用户登录
  15. LOGIN(state, data) {
  16. const userInfo = JSON.parse(data)
  17. state.userInfo = userInfo
  18. state.hasLogin = true
  19. state.isWxAuthorize = true
  20. state.userId = userInfo.userId
  21. state.userIdentity = userInfo.userIdentity
  22. uni.setStorageSync('openId', userInfo.openId)
  23. },
  24. // 退出登录
  25. LOGIN_OUT(state, data) {
  26. const userInfo = JSON.parse(data) || null
  27. state.userInfo = userInfo
  28. state.hasLogin = false
  29. state.isWxAuthorize = false
  30. state.userId = ''
  31. state.userIdentity = ''
  32. userInfo && uni.setStorageSync('openId', userInfo.openId)
  33. },
  34. // 设置邀请用户id
  35. setInviteUserId(state, id) {
  36. state.inviteUserId = parseInt(id)
  37. }
  38. }
  39. const actions = {
  40. // 微信授权登录
  41. async wechatlogin({ commit, dispatch, state }) {
  42. // 获取code
  43. const code = await authorize.getCode('weixin')
  44. return userService.UserWechatAuthorLogin({ code })
  45. .then(response => {
  46. console.log('登录成功')
  47. commit('LOGIN', response.data)
  48. dispatch('cart/getCartNumber', state.userId, { root: true }) // 获取购物车数量信息
  49. dispatch('coupon/getCouponActivity', null, { root: true }) // 获取优惠券弹窗信息
  50. dispatch('coupon/initReceiveCouponCount', null, { root: true }) // 获取已领取优惠券数量
  51. })
  52. .catch(error => {
  53. console.log('游客')
  54. dispatch('coupon/getCouponActivity', null, { root: true }) // 游客也要获取优惠券弹窗信息
  55. commit('LOGIN_OUT', error.data)
  56. })
  57. },
  58. // 手机号注册登录
  59. customLogin({ commit, state, dispatch }, params) {
  60. const redirectUrl = uni.getStorageSync('login_redirect_url') || ''
  61. params.shareUserId = state.inviteUserId
  62. return userService.UserMobileLogin(params)
  63. .then(response => {
  64. // 保存用户信息
  65. commit('LOGIN', response.data)
  66. dispatch('cart/getCartNumber', state.userId, { root: true }) // 获取购物车数量信息
  67. dispatch('coupon/getCouponActivity', null, { root: true }) // 获取优惠券弹窗信息
  68. dispatch('coupon/initReceiveCouponCount', null, { root: true }) // 获取已领取优惠券数量
  69. // 登录成功提示
  70. showMsg('登录成功', 1500, false, 'success')
  71. if (redirectUrl) {
  72. setTimeout(() => {
  73. uni.redirectTo({ url: redirectUrl })
  74. }, 1500)
  75. } else {
  76. setTimeout(() => {
  77. switchTabTo('/pages/tabBar/index/index')
  78. }, 1500)
  79. }
  80. })
  81. .catch(error => {
  82. showMsg(error.msg, 2000)
  83. })
  84. }
  85. }
  86. export default {
  87. namespaced: true,
  88. state,
  89. mutations,
  90. actions
  91. }