index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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);//vue的插件机制
  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. },
  48. getters: {
  49. isLock(state, key) {
  50. return key => {
  51. const lockTime = state.lockData[key]
  52. const timestamp = Util.getTimestamp(null)
  53. console.log('isLock', key, lockTime, timestamp)
  54. if (lockTime && lockTime > timestamp) {
  55. return true
  56. }
  57. return false
  58. }
  59. },
  60. accessToken(state) {
  61. if (state.accessToken === null) {
  62. state.accessToken = Cache.get('accessToken')
  63. }
  64. return state.accessToken
  65. },
  66. activityId(state) {
  67. if (state.activityId === null) {
  68. state.activityId = Cache.get('activityId') || 0
  69. }
  70. return state.activityId
  71. },
  72. isLogin(state) {
  73. if (state.accessToken === null) {
  74. state.accessToken = Cache.get('accessToken')
  75. }
  76. return !!state.accessToken
  77. },
  78. globalConfig(state) {
  79. if (state.globalConfig === null) {
  80. state.globalConfig = Cache.get('globalConfig')
  81. }
  82. return state.globalConfig
  83. },
  84. }
  85. })
  86. export default store