getCategoryList.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import Vue from 'vue'
  2. export default function getCategoryList (payload = {}, option = {}) {
  3. return new Promise(async (resolve, reject) => {
  4. // 筛选项
  5. const filters = {
  6. name: {
  7. type: 'input',
  8. title: '分类名称',
  9. key: 'name',
  10. props: {
  11. placeholder: '输入分类名称',
  12. clearable: true,
  13. },
  14. },
  15. }
  16. // 操作项
  17. const actions = [
  18. {
  19. title: '添加分类',
  20. action: 'goCategoryDetail',
  21. props: {
  22. type: 'primary',
  23. },
  24. },
  25. ]
  26. const sortActions = [
  27. {
  28. title: '保存',
  29. action: 'editCategorySort',
  30. props: {
  31. type: 'success',
  32. },
  33. },
  34. ]
  35. // 表格项
  36. const columns = {
  37. name: {
  38. title: '分类名称',
  39. key: 'name',
  40. align: 'center',
  41. width: 100,
  42. },
  43. level: {
  44. title: '级别',
  45. key: 'level',
  46. align: 'center',
  47. width: 80,
  48. format: {
  49. type: 'config',
  50. },
  51. },
  52. ico: {
  53. nodeType: 'image',
  54. payload: {
  55. key: 'ico',
  56. title: '分类图标',
  57. width: 80,
  58. height: 80,
  59. },
  60. },
  61. desc: {
  62. title: '分类描述',
  63. key: 'desc',
  64. align: 'center',
  65. },
  66. is_open: {
  67. nodeType: 'iSwitch',
  68. payload: {
  69. title: '开启',
  70. key: 'is_open',
  71. action: 'switchCategory',
  72. },
  73. },
  74. }
  75. const sortColumns = {
  76. name: {
  77. title: '分类名称',
  78. key: 'name',
  79. align: 'center',
  80. },
  81. level: {
  82. title: '级别',
  83. key: 'level',
  84. align: 'center',
  85. format: {
  86. type: 'config',
  87. },
  88. },
  89. sort: {
  90. nodeType: 'input',
  91. payload: {
  92. title: '排序',
  93. key: 'sort',
  94. props: {
  95. type: 'number',
  96. number: true,
  97. },
  98. },
  99. },
  100. }
  101. // 表格扩展(数据操作)
  102. const columnsExtra = {
  103. nodeType: 'action',
  104. payload: {
  105. title: '操作',
  106. list: [
  107. {
  108. title: '编辑',
  109. action: 'goCategoryDetail',
  110. props: {
  111. loading: false,
  112. },
  113. },
  114. {
  115. title: '删除',
  116. action: 'deleteCategory',
  117. },
  118. ],
  119. },
  120. }
  121. const sortColumnsExtra = {
  122. nodeType: 'action',
  123. payload: {
  124. title: '操作',
  125. list: [
  126. {
  127. title: '商品排序',
  128. action: 'goCategoryProductSort',
  129. props: {
  130. loading: false,
  131. type: 'primary',
  132. },
  133. },
  134. ],
  135. },
  136. }
  137. try {
  138. const isSort = option.type === 'sort'
  139. if (isSort) payload.per_page = 1000
  140. const { data, extra, meta } = await Vue.http.get('/product/category', {
  141. params: payload,
  142. })
  143. // 处理级别功能和操作
  144. const handleLevel = {
  145. level1 () {
  146. actions.push({
  147. title: '排序',
  148. action: 'goCategorySort',
  149. actionOption: {
  150. parent_id: 0,
  151. type: 'sort',
  152. },
  153. props: {
  154. type: 'primary',
  155. },
  156. })
  157. this.level2()
  158. },
  159. level2 () {
  160. columnsExtra.payload.list.unshift({
  161. title: '查看子分类',
  162. action: 'goCategoryList',
  163. props: {
  164. loading: false,
  165. type: 'primary',
  166. },
  167. },)
  168. },
  169. level3 () {},
  170. }
  171. if (data.length) handleLevel[`level${data[0].level}`]()
  172. // 返回数据
  173. const response = {
  174. filters: isSort ? [] : extra.filters.map(key => filters[key]).filter(Boolean),
  175. actions: isSort ? sortActions : actions,
  176. data,
  177. columns: extra.columns
  178. .map(key => (isSort ? sortColumns : columns)[key])
  179. .filter(Boolean)
  180. .concat(isSort ? sortColumnsExtra : columnsExtra),
  181. page: isSort ? {} : meta.pagination,
  182. }
  183. resolve(response)
  184. } catch (e) {
  185. reject(e)
  186. }
  187. })
  188. }