平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 line
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', {
  36. content: value,
  37. datetime: new Date().getTime()
  38. })
  39. }
  40. }
  41. const store = new Vuex.Store({
  42. state: {
  43. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  44. userInfo: lifeData.userInfo || null,
  45. token: lifeData.accessToken || '',
  46. refreshToken: lifeData.refreshToken || '',
  47. allOptions: lifeData.allOptions || {
  48. monitorList: [], //摄像头列表
  49. monitorList:[], //楼栋列表
  50. },
  51. },
  52. actions: {
  53. async getUserInfo(context) {
  54. const {
  55. data
  56. } = await getLoginUser();
  57. if (data) {
  58. context.commit('$uStore', {
  59. name: 'userInfo',
  60. value: data
  61. })
  62. }
  63. return data;
  64. },
  65. setToken(context, payload) {
  66. context.commit('$uStore', {
  67. name: 'token',
  68. value: payload.token
  69. })
  70. if (payload.refreshToken) {
  71. context.commit('$uStore', {
  72. name: 'refreshToken',
  73. value: payload.refreshToken || ''
  74. })
  75. }
  76. },
  77. clearToken(context) {
  78. context.commit('$uStore', {
  79. name: 'token',
  80. value: ''
  81. })
  82. context.commit('$uStore', {
  83. name: 'refreshToken',
  84. value: ''
  85. })
  86. },
  87. async getAllOptions(context) {
  88. let allOptions = {}
  89. // 摄像头列表
  90. await monitorList({
  91. pageSize: 1000
  92. }).then(res => {
  93. if (res.code != 200) return
  94. allOptions.monitorList = res.data.list.map(e => {
  95. e.value = e.sensorId
  96. e.label = e.sensorName
  97. return e
  98. })
  99. })
  100. // 楼栋列表
  101. await buildingList({
  102. pageSize: 1000
  103. }).then(res => {
  104. if (res.code != 200) return
  105. allOptions.monitorList = res.data.list.map(e => {
  106. e.value = e.sensorId
  107. e.label = e.sensorName
  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