import Vue from 'vue' import store from './store' import getCategories from './getCategories' import getAttributes from './getAttributes' import getAttributesDetail from './getAttributesDetail' import getLabels from './getLabels' import getCity from './getCity' export { getAttributesDetail, } export default function getConfig (payload, option = {}) { return new Promise(async (resolve, reject) => { const asyncOptionList = { category_id1: { action: getCategories, payload: { parent_id: 0 }, }, category_id2: { action: getCategories, payload: { parent_id: option.category_id1 }, }, category_id3: { action: getCategories, payload: { parent_id: option.category_id2 }, }, attribute_category_id: { action: getAttributes, }, } const autoOptionList = { label: { action: getLabels, }, city_ids: { action: getCity, }, } if (!payload || !payload.keyword) { console.warn('获取公共配置必须带有key字段的参数') reject(payload) } if (Array.isArray(payload.keyword)) { const option = {} const getList = async keyword => { try { if (store.getAll()[keyword]) { option[keyword] = store.getAll()[keyword] return } if (autoOptionList[keyword]) { const { action, payload } = autoOptionList[keyword] const list = await action(payload) store.set(keyword, list) option[keyword] = list return } store.setAll(await Vue.http.get('/product/config')) option[keyword] = store.getAll()[keyword] } catch (e) { console.error('Config Error: ', e) } } for (let keyword of payload.keyword) { await getList(keyword) } if (Object.keys(option).length !== payload.keyword.length) { return reject(option) } return resolve(option) } const list = store.get(payload.keyword) if (payload.type === 'async') { try { const config = asyncOptionList[payload.keyword] resolve(await config.action(config.payload)) } catch (e) { reject(e) } } else { if (!list) { try { const configData = await Vue.http.get('/product/config') store.setAll(configData) resolve(store.get(payload.keyword) || []) } catch (e) { reject(e) } } else { resolve(list) } } }) }