index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. // import {request} from '@/common/js/request'
  4. Vue.use(Vuex)
  5. const store = new Vuex.Store({
  6. state: {
  7. openExamine: false, // 是否开启审核状态。用于小程序、App 等审核时,关闭部分功能。TODO 芋艿:暂时没找到刷新的地方
  8. token: '', // 用户身份 Token
  9. userInfo: {}, // 用户基本信息
  10. timerIdent: false, // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看
  11. orderCount: {}, // 订单数量
  12. },
  13. getters: {
  14. hasLogin(state){
  15. return !!state.token;
  16. }
  17. },
  18. mutations: {
  19. //更新state数据
  20. setStateAttr(state, param){
  21. if(param instanceof Array){
  22. for(let item of param){
  23. state[item.key] = item.val;
  24. }
  25. }else{
  26. state[param.key] = param.val;
  27. }
  28. },
  29. //更新token
  30. setToken(state, data){
  31. const {token, tokenExpired} = data;
  32. state.token = token;
  33. uni.setStorageSync('uniIdToken', token);
  34. uni.setStorageSync('tokenExpired', tokenExpired);
  35. this.dispatch('getUserInfo'); //更新用户信息
  36. this.dispatch('getCartCount');//更新购物车数量
  37. uni.$emit('refreshCart');//刷新购物车
  38. this.dispatch('getOrderCount'); //更新订单数量
  39. },
  40. // 退出登录
  41. logout(state) {
  42. state.token = '';
  43. uni.removeStorageSync('uniIdToken');
  44. this.dispatch('getCartCount');//更新购物车数量
  45. uni.$emit('refreshCart');//刷新购物车
  46. this.dispatch('getOrderCount'); //更新订单数量
  47. setTimeout(()=>{
  48. state.userInfo = {};
  49. }, 1100)
  50. },
  51. },
  52. actions: {
  53. //更新用户信息
  54. async getUserInfo({state, commit}){
  55. const res = await request('user', 'get', {}, {
  56. checkAuthInvalid: false
  57. });
  58. if(res.status === 1){
  59. const userInfo = res.data;
  60. commit('setStateAttr', {
  61. key: 'userInfo',
  62. val: userInfo
  63. })
  64. }
  65. },
  66. //更新用户订单数量
  67. async getOrderCount({state, commit}){
  68. let data = {
  69. c0: 0,
  70. c1: 0,
  71. c2: 0,
  72. c3: 0
  73. }
  74. if(state.token){
  75. try {
  76. const res = await request('order', 'getOrderCount');
  77. data = res;
  78. }catch (err){
  79. console.error('更新用户订单数量 => ', err);
  80. }
  81. }
  82. commit('setStateAttr', {
  83. key: 'orderCount',
  84. val: data
  85. })
  86. }
  87. }
  88. })
  89. export default store