index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import Util from '../common/util.js'
  4. import Cache from '../common/cache.js'
  5. // modules
  6. import UserModule from './module/user.js'
  7. import ExamineModule from './module/examine.js'
  8. Vue.use(Vuex); //vuex的插件机制
  9. const store = new Vuex.Store({
  10. state: {
  11. lockData: {},
  12. accessToken: null,
  13. activityId: null,
  14. globalConfig: {},
  15. ceShi: true
  16. },
  17. modules: {
  18. user: UserModule,
  19. examine: ExamineModule,
  20. },
  21. mutations: {
  22. qiehuan(state, val) {
  23. state.ceShi = val
  24. },
  25. lock(state, key) {
  26. state.lockData[key] = Util.getTimestamp(null) + 5
  27. },
  28. unlock(state, key) {
  29. state.lockData[key] = Util.getTimestamp(null) + 2
  30. },
  31. setAccessToken(state, accessToken) {
  32. state.accessToken = accessToken
  33. Cache.put('accessToken', accessToken)
  34. },
  35. delAccessToken(state) {
  36. state.accessToken = null;
  37. Cache.remove('accessToken')
  38. },
  39. setActivityId(state, activityId) {
  40. state.activityId = activityId
  41. Cache.put('activityId', activityId)
  42. },
  43. delActivityId(state) {
  44. state.activityId = null
  45. Cache.remove('activityId')
  46. },
  47. setGlobalConfig(state, config) {
  48. state.globalConfig = config
  49. Cache.put('globalConfig', config) // 缓存5分钟
  50. },
  51. addCartItem(state, item) {
  52. let exists = false
  53. state.cartList.filter((sItem, sIndex) => {
  54. if (sItem.goods_id == item.goods_id && sItem.goods_type == item.goods_type) {
  55. exists = true
  56. }
  57. })
  58. if (!exists) {
  59. state.cartList.push(item)
  60. }
  61. },
  62. delCartItem(state, id) {
  63. state.cartList.filter((sItem, sIndex) => {
  64. if (sItem.id == id) {
  65. state.cartList.splice(sIndex, 1)
  66. }
  67. })
  68. },
  69. },
  70. getters: {
  71. isLock(state, key) {
  72. return key => {
  73. const lockTime = state.lockData[key]
  74. const timestamp = Util.getTimestamp(null)
  75. console.log('isLock', key, lockTime, timestamp)
  76. if (lockTime && lockTime > timestamp) {
  77. return true
  78. }
  79. return false
  80. }
  81. },
  82. accessToken(state) {
  83. if (state.accessToken === null) {
  84. state.accessToken = Cache.get('accessToken')
  85. }
  86. return state.accessToken
  87. },
  88. activityId(state) {
  89. if (state.activityId === null) {
  90. state.activityId = Cache.get('activityId') || 0
  91. }
  92. return state.activityId
  93. },
  94. isLogin(state) {
  95. if (state.accessToken === null) {
  96. state.accessToken = Cache.get('accessToken')
  97. }
  98. return !!state.accessToken
  99. },
  100. globalConfig(state) {
  101. if (state.globalConfig === null) {
  102. state.globalConfig = Cache.get('globalConfig')
  103. }
  104. return state.globalConfig
  105. },
  106. }
  107. })
  108. export default store