addtogood.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. // pages/addtogood/addtogood.js
  2. const app = getApp();
  3. const api = require('../../api/api');
  4. const storageManager = require("../../utils/storage-manager.js");
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. navbarData: {
  11. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  12. title: '添加商品', //导航栏 中间的标题
  13. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  14. },
  15. enterType: '', //进入方式
  16. arealength: 0,
  17. showLadderPrice: false, //阶梯价弹出层显示
  18. showCategory: false, //分类弹出层显示
  19. showSpecification: false, //规格弹出层显示
  20. specificationText: '统一规格', //
  21. /* 表单 */
  22. id: 0, //商品id
  23. goodName: '', //商品名称
  24. goodIntro: '', //商品简介
  25. imageList: [], //图片列表
  26. scribePrice: '', //划线价格
  27. specifications: [], //规格数组
  28. price: '', //售价
  29. stock: '', //库存
  30. categorys: [], //分类数组
  31. categoryIndex: 0, //分类索引
  32. isAddToGoodsBank: 1, //是否添加到商品库
  33. startingPurchase: '', //起购数量
  34. purchaseRestrictionsNumber: '', //限购数量
  35. purchaseRestrictionsSecond: '', //限购次数
  36. startingPurchaseNumber: '', //起购团数
  37. prices: [], //阶梯价格数组
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad(options) {
  43. let that = this;
  44. that.setData({
  45. enterType: options.enterType
  46. })
  47. // 获取分类列表
  48. wx.showLoading({
  49. title: '加载中',
  50. mask: true
  51. })
  52. wx.request({
  53. url: api.Classification_list,
  54. header: {
  55. 'Authorization': wx.getStorageSync('token')
  56. },
  57. method: 'POST',
  58. success(res) {
  59. console.log(res);
  60. if (res.data.code === 1) {
  61. that.setData({
  62. categorys: res.data.data
  63. })
  64. // 编辑商品
  65. if (options.enterType == 'edit') {
  66. that.setData({
  67. id: Number(options.id)
  68. })
  69. wx.showLoading({
  70. title: '加载中',
  71. mask: true
  72. })
  73. wx.request({
  74. url: api.Commodity_info,
  75. header: {
  76. 'Authorization': wx.getStorageSync('token')
  77. },
  78. data: {
  79. id: that.data.id
  80. },
  81. method: 'POST',
  82. success(res) {
  83. wx.hideLoading()
  84. console.log(res);
  85. if (res.data.code === 1) {
  86. let categoryIndex = that.data.categorys.findIndex(
  87. (item) => {
  88. return item.id == res.data.data.classification_id;
  89. }
  90. )
  91. that.setData({
  92. goodName: res.data.data.commodity_name,
  93. goodIntro: res.data.data.commodity_introduction,
  94. imageList: res.data.data.commodity_img,
  95. scribePrice: res.data.data.scribe_price,
  96. specifications: res.data.data.specifications == null ? [] : res.data.data.specifications,
  97. price: res.data.data.price,
  98. stock: res.data.data.stock,
  99. categoryIndex,
  100. isAddToGoodsBank: res.data.data.commodity_warehouse,
  101. startingPurchase: res.data.data.starting_purchase,
  102. purchaseRestrictionsNumber: res.data.data.purchase_restrictions_number,
  103. purchaseRestrictionsSecond: res.data.data.purchase_restrictions_second,
  104. startingPurchaseNumber: res.data.data.starting_purchase_number,
  105. prices: res.data.data.step_price == null ? [] : res.data.data.step_price,
  106. // 规格名称
  107. specificationText: res.data.data.specifications == null ? '统一规格' : res.data.data.specifications[0].title + '/' + res.data.data.specifications[0].price + '加币'
  108. })
  109. } else {
  110. wx.showToast({
  111. title: res.data.msg,
  112. mask: true,
  113. icon: 'none'
  114. })
  115. }
  116. },
  117. fail(err) {
  118. wx.hideLoading()
  119. wx.showToast({
  120. title: '发起网络请求失败',
  121. icon: 'none',
  122. mask: true
  123. })
  124. }
  125. })
  126. }
  127. } else {
  128. wx.showToast({
  129. title: res.data.msg,
  130. mask: true,
  131. icon: 'none'
  132. })
  133. }
  134. },
  135. fail(err) {
  136. wx.showToast({
  137. title: '发起网络请求失败',
  138. icon: 'none',
  139. mask: true
  140. })
  141. },
  142. complete() {
  143. wx.hideLoading()
  144. }
  145. })
  146. },
  147. /**
  148. * 生命周期函数--监听页面初次渲染完成
  149. */
  150. onReady() {
  151. },
  152. /**
  153. * 生命周期函数--监听页面显示
  154. */
  155. onShow() {
  156. },
  157. /**
  158. * 生命周期函数--监听页面隐藏
  159. */
  160. onHide() {
  161. },
  162. /**
  163. * 生命周期函数--监听页面卸载
  164. */
  165. onUnload() {
  166. },
  167. /**
  168. * 页面相关事件处理函数--监听用户下拉动作
  169. */
  170. onPullDownRefresh() {
  171. },
  172. /**
  173. * 页面上拉触底事件的处理函数
  174. */
  175. onReachBottom() {
  176. },
  177. /**
  178. * 用户点击右上角分享
  179. */
  180. onShareAppMessage() {
  181. },
  182. // 输入商品名称
  183. inputGoodName(e) {
  184. this.setData({
  185. goodName: e.detail.value
  186. })
  187. },
  188. // 输入商品简介
  189. inputGoodIntro(e) {
  190. this.setData({
  191. goodIntro: e.detail.value,
  192. arealength: e.detail.value.length
  193. })
  194. },
  195. // 添加商品图片
  196. uploadSmallImage() {
  197. let that = this;
  198. if (that.data.imageList.length < 5) {
  199. // wx.showLoading({
  200. // title: '文件上传中',
  201. // mask: true
  202. // })
  203. wx.request({
  204. url: api.getSignedUrl,
  205. data: {
  206. type: 'image'
  207. },
  208. success: (oss) => {
  209. console.log(oss);
  210. if (oss.statusCode === 200) {
  211. wx.chooseMedia({
  212. count: 1,
  213. mediaType: ['image'],
  214. success: (img) => {
  215. console.log(img);
  216. let key = "image/" + new Date().getTime() + Math.floor(Math.random() * 150) + '.png';
  217. wx.showLoading({
  218. title: '文件上传中',
  219. mask: true
  220. })
  221. wx.uploadFile({
  222. filePath: img.tempFiles[0].tempFilePath,
  223. name: 'file',
  224. url: 'https://' + oss.data.data.host, //输入你的bucketname.endpoint
  225. formData: {
  226. key: key,
  227. policy: oss.data.data.policy, // 输入你获取的的policy
  228. OSSAccessKeyId: oss.data.data.OSSAccessKeyId, // 输入你的AccessKeyId
  229. success_action_status: '200', // 让服务端返回200,不然,默认会返回204
  230. signature: oss.data.data.Signature, // 输入你获取的的signature
  231. },
  232. success: (res) => {
  233. if (res.statusCode == 200) {
  234. let src = 'https://' + oss.data.data.host + '/' + key;
  235. console.log(src);
  236. let imageList = that.data.imageList;;
  237. imageList.push(src);
  238. that.setData({
  239. imageList
  240. })
  241. }
  242. },
  243. fail: (err) => {
  244. console.log(err);
  245. },
  246. complete: () => {
  247. wx.hideLoading()
  248. }
  249. })
  250. },
  251. fail: (err) => {
  252. wx.hideLoading()
  253. }
  254. })
  255. } else {
  256. wx.showToast({
  257. title: '服务器出现错误',
  258. icon: 'none'
  259. })
  260. }
  261. },
  262. fail: (err) => {
  263. console.log(err);
  264. }
  265. })
  266. }
  267. },
  268. // 删除某个小图
  269. deleteSmallImage(e) {
  270. let that = this;
  271. let index = e.currentTarget.dataset.index;
  272. let imageList = that.data.imageList;
  273. imageList.splice(index, 1);
  274. this.setData({
  275. imageList
  276. })
  277. },
  278. // 输入商品划线价格
  279. inputScribePrice(e) {
  280. this.setData({
  281. scribePrice: e.detail.value
  282. })
  283. },
  284. // 打开规格弹出层
  285. showSpecificationPopup() {
  286. let that = this;
  287. if (that.data.prices.length == 0) {
  288. if (that.data.specifications.length == 0) {
  289. that.setData({
  290. showSpecification: true,
  291. specifications: [{
  292. title: '',
  293. price: ''
  294. },
  295. {
  296. title: '',
  297. price: ''
  298. }
  299. ]
  300. })
  301. } else {
  302. that.setData({
  303. showSpecification: true
  304. })
  305. }
  306. } else {
  307. wx.showModal({
  308. title: '提示',
  309. content: '规格和阶梯价不可同时设置,如需设置规格,则会清除阶梯价内所有设置',
  310. success(res) {
  311. if (res.confirm) {
  312. if (that.data.specifications.length == 0) {
  313. that.setData({
  314. prices: [],
  315. showSpecification: true,
  316. specifications: [{
  317. title: '',
  318. price: ''
  319. },
  320. {
  321. title: '',
  322. price: ''
  323. }
  324. ]
  325. })
  326. } else {
  327. that.setData({
  328. prices: [],
  329. showSpecification: true
  330. })
  331. }
  332. } else if (res.cancel) {
  333. console.log('用户点击取消')
  334. }
  335. }
  336. })
  337. }
  338. },
  339. // 关闭规格弹出层
  340. closeSpecificationPopup() {
  341. let that = this;
  342. let isOK = that.judgeNoyNullSpecifications(that.data.specifications);
  343. if (isOK) {
  344. that.setData({
  345. specificationText: that.data.specifications[0].title + '/' + that.data.specifications[0].price + '加币',
  346. price: Number(that.data.specifications[0].price),
  347. showSpecification: false
  348. })
  349. } else {
  350. wx.showToast({
  351. title: '您未完整录入数据',
  352. icon: 'none',
  353. mask: true,
  354. success() {
  355. setTimeout(() => {
  356. that.setData({
  357. specificationText: '统一规格',
  358. specifications: [],
  359. showSpecification: false
  360. })
  361. }, 1500)
  362. }
  363. })
  364. }
  365. },
  366. // 规格输入
  367. specificationsInput(e) {
  368. let index = e.currentTarget.dataset.index;
  369. let value = e.detail.value;
  370. let list = this.data.specifications;
  371. list[index].title = value;
  372. this.setData({
  373. specifications: list
  374. })
  375. },
  376. // 规格金额输入
  377. specificationsPriceInput(e) {
  378. let index = e.currentTarget.dataset.index;
  379. let value = e.detail.value;
  380. let list = this.data.specifications;
  381. list[index].price = value;
  382. this.setData({
  383. specifications: list
  384. })
  385. },
  386. // 添加规格
  387. addSpecification() {
  388. let bulk = {
  389. title: '',
  390. price: ''
  391. }
  392. let specifications = this.data.specifications;
  393. specifications.push(bulk)
  394. this.setData({
  395. specifications: specifications
  396. })
  397. },
  398. // 删除规格
  399. delSpecification(e) {
  400. let index = e.currentTarget.dataset.index
  401. let list = this.data.specifications;
  402. list.splice(index, 1);
  403. this.setData({
  404. specifications: list
  405. })
  406. },
  407. // 输入售价
  408. inputPrice(e) {
  409. if(this.data.specifications[0].title == '统一规格') {
  410. let specifications = this.data.specifications;
  411. specifications[0].price = e.detail.value;
  412. this.setData({
  413. specifications,
  414. })
  415. this.setData({
  416. specificationText: this.data.specifications[0].title + '/' + this.data.specifications[0].price + '加币',
  417. })
  418. }
  419. console.log(e.detail.value)
  420. this.setData({
  421. price: e.detail.value
  422. })
  423. },
  424. // 输入库存
  425. inputStock(e) {
  426. this.setData({
  427. stock: e.detail.value
  428. })
  429. },
  430. // 打开分类弹出层
  431. showCategoryPopup() {
  432. this.setData({
  433. showCategory: true
  434. })
  435. },
  436. // 关闭分类弹出层
  437. closeCategoryPopup() {
  438. this.setData({
  439. showCategory: false
  440. })
  441. },
  442. // 选择分类
  443. selectCategory(e) {
  444. this.setData({
  445. categoryIndex: e.currentTarget.dataset.index
  446. })
  447. },
  448. // 是否添加到商品库
  449. changeIsAddToGoodsBank(e) {
  450. this.setData({
  451. isAddToGoodsBank: e.detail
  452. })
  453. },
  454. // 输入起购数量
  455. inputStartingPurchase(e) {
  456. this.setData({
  457. startingPurchase: e.detail.value
  458. })
  459. },
  460. // 输入限购数量
  461. inputPurchaseRestrictionsNumber(e) {
  462. this.setData({
  463. purchaseRestrictionsNumber: e.detail.value
  464. })
  465. },
  466. // 输入限购次数
  467. inputPurchaseRestrictionsSecond(e) {
  468. this.setData({
  469. purchaseRestrictionsSecond: e.detail.value
  470. })
  471. },
  472. // 输入起购团数
  473. inputStartingPurchaseNumber(e) {
  474. this.setData({
  475. startingPurchaseNumber: e.detail.value
  476. })
  477. },
  478. // 打开阶梯价弹出层
  479. showLadderPricePopup() {
  480. let that = this;
  481. if (that.data.specifications.length == 0) {
  482. if (that.data.prices.length == 0) {
  483. that.setData({
  484. showLadderPrice: true,
  485. prices: [{
  486. number: '',
  487. price: ''
  488. },
  489. {
  490. number: '',
  491. price: ''
  492. }
  493. ]
  494. })
  495. } else {
  496. that.setData({
  497. showLadderPrice: true
  498. })
  499. }
  500. } else {
  501. wx.showModal({
  502. title: '提示',
  503. content: '规格和阶梯价不可同时设置,如需设置阶梯价,则会清除规格内所有设置',
  504. success(res) {
  505. if (res.confirm) {
  506. if (that.data.prices.length == 0) {
  507. that.setData({
  508. specifications: [],
  509. showLadderPrice: true,
  510. prices: [{
  511. number: '',
  512. price: ''
  513. },
  514. {
  515. number: '',
  516. price: ''
  517. }
  518. ]
  519. })
  520. } else {
  521. that.setData({
  522. specifications: [],
  523. showLadderPrice: true
  524. })
  525. }
  526. } else if (res.cancel) {
  527. console.log('用户点击取消')
  528. }
  529. }
  530. })
  531. }
  532. },
  533. // 关闭阶梯价弹出层
  534. closeLadderPricePopup() {
  535. let that = this;
  536. let isOK = that.judgeNoyNullStepPrice(that.data.prices);
  537. if (isOK) {
  538. that.setData({
  539. specificationText: '统一规格',
  540. price: Number(that.data.prices[0].price),
  541. showLadderPrice: false
  542. })
  543. } else {
  544. wx.showToast({
  545. title: '您未完整录入数据',
  546. icon: 'none',
  547. mask: true,
  548. success() {
  549. setTimeout(() => {
  550. that.setData({
  551. specificationText: '统一规格',
  552. prices: [],
  553. showLadderPrice: false
  554. })
  555. }, 1500)
  556. }
  557. })
  558. }
  559. },
  560. // 阶梯价个数输入
  561. ladderPriceNumInput(e) {
  562. let index = e.currentTarget.dataset.index;
  563. let value = e.detail.value;
  564. let list = this.data.prices;
  565. list[index].number = value;
  566. this.setData({
  567. prices: list
  568. })
  569. },
  570. // 阶梯价金额输入
  571. ladderPricePriceInput(e) {
  572. let index = e.currentTarget.dataset.index;
  573. let value = e.detail.value;
  574. let list = this.data.prices;
  575. list[index].price = value;
  576. this.setData({
  577. prices: list
  578. })
  579. },
  580. // 添加阶梯
  581. addbulk() {
  582. let bulk = {
  583. number: '',
  584. price: ''
  585. }
  586. let prices = this.data.prices;
  587. prices.push(bulk)
  588. this.setData({
  589. prices: prices
  590. })
  591. },
  592. // 删除阶梯
  593. delbulk(e) {
  594. let index = e.currentTarget.dataset.index
  595. let list = this.data.prices;
  596. list.splice(index, 1);
  597. this.setData({
  598. prices: list
  599. })
  600. },
  601. // 确认添加
  602. submit() {
  603. let that = this;
  604. // 商品名称必录校验
  605. if (that.data.goodName == '') {
  606. wx.showToast({
  607. title: '商品名称未录入',
  608. icon: 'none',
  609. mask: true
  610. })
  611. return
  612. }
  613. // 商品简介必录校验
  614. if (that.data.goodIntro == '') {
  615. wx.showToast({
  616. title: '商品简介未录入',
  617. icon: 'none',
  618. mask: true
  619. })
  620. return
  621. }
  622. // 商品图片必录校验
  623. if (that.data.imageList.length == 0) {
  624. wx.showToast({
  625. title: '商品图片未添加',
  626. icon: 'none',
  627. mask: true
  628. })
  629. return
  630. }
  631. // 划线价格必录校验
  632. if (that.data.scribePrice == '') {
  633. wx.showToast({
  634. title: '划线价格未录入',
  635. icon: 'none',
  636. mask: true
  637. })
  638. return
  639. }
  640. // 售价必录校验
  641. if (that.data.price == '') {
  642. wx.showToast({
  643. title: '售价未录入',
  644. icon: 'none',
  645. mask: true
  646. })
  647. return
  648. }
  649. // 库存
  650. let stock = that.data.stock;
  651. if (stock == '') {
  652. stock = '99999999';
  653. }
  654. // 起购数量必录校验
  655. /*
  656. if (that.data.startingPurchase == '') {
  657. wx.showToast({
  658. title: '起购数量未录入',
  659. icon: 'none',
  660. mask: true
  661. })
  662. return
  663. }
  664. */
  665. // 限购数量必录校验
  666. /*
  667. if (that.data.purchaseRestrictionsNumber == '') {
  668. wx.showToast({
  669. title: '限购数量未录入',
  670. icon: 'none',
  671. mask: true
  672. })
  673. return
  674. }
  675. */
  676. // 限购次数必录校验
  677. /*
  678. if (that.data.purchaseRestrictionsSecond == '') {
  679. wx.showToast({
  680. title: '限购次数未录入',
  681. icon: 'none',
  682. mask: true
  683. })
  684. return
  685. }
  686. */
  687. // 起购团数必录校验
  688. /*
  689. if (that.data.startingPurchaseNumber == '') {
  690. wx.showToast({
  691. title: '起购团数未录入',
  692. icon: 'none',
  693. mask: true
  694. })
  695. return
  696. }
  697. */
  698. // 如果不录规格也不录阶梯价-规格为统一规格,价格为售价
  699. if (that.data.specifications.length == 0 && that.data.prices.length == 0) {
  700. let list = that.data.specifications;
  701. let obj = {};
  702. obj.title = '统一规格';
  703. obj.price = that.data.price;
  704. list.push(obj);
  705. that.setData({
  706. specifications: list
  707. })
  708. }
  709. wx.showLoading({
  710. title: '提交中',
  711. mask: true
  712. })
  713. wx.request({
  714. url: api.Commodity_add,
  715. header: {
  716. 'Authorization': wx.getStorageSync('token')
  717. },
  718. method: 'POST',
  719. data: {
  720. commodity_name: that.data.goodName,
  721. commodity_img: that.data.imageList,
  722. commodity_introduction: that.data.goodIntro,
  723. scribe_price: that.data.scribePrice,
  724. specifications: that.data.specifications,
  725. price: that.data.price,
  726. stock: stock,
  727. classification_id: that.data.categorys[that.data.categoryIndex].id,
  728. commodity_warehouse: that.data.isAddToGoodsBank,
  729. starting_purchase: that.data.startingPurchase,
  730. purchase_restrictions_number: that.data.purchaseRestrictionsNumber,
  731. purchase_restrictions_second: that.data.purchaseRestrictionsSecond,
  732. starting_purchase_number: that.data.startingPurchaseNumber,
  733. step_price: that.data.prices
  734. },
  735. success(res) {
  736. console.log(res);
  737. wx.hideLoading()
  738. if (res.data.code === 1) {
  739. wx.showToast({
  740. title: '提交成功',
  741. icon: 'success',
  742. mask: true,
  743. success() {
  744. if (that.data.enterType == 'announce') {
  745. // 获取商品详情
  746. let id = Number(res.data.data); //商品id
  747. wx.request({
  748. url: api.Commodity_info,
  749. header: {
  750. 'Authorization': wx.getStorageSync('token')
  751. },
  752. data: {
  753. id: id
  754. },
  755. success(ress) {
  756. console.log(ress);
  757. if (ress.data.code === 1) {
  758. let arr = [];
  759. let obj = ress.data.data;
  760. arr.push(obj);
  761. storageManager.set('subPageResult', arr);
  762. setTimeout(() => {
  763. wx.navigateBack({
  764. delta: 1,
  765. })
  766. }, 1500)
  767. }
  768. }
  769. })
  770. } else {
  771. setTimeout(() => {
  772. wx.navigateBack({
  773. delta: 1,
  774. })
  775. }, 1500)
  776. }
  777. }
  778. })
  779. } else {
  780. wx.showToast({
  781. title: res.data.msg,
  782. mask: true,
  783. icon: 'none'
  784. })
  785. }
  786. },
  787. fail(err) {
  788. wx.hideLoading()
  789. wx.showToast({
  790. title: '发起网络请求失败',
  791. icon: 'none',
  792. mask: true
  793. })
  794. },
  795. complete() {
  796. // wx.hideLoading()
  797. }
  798. })
  799. },
  800. // 确认编辑
  801. edit() {
  802. let that = this;
  803. // 商品名称必录校验
  804. if (that.data.goodName == '') {
  805. wx.showToast({
  806. title: '商品名称未录入',
  807. icon: 'none',
  808. mask: true
  809. })
  810. return
  811. }
  812. // 商品简介必录校验
  813. if (that.data.goodIntro == '') {
  814. wx.showToast({
  815. title: '商品简介未录入',
  816. icon: 'none',
  817. mask: true
  818. })
  819. return
  820. }
  821. // 商品图片必录校验
  822. if (that.data.imageList.length == 0) {
  823. wx.showToast({
  824. title: '商品图片未添加',
  825. icon: 'none',
  826. mask: true
  827. })
  828. return
  829. }
  830. // 划线价格必录校验
  831. if (that.data.scribePrice == '') {
  832. wx.showToast({
  833. title: '划线价格未录入',
  834. icon: 'none',
  835. mask: true
  836. })
  837. return
  838. }
  839. // 售价必录校验
  840. if (that.data.price == '') {
  841. wx.showToast({
  842. title: '售价未录入',
  843. icon: 'none',
  844. mask: true
  845. })
  846. return
  847. }
  848. // 库存
  849. let stock = that.data.stock;
  850. if (stock == '') {
  851. stock = '99999999';
  852. }
  853. // 如果不录规格也不录阶梯价-规格为统一规格,价格为售价
  854. if (that.data.specifications.length == 0 && that.data.prices.length == 0) {
  855. let list = that.data.specifications;
  856. let obj = {};
  857. obj.title = '统一规格';
  858. obj.price = that.data.price;
  859. list.push(obj);
  860. that.setData({
  861. specifications: list
  862. })
  863. }
  864. wx.showLoading({
  865. title: '提交中',
  866. mask: true
  867. })
  868. wx.request({
  869. url: api.Commodity_edit,
  870. header: {
  871. 'Authorization': wx.getStorageSync('token')
  872. },
  873. method: 'POST',
  874. data: {
  875. id: that.data.id,
  876. commodity_name: that.data.goodName,
  877. commodity_img: that.data.imageList,
  878. commodity_introduction: that.data.goodIntro,
  879. scribe_price: that.data.scribePrice,
  880. specifications: that.data.specifications,
  881. price: that.data.price,
  882. stock: stock,
  883. classification_id: that.data.categorys[that.data.categoryIndex].id,
  884. commodity_warehouse: that.data.isAddToGoodsBank,
  885. starting_purchase: that.data.startingPurchase,
  886. purchase_restrictions_number: that.data.purchaseRestrictionsNumber,
  887. purchase_restrictions_second: that.data.purchaseRestrictionsSecond,
  888. starting_purchase_number: that.data.startingPurchaseNumber,
  889. step_price: that.data.prices
  890. },
  891. success(res) {
  892. console.log(res);
  893. wx.hideLoading()
  894. if (res.data.code === 1) {
  895. wx.showToast({
  896. title: res.data.msg,
  897. icon: 'success',
  898. mask: true,
  899. success() {
  900. setTimeout(()=>{
  901. wx.setStorageSync('isReload', true)
  902. if(wx.getStorageSync('obj')) {
  903. let obj = wx.getStorageSync('obj')
  904. obj.stock = that.data.stock;
  905. obj.price = that.data.price;
  906. obj.name = that.data.goodName;
  907. wx.setStorageSync('obj', obj)
  908. }
  909. wx.navigateBack()
  910. }, 1500)
  911. }
  912. })
  913. } else {
  914. wx.showToast({
  915. title: res.data.msg,
  916. mask: true,
  917. icon: 'none'
  918. })
  919. }
  920. },
  921. fail(err) {
  922. wx.hideLoading()
  923. wx.showToast({
  924. title: '发起网络请求失败',
  925. icon: 'none',
  926. mask: true
  927. })
  928. }
  929. })
  930. },
  931. // 阶梯价-判断不为空
  932. judgeNoyNullStepPrice(arr) {
  933. let isOK = true;
  934. for (let i = 0; i < arr.length; i++) {
  935. if (arr[i].title == '' || arr[i].price == '') {
  936. isOK = false;
  937. }
  938. }
  939. return isOK;
  940. },
  941. // 规格-判断不为空
  942. judgeNoyNullSpecifications(arr) {
  943. let isOK = true;
  944. for (let i = 0; i < arr.length; i++) {
  945. if (arr[i].number == '' || arr[i].price == '') {
  946. isOK = false;
  947. }
  948. }
  949. return isOK;
  950. },
  951. })