spec.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <div id="spec">
  3. <template v-if="view === 'list'">
  4. <el-row>
  5. <el-col :span="24" class="btn">
  6. <el-col :span="2">
  7. <el-button size="mini" type="primary" @click="toBack()">返回</el-button>
  8. </el-col>
  9. <el-col :span="20">
  10. <el-breadcrumb separator-class="el-icon-arrow-right">
  11. <el-breadcrumb-item v-for="(item, index) in data" :key="index">{{ item.name }}</el-breadcrumb-item>
  12. </el-breadcrumb>
  13. </el-col>
  14. </el-col>
  15. </el-row>
  16. <data-search :fields="searchFields" v-model="searchInfo" @query="search">
  17. <template #status>
  18. <el-option v-for="i in statusList" :key="i.model" :label="i.label" :value="i.value"></el-option>
  19. </template>
  20. </data-search>
  21. <data-btn :fields="btnFields" @add="toAdd" />
  22. <data-table ref="dataTable" :fields="fields" :opera="opera" :data="list" :total="total" @edit="toEdit" @delete="toDelete" @query="search" @copy="toCopy">
  23. <template #code="{ row, item }">
  24. <el-link type="primary" @click="toData(row)">{{ row[item.model] }}</el-link>
  25. </template>
  26. </data-table>
  27. </template>
  28. <template v-else>
  29. <el-row>
  30. <el-col :span="24">
  31. <el-button type="primary" size="mini" @click="toBackList()">返回</el-button>
  32. </el-col>
  33. <el-col :span="24">
  34. <data-form :span="12" :fields="infoFields" :rules="rules" v-model="form" labelWidth="150px" @save="toSave">
  35. <template #status>
  36. <el-option v-for="i in statusList" :key="i.model" :label="i.label" :value="i.value"></el-option>
  37. </template>
  38. <template #can_group>
  39. <el-option v-for="i in tfList" :key="i.model" :label="i.label" :value="i.value"></el-option>
  40. </template>
  41. </data-form>
  42. </el-col>
  43. </el-row>
  44. </template>
  45. </div>
  46. </template>
  47. <script>
  48. // 找到该商品下的规格定义
  49. const _ = require('lodash');
  50. import methodUtil from '@/util/opera';
  51. import { mapState, createNamespacedHelpers } from 'vuex';
  52. const { mapActions } = createNamespacedHelpers('goodsSpec');
  53. const { mapActions: dictData } = createNamespacedHelpers('dictData');
  54. const { mapActions: goods } = createNamespacedHelpers('goods');
  55. export default {
  56. name: 'spec',
  57. props: {},
  58. components: {},
  59. data: function () {
  60. return {
  61. view: 'list',
  62. fields: [
  63. { label: '规格名称', model: 'name', showTip: false },
  64. { label: '库存', model: 'num' },
  65. { label: '实际销售价格', model: 'sell_money' },
  66. { label: '划掉销售价格', model: 'flow_money' },
  67. { label: '运费', model: 'freight' },
  68. { label: '状态', model: 'status', format: (i) => (i === '0' ? '使用中' : '已禁用') },
  69. {
  70. label: '是否可以团购',
  71. model: 'can_group',
  72. format: (i) => {
  73. let data = this.tfList.find((f) => f.value == i);
  74. if (data) return data.label;
  75. else return '暂无';
  76. },
  77. },
  78. { label: '团购金额', model: 'group_config.money' },
  79. { label: '开团人数', model: 'group_config.need_person' },
  80. { label: '排序', model: 'sort' },
  81. ],
  82. opera: [
  83. { label: '修改', method: 'edit' },
  84. { label: '复制', method: 'copy' },
  85. { label: '删除', method: 'delete', confirm: true, type: 'danger' },
  86. ],
  87. list: [],
  88. total: 0,
  89. limit: 10,
  90. btnFields: [{ label: '添加', method: 'add' }],
  91. defaultSearch: {},
  92. searchInfo: {},
  93. searchFields: [
  94. { label: '规格名称', model: 'name' },
  95. { label: '状态', model: 'status', type: 'select' },
  96. ],
  97. infoFields: [
  98. { label: '规格名称', model: 'name' },
  99. { label: '库存', model: 'num', type: 'number' },
  100. { label: '实际销售价格', model: 'sell_money', type: 'number' },
  101. { label: '划掉销售价格', model: 'flow_money', type: 'number' },
  102. { label: '排序', model: 'sort', type: 'number' },
  103. { label: '运费', model: 'freight', type: 'number' },
  104. { label: '状态', model: 'status', type: 'select' },
  105. { label: '是否可以团购', model: 'can_group', type: 'select' },
  106. { label: '团购金额', model: 'money', type: 'number' },
  107. { label: '开团人数', model: 'need_person', type: 'number' },
  108. { label: '图片', model: 'file', type: 'upload', url: '/files/point/goodsSpec/upload' },
  109. ],
  110. rules: {
  111. freight: [{ required: true, message: '请输入运费', trigger: 'blur' }],
  112. },
  113. form: {},
  114. statusList: [],
  115. // 是否可以团购
  116. tfList: [],
  117. data: [],
  118. };
  119. },
  120. computed: {
  121. ...mapState(['user']),
  122. goods() {
  123. return this.$route.params.id;
  124. },
  125. },
  126. created() {
  127. this.defaultSearch.goods = this.goods;
  128. this.searchOthers();
  129. this.search();
  130. this.toData();
  131. },
  132. methods: {
  133. ...dictData({ getDict: 'query' }),
  134. ...mapActions(['query', 'fetch', 'update', 'delete', 'create']),
  135. ...goods({ goodsFetch: 'fetch' }),
  136. ...methodUtil,
  137. // 添加自定义
  138. initAddData() {
  139. const obj = { goods: this.goods, status: '0', can_group: '1', freight: 0 };
  140. this.$set(this, 'form', obj);
  141. },
  142. async toData(row) {
  143. let data = this.data;
  144. let res = await this.goodsFetch(this.goods);
  145. if (this.$checkRes(res)) data.push(res.data);
  146. this.search();
  147. },
  148. // 复制
  149. async toCopy({ data }) {
  150. this.$confirm('是否确认复制该商品?', '提示', {
  151. confirmButtonText: '确定',
  152. cancelButtonText: '取消',
  153. type: 'warning',
  154. }).then(async () => {
  155. data.name = data.name + '-复制';
  156. delete data.id;
  157. delete data._id;
  158. delete data.meta;
  159. delete data.__v;
  160. let res;
  161. res = await this.create(data);
  162. if (this.$checkRes(res)) {
  163. this.$message({ type: `success`, message: `复制成功` });
  164. this.search();
  165. this.toBackList();
  166. }
  167. });
  168. },
  169. // 保存
  170. async toSave({ data }) {
  171. let group_config = {};
  172. group_config.money = data.money;
  173. group_config.need_person = data.need_person;
  174. data.group_config = group_config;
  175. let res;
  176. if (data.id) res = await this.update(data);
  177. else res = await this.create(data);
  178. if (this.$checkRes(res)) {
  179. this.$message({ type: `success`, message: `维护信息成功` });
  180. this.search();
  181. this.toBackList();
  182. }
  183. },
  184. // 修改
  185. async toEdit({ data }) {
  186. const res = await this.fetch(data._id);
  187. if (this.$checkRes(res)) {
  188. let data = res.data;
  189. const group_config = _.get(data, 'group_config', {});
  190. data = { ...data, ...group_config };
  191. delete data.group_config;
  192. this.$set(this, `form`, data);
  193. this.view = 'info';
  194. } else this.$message.error('未找到指定数据');
  195. },
  196. // 返回
  197. toBack() {
  198. this.data.pop();
  199. window.history.go('-1');
  200. },
  201. // 返回列表
  202. toBackList() {
  203. this.view = 'list';
  204. this.form = {};
  205. },
  206. // 查询其他信息
  207. async searchOthers() {
  208. // 状态
  209. let res = await this.getDict({ code: 'status' });
  210. if (this.$checkRes(res)) this.$set(this, 'statusList', res.data);
  211. // 是否可以团购
  212. res = await this.getDict({ code: 'tf' });
  213. if (this.$checkRes(res)) this.$set(this, 'tfList', res.data);
  214. },
  215. },
  216. metaInfo() {
  217. return { title: this.$route.meta.title };
  218. },
  219. };
  220. </script>
  221. <style lang="less" scoped>
  222. .btn {
  223. margin: 10px 10px 20px 10px;
  224. .el-breadcrumb {
  225. font-size: 16px;
  226. line-height: 30px;
  227. }
  228. }
  229. </style>