index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * @Author: liuzhenlin 461480418@qq.ocm
  3. * @Date: 2023-01-19 16:03:20
  4. * @LastEditors: liuzhenlin
  5. * @LastEditTime: 2023-01-20 10:23:22
  6. * @Description: file content
  7. * @FilePath: \crm\store\index.js
  8. */
  9. import Vue from 'vue'
  10. import Vuex from 'vuex'
  11. import userApi from '../api/system/user'
  12. import to from 'await-to-js'
  13. Vue.use(Vuex)
  14. const store = new Vuex.Store({
  15. state: {
  16. token: '', //用户token
  17. userId: '',
  18. username: '',
  19. nickName: '',
  20. postName: '',
  21. avatar: '',
  22. followPageDetail: {}, //跟进页面的客户/项目信息
  23. phone: '',
  24. },
  25. getters: {
  26. followPageDetail: (state) => state.followPageDetail,
  27. userId: (state) => state.userId,
  28. token: (state) => state.token,
  29. username: (state) => state.username,
  30. nickName: (state) => state.nickName,
  31. postName: (state) => state.postName,
  32. avatar: (state) => state.avatar,
  33. phone: (state) => state.phone,
  34. },
  35. //mutations定义同步操作的方法
  36. mutations: {
  37. /**
  38. * @description 设置token
  39. * @param {*} state
  40. * @param {*} token
  41. */
  42. setDetails(state, info) {
  43. state.followPageDetail = info
  44. },
  45. /**
  46. * @description 设置token
  47. * @param {*} state
  48. * @param {*} token
  49. */
  50. setToken(state, token) {
  51. state.token = token
  52. uni.setStorageSync('opms_token', token)
  53. },
  54. /**
  55. * @description 设置用户id
  56. * @param {*} state
  57. * @param {*} userId
  58. */
  59. setUserId(state, userId) {
  60. state.userId = userId
  61. },
  62. /**
  63. * @description 设置用户名字
  64. * @param {*} state
  65. * @param {*} username
  66. */
  67. setUsername(state, username) {
  68. state.username = username
  69. },
  70. /**
  71. * @description
  72. * @param {*} state
  73. * @param {*} nickName
  74. */
  75. setNickName(state, nickName) {
  76. state.nickName = nickName
  77. },
  78. setPostName(state, postName) {
  79. state.postName = postName
  80. },
  81. /**
  82. * @description 设置用头像
  83. * @param {*} state
  84. * @param {*} avatar
  85. */
  86. setAvatar(state, avatar) {
  87. state.avatar = avatar
  88. },
  89. /**
  90. * @description 手机号
  91. * @param {*} state
  92. * @param {*} phone
  93. */
  94. setPhone(state, phone) {
  95. state.phone = phone
  96. },
  97. },
  98. actions: {
  99. /**
  100. * @description 登录
  101. * @param {*} { commit }
  102. * @param {*} userInfo
  103. */
  104. async login({ commit, dispatch }, userInfo) {
  105. const [err, res] = await to(userApi.login(userInfo))
  106. if (err) return
  107. uni.showToast({
  108. title: '登录成功',
  109. icon: 'none',
  110. complete: async () => {
  111. commit('setToken', res.data.token)
  112. uni.reLaunch({
  113. url: '/pages/home/index',
  114. })
  115. await dispatch('getUserInfo')
  116. },
  117. })
  118. },
  119. /**
  120. * @description 获取用户信息接口
  121. * @param {*} { commit, dispatch, state }
  122. * @returns
  123. */
  124. async getUserInfo({ commit }) {
  125. const [err, res] = await to(userApi.getUserInfo())
  126. if (err) return
  127. const { id, userName, nickName, avatar, postName, phone } = res.data.entity
  128. if (id) commit('setUserId', id)
  129. if (userName) commit('setUsername', userName)
  130. if (nickName) commit('setNickName', nickName)
  131. if (postName) commit('setPostName', nickName)
  132. if (avatar) commit('setAvatar', avatar)
  133. if (phone) commit('setPhone', phone)
  134. },
  135. /**
  136. * @description 退出登录
  137. * @param {*} { dispatch }
  138. */
  139. async logout({ dispatch }) {
  140. await userApi.logout()
  141. await dispatch('resetAll')
  142. },
  143. /**
  144. * @description 重置token、roles、permission、router、tabsBar等
  145. * @param {*} { commit, dispatch }
  146. */
  147. async resetAll({ commit }) {
  148. commit('setUsername', '游客')
  149. commit('setAvatar', 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')
  150. commit('setToken', '')
  151. uni.removeStorageSync('opms_token')
  152. },
  153. },
  154. })
  155. export default store