UNPKG

vaneditor

Version:

``` 移动端ui框架统一用uView,css扩展语言用scss,全局数据管理vuex,统一样式主体风格颜色定义/src/uni.scss ```

283 lines (281 loc) 11.3 kB
import store from '@/store' import { getSplitTime } from '@/common/js/util' export default { data() { return { timer: '' } }, methods: { // 支付之前的校验 checkBeforePay() { return this.$ajax({ method: 'post', url: this.verifyUrl || '/weChat/order/verifyPayCondition', data: { orderCode: this.orderId } }) }, // 支付方法需要两个参数,一个是订单编号(下单之后返回的编号),一个是价格(元为单位) // 参数可以传入,也可以直接在data中写好正确赋值 async payOrder(orderId, price, hasCode) { // console.log('wxPay', this.payType) var WeChatNotifyUrlObj = { 'WECHAT_PAY': `${document.location.protocol}//${document.domain}/weChat/pay/payNotifyByWechat`, 'ABCBANK_PAY': `${document.location.protocol}//${document.domain}/weChat/pay/payNotifyByWeiFuTong` } this.orderId = orderId || this.orderId this.price = price || this.price this.hasCode = hasCode || this.hasCode || -1 if (!this.payType) { this.payType = "WECHAT_PAY" } // let checkResult = await this.checkBeforePay() let checkResult = { errCode: 8200 } if (parseInt(checkResult.errCode) === 8200) { let _data = { // body: 'test', // orderno: this.orderId, // total: price * 100, // notifyUrl: `${document.location.protocol}//${document.domain}/weChat/pay/payNotify`, // tradeType: 'JSAPI', // openid: sessionStorage.getItem('openId'), WeChatOrderCode: this.orderId, // WeChatNotifyUrl: `${document.location.protocol}//${document.domain}/weChat/pay/payNotifyByWechat`, // WeChatNotifyUrl: `${document.location.protocol}//${document.domain}/weChat/pay/payNotifyByWeiFuTong`, WeChatNotifyUrl: WeChatNotifyUrlObj[this.payType], WeChatTradeType: 'JSAPI', WeChatOpenId: sessionStorage.getItem('openId'), //uni.getStorageSync('openId'), // || sessionStorage.getItem('openId') || 'opjzC0hM10CsOznXIQsiYrKnDTLQ', WeChatTenantId: store.state.tenantId, // payType: 'ABCBANK_PAY' // 农行支付 payType: this.payType // WeChatPayInfoVo: JSON.stringify({ // orderCode: this.orderId, // notifyUrl: `${document.location.protocol}//${document.domain}/weChat/pay/payNotify`, // tradeType: 'JSAPI', // openid: sessionStorage.getItem('openId'), // tenantId: this.$store.state.tenantId // }) } // uni.showModal({ // title: 'msg: unifiedorder入参', // content: `openId: ${uni.getStorageSync('openId')} \n unifiedorder: ${JSON.stringify(_data)}`, // success(res) { // } // }) let _this = this this.$ajax({ method: 'post', url: '/weChat/pay/unifiedorder', data: _data }).then(result => { _this.weixinBridgeInfo = result.data.weChatPayInfoResult if (typeof WeixinJSBridge === 'undefined') { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', _this.onBridgeReady, false) } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', _this.onBridgeReady) document.attachEvent('onWeixinJSBridgeReady', _this.onBridgeReady) } } else { _this.onBridgeReady() } }).catch(result => { if (typeof (this.refresh) == 'function') { this.refresh() // orderStatus.vue-> this.refresh() // Toast.fail({ // message: '支付调用失败!', // duration: 2 // }) } }) } else { // Toast.fail({ // message: checkResult.message, // duration: 2 // }) uni.showToast({ title: `${checkResult.message}`, icon: 'none' }) } }, appPayOrder() { let _data = { WeChatOrderCode: this.orderId, WeChatNotifyUrl: 'http://store2test.lishicloud.com/weChat/pay/payNotifyByWechat', // WeChatNotifyUrl: `${document.location.protocol}//${document.domain}/weChat/pay/payNotifyByWeiFuTong`, WeChatTradeType: 'APP', // WeChatOpenId:'abc', WeChatTenantId: store.state.tenantId, // payType: 'ABCBANK_PAY' // 农行支付 payType: 'WECHAT_PAY' } this.$ajax({ method: 'POST', url: '/weChat/pay/unifiedorder', data: _data }).then(res => { let _data = JSON.stringify({ appid: res.data.weChatPayInfoResult.appId, noncestr: res.data.weChatPayInfoResult.nonceStr, package: 'Sign=WXPay', partnerid: res.data.weChatPayInfoResult.wxMchId, prepayid: res.data.weChatPayInfoResult.prepayId, timestamp: parseInt(res.data.weChatPayInfoResult.timeStamp), sign: res.data.weChatPayInfoResult.paySign }) uni.requestPayment({ provider: 'wxpay', orderInfo: _data, //微信、支付宝订单数据 success: function (res) { uni.redirectTo({ url: `/pages/common/paySuccess?orderId=${this.orderId}&hasCode=${this.hasCode}`, }) }, fail: function (err) { uni.showToast({ title: '支付失败,未安装微信app或取消支付!', icon: 'none' }) //err.errMsg // plus.nativeUI.alert('支付失败',function(){ // // }) uni.redirectTo({ url: '/pages/personalCenter/order/detail?orderCode=' + this.orderId }) } }) }).catch(res => {}) }, wxminiPayOrder(orderId) { let _this = this uni.login({ provider: 'weixin', success: function (loginRes) { let code = loginRes.code; let _data = { code: code, WeChatOrderCode: orderId, WeChatNotifyUrl: 'http://store2test.lishicloud.com/weChat/pay/payNotifyByWechat', // WeChatNotifyUrl: `${document.location.protocol}//${document.domain}/weChat/pay/payNotifyByWeiFuTong`, WeChatTradeType: 'JSAPI', // WeChatOpenId:'abc', WeChatTenantId: store.state.tenantId, // payType: 'ABCBANK_PAY' // 农行支付 payType: 'WECHAT_PAY' } _this.$ajax({ method: 'POST', url: '/weChat/pay/unifiedorder', data: _data }).then(res => { let _data = JSON.stringify({ appid: res.data.weChatPayInfoResult.appId, noncestr: res.data.weChatPayInfoResult.nonceStr, package: 'Sign=WXPay', partnerid: res.data.weChatPayInfoResult.wxMchId, prepayid: res.data.weChatPayInfoResult.prepayId, timestamp: parseInt(res.data.weChatPayInfoResult.timeStamp), sign: res.data.weChatPayInfoResult.paySign }) uni.requestPayment({ provider: 'wxpay', timestamp: parseInt(res.data.weChatPayInfoResult.timeStamp), noncestr: res.data.weChatPayInfoResult.nonceStr, package: 'Sign=WXPay', signType: 'MD5', paySign: res.data.weChatPayInfoResult.paySign, orderInfo: _data, //微信、支付宝订单数据 success: function (res) { uni.redirectTo({ url: `/pages/common/paySuccess?orderId=${this.orderId}&hasCode=${this.hasCode}`, }) }, fail: function (err) { uni.showToast({ title: '支付失败!', icon: 'none' }) uni.redirectTo({ url: '/pages/personalCenter/order/detail?orderCode=' + orderId }) } }) }).catch(res => {}) } }); }, onBridgeReady() { let _this = this window.jweixin.invoke( 'getBrandWCPayRequest', { 'appId': _this.weixinBridgeInfo.appId, // 公众号名称,由商户传入 'timeStamp': _this.weixinBridgeInfo.timeStamp, // 时间戳,自1970年以来的秒数 'nonceStr': _this.weixinBridgeInfo.nonceStr, // 随机串 'package': _this.weixinBridgeInfo.package, 'signType': _this.weixinBridgeInfo.signType, // 微信签名方式: 'paySign': _this.weixinBridgeInfo.paySign // 微信签名 }, (res) => { // alert(JSON.stringify(res)) // uni.showModal({ // title: 'msg: getBrandWCPayRequest', // content: `type: ${JSON.stringify(res)}`, // success(res) { // } // }) if (res.err_msg === 'get_brand_wcpay_request:ok') { // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回 ok,但并不保证它绝对可靠。 console.log('支付成功') uni.navigateTo({ url: `/pages/common/paySuccess?orderId=${this.orderId}&hasCode=${this.hasCode}`, }) // this.$router.push({ path: `/paySuccess/${this.orderId}/${this.hasCode}` }) } else { // this.$ajax({ // method: 'post', // url: '/common/getSystemTime' // }).then(result => { // let timestamp = (result && result.data) ? result.data : (new Date()).getTime() // }) console.log('失败') uni.navigateTo({ url: '/pages/personalCenter/order/detail?orderCode=' + _this.orderId }) // this.$router.push({ path: `/personalCenter/order/detail/${this.orderType}/${this.orderId}` }) // let url = `/payFail/${timestamp}/${this.price}/${this.orderId}` // this.$router.push({path: url}) } } ) }, // 待付款的倒计时 async getLeftTime() { this.$ajax({ method: 'post', url: '/weChat/common/getSystemTime', data: {}, }).then(res => { let startTime = res.data ? res.data : (new Date()).getTime() const timeSplit = 100 const timer = setInterval(() => { startTime += timeSplit this.leftTime = getSplitTime(this.creatTime, startTime, store.state.orderEffectTime) if (this.leftTime === '00:00') { this.timeout() clearInterval(timer) } }, timeSplit) this.$once('hook:beforeDestroy', () => { clearInterval(timer) }) }).catch(res => {}) } } }