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

145 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'];
  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. },
  53. actions: {
  54. async getUserInfo(context) {
  55. const {
  56. data
  57. } = await getLoginUser();
  58. if (data) {
  59. context.commit('$uStore', {
  60. name: 'userInfo',
  61. value: data
  62. })
  63. }
  64. return data;
  65. },
  66. setToken(context, payload) {
  67. context.commit('$uStore', {
  68. name: 'token',
  69. value: payload.token
  70. })
  71. if (payload.refreshToken) {
  72. context.commit('$uStore', {
  73. name: 'refreshToken',
  74. value: payload.refreshToken || ''
  75. })
  76. }
  77. },
  78. clearToken(context) {
  79. context.commit('$uStore', {
  80. name: 'token',
  81. value: ''
  82. })
  83. context.commit('$uStore', {
  84. name: 'refreshToken',
  85. value: ''
  86. })
  87. },
  88. async getAllOptions(context) {
  89. let allOptions = {}
  90. // 摄像头列表
  91. await monitorList({
  92. pageSize: 1000
  93. }).then(res => {
  94. if (res.code != 200) return
  95. allOptions.monitorList = res.data.list.map(e => {
  96. e.value = e.sensorId
  97. e.label = e.sensorName
  98. return e
  99. })
  100. })
  101. // 楼栋列表
  102. await buildingList().then(res => {
  103. if (res.code != 200) return
  104. allOptions.buildingList = res.data.map(e => {
  105. e.value = e.id
  106. e.label = e.name
  107. return e
  108. })
  109. })
  110. context.commit('$uStore', {
  111. name: 'allOptions',
  112. value: allOptions
  113. })
  114. }
  115. },
  116. mutations: {
  117. $uStore(state, payload) {
  118. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  119. let nameArr = payload.name.split('.');
  120. let saveKey = '';
  121. let len = nameArr.length;
  122. if (nameArr.length >= 2) {
  123. let obj = state[nameArr[0]];
  124. for (let i = 1; i < len - 1; i++) {
  125. obj = obj[nameArr[i]];
  126. }
  127. obj[nameArr[len - 1]] = payload.value;
  128. saveKey = nameArr[0];
  129. } else {
  130. // 单层级变量,在state就是一个普通变量的情况
  131. state[payload.name] = payload.value;
  132. saveKey = payload.name;
  133. }
  134. // 保存变量到本地,见顶部函数定义
  135. saveLifeData(saveKey, state[saveKey])
  136. }
  137. }
  138. })
  139. Vue.prototype.$store = store
  140. export default store