getCategory.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import Vue from 'vue'
  2. export default function getCategory (payload = {}) {
  3. return new Promise(async (resolve, reject) => {
  4. const id = payload.id
  5. // 名称
  6. const nameComponentData = {
  7. nodeType: 'item',
  8. required: true,
  9. label: '分类名称',
  10. nodeList: [
  11. {
  12. nodeType: 'input',
  13. keyword: 'name',
  14. hub: 'category-detail',
  15. props: {
  16. placeholder: '输入分类名称',
  17. },
  18. },
  19. ],
  20. }
  21. // 分类
  22. const levelComponentData = {
  23. nodeType: 'item',
  24. required: false,
  25. label: '上级分类',
  26. tip: '默认添加一级分类',
  27. nodeList: [
  28. {
  29. nodeType: 'select',
  30. keyword: 'category_id1',
  31. hub: 'category-detail',
  32. props: {
  33. placeholder: '选择一级分类',
  34. filterable: true,
  35. clearable: !id,
  36. // disabled: !!id,
  37. },
  38. },
  39. {
  40. nodeType: 'select',
  41. keyword: 'category_id2',
  42. hub: 'category-detail',
  43. props: {
  44. placeholder: '选择二级分类',
  45. filterable: true,
  46. clearable: !id,
  47. // disabled: !!id,
  48. },
  49. dependency: {
  50. type: 'hide',
  51. keyword: 'category_id1',
  52. },
  53. },
  54. ],
  55. }
  56. // 图标
  57. const icoComponentData = {
  58. nodeType: 'item',
  59. required: true,
  60. label: '分类图标',
  61. tip: '图标尺寸还不知道',
  62. nodeList: [
  63. {
  64. nodeType: 'uploader',
  65. keyword: 'ico',
  66. hub: 'category-detail',
  67. props: {},
  68. },
  69. ],
  70. }
  71. // 描述
  72. const descComponentData = {
  73. nodeType: 'item',
  74. required: false,
  75. label: '分类描述',
  76. nodeList: [
  77. {
  78. nodeType: 'input',
  79. keyword: 'desc',
  80. hub: 'category-detail',
  81. props: {
  82. type: 'textarea',
  83. },
  84. },
  85. ],
  86. }
  87. // 整体组件数据
  88. const componentData = [
  89. {
  90. nodeType: 'block',
  91. nodeList: [
  92. {
  93. nodeType: 'note',
  94. text: '默认添加一级分类',
  95. },
  96. nameComponentData,
  97. levelComponentData,
  98. icoComponentData,
  99. descComponentData,
  100. ],
  101. },
  102. ]
  103. const value = {
  104. name: null,
  105. category_id1: null,
  106. category_id2: null,
  107. ico: null,
  108. desc: null,
  109. }
  110. const error = {
  111. name: '',
  112. category_id1: '',
  113. category_id2: '',
  114. ico: '',
  115. desc: '',
  116. }
  117. const option = {
  118. category_id1: {
  119. type: 'async',
  120. list: [],
  121. refresh: true,
  122. },
  123. category_id2: {
  124. type: 'async',
  125. dependency: {
  126. keyword: 'category_id1',
  127. },
  128. list: [],
  129. refresh: true,
  130. },
  131. }
  132. const categoryDetailData = {
  133. componentData,
  134. value,
  135. error,
  136. option,
  137. }
  138. if (id) {
  139. try {
  140. const { data } = await Vue.http.get(`/product/category/detail?id=${id}`)
  141. const { name, category1, category2, ico, desc } = data
  142. if (category1.id) {
  143. option.category_id1.list.push({ value: category1.id, label: category1.name })
  144. value.category_id1 = category1.id
  145. }
  146. if (category2.id) {
  147. option.category_id2.list.push({ value: category2.id, label: category2.name })
  148. value.category_id2 = category2.id
  149. }
  150. value.id = data.id
  151. value.name = name
  152. value.ico = ico
  153. value.desc = desc
  154. levelComponentData.nodeList[0].props.disabled = !value.category_id1
  155. levelComponentData.nodeList[1].props.disabled = !value.category_id2
  156. resolve({
  157. componentData,
  158. value,
  159. error,
  160. option,
  161. })
  162. } catch (e) {
  163. console.log(e)
  164. }
  165. } else resolve(categoryDetailData)
  166. })
  167. }