// subpages/heart/pages/groupBuyPublish/groupBuyPublish.js import { $wuxActionSheet } from "../../../../dist/index" import { getTimestamp } from '../../../../utils/common' const api = require("../../utils/api") const config = require("../../../../utils/config") Page({ /** * 页面的初始数据 */ data: { dialogVisible: false, //提示框 showPicker: false, //是否显示底部时间选择器插件 dataForm: { id: '', //主键 更新时携带 groupBuyTitle: '', //标题 groupBuyContent: '', //内容 groupBuyEndTime: '', //截止时间 groupBuyMobile: '', //手机 groupBuyPriceNumber: [ { groupBuyNumber: '', groupBuyPrice: '' } ], //groupBuyNumber, groupBuyPrice 团购价格人数(最多五条) groupBuyImg: [], //团购图片(最多三张) pickupMethod: 0, //取货方式:1-社区送达,2-包邮到家 }, violationsCount: 0, //内容审核计数 isConReview: false, //内容审核标志 lock: false, //锁定发布按钮状态,防止连击 uploadImageList: [], imageId: 0, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ 'dataForm.id': options.id || '' }) if (options.id) { this.initDataForm() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ // onShareAppMessage: function () { // } onInputGroupBuyTitle (e) { this.setData({ 'dataForm.groupBuyTitle': e.detail.value }) }, onInputGroupBuyNumber (e) { let index = e.currentTarget.dataset.index this.data.dataForm.groupBuyPriceNumber[index].groupBuyNumber = parseInt(e.detail.value) this.setData({ 'dataForm.groupBuyPriceNumber': this.data.dataForm.groupBuyPriceNumber }) }, onInputGroupBuyPrice (e) { let index = e.currentTarget.dataset.index if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) { this.data.dataForm.groupBuyPriceNumber[index].groupBuyPrice = e.detail.value; } else { this.data.dataForm.groupBuyPriceNumber[index].groupBuyPrice = e.detail.value.substring(0, e.detail.value.length - 1); } this.setData({ 'dataForm.groupBuyPriceNumber': this.data.dataForm.groupBuyPriceNumber }) }, onInputGroupBuyMobile (e) { this.setData({ 'dataForm.groupBuyMobile': e.detail.value }) }, onInputContent (e) { this.setData({ 'dataForm.groupBuyContent': e.detail.value }) }, addPriceItem () { this.data.dataForm.groupBuyPriceNumber.push({groupBuyNumber:'',groupBuyPrice:''}) this.setData({ 'dataForm.groupBuyPriceNumber': this.data.dataForm.groupBuyPriceNumber }) }, minusPriceItem (e) { let index = e.currentTarget.dataset.index this.data.dataForm.groupBuyPriceNumber.splice(index, 1) this.setData({ 'dataForm.groupBuyPriceNumber': this.data.dataForm.groupBuyPriceNumber }) }, pickerCancel () { console.log('取消日期选择') this.setData({ showPicker: false, 'dataForm.groupBuyEndTime': '' }) }, pickerConfirm (e) { console.log('选择日期', e.detail.time) if (e.detail.time < getTimestamp()) { this.showToast("截止时间应该大于当前时间") } else { this.setData({ showPicker: false, 'dataForm.groupBuyEndTime': e.detail.time }) } }, //日期选择插件显示入口 selectTime (e) { this.data.showPicker = !this.data.showPicker this.setData({ showPicker: this.data.showPicker, 'dataForm.groupBuyEndTime': this.data.dataForm.groupBuyEndTime || getTimestamp() }) }, //选择取货方式 pickupMethodChange (e) { this.setData({ 'dataForm.pickupMethod': parseInt(e.detail.value) }) }, //发布 submitApply () { if (this.data.lock) { return false } console.log(this.data.dataForm.pickupMethod) if (!this.data.dataForm.groupBuyTitle.trim('')) { this.showToast("请填标题") return false } if (this.data.dataForm.groupBuyTitle.length>50) { this.showToast("标题限制在50字以内") return false } if (!this.data.dataForm.pickupMethod) { this.showToast("请选择取货方式") return false } if (!this.data.dataForm.groupBuyEndTime) { this.showToast("请填写截止时间") return false } if (!this.data.dataForm.groupBuyContent.trim('')) { this.showToast("请填写团购内容") return false } if (this.data.dataForm.groupBuyContent.length>500) { this.showToast("团购内容限制在500字以内") return false } if (!this.data.dataForm.groupBuyPriceNumber[0].groupBuyNumber || !this.data.dataForm.groupBuyPriceNumber[0].groupBuyPrice) { this.showToast("至少填写一条价格说明") return false } if (!this.data.dataForm.groupBuyMobile.trim('')) { this.showToast("请填写联系电话") return false } if (this.data.dataForm.groupBuyMobile.length>20) { this.showToast("联系电话限制在20字以内") return false } wx.showLoading({ title: "发布中", }) const imagesList = [] if (this.data.uploadImageList.length > 0) { const isUploadDown = this.data.uploadImageList.some(item => !item.uploaded) if (isUploadDown) { wx.showToast({ title: "请等待图片上传完成", icon: "none", duration: 1000 }) return false } } if (this.data.uploadImageList.length > 0) { this.data.uploadImageList.forEach(item => { imagesList.push(item.ossUrl) }) } this.data.dataForm.groupBuyPriceNumber.forEach(item => { item.groupBuyPrice = parseFloat(item.groupBuyPrice) }) this.setData({ lock: true }) const para = { ...this.data.dataForm } para.isConReview = this.data.isConReview para.groupBuyImg = imagesList console.log('submit', para) let that = this api.groupBuyUpdate(para).then(res => { this.setData({ lock: false }) if(res.code == 0) { wx.hideLoading() this.setData({ dialogVisible: !this.data.dialogVisible, 'dataForm.groupBuyTitle': '', violationsCount: 0, isConReview: false }) } else if (res.code == 533) { wx.hideLoading() this.data.violationsCount++ console.log(this.data.violationsCount) if (this.data.violationsCount == 1){ wx.showToast({ title: res.msg, icon: "none", duration: 2000 }) } else if (this.data.violationsCount == 2) { wx.hideLoading() wx.showModal({ title: '提示', content: '您提交的内容再次被判定为违规,您确定是否要提交?', success (res) { if (res.confirm) { console.log('用户点击确定') that.data.isConReview = true that.submitApply() } else if (res.cancel) { console.log('用户点击取消') that.setData({ violationsCount: 0, isConReview: false }) } } }) } } }).catch(err => { console.log('err',err) wx.hideLoading() this.setData({ lock: false }) }) }, initDataForm () { wx.showLoading({ title: '加载中...' }) api.getGroupBuyDetail(this.data.dataForm.id).then(res => { wx.hideLoading() res.data.groupBuyImg.forEach( (item, index) => { let ob = { imageId: index, ossUrl: item, imgUrl: item, uploaded: true } this.data.uploadImageList.push(ob) }) this.setData({ dataForm: res.data, uploadImageList: this.data.uploadImageList }) console.log(this.data.uploadImageList) }) }, //简化提示 showToast (title) { wx.showToast({ title: title, icon: "none", duration: 2000 }) }, // 关闭弹框 closeDialog () { wx.navigateBack({ delta: 1 }) }, // 弹框确定按钮 confirmDialog () { wx.navigateBack({ delta: 1 }) }, // 删除选中的图片 deleteImage (e) { const index = this.data.uploadImageList.findIndex(item => item.imageId === e.currentTarget.dataset.imageid) if (index > -1) { this.data.uploadImageList.splice(index, 1) this.setData({ uploadImageList: this.data.uploadImageList }) } }, // 选择图片 上传弹窗 - 上传图片方式 - 选择图片 - 上传图片 - 回调赋值 chooseImage () { const _this = this $wuxActionSheet().showSheet({ buttons: [ { text: "拍照" }, { text: "从相册中获取" }, ], className: "dialog-class", buttonClicked (index) { if (index === 0) { wx.chooseImage({ count: 1, sizeType: ["original", "compressed"], sourceType: ["camera"], success (res) { const uploadImageList = [..._this.data.uploadImageList] uploadImageList.push({ uploaded: false, ossUrl: "", imgUrl: res.tempFilePaths[0], imageId: ++_this.data.imageId }) _this.setData({ uploadImageList }) wx.uploadFile({ url: `${config.BASEURL()}group/topic/upload`, filePath: res.tempFilePaths[0], name: "file", header: { "Content-type": "multipart/form-data" }, success (fileRes){ uploadImageList[uploadImageList.length - 1].uploaded = true uploadImageList[uploadImageList.length - 1].ossUrl = JSON.parse(fileRes.data).data _this.setData({ uploadImageList }) } }) } }) } else if (index === 1) { wx.chooseImage({ count: 3 - _this.data.uploadImageList.length, sizeType: ["original", "compressed"], sourceType: ["album"], success (res) { const uploadImageList = [] const endIndex = _this.data.uploadImageList.length res.tempFilePaths.forEach(item => { uploadImageList.push({ uploaded: false, ossUrl: "", imgUrl: item, imageId: ++_this.data.imageId }) }) _this.setData({ uploadImageList: [..._this.data.uploadImageList,...uploadImageList] }) uploadImageList.forEach((item, index) => { return (function (index) { wx.uploadFile({ url: `${config.BASEURL()}group/topic/upload`, filePath: res.tempFilePaths[index], name: "file", header: { "Content-type": "multipart/form-data" }, success (fileRes){ uploadImageList[index].uploaded = true uploadImageList[index].ossUrl = JSON.parse(fileRes.data).data _this.data.uploadImageList = _this.data.uploadImageList.slice(0, endIndex) _this.setData({ uploadImageList: [..._this.data.uploadImageList, ...uploadImageList] }) } }) })(index) }) } }) } return true }, cancelText: "取消", cancel () {}, destructiveButtonClicked () {}, }) }, })