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, payQrcode: [], timer: null, moneyPopup: { money: 0, type: '', title: '' }, cash: 0, discount: {}, scanCodeType: 'scancode', scancodeFocus: true, authCode: '', autoComplete: { time: 8, timer: null }, remark: '', autoPrintTicket: true, balanceSafeVerify: false, // 余额使用安全验证 dynacodeData: { key: '', seconds: 120, timer: null, codeText: '获取验证码', isSend: false }, smsCode: '', paymentCode: '', safeVerifyType: 'payment_code', _outTradeNo: '' } }, computed: { balance() { if (this.memberInfo) { return parseFloat(this.memberInfo.balance_money) + parseFloat(this.memberInfo.balance); } return 0; }, promotionShow() { if (this.payInfo && (this.payInfo.offset.coupon_array || this.payInfo.collectmoney_config.reduction == 1)) { return true; } return false; } }, created() { this._outTradeNo = this.outTradeNo; if (this._outTradeNo) this.calculation(); if (typeof uni.getStorageSync('payAutoPrintTicket') == 'boolean') this.autoPrintTicket = uni.getStorageSync( 'payAutoPrintTicket'); }, 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.payQrcode = []; this.cash = 0; this.discount = {}; this.isRepeat = false; if (this.autoComplete.timer) clearInterval(this.autoComplete.timer); this.autoComplete.time = 8; this.remark = ''; this.balanceSafeVerify = false; this.smsCode = ''; this.paymentCode = ''; this.safeVerifyType = 'payment_code'; this.refreshDynacodeData(); }, 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 && res.data.length) { this.payQrcode = res.data; this._outTradeNo = this.payQrcode[0].out_trade_no; this.checkPayStatus(); } } }) }, 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.payInfo.collectmoney_config.balance_safe == 1 && !this.balanceSafeVerify) { this.$refs.safeVerifyPopup.open(); 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(); }, openRemark() { this.remark = this.payInfo.remark; this.$refs.remarkPopup.open() }, /** * 设置备注 */ 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.payInfo.remark = this.remark; this.$refs.remarkPopup.close(); } }); }, /** * 发送短信验证码 */ sendMobileCode() { if (this.dynacodeData.seconds != 120 || this.dynacodeData.isSend) return; this.dynacodeData.isSend = true; this.dynacodeData.timer = setInterval(() => { this.dynacodeData.seconds--; this.dynacodeData.codeText = this.dynacodeData.seconds + 's后可重新获取'; }, 1000); this.$api.sendRequest({ url: '/cashier/storeapi/member/memberverifycode', data: { member_id: this.payInfo.member_id }, success: res => { if (res.code >= 0) { this.dynacodeData.key = res.data.key; this.smsCode = ''; this.dynacodeData.isSend = false; } else { this.$util.showToast({ title: res.message }); this.refreshDynacodeData(); } }, fail: () => { this.$util.showToast({ title: 'request:fail' }); this.refreshDynacodeData(); } }); }, refreshDynacodeData() { clearInterval(this.dynacodeData.timer); this.dynacodeData = { key: '', seconds: 120, timer: null, codeText: '获取动态码', isSend: false }; }, /** * 验证短信验证码是否正确 */ verifySmsCode() { if (this.smsCode.trim() == '') { this.$util.showToast({ title: '请输入验证码' }); return; } if (this.isRepeat) return; this.isRepeat = true; this.$api.sendRequest({ url: '/cashier/storeapi/member/checksmscode', data: { key: this.dynacodeData.key, code: this.smsCode.trim() }, success: res => { if (res.code == 0) { this.balanceSafeVerify = true; this.$refs.safeVerifyPopup.close(); this.useBalance(); } else { this.$util.showToast({ title: res.message }); } this.isRepeat = false; } }) }, verifyPaymentCode(e) { if (e.detail.value) this.paymentCode = e.detail.value if (this.paymentCode.trim() == '') { this.$util.showToast({ title: '请输入付款码' }); return; } if (this.isRepeat) return; this.isRepeat = true; this.$api.sendRequest({ url: '/cashier/storeapi/member/checkpaymentcode', data: { member_id: this.payInfo.member_id, code: this.paymentCode.trim() }, success: res => { if (res.code == 0) { this.balanceSafeVerify = true; this.$refs.safeVerifyPopup.close(); this.useBalance(); } else { this.$util.showToast({ title: res.message }); } this.isRepeat = false; } }) } }, watch: { outTradeNo: function(nval, oval) { if (nval) { this._outTradeNo = 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) } }, autoPrintTicket: function(nval) { uni.setStorageSync('payAutoPrintTicket', nval) }, 'dynacodeData.seconds': { handler(newValue, oldValue) { if (newValue == 0) { this.refreshDynacodeData(); } }, immediate: true, deep: true } } }