Vue.component('m-edit-coupon-package', { template: ` 券包基础信息 基本规则 自主发放 充值赠送 所有门店 指定门店 优惠券
取消 保存
优惠券基础信息 即满【{{couponForm.consumption_threshold||'?'}}】元减【{{couponForm.discount_amount||'?'}}】元 基本规则 固定日期 领券当日开始 领券次日开始
-
天内有效
取消 保存
`, props: { 'showEdit': { type: Boolean, default: false, },//是否显示 'editId': { type: String, default: '', },//编辑的券包ID 'storeId': { type: String, default: '', },//门店ID,只有门店操作时会传 'isSee': { type: Boolean, default: true, },//是否只是查看 }, data() { let _t = this; return { formLoading: false,//表单初始化loading datePickerStartOptions: { disabledDate: (time) => { let nowDate = new Date(); let oneDay = 86400000; let endDate = _t.ruleForm.end_time ? (new Date(_t.ruleForm.end_time)) : new Date(nowDate.getTime() + (oneDay * 60)); return time.getTime() < nowDate - oneDay || time.getTime() > endDate; } },//开始时间限制 datePickerEndOptions: { disabledDate: (time) => { let nowDate = new Date(); let oneDay = 86400000; let startDate = _t.ruleForm.start_time ? (new Date(_t.ruleForm.start_time)) : nowDate; let endDate = new Date(nowDate.getTime() + (oneDay * 90)); return time.getTime() < startDate || time.getTime() > endDate; } },//结束时间限制 ruleForm: { coupon_name: '', coupon_amount: '', coupon_group_money: '', grant_type: '1', choose_store: '0', store_id_arr: [], choose_grade: '0', vip_grade_id: [], coupon_list: [],//优惠券列表 }, rules: { coupon_name: [{message: '请设置券包名称', required: true, trigger: 'change'}, { max: '30', message: '最大长度为 30 个字符', }], coupon_amount: [{message: '请设置发放总量', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { let reg = /^[+]{0,1}(\d+)$/; if (value === '' || value === undefined) { callback(new Error("请设置发放总量")); } else if (!reg.test(value) || value * 1 > 10000000) { callback(new Error("发放总量最大为一千万")); } else { callback(); } }, trigger: 'change' }], grant_type: [{message: '请选择发放类型', required: true, trigger: 'change'}], choose_store: [{message: '请选是否限制门店', required: true, trigger: 'change'}], store_id_arr: [{message: '请选择指定的门店', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { if (_t.ruleForm.choose_store == 1 && (!value || !value.length)) { callback(new Error("请选择指定的门店")); } else { callback(); } }, trigger: 'blur' }], choose_grade: [{message: '请选择是否限制会员等级', required: true, trigger: 'change'}], vip_grade_id: [{message: '请选择指定的会员等级', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { if (_t.ruleForm.choose_grade == 1 && (!value || !value.length)) { callback(new Error("请选择指定的会员等级")); } else { callback(); } }, trigger: 'blur' }], coupon_list: [{message: '请设置优惠券', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { if (!value || !value.length) { callback(new Error("请设置优惠券")); } else { callback(); } }, trigger: 'change' }], },//券包验证规则 btnLoading: false,//提交按钮状态 formDis: false,//表单大部分按钮是否禁止编辑 showEditCoupon: false,//是否显示单张券详情 couponForm: { coupon_name: '',//优惠券名称 discount_amount: '',//优惠券面额 consumption_threshold: '',//消费门槛 appoint_type: '1',//商品限制 appoint_id_arr: [],//商品/类目id validity_time_type: '1',//有效期类型 start_time: '',//开始时间 end_time: '',//结束时间 day_num: '',//有效天数 explain: '',//使用说明 class_list: [],//已选分类列表 goods_list: [],//已选商品列表 },//优惠券表单 couponRules: { coupon_name: [{message: '请设置优惠券名称', required: true, trigger: 'change'}, { max: '30', message: '最大长度为 30 个字符', }], discount_amount: [{message: '请设置优惠券面额', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { let reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d{0,2})$/; if (value === '' || value === undefined) { callback(new Error("请设置优惠券面额")); } else if (!reg.test(value) || value < 0.01) { callback(new Error("请设置正确的优惠券面额")); } else if (value * 1 > 10000000) { callback(new Error("优惠券面额最大为一千万")); } else { callback(); } }, trigger: 'change' }], consumption_threshold: [{message: '请设置消费门槛', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { let reg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d{0,2})$/; if (value === '' || value === undefined) { callback(new Error("请设置消费门槛")); } else if (!reg.test(value) || value < 0.01) { callback(new Error("请设置正确的消费门槛")); } else if (_t.couponForm.discount_amount * 1 >= value * 1) { callback(new Error("消费门槛不得小于优惠券面额")); } else if (value * 1 > 10000000) { callback(new Error("消费门槛最大为一千万")); } else { callback(); } }, trigger: 'change' }], appoint_type: [{message: '请选择使用商品限制', required: true, trigger: 'change'}], appoint_id_arr: [{message: '请选择商品/类目', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { if (_t.couponForm.appoint_type == 1 || value && value.length) { callback(); } else { let str = _t.couponForm.appoint_type == 2 ? '类目' : '商品'; callback(new Error("请选择指定的" + str)); } }, trigger: 'blur' }], validity_time_type: [{message: '请选择有效期类型', required: true, trigger: 'change'}], start_time: [{message: '请选择开始时间', required: true, trigger: ['change', 'blur']}, { validator: (rule, value, callback) => { if (!value) { callback(new Error('请选择开始时间')); } else { if (!_t.editId) { let start = Date.parse(new Date()); let end = start + 86400000 * 60; let a = Date.parse(new Date(value)); if (start > a) { callback(new Error('开始时间不得小于当前时间')); } else if (a > end) { callback(new Error('开始时间不得小于当前时间后60天')); } else { callback(); } } else { callback(); } } }, trigger: ['change', 'blur'] }], end_time: [{message: '请选择结束时间', required: true, trigger: ['change', 'blur']}, { validator: (rule, value, callback) => { if (!value) { callback(new Error('请选择结束时间')); } else { if (!_t.editId) { let start = Date.parse(new Date()); let end = start + 86400000 * 90; let a = Date.parse(new Date(value)); if (_t.couponForm.start_time) { let b = Date.parse(new Date(_t.couponForm.start_time)); if (a <= b) { callback(new Error('结束时间不得小于等于开始时间')); } } if (start > a) { callback(new Error('结束时间不得小于当前时间')); } else if (a > end) { callback(new Error('结束时间不得小于当前时间后90天')); } else { callback(); } } else { callback(); } } }, trigger: ['change', 'blur'] }], day_num: [{message: '请设置有效天数', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { if (!value) { callback(new Error('请设置有效天数')); } else if (value * 1 > 90) { callback(new Error('有效天数不得大于90天')); } else { callback(); } }, trigger: ['change', 'blur'] }], explain: [{message: '请设置使用说明', required: false, trigger: 'change'}, { max: '500', message: '最大长度为 500', }], },//优惠券验证规则 editCouponIndex: -1,//编辑的优惠券下标 } }, computed: { //计算优惠总额 coupon_group_money() { let money = 0; this.ruleForm.coupon_list.forEach((item) => { money = toDecimal(money + toDecimal(item.discount_amount * this.ruleForm.coupon_amount)); }); return money; } }, methods: { //关闭券包弹框 closeDialog() { this.$refs.ruleForm.resetFields(); this.btnLoading = false; this.$nextTick(() => { this.$emit('close'); }) }, //删除优惠券 removeCoupon(index) { this.ruleForm.coupon_list.splice(index, 1); }, //添加、编辑优惠券 editCoupon(item, index) { this.showEditCoupon = true; this.$nextTick(() => { if (!item) { this.editCouponIndex = -1; } else { let data = {...item}; data.appoint_id_arr = this.ruleForm.choose_store == 1 && (data.appoint_type == 2 || data.appoint_type == 3) ? data.appoint_id_arr.split(',') : [] for (let k in this.couponForm) { this.couponForm[k] = data[k]; } this.editCouponIndex = index; } }) }, //关闭优惠券弹框 closeEditCoupon() { this.$refs.couponForm.resetFields(); this.$nextTick(() => { this.showEditCoupon = false; }) }, //确认保存单张优惠券 sureEditCoupon() { this.$refs.couponForm.validate(valid => { if (!valid) return this.$message.warning('请检查必填项'); let data = JSON.parse(JSON.stringify(this.couponForm)); // console.log(this.editCouponIndex); if (this.editCouponIndex >= 0) { let coupon_list = JSON.parse(JSON.stringify(this.ruleForm.coupon_list)); coupon_list[this.editCouponIndex] = data; this.ruleForm.coupon_list = []; this.ruleForm.coupon_list = coupon_list; } else { this.ruleForm.coupon_list.unshift(data); } this.closeEditCoupon(); }); }, //修改发放类型 changeGrantType() { if (!this.editId && this.storeId) { this.ruleForm.choose_store = '1'; this.ruleForm.store_id_arr = [this.storeId]; this.changeChooseStore(); } else { this.ruleForm.choose_store = '0'; this.ruleForm.store_id_arr = []; this.changeLimitStore(); } this.ruleForm.choose_grade = '0'; this.ruleForm.vip_grade_id = []; }, //修改会员类型限制 changeGradeType() { this.ruleForm.vip_grade_id = []; }, //修改门店限制 changeLimitStore() { this.ruleForm.store_id_arr = []; this.changeChooseStore(); }, //修改指定门店 changeChooseStore() { this.ruleForm.coupon_list.forEach((item) => { item.appoint_type = '1'; item.appoint_id_arr = []; item.class_list = []; item.goods_list = []; }) }, //修改商品限制类型 changeAppointType() { this.couponForm.class_list = []; this.couponForm.goods_list = []; this.couponForm.appoint_id_arr = []; }, //修改有效期限制类型 changeValidityTimeType() { this.couponForm.start_time = ''; this.couponForm.end_time = ''; this.couponForm.day_num = ''; }, //选择分类 chooseClass() { this.$root.$refs.base.chooseClass(this.couponForm.appoint_id_arr, this.ruleForm.store_id_arr[0], (data) => { let classList = this.couponForm.class_list; classList.push.apply(classList, data); this.couponForm.class_list = []; this.couponForm.class_list = classList; data.forEach((item) => { this.couponForm.appoint_id_arr.push(item.id); }); this.$nextTick(() => { this.$refs.couponForm.validateField('appoint_id_arr'); }) }); }, //选择商品 chooseGoods() { this.$root.$refs.base.chooseGoods({ goods_ids: this.couponForm.appoint_id_arr, store_id: this.ruleForm.store_id_arr[0], class_id: [], }, (data) => { let goodsList = this.couponForm.goods_list; goodsList.push.apply(goodsList, data); this.couponForm.goods_list = []; this.couponForm.goods_list = goodsList; data.forEach((item) => { let key = item.goods_id; if (item.attribute_state == 2) { key += '_' + item.sku_id; } this.couponForm.appoint_id_arr.push(key); }); this.$refs.couponForm.validateField('appoint_id_arr'); }); }, //查看商品 seeGoods(item) { this.$root.$refs.base.chooseGoods({ goods_ids: [], store_id: this.ruleForm.store_id_arr[0], class_id: item.id_list, needChoose: false, }); }, //移除已选商品、分类ID removeAppointId(appoint_id) { let arr = []; this.couponForm.appoint_id_arr.forEach((id) => { if (appoint_id != id) { arr.push(id); } }); this.couponForm.appoint_id_arr = arr; }, //保存券包设置 saveCoupon() { this.$refs.ruleForm.validate(valid => { if (!valid) return this.$message.warning('请检查必填项'); this.$confirm('确认保存该券包吗?', "提示", { confirmButtonText: '确定', }).then(() => { this.btnLoading = true; let data = {}; let len = this.ruleForm.coupon_list.length; if (this.editId) { //编辑只允许修改以下字段 data.coupon_name = this.ruleForm.coupon_name; data.coupon_amount = this.ruleForm.coupon_amount; data.explain = this.ruleForm.explain; data.id = this.editId; data.coupon_list = []; for (let i = 0; i < len; i++) { let obj = { id: this.ruleForm.coupon_list[i].id, coupon_name: this.ruleForm.coupon_list[i].coupon_name, explain: this.ruleForm.coupon_list[i].explain, }; data.coupon_list.push(obj); } } else { data = { ...this.ruleForm }; data.vip_grade_id = data.choose_grade == '1' ? data.vip_grade_id.join(',') : ''; if (!data.vip_grade_id) data.vip_grade_id = '0'; data.store_id_arr = data.store_id_arr.join(','); if (!data.store_id_arr) data.store_id_arr = '0'; for (let i = 0; i < len; i++) { delete data.coupon_list[i].class_list; delete data.coupon_list[i].goods_list; data.coupon_list[i].appoint_id_arr = data.coupon_list[i].appoint_id_arr.join(','); } } $.ajax({ url: '/index.php/Retail/' + (this.storeId ? 'StoreCoupon' : 'Mcoupon') + '/' + (this.editId ? 'editPackage' : 'createCouponGroup'), type: "post", data: { coupon: JSON.stringify(data), }, success: (res) => { this.btnLoading = false; if (res.status == 1) { this.$message.success(res.message); this.closeDialog(); } else { this.$message.error(res.message); } }, error: (error) => { console.log(error); this.btnLoading = false; } }) }).catch(() => { }) }); }, //获取券包详情 getCouponDetails() { this.formLoading = true; $.ajax({ url: '/index.php/Retail/' + (this.storeId ? 'StoreCoupon' : 'Mcoupon') + '/couponGroupDetails', type: "post", data: { id: this.editId, }, success: (res) => { if (res.status == 1) { try { let data = res.data; data.vip_grade_id = (data.choose_grade == 1) ? data.vip_grade_id.split(',') : []; data.store_id_arr = (data.choose_store == 1) ? data.store_id_arr.split(',') : []; for (let k in this.ruleForm) { this.ruleForm[k] = data[k]; } } catch (e) { console.log(e); this.$alert('该优惠券异常', '错误提示', { confirmButtonText: '关闭', callback: action => { this.closeDialog(); } }); } } this.formLoading = false; } }) }, }, mounted() { // console.log(this.editId, this.storeId); if (!this.editId && this.storeId) { this.ruleForm.store_id_arr = [this.storeId]; this.ruleForm.choose_store = '1'; // console.log(this.ruleForm.choose_store); } }, watch: { showEdit(n) { if (n) { this.ruleForm.validity_time_type = '1'; if (this.editId) { this.getCouponDetails(); } this.formDis = this.editId ? true : false; if (!this.editId) { this.ruleForm.store_id_arr = this.storeId ? [this.storeId] : []; this.ruleForm.choose_store = this.storeId ? '1' : '0'; this.ruleForm.choose_grade = '0'; this.ruleForm.vip_grade_id = []; } } }, } });