export default { props: { outTradeNo: { type: String, default: '' } }, data(){ return { type: 'third', payType: { third: { type: 'third', name: '付款码支付', icon: 'iconsaomaqiang', background: '#f7931e' }, cash: { type: 'cash', name: '现金支付', icon: 'iconxianjin1', background: '#f5b719' }, own_wechatpay: { type: 'own_wechatpay', name: '个人微信', icon: 'iconwxpay', background: '#09bb07' }, own_alipay: { type: 'own_alipay', name: '个人支付宝', icon: 'iconzhifubaozhifu', background: '#1890ff'}, own_pos: { type: 'own_pos', name: '个人POS刷卡', icon: 'iconyinhangqia', background: '#ec6a55' } }, payInfo: null, paystatus: 'pay', isRepeat: false, qrcodeShow: false, qrcodeUrl: '', timer: null, moneyPopup: { money: 0, type: '', title: '' }, cash: 0, discount: {}, scanCodeType: 'scancode', scancodeFocus: true, authCode: '', autoComplete: { time: 8, timer: null }, remark: '', autoPrintTicket: true } }, computed:{ balance(){ if (this.memberInfo) { return parseFloat(this.memberInfo.balance_money) + parseFloat(this.memberInfo.balance); } return 0; } }, created() { if (this.outTradeNo) this.calculation(); }, destroyed() { clearInterval(this.timer); }, methods: { /** * 取消支付 */ cancelPayment(){ this.$emit('cancel', {}) this.clearPay(); }, /** * 支付成功 */ paySuccess(){ this.$emit('success', {}) this.clearPay(); }, clearPay(){ clearInterval(this.timer); this.type = 'third'; this.payInfo = null; this.paystatus = 'pay'; this.qrcodeUrl = ''; this.cash = 0; this.discount = {}; this.isRepeat = false; if (this.autoComplete.timer) clearInterval(this.autoComplete.timer); this.autoComplete.time = 8; this.remark = ''; this.autoPrintTicket = true; }, confirm(){ if (this.isRepeat) return; uni.showLoading({}) if (this.type == 'cash') { if (!this.cash) { this.cash = this.payInfo.pay_money; } else if (isNaN(parseFloat(this.cash)) || parseFloat(this.cash) < parseFloat(this.payInfo.pay_money)) { this.$util.showToast({title: '现金收款金额错误'}); return; } } this.isRepeat = true; let data = { pay_type: this.type, out_trade_no: this.outTradeNo, member_id: this.memberInfo ? this.memberInfo.member_id : 0, promotion: JSON.stringify(this.$util.deepClone(this.discount)), cash: this.type == 'cash' ? this.cash : 0 } this.$api.sendRequest({ url: '/cashier/storeapi/cashierpay/confirm', data, success: res => { uni.hideLoading(); if (res.code == 0) { this.paystatus = 'success'; this.$emit('getMemberInfo'); } else { this.isRepeat = false; this.$util.showToast({title: res.message}) } }, fail: res => { uni.hideLoading(); } }) }, calculation(){ let data = { pay_type: this.type, out_trade_no: this.outTradeNo, member_id: this.memberInfo ? this.memberInfo.member_id : 0, promotion: JSON.stringify(this.$util.deepClone(this.discount)), cash: this.type == 'cash' ? this.cash : 0 } this.$api.sendRequest({ url: '/cashier/storeapi/cashierpay/paycalculate', data, success: res => { if (res.code == 0) { this.payInfo = res.data; if (this.payInfo.pay_status == 1) { this.paystatus = 'success'; this.$emit('getMemberInfo'); } } else { this.$util.showToast({title: res.message}) } }, fail: res => { } }) }, /** * 打印小票 */ printTicket(){ this.$api.sendRequest({ url: '/cashier/storeapi/cashierorder/printticket', data: { order_id: this.payInfo.order_id }, success: res => { if (res.code != 0) { this.$util.showToast({title: res.message ? res.message : '小票打印失败'}) } } }) }, thirdConfirm(){ this.authCode = ''; this.scanCodeType = 'scancode'; this.scancodeFocus = true; this.$refs.thirdPopup.open(); }, /** * 获取支付二维码 */ getQrcode(){ this.$api.sendRequest({ url: '/cashier/storeapi/cashierpay/payqrcode', data: { out_trade_no: this.outTradeNo }, success: res => { if (res.code == 0) { this.qrcodeUrl = res.data.path; this.checkPayStatus(); } else { this.$util.showToast({title: res.message}) } } }) }, popupChange(){ if (this.timer) { clearInterval(this.timer); } }, /** * 扫码枪 */ scancode(e){ if (!e.detail.value) return; if (this.isRepeat) return; uni.showLoading({}) this.isRepeat = true; this.$api.sendRequest({ url: '/cashier/storeapi/cashierpay/createpay', data: { out_trade_no: this.outTradeNo }, success: res => { if (res.code == 0) { this.$api.sendRequest({ url: '/pay/pay/authcodePay', data: { out_trade_no: this.outTradeNo, auth_code: e.detail.value }, success: res => { this.authCode = ''; uni.hideLoading(); if (res.code >= 0) { this.$refs.thirdPopup.close(); this.paystatus = 'success'; } else { this.isRepeat = false; this.$util.showToast({title: res.message}) } } }) } else { uni.hideLoading(); this.isRepeat = false; this.$util.showToast({title: res.message}) } } }) }, /** * 查询支付状态 */ checkPayStatus(){ this.timer = setInterval(() => { this.$api.sendRequest({ url: '/cashier/storeapi/cashierpay/info', data: { out_trade_no: this.outTradeNo }, success: res => { if (res.code == 0 && res.data && res.data.pay_status == 2) { this.paystatus = 'success'; clearInterval(this.timer) } } }) }, 1500) }, /** * 打开金额弹窗 * @param {Object} data */ openMoneyPopup(data){ this.moneyPopup = Object.assign(this.moneyPopup, data); this.$refs.moneyPopup.open(); }, deleteCode(){ this.moneyPopup.money = this.moneyPopup.money.substr(0, this.moneyPopup.money.length - 1); }, moneyPopupConfirm(){ if (!this.moneyPopup.money.length) { this.$util.showToast({title: '请输入金额'}) return; } if (this.moneyPopup.type == 'reduction') { this.discount.reduction = parseFloat(this.moneyPopup.money); } else if (this.moneyPopup.type == 'cash') { this.cash = parseFloat(this.moneyPopup.money); } this.calculation(); this.$refs.moneyPopup.close(); }, keydown(value){ let arr = this.moneyPopup.money.split('.'); if (arr[1]) { if (value == '.' || arr[1].length == 2) return; if (value == '00' && arr[1].length == 1) value = '0'; } if (this.moneyPopup.type == 'reduction' && parseFloat(this.moneyPopup.money + value) > parseFloat(this.payInfo.pay_money)) return; if (parseFloat(this.moneyPopup.money + value) > 1000000) { this.$util.showToast({title: '最大不能超过1000000'}) return; } this.moneyPopup.money += value; }, /** * 切换支付方式 * @param {Object} type */ switchPayType(type){ this.type = type; if (type == 'cash') { if (this.cash) { this.openMoneyPopup({title: '收款金额', money: this.$util.moneyFormat(this.cash), type: 'cash'}) } else { this.openMoneyPopup({title: '收款金额', money: this.$util.moneyFormat(this.payInfo.pay_money), type: 'cash'}) } } else { this.calculation(); } }, /** * 减免金额 */ reduction(){ if (this.discount.reduction) { delete this.discount.reduction; this.calculation(); } else { this.openMoneyPopup({title: '减免金额', money: '', type: 'reduction'}) } }, /** * 使用积分 */ usePoint(){ if (this.payInfo.offset.point_array.point == 0) return; if (this.discount.is_use_point) { delete this.discount.is_use_point; } else { this.discount.is_use_point = 1; } this.calculation(); }, useBalance(){ if (this.balance == 0) return; if (this.discount.is_use_balance) { delete this.discount.is_use_balance; } else { this.discount.is_use_balance = 1; } this.calculation(); }, selectCoupon(){ if (!this.payInfo.offset.coupon_array.member_coupon_list.length) return; this.$refs.couponPopup.open(); }, selectCouponItem(data){ if (!this.discount.coupon_id) { this.discount.coupon_id = data.coupon_id; } else if (this.discount.coupon_id != data.coupon_id) { this.discount.coupon_id = data.coupon_id; } else { delete this.discount.coupon_id; } this.$forceUpdate(); this.calculation(); }, /** * 设置备注 */ remarkConfirm() { if (!this.remark) return; this.$api.sendRequest({ url: '/cashier/storeapi/cashierorder/orderRemark', data: { order_id: this.payInfo.order_id, remark: this.remark }, success: res => { this.$refs.remarkPopup.close(); } }); } }, watch:{ outTradeNo: function(nval, oval){ if (nval) this.calculation(); }, type: function(nval){ if (nval != 'third' && this.timer) { clearInterval(this.timer); } }, scanCodeType: function(nval){ if (nval == 'scancode') { this.scancodeFocus = true; if (this.timer) clearInterval(this.timer); } else { this.getQrcode(); } }, paystatus: function(nval){ if (nval == 'success') { if (this.autoPrintTicket) this.printTicket(); this.isRepeat = false; this.autoComplete.timer = setInterval(() => { if (this.autoComplete.time == 0) { this.paySuccess(); } else { this.autoComplete.time--; } }, 1000) } } } }