edit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. if (e.currentTarget.dataset.action == "edit"){
  263. let fileList = this.data.fileList;
  264. let index = e.currentTarget.dataset.index;
  265. let imgFile = fileList[index];
  266. wx.navigateTo({
  267. url: '/pages/image/resize?imgSrc=' + imgFile.url + '&idx=' + index,
  268. })
  269. } else {
  270. wx.navigateTo({
  271. url: '/pages/image/resize',
  272. })
  273. }
  274. },
  275. onFileRemove(e) {
  276. const {
  277. file,
  278. fileList
  279. } = e.detail
  280. this.setData({
  281. fileList: fileList.filter((n) => n.uid !== file.uid),
  282. })
  283. },
  284. /** 输入框变化 */
  285. onChange(e) {
  286. const {
  287. detail
  288. } = e.detail
  289. let dataKey = e.currentTarget.dataset.name
  290. this.setData({
  291. [dataKey]: e.detail
  292. })
  293. },
  294. /** 上架产品 */
  295. aboveProduct() {
  296. /** 产品分类校验 */
  297. if (!this.data.categoryChangeDone||this.data.categoryValue.length === 0) {
  298. $wuxToptips().show({
  299. icon: 'cancel',
  300. hidden: false,
  301. text: "请选择商品分类",
  302. duration: 2000
  303. })
  304. return false
  305. }
  306. /** 输入参数校验 */
  307. let param = {
  308. goodsName: null,
  309. sn: null,
  310. price: null,
  311. stock: null
  312. }
  313. let ps = {
  314. ...param,
  315. ...this.data
  316. }
  317. Object.keys(param).forEach(
  318. n => {
  319. if (!ps[n]) {
  320. this.setData({
  321. [n + '_error']: true
  322. })
  323. }
  324. }
  325. )
  326. let flag = true
  327. Object.keys(this.data).forEach(n => {
  328. if (n.indexOf('_error') > -1 && this.data[n]) {
  329. flag = false
  330. return false
  331. }
  332. })
  333. /** 图片校验 */
  334. let gallery_list = []
  335. if (this.data.fileList.length === 0) {
  336. $wuxToptips().show({
  337. icon: 'cancel',
  338. hidden: false,
  339. text: "请上传商品图片",
  340. duration: 2000
  341. })
  342. flag = false
  343. } else {
  344. this.data.fileList.forEach((item, index) => {
  345. let image = {
  346. original: item.url,
  347. img_id: (Reg.integer.test(item.uid) ? item.uid : -1),
  348. sort: index
  349. }
  350. gallery_list.push(image)
  351. })
  352. }
  353. if (!flag) {
  354. return false
  355. }
  356. /**  产品上架参数  */
  357. const goodsParam = {
  358. category_id: this.data.categoryValue[this.data.categoryValue.length - 1],
  359. exchange: {
  360. category_id: "",
  361. enable_exchange: 0,
  362. exchange_money: 0,
  363. exchange_point: 0
  364. },
  365. goods_gallery_list: [...gallery_list],
  366. goods_id: this.data.goods_id,
  367. goods_name: this.data.goodsName,
  368. goods_params_list: [],
  369. goods_transfee_charge: 1,
  370. has_changed: 0,
  371. intro: this.imgToHtml(this.data.pics),
  372. market_enable: 1,
  373. meta_description: '',
  374. meta_keywords: '',
  375. mktprice: this.data.price,
  376. price: this.data.price,
  377. cost: this.data.price,
  378. page_title: '',
  379. quantity: this.data.stock,
  380. shop_cat_id: 0,
  381. sku_list: [],
  382. sn: this.data.sn,
  383. template_id: 0,
  384. weight: 0
  385. }
  386. $wuxLoading().show({
  387. text: '正在保存..',
  388. mask: true
  389. })
  390. if (this.data.goods_id) {
  391. API_Goods.editGoods(this.data.goods_id, goodsParam).then(res => {
  392. if (res.goods_id) {
  393. $wuxLoading().hide()
  394. $wuxToptips().success({
  395. hidden: false,
  396. text: "保存成功",
  397. duration: 3000,
  398. success: () => {
  399. wx.navigateBack({
  400. detal: 1
  401. })
  402. }
  403. })
  404. } else {
  405. $wuxLoading().hide()
  406. $wuxToptips().error({
  407. icon: 'cancel',
  408. hidden: false,
  409. text: res.message,
  410. duration: 3000
  411. })
  412. }
  413. })
  414. } else {
  415. API_Goods.aboveGoods(goodsParam).then(
  416. res => {
  417. if (res.goods_id) {
  418. $wuxLoading().hide()
  419. $wuxToptips().success({
  420. hidden: false,
  421. text: "上架成功",
  422. duration: 2000,
  423. success: () => {
  424. wx.navigateBack({
  425. detal: 1
  426. })
  427. }
  428. })
  429. } else {
  430. $wuxLoading().hide()
  431. $wuxToptips().error({
  432. icon: 'cancel',
  433. hidden: false,
  434. text: res.message,
  435. duration: 2000
  436. })
  437. }
  438. }
  439. ).catch(err=>{
  440. $wuxLoading().hide()
  441. $wuxToptips().error({
  442. icon: 'cancel',
  443. hidden: false,
  444. text: res.message,
  445. duration: 2000
  446. })
  447. })
  448. }
  449. },
  450. /** 查询单个商品信息*/
  451. GET_GoodData() {
  452. const that = this
  453. API_Goods.getGoodData(that.data.goods_id, {}).then((response) => {
  454. /** 商品相册校验属性 */
  455. let images = []
  456. response.goods_gallery_list.forEach(key => {
  457. images.push({
  458. uid: key.img_id,
  459. status: 'done',
  460. url: key.original
  461. })
  462. })
  463. console.log('srcs',this.getImgSrc(response.intro))
  464. that.setData({
  465. pics:that.getImgSrc(response.intro),
  466. goodsName: response.goods_name,
  467. price: response.mktprice,
  468. stock: response.quantity,
  469. sn: response.sn,
  470. fileList: images,
  471. categoryChangeDone:true,
  472. categoryDisplay: response.category_name.replace(/>/g, '/').replace(/\s+/g, ''),
  473. categoryValue: [0, response.category_id]
  474. })
  475. })
  476. },
  477. /** 下架产品 */
  478. offProduct() {
  479. const that = this
  480. $wuxDialog().confirm({
  481. resetOnClose: true,
  482. closable: true,
  483. title: '操作提示',
  484. content: '你确定要下架商品?',
  485. onConfirm(e) {
  486. API_Goods.underGoods(that.data.goods_id, {}).then(res => {
  487. if (res === '') {
  488. $wuxToptips().success({
  489. hidden: false,
  490. text: '产品已下架',
  491. duration: 2000,
  492. success: () => {
  493. wx.navigateBack({
  494. detal: 1
  495. })
  496. }
  497. })
  498. } else {
  499. $wuxToptips().error({
  500. icon: 'cancel',
  501. hidden: false,
  502. text: res.message,
  503. duration: 2000
  504. })
  505. }
  506. }).catch(error => {
  507. $wuxToptips().error({
  508. icon: 'cancel',
  509. hidden: false,
  510. text: error.message,
  511. duration: 2000
  512. })
  513. })
  514. },
  515. onCancel(e) {
  516. },
  517. })
  518. },
  519. formSubmit(e) {
  520. console.log(e)
  521. API_Passport.saveFormId({ form_id: e.detail.formId }).then(res => {
  522. })
  523. },
  524. sendImage() {
  525. let _this = this
  526. _this.uploadPic().then(res=>{
  527. let resData = JSON.parse(res.data)
  528. let imgs=_this.data.pics
  529. imgs.push(resData.url)
  530. _this.setData({
  531. pics:imgs
  532. })
  533. console.log(_this.data.pics)
  534. }).catch(err=>{
  535. console.log('er',err)
  536. $wuxToptips().error({
  537. hidden: false,
  538. text: "图片上传失败",
  539. duration: 2000,
  540. success: () => {
  541. }
  542. })
  543. })
  544. },
  545. changePic(e){
  546. const {id}=e.currentTarget.dataset
  547. let _this = this
  548. _this.uploadPic().then(res=>{
  549. let resData = JSON.parse(res.data);
  550. console.log(res, resData);
  551. let imgs=_this.data.pics
  552. imgs[id]=resData.url
  553. _this.setData({
  554. pics:imgs
  555. })
  556. console.log(_this.data.pics)
  557. }).catch(err=>{
  558. console.log('err',err)
  559. $wuxToptips().error({
  560. hidden: false,
  561. text: "图片上传失败",
  562. duration: 2000,
  563. success: () => {
  564. }
  565. })
  566. })
  567. },
  568. deletePic(e){
  569. const {id}=e.currentTarget.dataset
  570. let imgs=this.data.pics
  571. let index=imgs.findIndex(p=>p.id===id)
  572. imgs.splice(index,1)
  573. this.setData({
  574. pics:imgs
  575. })
  576. },
  577. //同步上传图片
  578. uploadPic(){
  579. return new Promise((resolve,reject)=>{
  580. const _this=this
  581. wx.chooseImage({
  582. count: 1,
  583. sizeType: ['compressed'],
  584. sourceType: ['album', 'camera'],
  585. success: res => {
  586. console.log(res);
  587. let { tempFilePaths } = res;
  588. let tempFilePath = tempFilePaths[0];
  589. wx.getImageInfo({
  590. src: tempFilePath,
  591. success: function (image) {
  592. wx.uploadFile({
  593. url: _this.data.uploadUrl,
  594. filePath: tempFilePath,
  595. name: 'file',
  596. header: {
  597. 'Content-Type': 'multipart/form-data'
  598. },
  599. success: function(res) {
  600. console.log('success',res)
  601. resolve(res)
  602. },
  603. fail: function(res) {
  604. reject(res)
  605. }
  606. })
  607. }
  608. })
  609. }
  610. })
  611. })
  612. },
  613. imgToHtml(imgs){
  614. let html=''
  615. if(imgs instanceof Array) {
  616. imgs.forEach(item=>{
  617. html+='<p><img src="'+item+'" width="100%"></img></p>'
  618. })
  619. }
  620. return html;
  621. },
  622. // 提取图片地址
  623. getImgSrc(html){
  624. let imgReg=/<img\b.*?(?:\>|\/>)/gi
  625. var srcReg = /\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i
  626. var arr = []
  627. let imgArr=html.match(imgReg)
  628. if(imgArr&&imgArr.length>0){
  629. imgArr.forEach(item=>{
  630. console.log(item)
  631. let srcMatch=srcReg.exec(item)
  632. console.log('srcMatch',srcMatch)
  633. if(srcMatch&&srcMatch.length>1){
  634. console.log(srcMatch[1])
  635. arr.push(srcMatch[1])
  636. }
  637. })
  638. }
  639. return arr;
  640. }
  641. })