index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div id="goods">
  3. <el-row>
  4. <el-col
  5. :span="24"
  6. class="main animate__animated animate__backInRight"
  7. v-loading="loadings"
  8. element-loading-text="拼命加载中"
  9. element-loading-spinner="el-icon-loading"
  10. >
  11. <span v-if="view === 'list'">
  12. <el-col :span="24" class="one"> <span>尊荣商品</span> </el-col>
  13. <data-search :fields="searchFields" v-model="searchInfo" @query="search">
  14. <template #status>
  15. <el-option v-for="i in statusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
  16. </template>
  17. </data-search>
  18. <data-btn :fields="btnList" @add="toAdd"></data-btn>
  19. <data-table
  20. ref="dataTable"
  21. :fields="fields"
  22. :opera="opera"
  23. :data="list"
  24. :total="total"
  25. @query="search"
  26. @edit="toEdit"
  27. @puton="toPuton"
  28. @delete="toDelete"
  29. ></data-table>
  30. </span>
  31. <el-row v-if="view === 'info'">
  32. <el-col :span="24" style="margin: 0 0 10px 0">
  33. <el-col :span="2"><el-button type="primary" size="mini" @click="toBack()">返回</el-button></el-col>
  34. <el-col :span="2"><el-button type="primary" size="mini" @click="toSave(form)">保存</el-button></el-col>
  35. </el-col>
  36. <el-col :span="24">
  37. <data-form :fields="infoFields" :rules="rules" v-model="form" labelWidth="150px" @save="toSave">
  38. <template #status>
  39. <el-option v-for="i in statusList" :key="i.value" :label="i.label" :value="i.value"></el-option>
  40. </template>
  41. <template #brief>
  42. <editor v-model="form.brief" url="/files/point/zrGoods/upload" />
  43. </template>
  44. </data-form>
  45. </el-col>
  46. </el-row> </el-col
  47. ></el-row>
  48. </div>
  49. </template>
  50. <script>
  51. const _ = require('lodash');
  52. import methodsUtil from '@/util/opera';
  53. import { mapState, createNamespacedHelpers } from 'vuex';
  54. const { mapActions: zrGoods } = createNamespacedHelpers('zrGoods');
  55. const { mapActions: dictData } = createNamespacedHelpers('dictData');
  56. export default {
  57. name: 'index',
  58. props: {},
  59. components: { editor: () => import('@/components/editor.vue') },
  60. data: function () {
  61. return {
  62. loadings: true,
  63. view: 'list',
  64. fields: [
  65. { label: '商品名称', model: 'name', showTip: false },
  66. { label: '店铺名称', model: 'shop.name' },
  67. { label: '商品库存', model: 'num' },
  68. { label: '商品状态', model: 'status', format: (i) => this.getStatus(i) },
  69. { label: '排序', model: 'sort' },
  70. ],
  71. opera: [
  72. { label: '修改', method: 'edit' },
  73. { label: '上架', method: 'puton', display: (i) => i.status == '0' },
  74. { label: '下架', method: 'puton', display: (i) => i.status == '1' },
  75. { label: '删除', method: 'delete', confirm: true, type: 'danger' },
  76. ],
  77. btnList: [{ label: '添加', method: 'add' }],
  78. searchFields: [
  79. { label: '商品名称', model: 'name' },
  80. { label: '商品状态', model: 'status', type: 'select' },
  81. ],
  82. searchInfo: {},
  83. list: [],
  84. total: 0,
  85. // info部分
  86. infoFields: [
  87. { label: '商品名称', model: 'name' },
  88. { label: '简短简介', model: 'shot_brief', maxLength: 50 },
  89. { label: '预计发货时间', model: 'send_time' },
  90. { label: '商品状态', model: 'status', type: 'select' },
  91. { label: '商品库存', model: 'num', type: 'number' },
  92. { label: '排序', model: 'sort', type: 'number' },
  93. { label: '消耗尊荣', model: 'cost', type: 'number' },
  94. { label: '商品图片', model: 'file', type: 'upload', url: '/files/point/zrGoods/upload' },
  95. { label: '商品介绍', model: 'brief', custom: true },
  96. ],
  97. rules: {},
  98. form: {},
  99. props: { multiple: true, label: 'label', value: 'code' },
  100. statusList: [],
  101. };
  102. },
  103. created() {
  104. this.searchOthers();
  105. this.search();
  106. },
  107. methods: {
  108. ...dictData({ getDict: 'query' }),
  109. ...zrGoods(['query', 'delete', 'fetch', 'update', 'create']),
  110. ...methodsUtil,
  111. // 查询
  112. async search({ skip = 0, limit = this.$limit, ...others } = {}) {
  113. let query = { skip, limit, ...others };
  114. if (Object.keys(this.searchInfo).length > 0) query = { ...query, ...this.searchInfo };
  115. // 判断是否是个人店铺查询
  116. if (this.user.role.code == 'shopAdmin') query.shop = this.user.shop._id || this.user.shop.id;
  117. const res = await this.query(query);
  118. if (this.$checkRes(res)) {
  119. this.$set(this, `list`, res.data);
  120. this.$set(this, `total`, res.total);
  121. }
  122. this.loadings = false;
  123. },
  124. // 添加自定义
  125. initAddData() {
  126. const obj = { status: '1', shop: _.get(this.user.shop, '_id') };
  127. if (this.user.role.code == 'shopAdmin' && !obj.shop) obj.shop = this.user.shop._id || this.user.shop.id;
  128. this.$set(this, 'form', obj);
  129. },
  130. // 查询其他信息
  131. async searchOthers() {
  132. let res;
  133. res = await this.getDict({ code: 'goods_status' });
  134. if (this.$checkRes(res)) this.$set(this, `statusList`, res.data);
  135. },
  136. // 商品状态
  137. getStatus(data) {
  138. const res = this.statusList.find((f) => f.value === data);
  139. if (res) return res.label;
  140. return '';
  141. },
  142. // 保存
  143. async toSave({ data }) {
  144. let res;
  145. if (data == '' || data == undefined) {
  146. let form = this.form;
  147. if (form.id) res = await this.update(form);
  148. else res = await this.create(form);
  149. } else {
  150. if (data.id) res = await this.update(data);
  151. else res = await this.create(data);
  152. }
  153. if (this.$checkRes(res)) {
  154. this.$message({ type: `success`, message: `维护信息成功` });
  155. this.toBack();
  156. this.search();
  157. }
  158. },
  159. // 执行返回
  160. toBack() {
  161. this.form = {};
  162. this.view = 'list';
  163. },
  164. // 上架
  165. async toPuton({ data }) {
  166. this.$confirm('是否确认上架/下架该商品?', '提示', {
  167. confirmButtonText: '确定',
  168. cancelButtonText: '取消',
  169. type: 'warning',
  170. }).then(async () => {
  171. if (data.status == '1') data.status = '0';
  172. else data.status = '1';
  173. let res;
  174. if (data.id) res = await this.update(data);
  175. if (this.$checkRes(res)) this.$message({ type: `success`, message: `修改成功` });
  176. this.search();
  177. });
  178. },
  179. },
  180. computed: {
  181. ...mapState(['user']),
  182. },
  183. };
  184. </script>
  185. <style lang="less" scoped>
  186. .one {
  187. margin: 0 0 10px 0;
  188. span:nth-child(1) {
  189. font-size: 20px;
  190. font-weight: 700;
  191. margin-right: 10px;
  192. }
  193. }
  194. .el-col {
  195. margin: 10px 0;
  196. }
  197. </style>