<template>
<view class="box">
<!-- <view class="top">
获取地址栏目
</view>
<button @click="query">支付</button>
<view class="">
{{code}}
{{searchObj}}
</view> -->
<view>
{{ obj }}
</view>
<view style="margin: 20px 0; color: #333; font-size: 14px;">
<text v-for="(log, idx) in logList" :key="idx">{{ log }}<br/></text>
</view>
</view>
</template>
<script>
import config from './config/config.js';
export default {
data() {
return {
code: '',
obj: {},
searchObj: {
appId: '',
openId: '',
bizId: ''
},
logList: []
};
},
onLoad(option) {
this.code = option.code;
this.getOpenId(this.code);
},
onError(err) {
// uni.showToast({
// title:`${JSON.stringify(err)}`,
// icon:''
// })
},
mounted() {
if (!/MicroMessenger/i.test(navigator.userAgent)) {
alert('请在微信内置浏览器中打开');
}
},
methods: {
async getOpenId(e) {
let info = uni.getStorageSync('billInfo');
let data = {
code: e,
appId: info.appId
};
let res = await config.interfaces.getOpenId(data);
console.log(res, '获取到的openid');
if (res.code == 200) {
let data = {
bizType: info.bizType,
bizId: info.bizId,
payChannel: 'WX_PUB',
expend: {
open_id: res.data
}
};
if (data.bizType == 'MANUAL') {
data.billId = info.billId;
} else if (data.bizType == 'CONTRACT') {
data.submitAmount = info.submitAmount;
}
console.log(data, '参数');
this.getPayInfo(data);
} else {
uni.showToast({
title: res.msg,
icon: 'none'
});
}
},
async getPayInfo(e) {
let res = await config.interfaces.getPayInfo(e);
if (res.code == 200) {
console.log(res.data);
this.pay(res.data);
} else {
uni.showToast({
title: res.msg,
icon: 'none'
});
}
},
pay(res) {
this.logList.push('准备调起支付...');
const onBridgeReady = () => {
this.logList.push('WeixinJSBridgeReady 事件触发,开始调用支付API');
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{
appId: res.payInfo.appId, //公众号ID,由商户传入
timeStamp: res.payInfo.timeStamp, //时间戳,自1970年以来的秒数
nonceStr: res.payInfo.nonceStr, //随机串
package: res.payInfo.package,
signType: res.payInfo.signType, //微信签名方式:
paySign: res.payInfo.paySign
},
(res) => {
this.logList.push('支付回调: ' + JSON.stringify(res));
console.log(res, '支付');
if (res.err_msg == 'get_brand_wcpay_request:ok') {
this.logList.push('支付成功');
uni.showToast({
title: '支付成功',
icon: 'none'
});
uni.navigateBackMiniProgram({
extraData: {},
success: function () {
console.log('关闭应用成功');
},
fail: function () {
console.log('关闭应用失败');
}
});
} else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
this.logList.push('支付取消');
uni.showToast({
title: '支付取消,请重新扫码',
icon: 'none'
});
} else if (res.err_msg == 'get_brand_wcpay_request:fail') {
this.logList.push('支付失败');
uni.showToast({
title: '支付失败,请重新扫码',
icon: 'none'
});
} else {
this.logList.push('支付api: ' + res.err_msg);
uni.showToast({
title:"支付api:" + res.err_msg,
icon: 'none',
duration:5000
});
}
}
);
};
if (typeof WeixinJSBridge == "undefined") {
this.logList.push('WeixinJSBridge 未定义,等待事件...');
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
} else {
this.logList.push('WeixinJSBridge 已定义,直接调用onBridgeReady');
onBridgeReady();
}
}
}
};
</script>
<style scoped lang="scss">
.box {
width: 100%;
height: 100%;
box-sizing: border-box;
display: flex;
flex-direction: column;
}
</style>
小程序只能使用内部支付功能,webview里面不支持支付
在webview中需拉起小程序原生支付
不支持这样支付
在做这功能之前,相关话题不搜一下论坛的吗?
一大把类似的帖子
小程序不支持在webview里发起支付