shopOperation.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <template>
  2. <div class="m-container">
  3. <h2>
  4. <el-page-header
  5. @back="onCancel"
  6. :content="pageTitle[pageType] + '商品'"
  7. ></el-page-header>
  8. </h2>
  9. <div class="m-core">
  10. <el-form :model="form" :rules="rules" ref="form" label-width="120px">
  11. <el-form-item label="货号" prop="sn">
  12. <el-input
  13. v-model.trim="form.sn"
  14. :disabled="pageDisabled"
  15. placeholder="请输入货号"
  16. style="width: 400px"
  17. ></el-input>
  18. </el-form-item>
  19. <el-form-item label="品牌" prop="brand">
  20. <el-input
  21. v-model.trim="form.brand"
  22. :disabled="pageDisabled"
  23. placeholder="请输入品牌"
  24. style="width: 400px"
  25. ></el-input>
  26. </el-form-item>
  27. <!-- <el-form-item label="备查货号" prop="supplyChannel" v-if="pageType == 'create'">
  28. <el-input v-model.trim="form.supplyChannel" placeholder="请输入备查货号" style="width: 400px"></el-input>
  29. </el-form-item> -->
  30. <el-form-item label="商品名称" prop="name">
  31. <el-input
  32. v-model.trim="form.name"
  33. placeholder="请输入商品名称"
  34. :disabled="pageDisabled"
  35. style="width: 400px"
  36. ></el-input>
  37. </el-form-item>
  38. <el-form-item label="商品类型" prop="type">
  39. <el-select
  40. v-model.trim="form.type"
  41. placeholder="请选择商品类型"
  42. :disabled="pageDisabled"
  43. style="width: 400px !important"
  44. >
  45. <el-option
  46. v-for="(item, index) in goodsType"
  47. :key="index"
  48. :label="item.label"
  49. :value="item.value"
  50. ></el-option>
  51. </el-select>
  52. </el-form-item>
  53. <el-form-item label="商品分类" prop="goodsCategoryId">
  54. <el-select
  55. v-model.trim="form.goodsCategoryId"
  56. placeholder="请选择商品分类"
  57. :disabled="pageDisabled"
  58. style="width: 400px !important"
  59. filterable
  60. >
  61. <el-option
  62. v-for="item in categoryList"
  63. :key="item.value"
  64. :label="item.label"
  65. :value="item.value"
  66. >
  67. </el-option>
  68. </el-select>
  69. </el-form-item>
  70. <el-form-item label="商品型号" prop="specification">
  71. <el-input
  72. v-model.trim="form.specification"
  73. placeholder="请输入商品型号"
  74. :disabled="pageDisabled"
  75. style="width: 400px"
  76. ></el-input>
  77. </el-form-item>
  78. <!-- <el-form-item label="内部库存" prop="stockCount">
  79. <el-input type="number" :disabled="pageType != 'create'" v-model.trim="form.stockCount" placeholder="请输入内部库存" style="width: 400px"></el-input>
  80. </el-form-item>
  81. <el-form-item label="税务库存" prop="taxStockCount">
  82. <el-input type="number" :disabled="pageType != 'create'" v-model.trim="form.taxStockCount" placeholder="请输入税务库存" style="width: 400px"></el-input>
  83. </el-form-item> -->
  84. <el-form-item label="库存类型" prop="stockType">
  85. <el-select
  86. v-model="form.stockType"
  87. placeholder="请选择库存类型"
  88. :disabled="pageDisabled"
  89. style="width: 400px !important"
  90. >
  91. <el-option
  92. v-for="(item, index) in stockType"
  93. :key="index"
  94. :label="item.label"
  95. :value="item.value"
  96. ></el-option>
  97. </el-select>
  98. </el-form-item>
  99. <el-form-item label="市场价" prop="marketPrice">
  100. <el-input
  101. type="number"
  102. placeholder="请输入市场价"
  103. :disabled="pageDisabled"
  104. @mousewheel.native.prevent
  105. v-model.trim="form.marketPrice"
  106. style="width: 400px"
  107. ></el-input>
  108. </el-form-item>
  109. <el-form-item label="零售价" prop="discountPrice">
  110. <el-input
  111. type="number"
  112. placeholder="请输入零售价"
  113. @mousewheel.native.prevent
  114. :disabled="pageDisabled"
  115. v-model.trim="form.discountPrice"
  116. style="width: 400px"
  117. ></el-input>
  118. </el-form-item>
  119. <el-form-item label="商品团购价" prop="groupPurchasePrice">
  120. <el-input
  121. type="number"
  122. placeholder="请输入商品团购价"
  123. @mousewheel.native.prevent
  124. :disabled="pageDisabled"
  125. v-model.trim="form.groupPurchasePrice"
  126. style="width: 400px"
  127. ></el-input>
  128. </el-form-item>
  129. <el-form-item label="学员是否展示" prop="clientShow">
  130. <el-select
  131. v-model="form.clientShow"
  132. placeholder="请选择学员是否展示"
  133. style="width: 400px !important"
  134. >
  135. <el-option label="是" :value="1"></el-option>
  136. <el-option label="否" :value="0"></el-option>
  137. </el-select>
  138. </el-form-item>
  139. <el-form-item label="教务端是否展示" prop="educationalShow">
  140. <el-select
  141. v-model="form.educationalShow"
  142. placeholder="请选择教务端是否展示"
  143. style="width: 400px !important"
  144. >
  145. <el-option label="是" :value="1"></el-option>
  146. <el-option label="否" :value="0"></el-option>
  147. </el-select>
  148. </el-form-item>
  149. <el-form-item label="乐团是否展示" prop="musicGroupShow">
  150. <el-select
  151. v-model="form.musicGroupShow"
  152. placeholder="请选择乐团是否展示"
  153. style="width: 400px !important"
  154. >
  155. <el-option label="是" :value="1"></el-option>
  156. <el-option label="否" :value="0"></el-option>
  157. </el-select>
  158. </el-form-item>
  159. <el-form-item label="是否库存预警" prop="stockWarning">
  160. <el-select
  161. v-model="form.stockWarning"
  162. placeholder="请选择库存预警"
  163. :disabled="pageDisabled"
  164. style="width: 400px !important"
  165. >
  166. <el-option label="是" :value="1"></el-option>
  167. <el-option label="否" :value="0"></el-option>
  168. </el-select>
  169. </el-form-item>
  170. <el-form-item label="是否参与置换" prop="replacementShow">
  171. <el-select
  172. v-model="form.replacementShow"
  173. placeholder="请选择库存预警"
  174. :disabled="pageDisabled"
  175. style="width: 400px !important"
  176. >
  177. <el-option label="是" :value="1"></el-option>
  178. <el-option label="否" :value="0"></el-option>
  179. </el-select>
  180. </el-form-item>
  181. <el-form-item label="商品图片" prop="image">
  182. <el-upload
  183. class="avatar-uploader"
  184. action="/api-web/uploadFile"
  185. accept=".jpg, .jpeg, .png"
  186. :headers="headers"
  187. :disabled="pageDisabled"
  188. :show-file-list="false"
  189. :on-success="handleAvatarSuccess"
  190. :before-upload="beforeAvatarUpload"
  191. >
  192. <img v-if="form.image" :src="form.image" class="avatar" />
  193. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  194. </el-upload>
  195. </el-form-item>
  196. <el-form-item label="商品描述" prop="brief">
  197. <el-input
  198. type="textarea"
  199. v-model.trim="form.brief"
  200. :disabled="pageDisabled"
  201. style="width: 400px"
  202. ></el-input>
  203. </el-form-item>
  204. <el-form-item label="商品详情" prop="desc">
  205. <el-input
  206. type="textarea"
  207. v-model.trim="form.desc"
  208. :disabled="pageDisabled"
  209. style="width: 400px"
  210. ></el-input>
  211. </el-form-item>
  212. <el-form-item v-if="!pageDisabled">
  213. <el-button @click="onSubmit('form')" type="primary"
  214. >立即{{ pageType == "create" ? "创建" : "修改" }}</el-button
  215. >
  216. <el-button @click="onReSet('form')">重置</el-button>
  217. </el-form-item>
  218. </el-form>
  219. </div>
  220. </div>
  221. </template>
  222. <script>
  223. import {
  224. categoryListTree,
  225. goodsAdd,
  226. goodsUpdate,
  227. goodsSingleQuery,
  228. } from "@/api/businessManager";
  229. import { getToken } from "@/utils/auth";
  230. import { goodsType, stockType } from "@/utils/searchArray";
  231. let validPrice = (rule, value, callback) => {
  232. if ((value == "" && typeof value == "string") || value == null) {
  233. callback(new Error("请输入金额"));
  234. } else if (value < 0) {
  235. callback(new Error("输入金额必须大于或等于0"));
  236. } else if (value >= 100000) {
  237. callback(new Error("输入金额必须小于100000"));
  238. } else {
  239. callback();
  240. }
  241. };
  242. let validStock = (rule, value, callback) => {
  243. if ((value == "" && typeof value == "string") || value == null) {
  244. callback(new Error("请输入库存"));
  245. } else if (value < 0) {
  246. callback(new Error("库存数量必须大于或等于0"));
  247. } else {
  248. callback();
  249. }
  250. };
  251. export default {
  252. name: "shopOperation",
  253. data() {
  254. return {
  255. goodsType: goodsType,
  256. stockType: stockType,
  257. categoryList: [],
  258. pageType: null,
  259. pageTitle: {
  260. create: "添加",
  261. update: "修改",
  262. look: "查看",
  263. },
  264. pageDisabled: false,
  265. headers: {
  266. Authorization: getToken(),
  267. },
  268. form: {
  269. sn: null,
  270. brand: null,
  271. supplyChannel: null,
  272. name: null,
  273. type: null,
  274. goodsCategoryId: null,
  275. specification: null,
  276. stockCount: null,
  277. taxStockCount: null,
  278. stockType: null,
  279. marketPrice: null,
  280. discountPrice: null,
  281. groupPurchasePrice: null,
  282. clientShow: null,
  283. educationalShow: null,
  284. musicGroupShow: null,
  285. stockWarning: null,
  286. image: null,
  287. brief: null,
  288. desc: null,
  289. replacementShow:null
  290. },
  291. rules: {
  292. sn: [{ required: true, message: "请输入商品货号", trigger: "blur" }],
  293. brand: [
  294. {
  295. required: true,
  296. message: "请输入品牌",
  297. trigger: "blur",
  298. },
  299. {
  300. min: 2,
  301. max: 30,
  302. message: "长度在 2 到 30 个字符",
  303. trigger: "blur",
  304. },
  305. ],
  306. supplyChannel: [
  307. { required: true, message: "请输入备查货号", trigger: "blur" },
  308. ],
  309. name: [
  310. {
  311. required: true,
  312. message: "请输入商品名称",
  313. trigger: "blur",
  314. },
  315. {
  316. min: 2,
  317. max: 30,
  318. message: "长度在 2 到 30 个字符",
  319. trigger: "blur",
  320. },
  321. ],
  322. type: [
  323. {
  324. required: true,
  325. message: "请选择商品分类",
  326. trigger: "change",
  327. },
  328. ],
  329. goodsCategoryId: [
  330. {
  331. required: true,
  332. message: "请选择商品类型",
  333. trigger: "change",
  334. },
  335. ],
  336. specification: [
  337. {
  338. required: true,
  339. message: "请输入商品型号",
  340. trigger: "blur",
  341. },
  342. {
  343. min: 2,
  344. max: 30,
  345. message: "长度在 2 到 30 个字符",
  346. trigger: "blur",
  347. },
  348. ],
  349. stockCount: [
  350. { required: true, validator: validStock, trigger: "blur" },
  351. ],
  352. taxStockCount: [
  353. { required: true, validator: validStock, trigger: "blur" },
  354. ],
  355. stockType: [
  356. { required: true, message: "请选择库存类型", trigger: "change" },
  357. ],
  358. marketPrice: [
  359. {
  360. required: true,
  361. validator: validPrice,
  362. trigger: "blur",
  363. },
  364. ],
  365. discountPrice: [
  366. {
  367. required: true,
  368. validator: validPrice,
  369. trigger: "blur",
  370. },
  371. ],
  372. groupPurchasePrice: [
  373. {
  374. required: true,
  375. validator: validPrice,
  376. trigger: "blur",
  377. },
  378. ],
  379. clientShow: [
  380. { required: true, message: "请选择学员是否展示", trigger: "change" },
  381. ],
  382. replacementShow:[
  383. { required: true, message: "是否参与乐器置换", trigger: "change" },
  384. ],
  385. educationalShow: [
  386. {
  387. required: true,
  388. message: "请选择教务端是否展示",
  389. trigger: "change",
  390. },
  391. ],
  392. musicGroupShow: [
  393. { required: true, message: "请选择乐团是否展示", trigger: "change" },
  394. ],
  395. stockWarning: [
  396. { required: true, message: "请选择是否库存预警", trigger: "change" },
  397. ],
  398. image: [
  399. {
  400. required: true,
  401. message: "请选择图片",
  402. trigger: "blur",
  403. },
  404. ],
  405. brief: [
  406. {
  407. required: true,
  408. message: "请输入商品描述",
  409. trigger: "blur",
  410. },
  411. ],
  412. desc: [
  413. {
  414. required: true,
  415. message: "请输入商品详情",
  416. trigger: "blur",
  417. },
  418. ],
  419. },
  420. Fsearch: null,
  421. Frules: null,
  422. imageWidthM: 400,
  423. imageHeightM: 400,
  424. };
  425. },
  426. mounted() {
  427. this.init();
  428. },
  429. methods: {
  430. init() {
  431. let query = this.$route.query;
  432. if (query.paramInfo) {
  433. let paramInfo = JSON.parse(query.paramInfo);
  434. this.pageType = paramInfo.type;
  435. this.id = paramInfo.id;
  436. }
  437. this.pageDisabled = this.pageType == "look" ? true : false;
  438. this.getList();
  439. this.getCatagory();
  440. },
  441. onSubmit(formName) {
  442. this.$refs[formName].validate((valid) => {
  443. if (valid) {
  444. if (this.pageType == "create") {
  445. if (this.form.id) {
  446. // 判断有没有Id,如果有则删除
  447. delete this.form.id;
  448. }
  449. this.form.status = "NO"; // 默认上架
  450. goodsAdd(this.form).then((res) => {
  451. this.messageTips("添加", res);
  452. });
  453. } else if (this.pageType == "update") {
  454. goodsUpdate(this.form).then((res) => {
  455. this.messageTips("修改", res);
  456. });
  457. }
  458. } else {
  459. this.$nextTick(() => {
  460. let isError = document.getElementsByClassName("is-error");
  461. isError[0].scrollIntoView({
  462. block: "center",
  463. behavior: "smooth",
  464. });
  465. });
  466. return false;
  467. }
  468. });
  469. },
  470. messageTips(title, res) {
  471. if (res.code == 200) {
  472. this.$message.success(title + "成功");
  473. this.$store.dispatch("delVisitedViews", this.$route);
  474. this.$router.push({
  475. path: "/shopManager/shopManager",
  476. query: {
  477. pageInfo: this.Frules,
  478. searchForm: this.Fsearch,
  479. },
  480. });
  481. } else {
  482. this.$message.error(res.msg);
  483. }
  484. },
  485. onCancel() {
  486. this.$store.dispatch("delVisitedViews", this.$route);
  487. this.$router.push({
  488. path: "/shopManager/shopManager",
  489. });
  490. },
  491. onReSet(formName) {
  492. this.$refs[formName].resetFields();
  493. },
  494. getList() {
  495. if (this.pageType == "create") {
  496. this.form = {
  497. sn: null,
  498. brand: null,
  499. supplyChannel: null,
  500. name: null,
  501. type: null,
  502. goodsCategoryId: null,
  503. specification: null,
  504. stockCount: null,
  505. taxStockCount: null,
  506. stockType: null,
  507. marketPrice: null,
  508. discountPrice: null,
  509. groupPurchasePrice: null,
  510. clientShow: null,
  511. educationalShow: null,
  512. musicGroupShow: null,
  513. stockWarning: null,
  514. image: null,
  515. brief: null,
  516. desc: null,
  517. };
  518. if (this.$refs["form"]) {
  519. this.$refs["form"].resetFields();
  520. }
  521. } else {
  522. goodsSingleQuery(this.id).then((res) => {
  523. if (res.code == 200) {
  524. let result = res.data;
  525. this.form = {
  526. id: result.id,
  527. sn: result.sn,
  528. brand: result.brand,
  529. supplyChannel: result.supplyChannel,
  530. name: result.name,
  531. type: result.type,
  532. goodsCategoryId: result.goodsCategoryId,
  533. specification: result.specification,
  534. stockCount: result.stockCount,
  535. taxStockCount: result.taxStockCount,
  536. stockType: result.stockType,
  537. marketPrice: result.marketPrice,
  538. discountPrice: result.discountPrice,
  539. groupPurchasePrice: result.groupPurchasePrice,
  540. clientShow: result.clientShow,
  541. educationalShow: result.educationalShow,
  542. musicGroupShow: result.musicGroupShow,
  543. stockWarning: result.stockWarning,
  544. image: result.image,
  545. brief: result.brief,
  546. desc: result.desc,
  547. replacementShow:result.replacementShow
  548. };
  549. }
  550. });
  551. }
  552. },
  553. getCatagory() {
  554. categoryListTree({
  555. delFlag: 0,
  556. rows: 9999,
  557. }).then((res) => {
  558. let result = res.data;
  559. if (res.code == 200) {
  560. let tempArray = [];
  561. result.rows.forEach((row) => {
  562. tempArray.push({
  563. label: row.name,
  564. value: row.id,
  565. });
  566. });
  567. this.categoryList = tempArray;
  568. }
  569. });
  570. },
  571. handleAvatarSuccess(res, file) {
  572. this.form.image = res.data.url;
  573. },
  574. beforeAvatarUpload(file) {
  575. const imageType = {
  576. "image/png": true,
  577. "image/jpeg": true,
  578. };
  579. const isImage = imageType[file.type];
  580. const isLt2M = file.size / 1024 / 1024 < 2;
  581. const imageWidth = this.imageWidthM;
  582. const imageHeigh = this.imageHeightM;
  583. const _URL = window.URL || window.webkitURL;
  584. const isSize = new Promise((resolve, reject) => {
  585. const img = new Image();
  586. img.onload = function () {
  587. if (imageWidth && imageHeigh) {
  588. this.width === imageWidth && this.height === imageHeigh
  589. ? resolve()
  590. : reject(`请上传${imageWidth}x${imageHeigh}尺寸图片`);
  591. } else if (imageWidth && !imageHeigh) {
  592. this.width === imageWidth
  593. ? resolve()
  594. : reject(`请上传宽为${imageWidth}的图片`);
  595. } else if (!imageWidth && imageHeigh) {
  596. this.height === imageHeigh
  597. ? resolve()
  598. : reject(`请上传高为${imageHeigh}的图片`);
  599. } else {
  600. resolve();
  601. }
  602. };
  603. img.src = _URL.createObjectURL(file);
  604. }).then(
  605. () => {
  606. return file;
  607. },
  608. (src) => {
  609. this.$message.error(src);
  610. this.uploadImgLoading = false;
  611. return Promise.reject();
  612. }
  613. );
  614. console.log(isSize);
  615. if (!isImage) {
  616. this.$message.error("只能上传图片格式!");
  617. }
  618. if (!isLt2M) {
  619. this.$message.error("上传头像图片大小不能超过 2MB!");
  620. }
  621. return isImage && isLt2M && isSize;
  622. },
  623. },
  624. };
  625. </script>
  626. <style lang="scss" scoped>
  627. .el-button--primary {
  628. background: #14928a;
  629. border-color: #14928a;
  630. color: #fff;
  631. &:hover,
  632. &:active,
  633. &:focus {
  634. background: #14928a;
  635. border-color: #14928a;
  636. color: #fff;
  637. }
  638. }
  639. .el-row {
  640. margin-top: 40px;
  641. }
  642. .el-col {
  643. display: flex;
  644. align-items: center;
  645. margin-bottom: 20px;
  646. justify-content: flex-end;
  647. margin-right: 50%;
  648. }
  649. .el-input-group {
  650. width: 200px;
  651. margin: 0 20px;
  652. }
  653. /deep/.el-tree-node__content {
  654. height: 40px !important;
  655. }
  656. /deep/.avatar-uploader .el-upload {
  657. border: 1px dashed #d9d9d9;
  658. border-radius: 6px;
  659. cursor: pointer;
  660. position: relative;
  661. overflow: hidden;
  662. }
  663. .avatar-uploader .el-upload:hover {
  664. border-color: #409eff;
  665. }
  666. .avatar-uploader-icon {
  667. font-size: 28px;
  668. color: #8c939d;
  669. width: 120px;
  670. height: 120px;
  671. line-height: 120px;
  672. text-align: center;
  673. }
  674. .avatar {
  675. width: 120px;
  676. height: 120px;
  677. display: block;
  678. }
  679. </style>