/**
* 跳转到支付页面
*
* 此函数用于跳转到支付页面,支持移动端和非移动端两种场景。
* 移动端场景下,会根据设备类型(iOS或Android)调用相应的支付接口。
* 非移动端场景下,会调用预支付接口获取支付页面地址,然后跳转到支付页面。
*/
async redirectPay() {
// 判断是否是移动端
// 通过检查window.android和window.webkit属性来判断是否是移动端
if (window.android || window.webkit) {
// 判断是否是iOS设备
// 通过正则表达式匹配navigator.userAgent来判断是否是iOS设备
const isiOS = /(iPhone|iPad|iPod)/.test(navigator.userAgent);
// 设置支付结果回调函数
// 当支付结果返回时,会调用此函数处理支付结果
window.onPayRes = (res) => this.payResult(res);
if (isiOS) {
// 创建支付数据对象
// 包括支付渠道代码、订单类型、订单ID和访问令牌
const data = {
channelCode: 'alipay_app',
orderType: this.orderType,
id: this.id,
token: uni.getStorageSync('token')
}
// 发送支付数据到native app
// 通过webkit.messageHandlers.onPay.postMessage发送支付数据到native app
window.webkit.messageHandlers.onPay.postMessage(JSON.stringify(data));
} else {
// 如果是Android设备
// 调用window.android.onPay接口进行支付
window.android.onPay('alipay_app', this.orderType, this.id, uni.getStorageSync('token'))
}
} else {
// 如果不是移动端
// 调用预支付接口获取支付页面地址
let { code, data } = await this.prepay('alipay_wap');
if (code === 0) {
// 跳转到支付页面
// 通过location.href跳转到支付页面
location.href = data.displayContent
}
}
}
uniapp h5嵌入安卓和iOS进行数据交互
于 2024-08-20 09:34:35 首次发布