index.js 4.55 KB
import Vue from 'vue';
import Vuex from 'vuex';
import cache from '@/utils/cache';
import invoice from './modules/invoice';

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    authed: false, // 已登录状态
    scene: undefined,
    userInfo: cache.get('USERINFO'),
    dict: cache.get('DICT') || [],
    district: cache.get('DISTRICT') || [],
    orderTab: '', // 询价流程跳到订单详情页,订单详情页返回时订单列表页面的tab
    origin: '',
    permission: null,
  },
  getters: {
    userInfo: state => {
      return state.userInfo || cache.get('USERINFO');
    },
    permission: state => {
      return state.permission || cache.get('PERMISSION');
    },
    freightInfo: state => {
      const userInfo = state.userInfo || cache.get('USERINFO') || {};
      return userInfo.userInfo || {};
    },
    district: state => {
      return state.district || cache.get('DISTRICT');
    },
    districtValue: state => code => {
      const list = state.district || cache.get('DISTRICT') || [];
      let flattenList = [];
      function flatten(array) {  
        array.forEach(item => {
          flattenList.push({ code: item.code, name: item.name });
          if (item.children) {
            flatten(item.children);
          }
        });
      }
      flatten(list);
      return flattenList.find(item => item.code === code);
    },
    dict: state => {
      const dict = state.dict || [];
      return dict.length > 0 ? dict : cache.get('DICT') || [];
    },
    // 通过code查询数据字典详情
    dictInfo: state => code => {
      const stateDict = state.dict || [];
      const dict = stateDict.length > 0 ? stateDict : cache.get('DICT') || [];
      const targetDict = dict.find(param => param.code === code) || {};
      return targetDict;
    },
    // 通过code查询数据字典列表
    dictList: (state, getters) => code => {
      const targetDict = getters.dictInfo(code);
      const targetDictList = targetDict.dataDictionaryValueList || [];
      let hash = {};
      return targetDictList.reduce((result, item) => {
        if (!hash[item.valueCode]) {
          hash[item.valueCode] = true;
          if (item.valueCode && item.valueName) {
            result.push(item);
          }
        }
        return result; // 返回结果数组
      }, []);
    },
    // 通过code和value查询数据字典值
    dictValue: (state, getters) => (code, value) => {
      const matchItem = getters.dictObject(code, value) || {};
      return matchItem.valueName || value;
    },
    // 通过code和value查询数据字典值对象
    dictObject: (state, getters) => (code, value) => {
      const dictList = getters.dictList(code);
      const result = dictList.find(data => data.valueCode === value) || {};
      return result;
    },
    orderTab: state => {
      return state.orderTab || 0;
    },
  },
  mutations: {
    // 设置权限
    SET_PERMISSION(state, permission) {
      cache.put('PERMISSION', permission);
      state.permission = permission;
    },
    // 设置用户是否为已绑定账号
    SET_AUTHED(state, authed) {
      state.authed = authed;
      uni.$emit('authed', authed);
    },
    // 设置小程序来源的类型
    SET_ORIGIN(state, origin) {
      state.origin = origin;
    },
    // 设置用户信息
    SET_USER_INFO(state, userInfo) {
      const newUserInfo = { ...state.userInfo, ...userInfo };
      cache.put('USERINFO', newUserInfo);
      state.userInfo = newUserInfo;
    },
    // 设置用户信息
    SET_FREGHT_INFO(state, freightInfo) {
      const newUserInfo = { ...state.userInfo, userInfo: freightInfo };
      cache.put('USERINFO', newUserInfo);
      state.userInfo = newUserInfo;
    },
    // 设置数据字典
    SET_DICT(state, dict = []) {
      state.dict = dict;
      if (dict.length > 0) {
        cache.put('DICT', dict);
      }
    },
    // 设置行政区划
    SET_DISTRICT(state, district = []) {
      state.district = district;
      if (district.length > 0) {
        cache.put('DISTRICT', district);
      }
    },
    // 设置小程序场景
    SET_SCENE(state, scene) {
      state.scene = scene;
    },
    // 设置通用更新STATE方法
    UPDATE(state, { key, value }) {
      if (key) {
        state[key] = value;
      }
    },
    // 退出登录
    LOGOUT(state) {
      state.authed = false;
      state.userInfo = null;
      state.dict = [];
      state.district = [];
      uni.$emit('authed', false);
      cache.remove('USERINFO');
    },
    SET_ORDERTAB(state, orderTab) {
      state.orderTab = orderTab;
    },
  },
  modules:{
		invoice
  }
})

export default store;