spec.vue 8.4 KB

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