index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Vue from 'vue'
  2. import store from './store'
  3. import getCategories from './getCategories'
  4. import getAttributes from './getAttributes'
  5. import getAttributesDetail from './getAttributesDetail'
  6. import getLabels from './getLabels'
  7. import getCity from './getCity'
  8. export {
  9. getAttributesDetail,
  10. }
  11. export default function getConfig (payload, option = {}) {
  12. return new Promise(async (resolve, reject) => {
  13. const asyncOptionList = {
  14. category_id1: {
  15. action: getCategories,
  16. payload: { parent_id: 0 },
  17. },
  18. category_id2: {
  19. action: getCategories,
  20. payload: { parent_id: option.category_id1 },
  21. },
  22. category_id3: {
  23. action: getCategories,
  24. payload: { parent_id: option.category_id2 },
  25. },
  26. attribute_category_id: {
  27. action: getAttributes,
  28. },
  29. }
  30. const autoOptionList = {
  31. label: {
  32. action: getLabels,
  33. },
  34. city_ids: {
  35. action: getCity,
  36. },
  37. }
  38. if (!payload || !payload.keyword) {
  39. console.warn('获取公共配置必须带有key字段的参数')
  40. reject(payload)
  41. }
  42. if (Array.isArray(payload.keyword)) {
  43. const option = {}
  44. const getList = async keyword => {
  45. try {
  46. if (store.getAll()[keyword]) {
  47. option[keyword] = store.getAll()[keyword]
  48. return
  49. }
  50. if (autoOptionList[keyword]) {
  51. const { action, payload } = autoOptionList[keyword]
  52. const list = await action(payload)
  53. store.set(keyword, list)
  54. option[keyword] = list
  55. return
  56. }
  57. store.setAll(await Vue.http.get('/product/config'))
  58. option[keyword] = store.getAll()[keyword]
  59. } catch (e) {
  60. console.error('Config Error: ', e)
  61. }
  62. }
  63. for (let keyword of payload.keyword) {
  64. await getList(keyword)
  65. }
  66. if (Object.keys(option).length !== payload.keyword.length) {
  67. return reject(option)
  68. }
  69. return resolve(option)
  70. }
  71. const list = store.get(payload.keyword)
  72. if (payload.type === 'async') {
  73. try {
  74. const config = asyncOptionList[payload.keyword]
  75. resolve(await config.action(config.payload))
  76. } catch (e) {
  77. reject(e)
  78. }
  79. } else {
  80. if (!list) {
  81. try {
  82. const configData = await Vue.http.get('/product/config')
  83. store.setAll(configData)
  84. resolve(store.get(payload.keyword) || [])
  85. } catch (e) {
  86. reject(e)
  87. }
  88. } else {
  89. resolve(list)
  90. }
  91. }
  92. })
  93. }