// pages/mine/product/coupon/addcoupon.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { typeList: [{ id: '1', name: '无门槛', isInput: false, isSel: true, value: '', pValue: '0', key: 'type1' }, { id: '2', name: '订单满', isInput: true, isSel: false, pValue: '1', value: '', key: 'type2' }], //使用门槛 scopeList: [{ id: '1', name: '全部商品', isSel: true, pValue: '0', isShowBtn: false, }, { id: '2', name: '指定商品', isSel: false, pValue: '1', isShowBtn: true, }], dueTimeList: [{ id: '1', name: '', startDate: '', endDate: '', type: 'picker', isSel: true, pValue: '0', key: 'dueTime1', value: '' }, { id: '2', name: '领取当日开始', startDate: '', endDate: '', type: 'input', pValue: '1', isSel: false, key: 'dueTime2', value: '' }, { id: '3', name: '领取次日开始', startDate: '', endDate: '', pValue: '2', type: 'input', isSel: false, key: 'dueTime3', value: '' }], //有效期 rankList: [{ id: '1', name: '不限', isSel: true, pValue: '0', hint: '' }, { id: '2', name: '仅注册用户可领取', isSel: false, hint: '', pValue: '1', }, { id: '3', name: '仅新客可领取', isSel: false, hint: '该限制仅在客户主动领取优惠券时生效', pValue: '2', }], //身份限制 astrictList: [{ id: '1', name: '不限', isInput: false, isSel: true, value: '', key: 'type3', pValue: '0', }, { id: '2', name: '每人限领', isInput: true, isSel: false, value: '', pValue: '1', key: 'type4' }], //领取限制 showTemplete: { name: '商品详情页可领取优惠券', isSel: false }, shareBean: { name: '优惠券允许分享给好友领取', isSel: false }, startDate: '请选择', endDate: '请选择', couponName: '', //优惠券名称 couponCount: '', //优惠券张数 price: '', summary: '', //使用说明 isShowGoods: false, //是否显示添加的商品 goodsList: [], //选中的商品 shopId: '', //商店ID baseImageUrl: app.urls.baseImgUrl }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.setData({ shopId: options.shopId }) }, //使用门槛 doSelType(e) { var item = e.currentTarget.dataset.item var _self = this _self.data.typeList.forEach(it => { if (it.id == item.id) { it.isSel = true } else { it.isSel = false } }) _self.setData({ typeList: _self.data.typeList }) }, //可用商品 doSelScope(e) { var item = e.currentTarget.dataset.item var _self = this _self.data.scopeList.forEach(it => { if (it.id == item.id) { it.isSel = true if (it.id == '2') { _self.setData({ isShowGoods: true }) } else { _self.setData({ isShowGoods: false }) } } else { it.isSel = false } }) _self.setData({ scopeList: _self.data.scopeList }) }, //有效期 doSelDueTime(e) { var item = e.currentTarget.dataset.item // dueTimeList var item = e.currentTarget.dataset.item var _self = this _self.data.dueTimeList.forEach(it => { if (it.id == item.id) { it.isSel = true } else { it.isSel = false } }) _self.setData({ dueTimeList: _self.data.dueTimeList }) }, //日期选择 dateChange(e) { var _self = this var model = e.currentTarget.dataset.model var value = e.detail.value _self.data.dueTimeList.forEach(it => { if (it.id == '1') { it[model] = value _self.setData({ [model]: value }) } }) _self.setData({ dueTimeList: _self.data.dueTimeList }) console.log(e) }, //身份限制 doSelRank(e) { var item = e.currentTarget.dataset.item var _self = this _self.data.rankList.forEach(it => { if (it.id == item.id) { it.isSel = true } else { it.isSel = false } }) _self.setData({ rankList: _self.data.rankList }) }, //领取展示 doSelShow() { var _self = this _self.data.showTemplete.isSel = !_self.data.showTemplete.isSel _self.setData({ showTemplete: _self.data.showTemplete }) }, //使用条件 doSelCon(e) { var item = e.currentTarget.dataset.item var _self = this _self.data.astrictList.forEach(it => { if (it.id == item.id) { it.isSel = true } else { it.isSel = false } }) _self.setData({ astrictList: _self.data.astrictList }) }, //使用说明输入 textareaBInput(e) { this.setData({ summary: e.detail.value }) }, inputWatch(e) { var model = e.currentTarget.dataset.model var value = e.detail.value var _self = this switch (model) { case 'type2': //使用门槛 _self.data.typeList.forEach(it => { if (it.key == model) { it.value = value } }) _self.setData({ typeList: _self.data.typeList }) break case 'dueTime2': //有效期 case 'dueTime3': _self.data.dueTimeList.forEach(it => { if (it.key == model) { it.endDate = value } }) _self.setData({ dueTimeList: _self.data.dueTimeList }) break case 'type4': //每人限领 _self.data.astrictList.forEach(it => { if (it.key == model) { it.value = value } }) _self.setData({ astrictList: _self.data.astrictList }) break default: _self.setData({ [model]: value }) break } }, doShare() { var _self = this _self.data.shareBean.isSel = !_self.data.shareBean.isSel _self.setData({ shareBean: _self.data.shareBean }) }, //选择商品 addGoods() { var _self = this _self.data.scopeList.forEach(it => { if (it.id == '2') { it.isSel = true } else { it.isSel = false } }) _self.setData({ scopeList: _self.data.scopeList, isShowGoods: true }) wx.navigateTo({ url: '/pages/mine/product/coupon/choosegoods?shopId=' + this.data.shopId, }) }, //删除商品 delGoods(e) { console.log(e) var item = e.currentTarget.dataset.item var index = e.currentTarget.dataset.index var _self = this wx.showModal({ title: '警告', content: '确认要删除该商品吗?', success(res) { if (res.confirm) { //去除 _self.data.goodsList.splice(index, 1) _self.setData({ goodsList: _self.data.goodsList }) } } }) }, //校验参数 checkParams() { var _self = this if (_self.data.couponName == '') { wx.showToast({ title: '请输入卷名称', icon: 'error' }) return false } if (_self.data.price == 0) { wx.showToast({ title: '请输入优惠金额', icon: 'error' }) return false } var type = true _self.data.typeList.forEach(it => { if (it.isSel) { if (it.id == '2') { if (it.value == '') { wx.showToast({ title: '请输入使用门槛', icon: 'error' }) type = false } } } }) if (!type) { return false } var scope = true _self.data.scopeList.forEach(it => { if (it.id == '2' && it.isSel) { if (_self.data.goodsList.length <= 0) { wx.showToast({ title: '请选择适用商品', icon: 'error' }) scope = false } } }) if (!scope) { return false } if (_self.data.couponCount == '') { wx.showToast({ title: '请输入发行张数', icon: 'error' }) return false } var due = true _self.data.dueTimeList.forEach(it => { if (it.isSel) { console.log('有效期') if (it.id == '1') { console.log('有效期选中') //选择开始 if (_self.data.startDate == '请选择') { console.log('未输入开始时间') wx.showToast({ title: '请选择开始日期', icon: 'error' }) due = false } if (_self.data.endDate == '请选择') { wx.showToast({ title: '请选择结束日期', icon: 'error' }) due = false } } else { if (it.endDate == '') { wx.showToast({ title: '请输入使用期限', icon: 'error' }) due = false } } } }) if (!due) { return due } var astrict = true _self.data.astrictList.forEach(it => { if (it.id == '2' && it.isSel) { if (it.value == '') { wx.showToast({ title: '请输入每人限领', icon: 'error' }) astrict = false } } }) if (!astrict) { return false } return true }, buildParams() { var params = {} var _self = this params['shopId'] = _self.data.shopId //店铺ID params['couponName'] = _self.data.couponName //名称 params['amount'] = _self.data.price //优惠金额 _self.data.typeList.forEach(it => { if (it.isSel) { if (it.id == '2') { //输入 params['useType'] = it.pValue params['useAmount'] = it.value } else { params['useType'] = it.pValue } } }) _self.data.scopeList.forEach(it => { if (it.isSel) { if (it.id == '2') { //指定商品 params['useCommodity'] = it.pValue var ids = [] _self.data.goodsList.forEach(ii => { var item = { shopgoodId: ii.goodsId } ids.push(item) }) params['shopgoodVOList'] = ids } else { //全部商品 params['useCommodity'] = it.pValue } } }) params['distributionVolume'] = _self.data.couponCount //发放数量 //有效期 _self.data.dueTimeList.forEach(it => { if (it.isSel) { if (it.id == '1') { //时间范围 params['expiringDate'] = it.pValue params['startDate'] = it.startDate params['endDate'] = it.endDate } else { //次日开始 params['expiringDays'] = it.endDate params['expiringDate'] = it.pValue } } }) //身份限制 _self.data.rankList.forEach(it => { if (it.isSel) { params['identityRestrictions'] = it.pValue } }) //使用条件 _self.data.astrictList.forEach(it => { if (it.isSel) { if (it.isSel) { // 次数 params['quota'] = it.pValue params['quotaEveryone'] = it.value } else { // 不限 params['quota'] = it.pValue } } }) //领取展示 if (_self.data.showTemplete.isSel) { params['getShow'] = '0' } // 分享 if (_self.data.shareBean.isSel) { params['share'] = '0' } //使用说明 if (_self.data.summary != '') { params['useExplain'] = _self.data.summary } return params }, doSave() { var _self = this if (_self.checkParams()) { var params = _self.buildParams() wx.showLoading({ title: '添加中...', }) app.http.post(app.urls.doSaveCoupon, { header: { token: app.globalData.token }, data: params }) .then(res => { wx.hideLoading({}) wx.showToast({ title: '添加成功', icon: 'success' }) let arr = getCurrentPages() let lastPage = (arr.length >= 2) ? arr[arr.length - 2] : undefined lastPage.setData({ isAddPage: true }) wx.navigateBack({}) }) .catch(err => { wx.hideLoading({}) }) } }, onShow(e) { this.setData({ goodsList: this.data.goodsList }) } })