You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0 KiB

3 years ago
  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,//是否开启审核状态
  8. token: '',
  9. userInfo: {},
  10. timerIdent: false,//全局1s定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启
  11. cartCount: 0, //购物车数量
  12. orderCount: {}, //订单数量
  13. couponCount: 0, //可用优惠券数量
  14. },
  15. getters: {
  16. hasLogin(state){
  17. return !!state.token;
  18. }
  19. },
  20. mutations: {
  21. //更新state数据
  22. setStateAttr(state, param){
  23. if(param instanceof Array){
  24. for(let item of param){
  25. state[item.key] = item.val;
  26. }
  27. }else{
  28. state[param.key] = param.val;
  29. }
  30. },
  31. //更新token
  32. setToken(state, data){
  33. const {token, tokenExpired} = data;
  34. state.token = token;
  35. uni.setStorageSync('uniIdToken', token);
  36. uni.setStorageSync('tokenExpired', tokenExpired);
  37. this.dispatch('getUserInfo'); //更新用户信息
  38. this.dispatch('getCartCount');//更新购物车数量
  39. uni.$emit('refreshCart');//刷新购物车
  40. this.dispatch('getOrderCount'); //更新订单数量
  41. },
  42. //退出登录
  43. logout(state){
  44. state.token = '';
  45. uni.removeStorageSync('uniIdToken');
  46. this.dispatch('getCartCount');//更新购物车数量
  47. uni.$emit('refreshCart');//刷新购物车
  48. this.dispatch('getOrderCount'); //更新订单数量
  49. setTimeout(()=>{
  50. state.userInfo = {};
  51. }, 1100)
  52. },
  53. },
  54. actions: {
  55. //更新用户信息
  56. async getUserInfo({state, commit}){
  57. const res = await request('user', 'get', {}, {
  58. checkAuthInvalid: false
  59. });
  60. if(res.status === 1){
  61. const userInfo = res.data;
  62. commit('setStateAttr', {
  63. key: 'userInfo',
  64. val: userInfo
  65. })
  66. }
  67. },
  68. //更新购物车数量
  69. async getCartCount({state, commit}){
  70. let count = 0;
  71. if(state.token){
  72. try {
  73. const res = await request('cart', 'count');
  74. count = res.total || 0;
  75. }catch (err){
  76. console.error('更新购物车数量 => ', err);
  77. }
  78. }
  79. commit('setStateAttr', {
  80. key: 'cartCount',
  81. val: count
  82. })
  83. },
  84. //更新用户订单数量
  85. async getOrderCount({state, commit}){
  86. let data = {
  87. c0: 0,
  88. c1: 0,
  89. c2: 0,
  90. c3: 0
  91. }
  92. if(state.token){
  93. try {
  94. const res = await request('order', 'getOrderCount');
  95. data = res;
  96. }catch (err){
  97. console.error('更新用户订单数量 => ', err);
  98. }
  99. }
  100. commit('setStateAttr', {
  101. key: 'orderCount',
  102. val: data
  103. })
  104. },
  105. //更新用户优惠券数量
  106. async getCouponCount({state, commit}){
  107. let count = 0;
  108. if(state.token){
  109. try {
  110. const res = await request('coupon', 'getUserCouponCount');
  111. count = res.total;
  112. console.log(res);
  113. }catch (err){
  114. console.error('更新用户优惠券数量 => ', err);
  115. }
  116. }
  117. commit('setStateAttr', {
  118. key: 'couponCount',
  119. val: count
  120. })
  121. },
  122. }
  123. })
  124. export default store