Vue.component('m-edit-coupon', { template: ` 优惠券基础信息 即满【{{ruleForm.consumption_threshold||'?'}}】元减【{{ruleForm.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 chooseClassList: [],//已选分类列表 chooseGoodsList: [],//已选商品列表 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: '',//发放总量 discount_amount: '',//优惠券面额 consumption_threshold: '',//消费门槛 grant_type: '1',//发放类型 choose_store: '0',//是否限制门店 store_id_arr: [],//指定的门店id choose_grade: '0',//是否限制会员等级 vip_grade_id: [],//指定的会员等级 appoint_type: '1',//商品限制 appoint_id_arr: [],//商品/类目id validity_time_type: '1',//有效期类型 start_time: '',//开始时间 end_time: '',//结束时间 day_num: '',//有效天数 explain: '',//使用说明 }, 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' }], 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.ruleForm.discount_amount * 1 >= value * 1) { callback(new Error("消费门槛不得小于优惠券面额")); } else if (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' }], appoint_type: [{message: '请选择使用商品限制', required: true, trigger: 'change'}], appoint_id_arr: [{message: '请选择商品/类目', required: true, trigger: 'change'}, { validator: (rule, value, callback) => { if (_t.ruleForm.appoint_type == 1 || value && value.length) { callback(); } else { let str = _t.ruleForm.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.ruleForm.start_time) { let b = Date.parse(new Date(_t.ruleForm.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', }], }, btnLoading: false,//提交按钮状态 formDis: false,//表单是否能编辑 appointSendVip: false, //是否指定发送给某会员(仅新建时) vipSearchInput: '', //会员搜索关键字 vipSearchLoading: false,//会员搜索loading vipSearchList: [], //会员搜索结果列表 selectedVip: null, //已选中的会员 } }, computed: {}, methods: { //搜索会员(用于新建时指定发送) searchVipForCoupon() { if (!this.vipSearchInput || !this.vipSearchInput.trim()) { this.$message.warning('请输入手机号'); return; } this.vipSearchLoading = true; this.selectedVip = null; this.vipSearchList = []; $.ajax({ url: '/index.php/Retail/StoreCoupon/searchVipForCoupon', type: 'post', data: {search_info: this.vipSearchInput.trim()}, success: (res) => { this.vipSearchLoading = false; if (res.status == 1) { this.vipSearchList = res.data.list || []; if (this.vipSearchList.length === 0) { this.$message.warning('未找到匹配的用户,请确认手机号'); } } else { this.$message.error(res.message); } }, error: () => { this.vipSearchLoading = false; this.$message.error('搜索失败,请重试'); } }); }, //选中搜索结果中的某一行会员 selectVipRow(row) { this.selectedVip = row; }, //取消选中会员 clearSelectedVip() { this.selectedVip = null; }, //指定发送开关变化时清空选中 onAppointSendVipChange(val) { if (!val) { this.vipSearchInput = ''; this.vipSearchList = []; this.selectedVip = null; } }, //关闭弹框 closeDialog() { this.$refs.ruleForm.resetFields(); this.chooseClassList = []; this.chooseGoodsList = []; this.btnLoading = false; this.ruleForm.appoint_type = '1'; this.ruleForm.choose_grade = '0'; this.ruleForm.vip_grade_id = []; this.appointSendVip = false; this.vipSearchInput = ''; this.vipSearchList = []; this.selectedVip = null; this.vipSearchLoading = false; this.$nextTick(() => { this.$emit('close'); }); }, //修改发放类型 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.appoint_type = '1'; this.ruleForm.appoint_id_arr = []; }, //修改商品限制类型 changeAppointType() { this.chooseClassList = []; this.chooseGoodsList = []; this.ruleForm.appoint_id_arr = []; }, //修改有效期限制类型 changeValidityTimeType() { this.ruleForm.start_time = ''; this.ruleForm.end_time = ''; this.ruleForm.day_num = ''; }, //选择分类 chooseClass() { this.$root.$refs.base.chooseClass(this.ruleForm.appoint_id_arr, this.ruleForm.store_id_arr[0], (data) => { let classList = this.chooseClassList; classList.push.apply(classList, data); this.chooseClassList = []; this.chooseClassList = classList; data.forEach((item) => { this.ruleForm.appoint_id_arr.push(item.id); }) this.$refs.ruleForm.validateField('appoint_id_arr'); }); }, //选择商品 chooseGoods() { this.$root.$refs.base.chooseGoods({ goods_ids: this.appoint_id_arr, store_id: this.ruleForm.store_id_arr[0], class_id: [], }, (data) => { let goodsList = this.chooseGoodsList; goodsList.push.apply(goodsList, data); this.chooseGoodsList = []; this.chooseGoodsList = goodsList; data.forEach((item) => { let key = item.goods_id; if (item.attribute_state == 2) { key += '_' + item.sku_id; } this.ruleForm.appoint_id_arr.push(key); }) this.$refs.ruleForm.validateField('appoint_id_arr'); }); }, //移除已选商品、分类ID removeAppointId(appoint_id) { let arr = []; this.ruleForm.appoint_id_arr.forEach((id) => { if (appoint_id != id) { arr.push(id); } }); this.ruleForm.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 = {}; 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; } else { data = { ...this.ruleForm }; data.appoint_id_arr = data.appoint_id_arr.join(','); 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'; } } $.ajax({ url: '/index.php/Retail/' + (this.storeId ? 'StoreCoupon' : 'Mcoupon') + '/' + (this.editId ? 'editCoupon' : 'createCoupon'), type: "post", data: { coupon: JSON.stringify(data), }, success: (res) => { if (res.status == 1) { //新建且指定了发送会员时,创建后自动发放 if (!this.editId && this.storeId && this.appointSendVip && this.selectedVip && res.data && res.data.coupon_id) { $.ajax({ url: '/index.php/Retail/StoreCoupon/grantCouponToUser', type: 'post', data: { coupon_id: res.data.coupon_id, xcxvip_id: this.selectedVip.xcxvip_id, }, success: (grantRes) => { this.btnLoading = false; if (grantRes.status == 1) { this.$message.success('优惠券创建成功,已发送给指定用户'); } else { this.$message.warning('优惠券创建成功,但发送给用户失败:' + grantRes.message); } this.closeDialog(); }, error: () => { this.btnLoading = false; this.$message.warning('优惠券创建成功,但发送给用户时发生错误'); this.closeDialog(); } }); } else { this.btnLoading = false; this.$message.success(res.message); this.closeDialog(); } } else { this.btnLoading = false; this.$message.error(res.message); } }, error: (error) => { console.log(error); this.btnLoading = false; } }) }).catch((err) => { console.log(err); }) }); }, //获取券详情 getCouponDetails() { this.formLoading = true; $.ajax({ url: '/index.php/Retail/' + (this.storeId ? 'StoreCoupon' : 'Mcoupon') + '/couponInfo', type: 'post', data: { id: this.editId, }, success: res => { this.formLoading = false; this.$nextTick(() => { if (res.status == 1) { try { let data = res.data.coupon; data.appoint_id_arr = data.choose_store == 1 && (data.appoint_type == 2 || data.appoint_type == 3) ? data.appoint_id_arr.split(',') : []; 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]; } this.chooseClassList = res.data.class_list; this.chooseGoodsList = res.data.goods_list; } catch (e) { console.log(e); this.$alert('该优惠券异常', '错误提示', { confirmButtonText: '关闭', callback: action => { this.closeDialog(); } }); } } }) } }) }, }, mounted() { if (!this.editId && this.storeId) { this.$nextTick(() => { this.ruleForm.store_id_arr = [this.storeId]; this.ruleForm.choose_store = '1'; }) } }, watch: { showEdit(n) { if (n) { this.ruleForm.validity_time_type = '1'; this.appointSendVip = false; this.vipSearchInput = ''; this.vipSearchList = []; this.selectedVip = null; 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'; } } }, } });