getProductList.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import Vue from 'vue'
  2. import store from '@/store'
  3. export default function getProductList (payload = {}, option = {}) {
  4. const role = store.state.user.role
  5. // const role = 'shop'
  6. return new Promise(async (resolve, reject) => {
  7. // 筛选项
  8. const filters = {
  9. keyword: {
  10. type: 'input',
  11. title: '商品名称/编码',
  12. key: 'keyword',
  13. props: {
  14. placeholder: '商品名称/编码',
  15. clearable: true,
  16. },
  17. },
  18. category_id1: {
  19. type: 'select',
  20. title: '商品分类',
  21. key: 'category_id1',
  22. props: {
  23. placeholder: '商品分类',
  24. filterable: true,
  25. clearable: true,
  26. },
  27. option: {
  28. type: 'async',
  29. list: [],
  30. },
  31. },
  32. up_status: {
  33. type: 'select',
  34. title: '商品状态',
  35. key: 'up_status',
  36. props: {
  37. placeholder: '商品状态',
  38. filterable: true,
  39. clearable: true,
  40. },
  41. option: {
  42. list: [],
  43. },
  44. },
  45. }
  46. // 操作项
  47. let actions = [
  48. {
  49. title: '添加商品',
  50. action: 'goProductDetail',
  51. props: {
  52. type: 'primary',
  53. },
  54. },
  55. ]
  56. if (role !== 'shop') actions = []
  57. const sortActions = []
  58. // 表格项
  59. // "sale_name"
  60. const columns = {
  61. spu_code: {
  62. title: 'SKU编码',
  63. key: 'spu_code',
  64. align: 'center',
  65. width: 110,
  66. },
  67. id: {
  68. title: '商品ID',
  69. key: 'id',
  70. align: 'center',
  71. },
  72. img: {
  73. nodeType: 'image',
  74. payload: {
  75. key: 'img',
  76. title: '商品图片',
  77. width: 80,
  78. height: 80,
  79. },
  80. },
  81. 'name&sku_name': {
  82. title: '商品名称/编码',
  83. key: 'name&sku_name',
  84. align: 'center',
  85. width: 100,
  86. },
  87. sale_name: {
  88. title: '上架名称',
  89. key: 'sale_name',
  90. align: 'center',
  91. width: 100,
  92. },
  93. price: {
  94. title: '价格',
  95. key: 'price',
  96. align: 'center',
  97. width: 80,
  98. format: {
  99. type: 'price',
  100. },
  101. },
  102. category_name1: {
  103. title: '一级分类',
  104. key: 'category_name1',
  105. align: 'center',
  106. width: 100,
  107. },
  108. category_name2: {
  109. title: '二级分类',
  110. key: 'category_name2',
  111. align: 'center',
  112. width: 100,
  113. },
  114. category_name3: {
  115. title: '三级分类',
  116. key: 'category_name3',
  117. align: 'center',
  118. width: 100,
  119. },
  120. main_label_name: {
  121. title: '检测标签',
  122. key: 'main_label_name',
  123. align: 'center',
  124. width: 100,
  125. },
  126. label: {
  127. title: '商品标签',
  128. key: 'label',
  129. align: 'center',
  130. width: 100,
  131. render: (h, { row }) => {
  132. const element = []
  133. row.label.forEach(item => {
  134. element.push(h('div', [item]))
  135. })
  136. return h('div', {}, [element])
  137. },
  138. },
  139. total_stock: {
  140. title: '库存',
  141. key: 'total_stock',
  142. align: 'center',
  143. width: 80,
  144. },
  145. total_count: {
  146. title: '销量',
  147. key: 'total_count',
  148. align: 'center',
  149. width: 80,
  150. },
  151. up_status: {
  152. nodeType: 'iSwitch',
  153. payload: {
  154. title: role === 'shop' ? '上架' : '销售状态',
  155. key: role === 'shop' ? 'up_status' : 'sale_status',
  156. action: 'switchProduct',
  157. },
  158. },
  159. }
  160. const sortColumns = {}
  161. // 表格扩展(数据操作)
  162. const columnsExtra = {
  163. nodeType: 'action',
  164. payload: {
  165. title: '操作',
  166. list: [
  167. {
  168. title: role === 'shop' ? '编辑' : '查看',
  169. action: 'goProductDetail',
  170. props: {
  171. loading: false,
  172. type: 'primary',
  173. },
  174. },
  175. {
  176. title: '删除',
  177. action: 'deleteProductList',
  178. props: {
  179. loading: false,
  180. type: 'warning',
  181. },
  182. },
  183. ],
  184. },
  185. }
  186. if (role !== 'shop') columnsExtra.payload.list.splice(1, 1)
  187. const sortColumnsExtra = {}
  188. const url = role === 'shop' ? '/product/product' : '/product/operate/product'
  189. try {
  190. const isSort = option.type === 'sort'
  191. if (isSort) payload.per_page = 1000
  192. const { data, extra, meta } = await Vue.http.get(url, {
  193. params: payload,
  194. })
  195. const response = {
  196. filters: isSort ? [] : Object.keys(filters).map(i => filters[i]),
  197. actions: isSort ? sortActions : actions,
  198. data: data,
  199. columns: extra.columns
  200. .map(key => (isSort ? sortColumns : columns)[key])
  201. .filter(Boolean)
  202. .concat(isSort ? sortColumnsExtra : columnsExtra),
  203. page: isSort ? {} : meta.pagination,
  204. title: '商品列表',
  205. }
  206. resolve(response)
  207. } catch (e) {
  208. reject(e)
  209. }
  210. })
  211. }