|
- import Vue from 'vue'
- import Vuex from 'vuex'
- Vue.use(Vuex)
- import {
- getLoginUser
- } from '@/api/user.js'
- import {
- list as monitorList
- } from '@/api/monitor/index.js'
- import {
- list as buildingList
- } from '@/api/building/index.js'
-
- let lifeData = {};
-
- try {
- // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
- lifeData = uni.getStorageSync('lifeData');
- } catch (e) {
-
- }
-
- // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
- let saveStateKeys = ['token', 'userInfo', 'allOptions', 'refreshToken','sysInfo'];
-
- // 保存变量到本地存储中
- const saveLifeData = function(key, value) {
- // 判断变量名是否在需要存储的数组中
- if (saveStateKeys.indexOf(key) != -1) {
- // 获取本地存储的lifeData对象,将变量添加到对象中
- let tmp = uni.getStorageSync('lifeData');
- // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
- tmp = tmp ? tmp : {};
- tmp[key] = value;
- // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
- uni.setStorageSync(key, value);
- uni.setStorageSync('lifeData', tmp);
- }
- if (key == 'refreshToken') {
- uni.setStorageSync('token',value)
- // {
- // content: value,
- // datetime: new Date().getTime()
- // }
- }
- }
- const store = new Vuex.Store({
- state: {
- // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
- userInfo: lifeData.userInfo || null,
- token: lifeData.accessToken || '',
- refreshToken: lifeData.refreshToken || '',
- allOptions: lifeData.allOptions || {
- monitorList: [], //摄像头列表
- buildingList:[], //楼栋列表
- },
- sysInfo: []
- },
- actions: {
- async getUserInfo(context) {
- const {
- data
- } = await getLoginUser();
- if (data) {
- context.commit('$uStore', {
- name: 'userInfo',
- value: data
- })
- }
- return data;
- },
- setToken(context, payload) {
- context.commit('$uStore', {
- name: 'token',
- value: payload.token
- })
- if (payload.refreshToken) {
- context.commit('$uStore', {
- name: 'refreshToken',
- value: payload.refreshToken || ''
- })
- }
- },
- clearToken(context) {
- context.commit('$uStore', {
- name: 'token',
- value: ''
- })
- context.commit('$uStore', {
- name: 'refreshToken',
- value: ''
- })
- },
- async getAllOptions(context) {
- let allOptions = {}
- // 摄像头列表
- await monitorList({
- pageSize: 1000
- }).then(res => {
- if (res.code != 200) return
- allOptions.monitorList = res.data.list.map(e => {
- e.value = e.sensorId
- e.label = e.sensorName
- return e
- })
- })
- // 楼栋列表
- await buildingList().then(res => {
- if (res.code != 200) return
- allOptions.buildingList = res.data.map(e => {
- e.value = e.id
- e.label = e.name
- return e
- })
- })
- context.commit('$uStore', {
- name: 'allOptions',
- value: allOptions
- })
- }
- },
- mutations: {
- $uStore(state, payload) {
- // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
- let nameArr = payload.name.split('.');
- let saveKey = '';
- let len = nameArr.length;
- if (nameArr.length >= 2) {
- let obj = state[nameArr[0]];
- for (let i = 1; i < len - 1; i++) {
- obj = obj[nameArr[i]];
- }
- obj[nameArr[len - 1]] = payload.value;
- saveKey = nameArr[0];
- } else {
- // 单层级变量,在state就是一个普通变量的情况
- state[payload.name] = payload.value;
- saveKey = payload.name;
- }
- // 保存变量到本地,见顶部函数定义
- saveLifeData(saveKey, state[saveKey])
- }
- }
- })
- Vue.prototype.$store = store
- export default store
|