index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. avatar: '',
  21. followPageDetail: {}, //跟进页面的客户/项目信息
  22. },
  23. getters: {
  24. followPageDetail: (state) => state.followPageDetail,
  25. userId: (state) => state.userId,
  26. token: (state) => state.token,
  27. username: (state) => state.username,
  28. nickName: (state) => state.nickName,
  29. avatar: (state) => state.avatar,
  30. },
  31. //mutations定义同步操作的方法
  32. mutations: {
  33. /**
  34. * @description 设置token
  35. * @param {*} state
  36. * @param {*} token
  37. */
  38. setDetails(state, info) {
  39. state.followPageDetail = info
  40. },
  41. /**
  42. * @description 设置token
  43. * @param {*} state
  44. * @param {*} token
  45. */
  46. setToken(state, token) {
  47. state.token = token
  48. uni.setStorageSync('opms_token', token)
  49. },
  50. /**
  51. * @description 设置用户id
  52. * @param {*} state
  53. * @param {*} userId
  54. */
  55. setUserId(state, userId) {
  56. state.userId = userId
  57. },
  58. /**
  59. * @description 设置用户名字
  60. * @param {*} state
  61. * @param {*} username
  62. */
  63. setUsername(state, username) {
  64. state.username = username
  65. },
  66. /**
  67. * @description
  68. * @param {*} state
  69. * @param {*} nickName
  70. */
  71. setNickName(state, nickName) {
  72. state.nickName = nickName
  73. },
  74. /**
  75. * @description 设置用头像
  76. * @param {*} state
  77. * @param {*} avatar
  78. */
  79. setAvatar(state, avatar) {
  80. state.avatar = avatar
  81. },
  82. },
  83. actions: {
  84. /**
  85. * @description 登录
  86. * @param {*} { commit }
  87. * @param {*} userInfo
  88. */
  89. async login({ commit, dispatch }, userInfo) {
  90. const [err, res] = await to(userApi.login(userInfo))
  91. if (err) return
  92. uni.showToast({
  93. title: '登录成功',
  94. icon: 'none',
  95. complete: async() => {
  96. commit('setToken', res.data.token)
  97. uni.reLaunch({
  98. url: '/pages/home/index',
  99. })
  100. await dispatch('getUserInfo')
  101. },
  102. })
  103. },
  104. /**
  105. * @description 获取用户信息接口
  106. * @param {*} { commit, dispatch, state }
  107. * @returns
  108. */
  109. async getUserInfo({ commit }) {
  110. const [err, res] = await to(userApi.getUserInfo())
  111. if (err) return
  112. const { id, userName, nickName, avatar } = res.data.entity
  113. if (id) commit('setUserId', id)
  114. if (userName) commit('setUsername', userName)
  115. if (nickName) commit('setNickName', nickName)
  116. if (avatar) commit('setAvatar', avatar)
  117. },
  118. /**
  119. * @description 退出登录
  120. * @param {*} { dispatch }
  121. */
  122. async logout({ dispatch }) {
  123. await userApi.logout()
  124. await dispatch('resetAll')
  125. },
  126. /**
  127. * @description 重置token、roles、permission、router、tabsBar等
  128. * @param {*} { commit, dispatch }
  129. */
  130. async resetAll({ commit }) {
  131. commit('setUsername', '游客')
  132. commit('setAvatar', 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')
  133. commit('setToken', '')
  134. uni.removeStorageSync('opms_token')
  135. },
  136. },
  137. })
  138. export default store