app.js 5.37 KB
import baseApiInterface from '@/api/base'

const state = {
  accountList: [],
  accountVal: null,
  mallList: [],
  mallVal: null,
  multiMallVal: [],
  floorList: [],
  floorVal: null,
  multiFloorVal: [],
  zoneList: [],
  zoneVal: null,
  multiZoneVal: [],
  gateList: [],
  gateVal: null,
  multiGateVal: [],
  cacheConditions: {}
}

// account
function getAccountList(query) {
  return new Promise((resolve, reject) => {
    baseApiInterface.account(query)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

// mall
function getMallList(query) {
  return new Promise((resolve, reject) => {
    baseApiInterface.mall(query)
      .then(res => {
        resolve(res)
      })
      .catch(error => {
        reject(error)
      })
  })
}

// floor
function getFloorList(query) {
  return new Promise((resolve, reject) => {
    baseApiInterface.floor(query)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

// zone
function getZoneList(query) {
  return new Promise((resolve, reject) => {
    baseApiInterface.zone(query)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

// gate
function getGateList(query) {
  return new Promise((resolve, reject) => {
    baseApiInterface.gate(query)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

const mutations = {
  SET_ACCOUNT_LIST: (state, accountList) => {
    state.accountList = accountList
  },
  SET_ACCOUNT_VAL: (state, accountVal) => {
    state.accountVal = accountVal
  },
  SET_MALL_LIST: (state, mallList) => {
    state.mallList = mallList
  },
  SET_MALL_VAL: (state, mallVal) => {
    state.mallVal = mallVal
  },
  SET_MULTI_MALL_VAL: (state, multiMallVal) => {
    state.multiMallVal = multiMallVal
  },
  SET_FLOOR_LIST: (state, floorList) => {
    state.floorList = floorList
  },
  SET_FLOOR_VAL: (state, floorVal) => {
    state.floorVal = floorVal
  },
  SET_MULTI_FLOOR_VAL: (state, multiFloorVal) => {
    state.multiFloorVal = multiFloorVal
  },
  SET_ZONE_LIST: (state, zoneList) => {
    state.zoneList = zoneList
  },
  SET_ZONE_VAL: (state, zoneVal) => {
    state.zoneVal = zoneVal
  },
  SET_MULTI_ZONE_VAL: (state, multiZoneVal) => {
    state.multiZoneVal = multiZoneVal
  },
  SET_GATE_LIST: (state, gateList) => {
    state.gateList = gateList
  },
  SET_GATE_VAL: (state, gateVal) => {
    state.gateVal = gateVal
  },
  SET_MULTI_GATE_VAL: (state, multiGateVal) => {
    state.multiGateVal = multiGateVal
  },
  SET_CACHE_VAL: (state, { key, val }) => {
    state.cacheConditions[key] = val
  }
}

const actions = {
  async getAccounts({ commit }, userType, blocQuery) {
    const { data } = await getAccountList(blocQuery)
    console.log('account data', data)
    commit('SET_ACCOUNT_LIST', data.data)
    if (data.data.length) {
      let singleBloc = null
      data.data.forEach((item, index) => {
        if (index === 0) {
          singleBloc = item
        }
      })
      commit('SET_ACCOUNT_VAL', singleBloc)
      return Promise.resolve(singleBloc)
    } else {
      if (userType === 'super') {
        commit('SET_ACCOUNT_VAL', { id: 0, name: 'none' })
      } else {
        console.error('Bloc list is empty!!')
      }
    }
  },
  async getMalls({ commit }, mallQuery, needAll) {
    const { data } = await getMallList(mallQuery)
    commit('SET_MALL_LIST', data.data)
    let singleMall = null
    let multiMall = []
    let temp_multiMall = []
    let temp_mall = null

    multiMall = data.data.map((item, index) => {
      if (index === 0) {
        singleMall = item
      }
      return item
    })
    console.log('<===========================split line===========================>')
    temp_multiMall = multiMall.length ? multiMall.map(item => item.id) : []
    temp_mall = null || singleMall && singleMall.id
    console.log('multiMall=====>', multiMall.map(item => item.id), temp_multiMall)
    console.log('<===========================split line end===========================>')
    commit('SET_MULTI_MALL_VAL', temp_multiMall)
    commit('SET_MALL_VAL', temp_mall)
  },
  async getFloors({ commit }, floorQuery) {
    const { data } = await getFloorList(floorQuery)
    commit('SET_FLOOR_LIST', data.data)
    let singleFloor = null
    let multiFloor = []
    let temp_multiFloor = []
    let temp_floor = null
    multiFloor = data.data.map((item, index) => {
      if (index === 0) {
        singleFloor = item
      }
      return item
    })
    temp_multiFloor = multiFloor.length ? multiFloor.map(item => item.id) : []
    temp_floor = null || singleFloor && singleFloor.id
    commit('SET_MULTI_FLOOR_VAL', temp_multiFloor)
    commit('SET_FLOOR_VAL', temp_floor)
  },
  async getZones({ commit }, zoneQuery) {
    const { data } = await getZoneList(zoneQuery)
    commit('SET_ZONE_LIST', data.data)
    let singleZone = null
    let multiZone = []
    let temp_multiZone = []
    let temp_zone = null

    multiZone = data.data.map((item, index) => {
      if (index === 0) {
        singleZone = item
      }
      return item
    })
    temp_multiZone = multiZone.length ? multiZone.map(item => item.id) : []
    temp_zone = null || singleZone && singleZone.id
    commit('SET_MULTI_ZONE_VAL', temp_multiZone)
    commit('SET_ZONE_VAL', temp_zone)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}