平安校园
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 lines
3.6 KiB

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. import {
  5. getLoginUser
  6. } from '@/api/user.js'
  7. import {
  8. list as monitorList
  9. } from '@/api/monitor/index.js'
  10. import {
  11. list as buildingList
  12. } from '@/api/building/index.js'
  13. let lifeData = {};
  14. try {
  15. // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
  16. lifeData = uni.getStorageSync('lifeData');
  17. } catch (e) {
  18. }
  19. // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
  20. let saveStateKeys = ['token', 'userInfo', 'allOptions', 'refreshToken','sysInfo'];
  21. // 保存变量到本地存储中
  22. const saveLifeData = function(key, value) {
  23. // 判断变量名是否在需要存储的数组中
  24. if (saveStateKeys.indexOf(key) != -1) {
  25. // 获取本地存储的lifeData对象,将变量添加到对象中
  26. let tmp = uni.getStorageSync('lifeData');
  27. // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
  28. tmp = tmp ? tmp : {};
  29. tmp[key] = value;
  30. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  31. uni.setStorageSync(key, value);
  32. uni.setStorageSync('lifeData', tmp);
  33. }
  34. if (key == 'refreshToken') {
  35. uni.setStorageSync('token',value)
  36. // {
  37. // content: value,
  38. // datetime: new Date().getTime()
  39. // }
  40. }
  41. }
  42. const store = new Vuex.Store({
  43. state: {
  44. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  45. userInfo: lifeData.userInfo || null,
  46. token: lifeData.accessToken || '',
  47. refreshToken: lifeData.refreshToken || '',
  48. allOptions: lifeData.allOptions || {
  49. monitorList: [], //摄像头列表
  50. buildingList:[], //楼栋列表
  51. },
  52. sysInfo: []
  53. },
  54. actions: {
  55. async getUserInfo(context) {
  56. const {
  57. data
  58. } = await getLoginUser();
  59. if (data) {
  60. context.commit('$uStore', {
  61. name: 'userInfo',
  62. value: data
  63. })
  64. }
  65. return data;
  66. },
  67. setToken(context, payload) {
  68. context.commit('$uStore', {
  69. name: 'token',
  70. value: payload.token
  71. })
  72. if (payload.refreshToken) {
  73. context.commit('$uStore', {
  74. name: 'refreshToken',
  75. value: payload.refreshToken || ''
  76. })
  77. }
  78. },
  79. clearToken(context) {
  80. context.commit('$uStore', {
  81. name: 'token',
  82. value: ''
  83. })
  84. context.commit('$uStore', {
  85. name: 'refreshToken',
  86. value: ''
  87. })
  88. },
  89. async getAllOptions(context) {
  90. let allOptions = {}
  91. // 摄像头列表
  92. await monitorList({
  93. pageSize: 1000
  94. }).then(res => {
  95. if (res.code != 200) return
  96. allOptions.monitorList = res.data.list.map(e => {
  97. e.value = e.sensorId
  98. e.label = e.sensorName
  99. return e
  100. })
  101. })
  102. // 楼栋列表
  103. await buildingList().then(res => {
  104. if (res.code != 200) return
  105. allOptions.buildingList = res.data.map(e => {
  106. e.value = e.id
  107. e.label = e.name
  108. return e
  109. })
  110. })
  111. context.commit('$uStore', {
  112. name: 'allOptions',
  113. value: allOptions
  114. })
  115. }
  116. },
  117. mutations: {
  118. $uStore(state, payload) {
  119. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  120. let nameArr = payload.name.split('.');
  121. let saveKey = '';
  122. let len = nameArr.length;
  123. if (nameArr.length >= 2) {
  124. let obj = state[nameArr[0]];
  125. for (let i = 1; i < len - 1; i++) {
  126. obj = obj[nameArr[i]];
  127. }
  128. obj[nameArr[len - 1]] = payload.value;
  129. saveKey = nameArr[0];
  130. } else {
  131. // 单层级变量,在state就是一个普通变量的情况
  132. state[payload.name] = payload.value;
  133. saveKey = payload.name;
  134. }
  135. // 保存变量到本地,见顶部函数定义
  136. saveLifeData(saveKey, state[saveKey])
  137. }
  138. }
  139. })
  140. Vue.prototype.$store = store
  141. export default store