index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div id="goods">
  3. <template v-if="view === 'list'">
  4. <data-search :fields="searchFields" v-model="searchInfo" @query="search">
  5. <template #status>
  6. <el-option v-for="i in goodsStatusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
  7. </template>
  8. </data-search>
  9. <data-btn :fields="btnList" @add="toAdd"></data-btn>
  10. <data-table
  11. ref="dataTable"
  12. :fields="fields"
  13. :opera="opera"
  14. :data="list"
  15. :total="total"
  16. :limit="limit"
  17. @query="search"
  18. @edit="toEdit"
  19. @delete="toDelete"
  20. @spec="toSpec"
  21. ></data-table>
  22. </template>
  23. <template v-else>
  24. <el-row>
  25. <el-col :span="24">
  26. <el-button icon="el-icon-back" size="mini" @click="toBack()">返回</el-button>
  27. </el-col>
  28. <el-col :span="24">
  29. <data-form :fields="infoFields" :rules="rules" v-model="form" labelWidth="150px" @save="toSave">
  30. <template #tags="{ item }">
  31. <el-cascader v-model="form[item.model]" :options="tagsList" :props="props" clearable filterable :show-all-levels="false"></el-cascader>
  32. </template>
  33. <template #status>
  34. <el-option v-for="i in goodsStatusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
  35. </template>
  36. </data-form>
  37. </el-col>
  38. </el-row>
  39. </template>
  40. </div>
  41. </template>
  42. <script>
  43. const _ = require('lodash');
  44. import methodsUtil from '@/util/opera';
  45. import { mapState, createNamespacedHelpers } from 'vuex';
  46. const { mapActions: selfShop } = createNamespacedHelpers('selfShop');
  47. const { mapActions: goods } = createNamespacedHelpers('goods');
  48. const { mapActions: goodsTags } = createNamespacedHelpers('goodsTags');
  49. const { mapActions: dictData } = createNamespacedHelpers('dictData');
  50. export default {
  51. name: 'index',
  52. props: {},
  53. components: {},
  54. data: function () {
  55. return {
  56. view: 'list',
  57. fields: [
  58. { label: '商品名称', model: 'name' },
  59. { label: '店铺名称', model: 'shop.name' },
  60. { label: '商品标签', model: 'tags', format: (i) => this.getTags(i) },
  61. { label: '商品状态', model: 'status', format: (i) => this.getStatus(i) },
  62. ],
  63. opera: [
  64. { label: '修改', method: 'edit' },
  65. { label: '库存管理', method: 'spec' },
  66. { label: '删除', method: 'delete', confirm: true, type: 'danger' },
  67. ],
  68. btnList: [{ label: '添加', method: 'add' }],
  69. searchFields: [
  70. { label: '商品名称', model: 'name' },
  71. { label: '商品状态', model: 'status', type: 'select' },
  72. ],
  73. searchInfo: {},
  74. list: [],
  75. total: 0,
  76. limit: 10,
  77. // info部分
  78. infoFields: [
  79. { label: '商品名称', model: 'name' },
  80. { label: '商品标签', model: 'tags', custom: true },
  81. { label: '简短简介', model: 'shot_brief', maxLength: 50 },
  82. { label: '预计发货时间', model: 'send_time' },
  83. { label: '商品图片', model: 'file', type: 'upload', url: '/files/point/goods/upload' },
  84. { label: '商品介绍', model: 'brief', type: 'textarea' },
  85. { label: '商品状态', model: 'status', type: 'select' },
  86. ],
  87. rules: {},
  88. form: {},
  89. tagsList: [],
  90. goodsStatusList: [],
  91. props: { multiple: true, label: 'label', value: 'code' },
  92. shop: {},
  93. };
  94. },
  95. created() {
  96. this.searchOthers();
  97. this.search();
  98. },
  99. methods: {
  100. ...dictData({ getDict: 'query' }),
  101. ...goodsTags(['tree']),
  102. ...selfShop(['getInfo', 'getGoods', 'goodsCreate']),
  103. ...goods(['delete', 'fetch', 'update', 'create']),
  104. ...methodsUtil,
  105. async search({ skip = 0, limit = this.limit, ...others } = {}) {
  106. let query = { skip, limit, ...others };
  107. if (Object.keys(this.searchInfo).length > 0) query = { ...query, ...this.searchInfo };
  108. const res = await this.getGoods(query);
  109. if (this.$checkRes(res)) {
  110. this.$set(this, `list`, res.data);
  111. this.$set(this, `total`, res.total);
  112. }
  113. },
  114. initAddData() {
  115. const obj = {
  116. // tags: [],
  117. status: '1',
  118. shop: _.get(this.shop, '_id'),
  119. };
  120. this.$set(this, 'form', obj);
  121. },
  122. async searchOthers() {
  123. let res = await this.tree();
  124. if (this.$checkRes(res)) this.$set(this, `tagsList`, res.data);
  125. res = await this.getDict({ code: 'goods_status' });
  126. if (this.$checkRes(res)) this.$set(this, `goodsStatusList`, res.data);
  127. res = await this.getInfo();
  128. if (this.$checkRes(res)) this.$set(this, `shop`, res.data);
  129. },
  130. getStatus(data) {
  131. const res = this.goodsStatusList.find((f) => f.value === data);
  132. if (res) return res.label;
  133. return '';
  134. },
  135. getTags(data) {
  136. let list = this.tagsList;
  137. const getChildren = (list) =>
  138. list.map((i) => {
  139. if (i.children) return getChildren(i.children);
  140. return i;
  141. });
  142. list = _.flattenDeep(getChildren(list));
  143. const arr = [];
  144. for (const ts of data) {
  145. const last = _.last(ts);
  146. const r = list.find((f) => f.code === last);
  147. if (r) arr.push(r.label);
  148. }
  149. return arr.join(';');
  150. },
  151. toSpec({ data }) {
  152. console.log(data);
  153. },
  154. },
  155. };
  156. </script>
  157. <style lang="less" scoped></style>