add.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <template>
  2. <div class="app-content">
  3. <!-- 花材基本信息 -->
  4. <form @submit="formSubmit">
  5. <div class="module-com input-line-wrap">
  6. <tui-list-cell class="line-cell" :hover="false">
  7. <div class="tui-title">花材名称</div>
  8. <input v-model="form.name" placeholder-class="phcolor" class="tui-input" name="name" placeholder="请填写名称" @input="fillName($event)" maxlength="50" type="text" />
  9. </tui-list-cell>
  10. <tui-list-cell class="line-cell" :hover="false">
  11. <div class="tui-title">花材品种</div>
  12. <view style="color:#CCCCCC;width:450upx;height:45upx;" @click.stop="notModify">{{form.name}}</view>
  13. </tui-list-cell>
  14. <tui-list-cell class="line-cell" :hover="false">
  15. <div class="tui-title">拼音缩写</div>
  16. <input
  17. v-model="form.py"
  18. placeholder-class="phcolor"
  19. class="tui-input"
  20. name="py"
  21. placeholder="请填写拼音首字母缩写"
  22. maxlength="50"
  23. type="text"
  24. />
  25. </tui-list-cell>
  26. <tui-list-cell class="line-cell" :hover="false">
  27. <div class="tui-title">花店价</div>
  28. <input
  29. v-model="form.price"
  30. placeholder-class="phcolor"
  31. class="tui-input"
  32. name="price"
  33. @focus="form.price=''"
  34. placeholder="请填写"
  35. maxlength="50"
  36. type="digit"
  37. />
  38. </tui-list-cell>
  39. <tui-list-cell class="line-cell" :hover="false">
  40. <div class="tui-title">今日特价</div>
  41. <input
  42. v-model="form.discountPrice"
  43. placeholder-class="phcolor"
  44. class="tui-input"
  45. name="discountPrice"
  46. @focus="form.discountPrice=''"
  47. placeholder="不做特价填0"
  48. maxlength="50"
  49. type="digit"
  50. />
  51. </tui-list-cell>
  52. <tui-list-cell class="line-cell" :hover="false">
  53. <div class="tui-title">库存</div>
  54. <input v-model="form.stock" style="width:340upx;" placeholder-class="phcolor" @focus="form.stock=''" @input="changeStock($event)" class="tui-input" name="stock" placeholder="请输入" type="digit"/>
  55. <button v-if="productData.id > 0" class="admin-button-com blue small" @click="goStockDetail(productData.id)">库存明细</button>
  56. </tui-list-cell>
  57. <tui-list-cell class="line-cell" :arrow="true">
  58. <div class="tui-title">分类</div>
  59. <picker
  60. mode="selector"
  61. :value="form.classId"
  62. :range="itemClassData"
  63. range-key="name"
  64. @change="changeItemClassFn"
  65. class="tui-input"
  66. >
  67. <input v-model="form.classId" name="classId" type="text" hidden />
  68. <div v-if="form.classId" style="height:45upx;" >{{ itemClassSelectedData.name }}</div>
  69. <div class="tui-placeholder" style="height:45upx;" v-else>请选择</div>
  70. </picker>
  71. </tui-list-cell>
  72. <tui-list-cell class="line-cell" :arrow="true">
  73. <div class="tui-title">大单位</div>
  74. <picker
  75. mode="selector"
  76. :value="form.bigUnitId"
  77. :range="unitData"
  78. range-key="name"
  79. @change="changeBigUnitFn"
  80. class="tui-input"
  81. >
  82. <input v-model="form.bigUnitId" name="bigUnitId" type="text" hidden />
  83. <div v-if="form.bigUnitId">{{ bigUnitSelectedData.name }}</div>
  84. <div class="tui-placeholder" style="height:45upx;" v-else>请选择</div>
  85. </picker>
  86. </tui-list-cell>
  87. <tui-list-cell class="line-cell" :arrow="true">
  88. <div class="tui-title">小单位</div>
  89. <picker
  90. mode="selector"
  91. :value="form.smallUnitId"
  92. :range="unitData"
  93. range-key="name"
  94. @change="changeSmallUnitFn"
  95. class="tui-input"
  96. >
  97. <input v-model="form.smallUnitId" name="smallUnitId" type="text" hidden />
  98. <div v-if="form.smallUnitId">{{ smallUnitSelectedData.name }}</div>
  99. <div class="tui-placeholder" v-else>请选择</div>
  100. </picker>
  101. </tui-list-cell>
  102. <tui-list-cell class="line-cell" :hover="false">
  103. <div class="tui-title">散客加价</div>
  104. <input
  105. v-model="form.skAddPrice"
  106. placeholder-class="phcolor"
  107. class="tui-input"
  108. name="skAddPrice"
  109. @focus="form.skAddPrice=''"
  110. placeholder="请填写"
  111. maxlength="50"
  112. type="digit"
  113. />
  114. </tui-list-cell>
  115. <tui-list-cell class="line-cell" :hover="false">
  116. <div class="tui-title">花店加价</div>
  117. <input
  118. v-model="form.addPrice"
  119. placeholder-class="phcolor"
  120. class="tui-input"
  121. name="addPrice"
  122. @focus="form.addPrice=''"
  123. placeholder="请填写"
  124. maxlength="50"
  125. type="digit"
  126. />
  127. </tui-list-cell>
  128. <tui-list-cell class="line-cell" :hover="false">
  129. <div class="tui-title">单位比</div>
  130. <input
  131. v-model="form.ratio"
  132. placeholder-class="phcolor"
  133. class="tui-input"
  134. name="ratio"
  135. placeholder="如:1扎=20支,请填写20"
  136. maxlength="50"
  137. type="number"
  138. />
  139. </tui-list-cell>
  140. <tui-list-cell class="line-cell" :hover="false">
  141. <div class="tui-title">每扎重量</div>
  142. <input
  143. v-model="form.weight"
  144. placeholder-class="phcolor"
  145. class="tui-input"
  146. name="weight"
  147. placeholder="单位:公斤"
  148. maxlength="50"
  149. type="digit"
  150. />
  151. </tui-list-cell>
  152. <tui-list-cell class="line-cell" :hover="false">
  153. <div class="tui-title">库存预警值</div>
  154. <input
  155. v-model="form.stockWarning"
  156. placeholder-class="phcolor"
  157. class="tui-input"
  158. name="stockWarning"
  159. placeholder="请填写"
  160. maxlength="50"
  161. type="number"
  162. />
  163. </tui-list-cell>
  164. <tui-list-cell class="line-cell" :hover="false">
  165. <div class="tui-title">上架状态</div>
  166. <div class="switch_bx">
  167. <switch :checked="form.status == 2 ? false : true" @change="statusChange" style="transform: scale(0.8,0.8)" />
  168. <text v-if="form.status == 1">上架</text>
  169. <text v-else>下架</text>
  170. </div>
  171. </tui-list-cell>
  172. <tui-list-cell class="line-cell" :hover="false">
  173. <div class="tui-title">状态</div>
  174. <div class="switch_bx">
  175. <switch :checked="form.delStatus == 0 ? true : false" @change="delStatusChange" style="transform: scale(0.8,0.8)" />
  176. <text v-if="form.delStatus == 0">启用</text>
  177. <text v-else>停用</text>
  178. </div>
  179. </tui-list-cell>
  180. </div>
  181. <div class="module-com input-line-wrap goods-wrap">
  182. <div class="module-tit">
  183. <span>花材图片</span>
  184. <span class="app-color-3">(建议比例 1:1)</span>
  185. </div>
  186. <view class="module-det">
  187. <htz-image-upload :max="1" :compress="true" v-model="currentImgData" :headers="headers"
  188. @uploadSuccess="imgUploadSuccess" @imgDelete="imgDeleteFn" :action="getLoginInfo.imgUploadApi">
  189. </htz-image-upload>
  190. </view>
  191. </div>
  192. <div class="app-footer">
  193. <button class="admin-button-com big default" @click="goBack()">取消</button>
  194. <button v-if="option.ptItemId" class="admin-button-com big blue" formType="submit" @click="status = 1">添加</button>
  195. <button v-else class="admin-button-com big blue" formType="submit" @click="status = 1">确认</button>
  196. </div>
  197. </form>
  198. </div>
  199. </template>
  200. <script>
  201. import TuiListCell from "@/components/plugin/list-cell";
  202. const form = require("@/utils/formValidation.js");
  203. import { getAllItemClass } from "@/api/item-class";
  204. import { getAllUnit } from "@/api/unit";
  205. import { addProduct,updateProduct,getProductDetail,py } from "@/api/product";
  206. import { getPtItemDetail } from "@/api/pt-item";
  207. import { mapGetters } from "vuex"
  208. //图片上传插件来源:https://ext.dcloud.net.cn/plugin?id=2922 已改造,不能再升级 shish 20211228
  209. import htzImageUpload from './components/htz-image-upload/htz-image-upload.vue'
  210. export default {
  211. name: "item-detail",
  212. components: {
  213. TuiListCell,
  214. htzImageUpload
  215. },
  216. data() {
  217. return {
  218. status: 1,
  219. currentImgData: [],
  220. form: {
  221. id:0,
  222. name:"",
  223. py:"",
  224. stock:'',
  225. ratio:'',
  226. bigUnitId:1,
  227. smallUnitId:2,
  228. classId:'',
  229. price:'',
  230. skAddPrice:'',
  231. addPrice:'',
  232. discountPrice:'',
  233. weight:'',
  234. cover:[],
  235. stockWarning:'',
  236. status:2,
  237. delStatus:0,
  238. itemId:0,
  239. },
  240. itemClassSelectedData:{},
  241. itemClassData:[],
  242. unitData:[],
  243. smallUnitSelectedData:{},
  244. bigUnitSelectedData:{},
  245. hasRequest:0,
  246. productData:{id:0},
  247. headers:{token:''}
  248. };
  249. },
  250. computed:{
  251. ...mapGetters(["getLoginInfo","getDictionariesInfo"]),
  252. },
  253. onLoad(option) {
  254. if(option.id){
  255. this.form.id = this.option.id
  256. }
  257. const token = uni.getStorageSync('token')
  258. this.headers.token = token
  259. },
  260. watch:{
  261. },
  262. methods: {
  263. notModify(){
  264. this.$msg('不可修改')
  265. },
  266. goStockDetail(id){
  267. this.$util.pageTo({url: "/pagesStorehouse/stockWarn/detailed?id="+id})
  268. },
  269. statusChange(e){
  270. this.form.status = e.detail.value ? 1 : 2;
  271. },
  272. delStatusChange(e){
  273. this.form.delStatus = e.detail.value ? 0 : 1;
  274. },
  275. //如果库存被修改则花材状态变化
  276. changeStock(event){
  277. let num = Number(event.target.value)
  278. if(num > 0){
  279. this.form.status = 1
  280. }else{
  281. this.form.status = 2
  282. }
  283. },
  284. //获取拼音首字母 shish 20210707
  285. fillName(event){
  286. let val = event.target.value
  287. py({
  288. name:val
  289. }).then(res => {
  290. if(res.data.py){
  291. this.form.py = res.data.py
  292. }
  293. });
  294. },
  295. goBack(){
  296. uni.navigateBack({
  297. delta: 1
  298. });
  299. },
  300. changeBigUnitFn(e){
  301. this.bigUnitSelectedData = this.unitData[e.detail.value];
  302. this.form.bigUnitId = this.bigUnitSelectedData.id;
  303. },
  304. changeSmallUnitFn(e){
  305. this.smallUnitSelectedData = this.unitData[e.detail.value];
  306. this.form.smallUnitId = this.smallUnitSelectedData.id;
  307. },
  308. changeItemClassFn(e) {
  309. this.itemClassSelectedData = this.itemClassData[e.detail.value];
  310. this.form.classId = this.itemClassSelectedData.id;
  311. },
  312. //从平台导入花材 shish 20210826
  313. getPtItemInfo(ptItemId){
  314. getPtItemDetail({id:ptItemId}).then(res=>{
  315. if (this.$util.isEmpty(res.data)) {
  316. return;
  317. }
  318. Object.keys(this.form).forEach((i, index) => {
  319. this.form[i] = res.data[i];
  320. });
  321. this.currentImgData.push(res.data.cover)
  322. this.form.cover = [res.data.shortCover]
  323. this.form.stock = ''
  324. this.form.itemId = ptItemId
  325. this.form.classId = ''
  326. this.form.status = 1
  327. this.form.delStatus = 0
  328. this.form.id = 0
  329. this.form.price = ''
  330. this.form.skAddPrice =''
  331. this.form.addPrice =''
  332. this.form.discountPrice = ''
  333. })
  334. },
  335. getDetail() {
  336. return getProductDetail({ id: this.option.id }).then(res => {
  337. if (this.$util.isEmpty(res.data)) {
  338. return;
  339. }
  340. Object.keys(this.form).forEach((i, index) => {
  341. this.form[i] = res.data[i];
  342. });
  343. this.currentImgData.push(res.data.cover)
  344. this.form.cover = [res.data.shortCover]
  345. this.form.price = res.data.prePrice
  346. this.itemClassSelectedData = this.itemClassData.filter(
  347. e => this.form.classId == e.id
  348. )[0];
  349. this.smallUnitSelectedData = this.unitData.filter(
  350. e => this.form.smallUnitId == e.id
  351. )[0];
  352. this.bigUnitSelectedData = this.unitData.filter(
  353. e => this.form.bigUnitId == e.id
  354. )[0];
  355. this.productData = res.data
  356. });
  357. },
  358. imgUploadSuccess(res) {
  359. var _res = JSON.parse(res.data)
  360. if(_res.code == 1){
  361. this.currentImgData.push(_res.data.smallUrl)
  362. this.form.cover.push(_res.data.shortUrl)
  363. }
  364. },
  365. imgDeleteFn(res){
  366. const index = res.index
  367. this.form.cover.splice(index,1)
  368. },
  369. init() {
  370. //从平台导入花材
  371. if(!this.$util.isEmpty(this.option.ptItemId)){
  372. uni.setNavigationBarTitle({title: "确认花材信息"})
  373. }
  374. if(this.hasRequest == 1){
  375. return
  376. }
  377. this._getAllUnit().then(res=>{
  378. this._getAllItemClass().then(res => {
  379. if(this.form.id == 0){
  380. this.smallUnitSelectedData = this.unitData.filter(e => 2 == e.id)[0];
  381. this.bigUnitSelectedData = this.unitData.filter(e => 1 == e.id)[0];
  382. //从平台导入花材
  383. if(!this.$util.isEmpty(this.option.ptItemId)){
  384. this.getPtItemInfo(this.option.ptItemId)
  385. }
  386. }else{
  387. this.getDetail();
  388. }
  389. });
  390. })
  391. this.hasRequest = 1
  392. },
  393. async _getAllUnit() {
  394. await getAllUnit().then(res => {
  395. if (this.$util.isEmpty(res.data)){
  396. return false;
  397. }
  398. this.unitData = res.data.list;
  399. });
  400. },
  401. async _getAllItemClass() {
  402. await getAllItemClass().then(res => {
  403. if (this.$util.isEmpty(res.data)){
  404. return false;
  405. }
  406. this.itemClassData = res.data;
  407. });
  408. },
  409. // 确认
  410. confirmFn() {
  411. if (this.$util.isEmpty(this.form.cover)) {
  412. this.$msg("请上传图片")
  413. return false
  414. }
  415. let requestHost = addProduct
  416. if(this.form.id && this.form.id != 0){
  417. requestHost = updateProduct;
  418. }else{
  419. if(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.form.stock) == false){
  420. uni.showToast({title:"请填写库存,没有请填0",icon:"none"})
  421. return;
  422. }
  423. if(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.form.discountPrice) == false){
  424. uni.showToast({title:"请填写特价,没有特价请填0",icon:"none"})
  425. return;
  426. }
  427. if(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.form.price) == false){
  428. uni.showToast({title:"请填写售价",icon:"none"})
  429. return;
  430. }
  431. if(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.form.skAddPrice) == false){
  432. uni.showToast({title:"请填写散客加价",icon:"none"})
  433. return;
  434. }
  435. if(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(this.form.addPrice) == false){
  436. uni.showToast({title:"请填写花店加价",icon:"none"})
  437. return;
  438. }
  439. }
  440. let form = JSON.parse(JSON.stringify(this.form))
  441. uni.showLoading({mask:true})
  442. requestHost({...form,cover:this.form.cover[0]}).then(res => {
  443. uni.hideLoading()
  444. if(res.code == 1){
  445. this.$msg("操作成功!");
  446. //返回上一页带参数
  447. let pages = getCurrentPages();
  448. let prevPage = pages[ pages.length - 2 ];
  449. let hidden = 0
  450. if(this.form.stock > this.form.stockWarning){
  451. hidden = 1
  452. }
  453. prevPage.$vm.modifyStock = {id:this.form.id,stock:this.form.stock,hidden:hidden};
  454. uni.navigateBack({})
  455. }
  456. });
  457. },
  458. // 表单验证
  459. formSubmit(e) {
  460. // 表单规则
  461. let rules = [
  462. {
  463. name: "name",
  464. rule: ["required"],
  465. msg: ["请填写名称"]
  466. },
  467. {
  468. name: "py",
  469. rule: ["required"],
  470. msg: ["请填写拼音缩写"]
  471. },{
  472. name: "stock",
  473. rule: ["required"],
  474. msg: ["请填写库存,没有填0"]
  475. },
  476. {
  477. name: "bigUnitId",
  478. rule: ["required"],
  479. msg: ["请选择大单位"]
  480. },
  481. {
  482. name: "smallUnitId",
  483. rule: ["required"],
  484. msg: ["请选择小单位"]
  485. },
  486. {
  487. name: "ratio",
  488. rule: ["required"],
  489. msg: ["请填写单位比"]
  490. },
  491. {
  492. name: "price",
  493. rule: ["required"],
  494. msg: ["请填写售价"]
  495. },
  496. {
  497. name: "skAddPrice",
  498. rule: ["required"],
  499. msg: ["请填写散客加价"]
  500. },
  501. {
  502. name: "addPrice",
  503. rule: ["required"],
  504. msg: ["请填写花店加价"]
  505. },
  506. {
  507. name: "discountPrice",
  508. rule: ["required"],
  509. msg: ["不做特价请填0"]
  510. },
  511. {
  512. name: "weight",
  513. rule: ["required"],
  514. msg: ["请填写重量"]
  515. },
  516. {
  517. name: "classId",
  518. rule: ["required"],
  519. msg: ["请选择分类"]
  520. },
  521. {
  522. name: "stockWarning",
  523. rule: ["required"],
  524. msg: ["请填写库存预警值"]
  525. }
  526. ];
  527. // 进行表单检查
  528. let formData = e.detail.value;
  529. let checkRes = form.validation(formData, rules);
  530. // 验证通过!
  531. if (!checkRes) {
  532. this.confirmFn()
  533. } else {
  534. this.$msg(checkRes);
  535. }
  536. }
  537. }
  538. };
  539. </script>
  540. <style lang="scss" scoped>
  541. .app-content {
  542. min-height: calc(100vh - 100upx);
  543. padding-bottom: 150upx;
  544. }
  545. // 公共模块
  546. .module-com {
  547. margin-bottom: 20upx;
  548. background-color: #fff;
  549. color: $fontColor2;
  550. .module-tit {
  551. padding: 20upx 30upx;
  552. font-size: 28upx;
  553. }
  554. .module-det {
  555. padding: 0 30upx;
  556. }
  557. }
  558. .category-wrap {
  559. align-items: flex-start;
  560. padding-bottom: 0 !important;
  561. .tui-input {
  562. @include disFlex(center, flex-start);
  563. flex-wrap: wrap;
  564. .admin-button-com {
  565. margin-right: 20upx;
  566. margin-bottom: 20upx;
  567. position: relative;
  568. z-index: 9999;
  569. }
  570. }
  571. }
  572. .summary-wrap {
  573. .goods-summary {
  574. height: 400upx;
  575. }
  576. }
  577. .app-footer {
  578. justify-content: space-evenly;
  579. z-index: 9999;
  580. .admin-button-com {
  581. width: 46%;
  582. }
  583. }
  584. </style>