index.js 997 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import {getSessionData,setSessionData} from '../config/common.js'
  4. Vue.use(Vuex)
  5. const store = new Vuex.Store({
  6. state: {
  7. userInfo: uni.getStorageSync('auth')? JSON.parse(uni.getStorageSync('auth')) : {},
  8. token: getSessionData('token')? getSessionData('token') : '',
  9. },
  10. mutations: {
  11. setToken(state, data){
  12. state.token = data.access_token;
  13. setSessionData('token', state.token)
  14. state.userInfo = data.info;
  15. uni.setStorage({
  16. key: 'auth',
  17. data: JSON.stringify(data.info)
  18. })
  19. // 登录
  20. uni.setTabBarItem({
  21. index: 4,
  22. text: '我的'
  23. })
  24. },
  25. clearToken(state){
  26. state.token = '';
  27. state.userInfo = {};
  28. uni.removeStorage({
  29. key: 'token'
  30. })
  31. uni.removeStorage({
  32. key: 'auth',
  33. success: function (res){
  34. uni.reLaunch({
  35. url: 'pages/index/index', // 退出到首页
  36. })
  37. }
  38. })
  39. },
  40. },
  41. actions: {},
  42. getters: {
  43. userInfo: state=> {
  44. }
  45. },
  46. })
  47. export default store