edit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // pages/goods/edit.js
  2. import * as API_Category from '../../api/category.js'
  3. import * as API_Goods from '../../api/goods.js'
  4. import * as Reg from '../../utils/RegExp.js'
  5. import * as API_Passport from '../../api/passport.js'
  6. import {
  7. guid
  8. } from '../../utils/util.js'
  9. import {
  10. $wuxToptips,
  11. $wuxToast,
  12. $wuxLoading,
  13. $wuxDialog,
  14. $wuxForm
  15. } from '../../wux/index.js'
  16. const app = getApp()
  17. Page({
  18. /**
  19. * 页面的初始数据
  20. */
  21. data: {
  22. goods_id: null,
  23. uploadUrl: app.globalData.api_domain.api_base + 'uploaders?scene=goods',
  24. options2: [],
  25. categoryValue: [],
  26. defaultFieldNames: {
  27. label: 'name',
  28. value: 'category_id',
  29. children: 'children'
  30. },
  31. categoryDisplay: '选择商品分类',
  32. categoryChangeDone:false,
  33. /** test */
  34. goodsNameError: false,
  35. fileList: [],
  36. pics:[]
  37. },
  38. /**
  39. * 生命周期函数--监听页面加载
  40. */
  41. onLoad: function(options) {
  42. let shop = app.globalData.sellerShop
  43. if(shop.shop_type==='2'){
  44. this.getCategorys(0)
  45. }else{
  46. this.getCategory(-1)
  47. }
  48. var that = this
  49. /** 获取商品id参数 */
  50. if (options.id) {
  51. wx.setNavigationBarTitle({
  52. title: '产品编辑',
  53. })
  54. var goods_id = decodeURIComponent(options.id)
  55. this.setData({
  56. goods_id: goods_id
  57. })
  58. this.GET_GoodData()
  59. }
  60. },
  61. /**
  62. * 生命周期函数--监听页面初次渲染完成
  63. */
  64. onReady: function() {
  65. },
  66. /**
  67. * 生命周期函数--监听页面显示
  68. */
  69. onShow: function() {
  70. // console.log("onshow",app.globalData.goodsImg);
  71. //清理 fileList
  72. if (app.globalData.goodsImg){
  73. let fileList = this.data.fileList
  74. let goodsImg = app.globalData.goodsImg;
  75. console.log(goodsImg);
  76. if (goodsImg.idx == -1){
  77. fileList.push({
  78. url: goodsImg.src,
  79. uid: guid(),
  80. status: 'done'
  81. })
  82. } else {
  83. fileList[goodsImg.idx] = {
  84. url: goodsImg.src,
  85. uid: guid(),
  86. status: 'done'
  87. }
  88. }
  89. this.setData({
  90. fileList: fileList
  91. })
  92. app.globalData.goodsImg = "";
  93. }
  94. },
  95. /**
  96. * 生命周期函数--监听页面隐藏
  97. */
  98. onHide: function() {
  99. },
  100. /**
  101. * 生命周期函数--监听页面卸载
  102. */
  103. onUnload: function() {
  104. },
  105. /**
  106. * 页面相关事件处理函数--监听用户下拉动作
  107. */
  108. onPullDownRefresh: function() {
  109. },
  110. /**
  111. * 页面上拉触底事件的处理函数
  112. */
  113. onReachBottom: function() {
  114. },
  115. /**
  116. * 用户点击右上角分享
  117. */
  118. onShareAppMessage: function() {
  119. },
  120. /**
  121. * 打开分类选择器
  122. */
  123. showCategoryPicker() {
  124. if (!this.data.goods_id) {
  125. this.setData({
  126. categoryShow: true
  127. })
  128. }
  129. },
  130. /**
  131. * 关闭分类选择器
  132. */
  133. closeCategoryPicker() {
  134. this.setData({
  135. categoryShow: false
  136. })
  137. },
  138. categoryChange(e) {
  139. this.setData({
  140. categoryChangeDone: e.detail.done,
  141. categoryValue: e.detail.value,
  142. categoryDisplay: e.detail.done ? e.detail.options.map((n) => n.name).join('/') : '选择商品分类'
  143. })
  144. },
  145. getCategorys(id) {
  146. API_Category.getCategory(id).then(res => {
  147. this.setData({
  148. options2: res
  149. })
  150. })
  151. },
  152. /** 表单输入 */
  153. inputChange(e) {
  154. let dataKey = e.currentTarget.dataset.name
  155. this.setData({
  156. [dataKey]: e.detail.value
  157. })
  158. },
  159. onError(e) {
  160. const that = this
  161. let dataKey = e.currentTarget.dataset.name
  162. let {
  163. label,
  164. rules
  165. } = e.currentTarget.dataset
  166. const {
  167. value
  168. } = e.detail
  169. if (!value && rules.indexOf('required') > -1) {
  170. label = label + '必填'
  171. } else {
  172. label = label + '格式错误'
  173. }
  174. $wuxToptips().show({
  175. icon: 'cancel',
  176. hidden: false,
  177. text: label,
  178. duration: 3000,
  179. success() {
  180. that.setData({
  181. [dataKey + '_focus']: true
  182. })
  183. },
  184. })
  185. },
  186. onBlur(e) {
  187. const that = this
  188. let dataKey = e.currentTarget.dataset.name
  189. const {
  190. rules
  191. } = e.currentTarget.dataset
  192. const {
  193. value
  194. } = e.detail
  195. if (rules) {
  196. that.setData({
  197. [dataKey + '_error']: false
  198. })
  199. rules.split(',').forEach(n => {
  200. if (n === 'required' && !value) {
  201. that.setData({
  202. [dataKey + '_error']: true
  203. })
  204. }
  205. if (n !== 'required' && !Reg[n].test(value)) {
  206. that.setData({
  207. [dataKey + '_error']: true
  208. })
  209. }
  210. })
  211. }
  212. },
  213. onFileUploadSuccess(e) {
  214. const {
  215. file,
  216. fileList
  217. } = e.detail
  218. //转换image对象
  219. fileList.forEach(item => {
  220. item.url = JSON.parse(item.res.data).url
  221. })
  222. this.setData({
  223. fileList: fileList
  224. })
  225. },
  226. onFileChange(e) {
  227. const {
  228. file
  229. } = e.detail
  230. if (file.status === 'uploading') {
  231. this.setData({
  232. progress: 0,
  233. })
  234. wx.showLoading()
  235. } else if (file.status === 'done') {
  236. this.setData({
  237. imageUrl: file.url,
  238. })
  239. }
  240. },
  241. onFileComplete(e) {
  242. const {
  243. data,
  244. errMsg
  245. } = e.detail
  246. //转换image对象
  247. let fileList = this.data.fileList
  248. if (errMsg === 'uploadFile:ok') {
  249. fileList.push({
  250. url: JSON.parse(data).url,
  251. uid: guid(),
  252. status: 'done'
  253. })
  254. this.setData({
  255. fileList: fileList
  256. })
  257. }
  258. wx.hideLoading()
  259. },
  260. navToResize(e){
  261. console.log(e);
  262. const that = this
  263. if (e.currentTarget.dataset.action === "edit"){
  264. let index = e.currentTarget.dataset.index;
  265. let fileList = this.data.fileList;
  266. let imgFile = fileList[index];
  267. console.log('imgFile.uid===', imgFile.uid)
  268. wx.showModal({
  269. title: '操作提示',
  270. content: '请选择您的操作?',
  271. confirmText: '修改图片',
  272. confirmColor: '#576B95',
  273. cancelText: '删除图片',
  274. cancelColor: '#d02a2a',
  275. success: function (res) {
  276. if (res.confirm) {
  277. console.log('imgFile===', imgFile)
  278. wx.navigateTo({
  279. url: '/pages/image/resize?imgSrc=' + imgFile.url + '&idx=' + index,
  280. })
  281. } else {
  282. that.setData({
  283. fileList: fileList.filter((n) => n.uid !== imgFile.uid),
  284. })
  285. }
  286. }
  287. })
  288. } else {
  289. wx.navigateTo({
  290. url: '/pages/image/resize',
  291. })
  292. }
  293. },
  294. onFileRemove(e) {
  295. const {
  296. file,
  297. fileList
  298. } = e.detail
  299. this.setData({
  300. fileList: fileList.filter((n) => n.uid !== file.uid),
  301. })
  302. },
  303. /** 输入框变化 */
  304. onChange(e) {
  305. const {
  306. detail
  307. } = e.detail
  308. let dataKey = e.currentTarget.dataset.name
  309. this.setData({
  310. [dataKey]: e.detail
  311. })
  312. },
  313. /** 上架产品 */
  314. aboveProduct() {
  315. /** 产品分类校验 */
  316. if (!this.data.categoryChangeDone||this.data.categoryValue.length === 0) {
  317. $wuxToptips().show({
  318. icon: 'cancel',
  319. hidden: false,
  320. text: "请选择商品分类",
  321. duration: 2000
  322. })
  323. return false
  324. }
  325. /** 输入参数校验 */
  326. let param = {
  327. goodsName: null,
  328. sn: null,
  329. price: null,
  330. stock: null
  331. }
  332. let ps = {
  333. ...param,
  334. ...this.data
  335. }
  336. Object.keys(param).forEach(
  337. n => {
  338. if (!ps[n]) {
  339. this.setData({
  340. [n + '_error']: true
  341. })
  342. }
  343. }
  344. )
  345. let flag = true
  346. Object.keys(this.data).forEach(n => {
  347. if (n.indexOf('_error') > -1 && this.data[n]) {
  348. flag = false
  349. return false
  350. }
  351. })
  352. /** 图片校验 */
  353. let gallery_list = []
  354. if (this.data.fileList.length === 0) {
  355. $wuxToptips().show({
  356. icon: 'cancel',
  357. hidden: false,
  358. text: "请上传商品图片",
  359. duration: 2000
  360. })
  361. flag = false
  362. } else {
  363. this.data.fileList.forEach((item, index) => {
  364. let image = {
  365. original: item.url,
  366. img_id: (Reg.integer.test(item.uid) ? item.uid : -1),
  367. sort: index
  368. }
  369. gallery_list.push(image)
  370. })
  371. }
  372. if (!flag) {
  373. return false
  374. }
  375. /**  产品上架参数  */
  376. const goodsParam = {
  377. category_id: this.data.categoryValue[this.data.categoryValue.length - 1],
  378. exchange: {
  379. category_id: "",
  380. enable_exchange: 0,
  381. exchange_money: 0,
  382. exchange_point: 0
  383. },
  384. goods_gallery_list: [...gallery_list],
  385. goods_id: this.data.goods_id,
  386. goods_name: this.data.goodsName,
  387. goods_params_list: [],
  388. goods_transfee_charge: 1,
  389. has_changed: 0,
  390. intro: this.imgToHtml(this.data.pics),
  391. market_enable: 1,
  392. meta_description: '',
  393. meta_keywords: '',
  394. mktprice: this.data.price,
  395. price: this.data.price,
  396. cost: this.data.price,
  397. page_title: '',
  398. quantity: this.data.stock,
  399. shop_cat_id: 0,
  400. sku_list: [],
  401. sn: this.data.sn,
  402. template_id: 0,
  403. weight: 0
  404. }
  405. $wuxLoading().show({
  406. text: '正在保存..',
  407. mask: true
  408. })
  409. if (this.data.goods_id) {
  410. API_Goods.editGoods(this.data.goods_id, goodsParam).then(res => {
  411. if (res.goods_id) {
  412. $wuxLoading().hide()
  413. $wuxToptips().success({
  414. hidden: false,
  415. text: "保存成功",
  416. duration: 3000,
  417. success: () => {
  418. wx.navigateBack({
  419. detal: 1
  420. })
  421. }
  422. })
  423. } else {
  424. $wuxLoading().hide()
  425. $wuxToptips().error({
  426. icon: 'cancel',
  427. hidden: false,
  428. text: res.message,
  429. duration: 3000
  430. })
  431. }
  432. })
  433. } else {
  434. API_Goods.aboveGoods(goodsParam).then(
  435. res => {
  436. if (res.goods_id) {
  437. $wuxLoading().hide()
  438. $wuxToptips().success({
  439. hidden: false,
  440. text: "上架成功",
  441. duration: 2000,
  442. success: () => {
  443. wx.navigateBack({
  444. detal: 1
  445. })
  446. }
  447. })
  448. } else {
  449. $wuxLoading().hide()
  450. $wuxToptips().error({
  451. icon: 'cancel',
  452. hidden: false,
  453. text: res.message,
  454. duration: 2000
  455. })
  456. }
  457. }
  458. ).catch(err=>{
  459. $wuxLoading().hide()
  460. $wuxToptips().error({
  461. icon: 'cancel',
  462. hidden: false,
  463. text: res.message,
  464. duration: 2000
  465. })
  466. })
  467. }
  468. },
  469. /** 查询单个商品信息*/
  470. GET_GoodData() {
  471. const that = this
  472. API_Goods.getGoodData(that.data.goods_id, {}).then((response) => {
  473. /** 商品相册校验属性 */
  474. let images = []
  475. response.goods_gallery_list.forEach(key => {
  476. images.push({
  477. uid: key.img_id,
  478. status: 'done',
  479. url: key.original
  480. })
  481. })
  482. console.log('srcs',this.getImgSrc(response.intro))
  483. that.setData({
  484. pics:that.getImgSrc(response.intro),
  485. goodsName: response.goods_name,
  486. price: response.mktprice,
  487. stock: response.quantity,
  488. sn: response.sn,
  489. fileList: images,
  490. categoryChangeDone:true,
  491. categoryDisplay: response.category_name.replace(/>/g, '/').replace(/\s+/g, ''),
  492. categoryValue: [0, response.category_id]
  493. })
  494. })
  495. },
  496. /** 下架产品 */
  497. offProduct() {
  498. const that = this
  499. $wuxDialog().confirm({
  500. resetOnClose: true,
  501. closable: true,
  502. title: '操作提示',
  503. content: '你确定要下架商品?',
  504. onConfirm(e) {
  505. API_Goods.underGoods(that.data.goods_id, {}).then(res => {
  506. if (res === '') {
  507. $wuxToptips().success({
  508. hidden: false,
  509. text: '产品已下架',
  510. duration: 2000,
  511. success: () => {
  512. wx.navigateBack({
  513. detal: 1
  514. })
  515. }
  516. })
  517. } else {
  518. $wuxToptips().error({
  519. icon: 'cancel',
  520. hidden: false,
  521. text: res.message,
  522. duration: 2000
  523. })
  524. }
  525. }).catch(error => {
  526. $wuxToptips().error({
  527. icon: 'cancel',
  528. hidden: false,
  529. text: error.message,
  530. duration: 2000
  531. })
  532. })
  533. },
  534. onCancel(e) {
  535. },
  536. })
  537. },
  538. formSubmit(e) {
  539. console.log(e)
  540. API_Passport.saveFormId({ form_id: e.detail.formId }).then(res => {
  541. })
  542. },
  543. sendImage() {
  544. let _this = this
  545. _this.uploadPic().then(res=>{
  546. let resData = JSON.parse(res.data)
  547. let imgs=_this.data.pics
  548. imgs.push(resData.url)
  549. _this.setData({
  550. pics:imgs
  551. })
  552. console.log(_this.data.pics)
  553. }).catch(err=>{
  554. console.log('er',err)
  555. $wuxToptips().error({
  556. hidden: false,
  557. text: "图片上传失败",
  558. duration: 2000,
  559. success: () => {
  560. }
  561. })
  562. })
  563. },
  564. changePic(e){
  565. const {id}=e.currentTarget.dataset
  566. let _this = this
  567. _this.uploadPic().then(res=>{
  568. let resData = JSON.parse(res.data);
  569. console.log(res, resData);
  570. let imgs=_this.data.pics
  571. imgs[id]=resData.url
  572. _this.setData({
  573. pics:imgs
  574. })
  575. console.log(_this.data.pics)
  576. }).catch(err=>{
  577. console.log('err',err)
  578. $wuxToptips().error({
  579. hidden: false,
  580. text: "图片上传失败",
  581. duration: 2000,
  582. success: () => {
  583. }
  584. })
  585. })
  586. },
  587. deletePic(e){
  588. const {id}=e.currentTarget.dataset
  589. let imgs=this.data.pics
  590. let index=imgs.findIndex(p=>p.id===id)
  591. imgs.splice(index,1)
  592. this.setData({
  593. pics:imgs
  594. })
  595. },
  596. //同步上传图片
  597. uploadPic(){
  598. return new Promise((resolve,reject)=>{
  599. const _this=this
  600. wx.chooseImage({
  601. count: 1,
  602. sizeType: ['compressed'],
  603. sourceType: ['album', 'camera'],
  604. success: res => {
  605. console.log(res);
  606. let { tempFilePaths } = res;
  607. let tempFilePath = tempFilePaths[0];
  608. wx.getImageInfo({
  609. src: tempFilePath,
  610. success: function (image) {
  611. wx.uploadFile({
  612. url: _this.data.uploadUrl,
  613. filePath: tempFilePath,
  614. name: 'file',
  615. header: {
  616. 'Content-Type': 'multipart/form-data'
  617. },
  618. success: function(res) {
  619. console.log('success',res)
  620. resolve(res)
  621. },
  622. fail: function(res) {
  623. reject(res)
  624. }
  625. })
  626. }
  627. })
  628. }
  629. })
  630. })
  631. },
  632. imgToHtml(imgs){
  633. let html=''
  634. if(imgs instanceof Array) {
  635. imgs.forEach(item=>{
  636. html+='<p><img src="'+item+'" width="100%"></img></p>'
  637. })
  638. }
  639. return html;
  640. },
  641. // 提取图片地址
  642. getImgSrc(html){
  643. let imgReg=/<img\b.*?(?:\>|\/>)/gi
  644. var srcReg = /\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i
  645. var arr = []
  646. let imgArr=html.match(imgReg)
  647. if(imgArr&&imgArr.length>0){
  648. imgArr.forEach(item=>{
  649. console.log(item)
  650. let srcMatch=srcReg.exec(item)
  651. console.log('srcMatch',srcMatch)
  652. if(srcMatch&&srcMatch.length>1){
  653. console.log(srcMatch[1])
  654. arr.push(srcMatch[1])
  655. }
  656. })
  657. }
  658. return arr;
  659. }
  660. })