index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. },
  16. modules: {
  17. user: UserModule,
  18. examine: ExamineModule,
  19. },
  20. mutations: {
  21. lock(state, key) {
  22. state.lockData[key] = Util.getTimestamp(null) + 5
  23. },
  24. unlock(state, key) {
  25. state.lockData[key] = Util.getTimestamp(null) + 2
  26. },
  27. setAccessToken(state, accessToken) {
  28. state.accessToken = accessToken
  29. Cache.put('accessToken', accessToken)
  30. },
  31. delAccessToken(state) {
  32. state.accessToken = null;
  33. Cache.remove('accessToken')
  34. },
  35. setActivityId(state, activityId) {
  36. state.activityId = activityId
  37. Cache.put('activityId', activityId)
  38. },
  39. delActivityId(state) {
  40. state.activityId = null
  41. Cache.remove('activityId')
  42. },
  43. setGlobalConfig(state, config) {
  44. state.globalConfig = config
  45. Cache.put('globalConfig', config) // 缓存5分钟
  46. },
  47. addCartItem(state, item) {
  48. let exists = false
  49. state.cartList.filter((sItem, sIndex) => {
  50. if (sItem.goods_id == item.goods_id && sItem.goods_type == item.goods_type) {
  51. exists = true
  52. }
  53. })
  54. if (!exists) {
  55. state.cartList.push(item)
  56. }
  57. },
  58. delCartItem(state, id) {
  59. state.cartList.filter((sItem, sIndex) => {
  60. if (sItem.id == id) {
  61. state.cartList.splice(sIndex, 1)
  62. }
  63. })
  64. },
  65. },
  66. getters: {
  67. isLock(state, key) {
  68. return key => {
  69. const lockTime = state.lockData[key]
  70. const timestamp = Util.getTimestamp(null)
  71. console.log('isLock', key, lockTime, timestamp)
  72. if (lockTime && lockTime > timestamp) {
  73. return true
  74. }
  75. return false
  76. }
  77. },
  78. accessToken(state) {
  79. if (state.accessToken === null) {
  80. state.accessToken = Cache.get('accessToken')
  81. }
  82. return state.accessToken
  83. },
  84. activityId(state) {
  85. if (state.activityId === null) {
  86. state.activityId = Cache.get('activityId') || 0
  87. }
  88. return state.activityId
  89. },
  90. isLogin(state) {
  91. if (state.accessToken === null) {
  92. state.accessToken = Cache.get('accessToken')
  93. }
  94. return !!state.accessToken
  95. },
  96. globalConfig(state) {
  97. if (state.globalConfig === null) {
  98. state.globalConfig = Cache.get('globalConfig')
  99. }
  100. return state.globalConfig
  101. },
  102. }
  103. })
  104. export default store