index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. },
  16. mutations: {
  17. SET_TOKEN: (state, token) => {
  18. state.token = token
  19. },
  20. SET_NAME: (state, { username, realname, welcome }) => {
  21. state.username = username
  22. state.realname = realname
  23. state.welcome = welcome
  24. },
  25. SET_AVATAR: (state, avatar) => {
  26. state.avatar = avatar
  27. }
  28. },
  29. actions: {
  30. // 登录
  31. mLogin({ commit }, userInfo) {
  32. return new Promise((resolve, reject) => {
  33. api.login(userInfo).then(response => {
  34. if(response.data.code ==200){
  35. const result = response.data.result
  36. const userInfo = result.userInfo
  37. uni.setStorageSync(ACCESS_TOKEN,result.token);
  38. uni.setStorageSync(USER_INFO,userInfo);
  39. commit('SET_TOKEN', result.token)
  40. commit('SET_AVATAR', userInfo.avatar)
  41. commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
  42. resolve(response)
  43. }else{
  44. resolve(response)
  45. }
  46. }).catch(error => {
  47. console.log("catch===>response",response)
  48. reject(error)
  49. })
  50. })
  51. },
  52. //手机号登录
  53. PhoneLogin({ commit }, userInfo) {
  54. return new Promise((resolve, reject) => {
  55. api.phoneNoLogin(userInfo).then(response => {
  56. if(response.data.code ==200){
  57. const result = response.data.result
  58. const userInfo = result.userInfo
  59. uni.setStorageSync(ACCESS_TOKEN,result.token);
  60. uni.setStorageSync(USER_INFO,userInfo);
  61. commit('SET_TOKEN', result.token)
  62. commit('SET_NAME', { username: userInfo.username,realname: userInfo.realname})
  63. commit('SET_AVATAR', userInfo.avatar)
  64. resolve(response)
  65. }else{
  66. reject(response)
  67. }
  68. }).catch(error => {
  69. reject(error)
  70. })
  71. })
  72. },
  73. // 登出
  74. Logout({ commit, state }) {
  75. return new Promise((resolve) => {
  76. let logoutToken = state.token;
  77. commit('SET_TOKEN', '')
  78. uni.removeStorageSync(ACCESS_TOKEN)
  79. api.logout(logoutToken).then(() => {
  80. resolve()
  81. }).catch(() => {
  82. resolve()
  83. })
  84. })
  85. },
  86. },
  87. getters:{
  88. token: state => state.token,
  89. username: state => {state.userid=uni.getStorageSync(USER_INFO).username; return state.username},
  90. nickname: state => {state.userid=uni.getStorageSync(USER_INFO).realname; return state.user.realname},
  91. avatar: state => {state.userid=uni.getStorageSync(USER_INFO).avatar; return state.user.avatar},
  92. userid:state => {state.userid=uni.getStorageSync(USER_INFO).id; return state.userid},
  93. }
  94. })