index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import api from "@/api/api"
  4. import MinCache from'@/common/util/MinCache.js'
  5. import { ACCESS_TOKEN,USER_NAME,USER_INFO } from "@/common/util/constants"
  6. Vue.use(Vuex)
  7. export default new Vuex.Store({
  8. state: {
  9. token: '',
  10. userid:'',
  11. username: '',
  12. realname: '',
  13. welcome: '',
  14. avatar: '',
  15. dicts: '',
  16. showColum: {}
  17. },
  18. mutations: {
  19. SET_TOKEN: (state, token) => {
  20. state.token = token
  21. },
  22. SET_NAME: (state, { username, realname, welcome }) => {
  23. state.username = username
  24. state.realname = realname
  25. state.welcome = welcome
  26. },
  27. SET_AVATAR: (state, avatar) => {
  28. state.avatar = avatar
  29. },
  30. SET_SHOWCOLUM: (state, showColum) => {
  31. state.showColum = showColum
  32. },
  33. SET_DICT: (state, dicts) => {
  34. state.dicts = dicts
  35. }
  36. },
  37. actions: {
  38. // 登录
  39. mLogin({ commit }, userInfo) {
  40. return new Promise((resolve, reject) => {
  41. api.login(userInfo).then(response => {
  42. if(response.data.code ==200){
  43. const result = response.data.result
  44. const userInfo = result.userInfo
  45. uni.setStorageSync(ACCESS_TOKEN,result.token);
  46. uni.setStorageSync(USER_INFO,userInfo);
  47. commit('SET_TOKEN', result.token)
  48. commit('SET_DICT', result.sysAllDictItems)
  49. commit('SET_AVATAR', userInfo.avatar)
  50. commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
  51. resolve(response)
  52. }else{
  53. resolve(response)
  54. }
  55. }).catch(error => {
  56. console.log("catch===>response",response)
  57. reject(error)
  58. })
  59. })
  60. },
  61. //手机号登录
  62. PhoneLogin({ commit }, userInfo) {
  63. return new Promise((resolve, reject) => {
  64. api.phoneNoLogin(userInfo).then(response => {
  65. if(response.data.code ==200){
  66. const result = response.data.result
  67. const userInfo = result.userInfo
  68. uni.setStorageSync(ACCESS_TOKEN,result.token);
  69. uni.setStorageSync(USER_INFO,userInfo);
  70. commit('SET_TOKEN', result.token)
  71. commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
  72. commit('SET_AVATAR', userInfo.avatar)
  73. resolve(response)
  74. }else{
  75. reject(response)
  76. }
  77. }).catch(error => {
  78. reject(error)
  79. })
  80. })
  81. },
  82. // 登出
  83. Logout({ commit, state }) {
  84. return new Promise((resolve) => {
  85. let logoutToken = state.token;
  86. commit('SET_TOKEN', '')
  87. uni.removeStorageSync(ACCESS_TOKEN)
  88. api.logout(logoutToken).then(() => {
  89. resolve()
  90. }).catch(() => {
  91. resolve()
  92. })
  93. })
  94. },
  95. },
  96. getters:{
  97. token: state => state.token,
  98. username: state => {state.userid=uni.getStorageSync(USER_INFO).username; return state.username},
  99. nickname: state => {state.userid=uni.getStorageSync(USER_INFO).realname; return state.user.realname},
  100. avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.user.avatar},
  101. userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
  102. showColum:state => {state.userid=uni.getStorageSync(USER_INFO).showColum; return state.showColum},
  103. }
  104. })