index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { T_STORAGE_KEY, USER_INFO } from '../constant'
  2. import { getAnotherTFTokenApi } from '../api/anotherTFInterface'
  3. import Vue from 'vue'
  4. import Vuex from 'vuex'
  5. Vue.use(Vuex)
  6. const store = new Vuex.Store({
  7. state: {
  8. userInfo: uni.getStorageSync(USER_INFO)
  9. },
  10. getters: {
  11. userId(state) {
  12. return state.userInfo.id || null
  13. }
  14. },
  15. mutations: {
  16. // 更新state数据
  17. setStateAttr(state, param) {
  18. if (param instanceof Array) {
  19. for (const item of param) {
  20. state[item.key] = item.val
  21. }
  22. } else {
  23. state[param.key] = param.val
  24. }
  25. },
  26. 'CHNAGE_USER_INFO'(state, userInfo) {
  27. state.userInfo = userInfo
  28. uni.setStorageSync(USER_INFO, userInfo)
  29. }
  30. },
  31. actions: {
  32. setUserData({ state, commit }, data) {
  33. commit('setStateAttr', {
  34. key: 'userInfo',
  35. val: data
  36. })
  37. uni.setStorageSync(USER_INFO, data)
  38. },
  39. // 密码登录
  40. loginAction({ commit, dispatch }, loginData) {
  41. return new Promise((resolve, reject) => {
  42. loginApi({ ...loginData })
  43. .then(async ({ data }) => {
  44. commit('CHNAGE_USER_INFO', data.userInfo)
  45. uni.showToast({
  46. title: '登录成功'
  47. })
  48. console.log(data)
  49. await dispatch('updateStorageKeyToken')
  50. resolve(data)
  51. })
  52. .catch((err) => {
  53. reject(err)
  54. })
  55. })
  56. },
  57. // 验证码登录
  58. codeLoginAction({ commit, dispatch }, loginData) {
  59. return new Promise((resolve, reject) => {
  60. verificationCodeApi({ ...loginData })
  61. .then(async ({ data }) => {
  62. commit('CHNAGE_USER_INFO', data.userInfo)
  63. uni.showToast({
  64. title: '登录成功'
  65. })
  66. console.log(data)
  67. await dispatch('updateStorageKeyToken')
  68. resolve(data)
  69. })
  70. .catch((err) => {
  71. reject(err)
  72. })
  73. })
  74. },
  75. // 微信登陆
  76. wxLogin({ commit, dispatch }, code) {
  77. return new Promise((resolve, reject) => {
  78. wxLoginApi({ code })
  79. .then(async ({ data }) => {
  80. commit('CHNAGE_USER_INFO', data.userInfo)
  81. uni.showToast({
  82. title: '登录成功'
  83. })
  84. await dispatch('updateStorageKeyToken')
  85. resolve(data)
  86. })
  87. .catch((err) => {
  88. reject(err)
  89. })
  90. })
  91. },
  92. logout({ commit }, isQuiet) {
  93. uni.removeStorageSync(USER_INFO)
  94. commit(CHNAGE_USER_INFO, '')
  95. clearAllCache()
  96. if (isQuiet) {
  97. uni.showToast({
  98. title: '退出成功'
  99. })
  100. setTimeout(() => {
  101. uni.switchTab({
  102. url: '/pages/community-center/community-centerr'
  103. })
  104. }, 2000)
  105. }
  106. },
  107. // 获取新团蜂token
  108. updateStorageKeyToken({ state, dispatch, commit }) {
  109. return new Promise((resolve, reject) => {
  110. const userInfo = uni.getStorageSync(USER_INFO)
  111. if (userInfo && userInfo.phone) {
  112. uni.showLoading({ mask: true })
  113. getAnotherTFTokenApi({ phone: userInfo.phone })
  114. .then((res) => {
  115. uni.setStorageSync(T_STORAGE_KEY, res.data)
  116. uni.hideLoading()
  117. resolve(res.data)
  118. })
  119. .catch((err) => {
  120. uni.hideLoading()
  121. resolve(err)
  122. })
  123. dispatch('updateIdentityInfo')
  124. } else {
  125. uni.showToast({
  126. title: '缺少用户手机号码'
  127. })
  128. resolve('缺少用户手机号码')
  129. }
  130. })
  131. }
  132. }
  133. })
  134. export default store