uni-app 接入支付宝租赁快捷下单插件支付宝验收不加载小程序的onLaunch方法
uni-app 接入支付宝租赁快捷下单插件支付宝验收不加载小程序的onLaunch方法
操作步骤:
- 使用uniapp编写代码(具体见插件接入文档https://xfpzl.yuque.com/dgw58m/apg2ie/gyovgcdyzpsbgfpy#n9cFk)
- 通过支付宝开发者上传版本
- 进入https://open.alipay.com/api/onlineAccept验收选择租赁商品交易、租赁快捷下单插件
- 支付宝扫码验收
预期结果:
完成支付宝租赁快捷下单插件验收
实际结果:
无法加载小程序的onLaunch生命周期导致无法触发插件回调
bug描述:
// 此为uniapp代码App.js
// #ifdef MP-ALIPAY
const plugin = requirePlugin('goodsDetailPlugin')
import {
startAPVerify
} from "@/utils/platform.js";
plugin.setPlaceOrderCallback((arg) => {
// console.log('arg', arg); // 可获取用户点击「立即购买」的信息
my.confirm({
title: '设置下订单回调',
content: JSON.stringify(arg),
});
/* my.redirectTo({
url: `pages/product/product?productId=${arg.outItemId}`, // 跳转到小程序的下单页面地址
}); */
})
// #endif
import globalData from "./utils/globalData.js"
import {
getAppId
} from "./utils/platform.js"
export default {
globalData,
data() {
return {
productId: '',
agreementNeedData: {},
checkBeforeAddOrderParam: {},
isNeedFace: false,
certificationRelated: {
"needIdCardPhoto": false,
"isRealName": "FINISH"
},
pluginOrderInit: {}
}
},
onLaunch: function() {
/* my.redirectTo({
url: `plugin://goodsDetailPlugin/rentGoodsDetail?outItemId=${1728270749993}` //outItemId:必填,商品编码
}) */
const infoObj = this.$getGlobalData("infoByAppId") || {}
console.log('onLaunch', infoObj)
this.$setGlobalData("information", infoObj)
// #ifdef MP-ALIPAY
my.checkBeforeAddOrder({
success: (addOrderRes) => {
console.log("App.js==>onLaunch交易组件获取成功:", addOrderRes)
this.checkBeforeAddOrderParam = addOrderRes;
}
})
console.log("App.js==>onLaunch快捷下单插件plugin:", plugin)
plugin.setRentOrderInitCallback((params) => {
this.pluginOrderInit = JSON.parse(JSON.stringify(params || {}));
this.productId = params.outItemId
my.confirm({
content: JSON.stringify(params),
});
return this.$request('hzsx/liteUserOrders/orderInitialize', {
...params
}, 'post', true)
.then((res) => {
this.isNeedFace = res.data.needFace
const pageData = {
productId: this.productId,
type: 'order',
tempOrderId: res.data.orderId
}
const serviceParam =
`pages/agreement/index?tempOrderId=${res.data.orderId}&productId=${this.productId}&type=`
res.data.bizParamData.serviceProtocolList = [{
protocolName: '租赁/授权及服务相关协议',
protocolUrl: `pages/agreement/index?pageData=${JSON.stringify(pageData)}`
}]
const agreementNeedData = {
buyOutPrice: '',
orderId: '',
salePrice: '',
skuDescription: '',
skuTitle: '',
totalRent: '',
orderByStagesDtoList: [],
reciveCity: '',
reciveName: '',
reciveAddress: '',
}
agreementNeedData.buyOutPrice = res.data?.buyOutPrice
agreementNeedData.orderId = res.data?.orderId
agreementNeedData.salePrice = res.data?.salePrice
agreementNeedData.skuDescription = res.data?.specDescription?.join(',')
agreementNeedData.skuTitle = res.data?.skuTitle?.join(',')
agreementNeedData.totalRent = res.data?.bizParamData?.costInfo?.totalRent
agreementNeedData.orderByStagesDtoList = res.data?.bizParamData?.costInfo?.stagePayPlan
?.stagePayPlanInfos?.map(ite => {
return {
statementDate: ite?.planPayTime,
currentPeriods: ite?.planPayNo,
totalPeriods: res.data?.bizParamData?.costInfo?.stagePayPlan?.stagePayPlanInfos?.length,
currentPeriodsRent: ite?.planPayPrice,
totalRent: res.data?.bizParamData?.costInfo?.totalRent
}
})
this.agreementNeedData = agreementNeedData
return res.data; // 直接返回请求结果对象
})
.catch((err) => {
return {
success: false,
errorCode: 'OTHER'
}; // 在发生错误时返回空对象或根据需求返回其他对象
});
});
plugin.setRentOrderChangeCallback((params) => {
// 商家需要在当前回调中完成以下内容:
// 1.邮费的计算
const {
prov = '省', city = '市', area = '区'
} = params.addressInfo
const agreementNeedData = {
reciveCity: '',
reciveName: '',
reciveAddress: '',
}
agreementNeedData.reciveCity = prov + city + area
agreementNeedData.reciveName = '【收货人】'
agreementNeedData.reciveAddress = `${prov}-${city}-${area}-详细地址`
this.agreementNeedData = Object.assign(this.agreementNeedData, agreementNeedData)
this.$request('hzsx/api/sysConfig/cacheConfirmData', this.agreementNeedData, 'post', true); // 生成临时协议
/* return {
freight: "0", // 商家实际计算出来的金额
success: true,
}; */
return this.$request('hzsx/liteUserOrders/shippingCalculations', params, 'post', true).then(res => {
res.data.freight = res.data.freight.toString()
return res.data
}).catch(() => {
return {
success: false,
errorCode: 'OTHER'
}
})
});
plugin.setRentOrderSubmitCallback((params) => {
return this.$request('hzsx/liteUserOrders/checkIsRealName', {}, 'post', true).then(realRes => {
const rentOrderParams =
`outItemId=${this.pluginOrderInit.outItemId}&outSkuId=${this.pluginOrderInit.outSkuId}&quantity=${this.pluginOrderInit.quantity}&rentStartTime=${this.pluginOrderInit.rentStartTime}&duration=${this.pluginOrderInit.duration}&source=merchantGoodsDetail&bizIdentity=rent`
if (realRes.data.isRealName == "UN_CERT") {
/* my.redirectTo({
url: `plugin://goodsDetailPlugin/rentGoodsDetail?outItemId=${this.productId}` //outItemId:必填,商品编码
}) */
this.$onTo(realRes.data.needIdCardPhoto ?
`/pages/certificates/certificates?rentOrderParams=${encodeURIComponent(rentOrderParams)}` :
`/pages/certificatesNoPhoto/certificatesNoPhoto?rentOrderParams=${encodeURIComponent(rentOrderParams)}`,
false, true)
return this.pluginFail('请先实名认证', 'AUTH_UN_CERT')
} else if (realRes.data.isRealName == "UN_UPLOAD") {
if (realRes.data.needIdCardPhoto) {
this.$onTo(
`/pages/certificates/certificates?rentOrderParams=${encodeURIComponent(rentOrderParams)}`,
false, true)
return this.pluginFail('请先上传身份证照片', 'AUTH_UN_UPLOAD')
} else {
// 创建订单
return this.createPluginOrder(params)
}
} else {
// 创建订单
return this.createPluginOrder(params)
}
}).catch((err) => {
return this.pluginFail(err.errorMessage, err.errorCode)
})
});
// #endif
},
onShareAppMessage() {
return {}
},
methods: {
/**
* 检测实名成功并创建订单
*/
createPluginOrder(params) {
if (this.isNeedFace) {
let postData = {};
postData.uid = this.$getUid();
return this.$request("hzsx/api/order/getFaceAuthCertifyUrl", postData, "post").then(
faceRes => { // 获取爱签人脸识别链接
const faceParams = faceRes.data
return this.startAPVerify(faceParams).then(verifyResult => { // 拉起人脸识别
if (verifyResult.resultStatus === "9000") { // 人脸成功
const cid = verifyResult.result?.certifyId
if (!cid) {
return this.pluginFail('实名认证失败', 'Face_FAIL')
} else {
const queryData = {
certifyId: cid,
orderId: this.agreementNeedData.orderId,
productId: this.productId,
passed: true
}
return this.$request("hzsx/api/components/faceAuthInitAsync", queryData, "get")
.then(() => { // 修改该订单为以人脸状态
uni.showToast({
title: "认证成功"
})
return this.pluginOrderInterface(params)
}).catch((err) => {
return this.pluginFail(err.errorMessage, err.errorCode)
})
}
} else {
return this.pluginFail('实名认证失败', 'Face_FAIL')
}
}).catch(() => {
return this.pluginFail('实名认证拉起失败', 'Face_COMP_FAIL')
})
}).catch((err) => {
return this.pluginFail(err.errorMessage, err.errorCode)
})
} else {
return this.pluginOrderInterface(params)
}
},
/**
* 组件创建订单接口
*/
pluginOrderInterface(params) {
return this.$request('hzsx/liteUserOrders/submitInitialize', { // 创建订单
...params,
sourceId: this.checkBeforeAddOrderParam.sourceId,
orderId: this.agreementNeedData.orderId
}, 'post', true).then(res => {
console.log('setRentOrderSubmitCallback创建订单接口', res.data);
return res.data
}).catch((err) => {
return this.pluginFail(err.errorMessage, err.errorCode);
})
},
/* 组件内部请求错误 */
pluginFail(errMsg, errCode) {
return {
success: false,
errorCode: errCode || 'OTHER',
errorMsg: errMsg || '系统错误'
}
},
}
}
更多关于uni-app 接入支付宝租赁快捷下单插件支付宝验收不加载小程序的onLaunch方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
请问这个问题解决了吗?
更多关于uni-app 接入支付宝租赁快捷下单插件支付宝验收不加载小程序的onLaunch方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html
根据问题描述,在uni-app接入支付宝租赁快捷下单插件时,验收过程中无法触发小程序的onLaunch生命周期方法。这个问题通常与支付宝小程序的插件加载机制有关。
主要问题分析:
- 插件模式下小程序的启动流程与普通小程序不同
- onLaunch可能被插件自身的生命周期覆盖
- 插件环境初始化可能早于小程序onLaunch执行
解决方案建议:
- 将插件初始化代码从onLaunch移到更早的生命周期
- 使用插件提供的ready事件确保执行时机
- 检查支付宝小程序基础库版本是否支持当前插件功能
关键代码修改建议:
// 将插件初始化代码放在全局作用域而非onLaunch中
// #ifdef MP-ALIPAY
App({
onLaunch() {
console.log('小程序onLaunch触发')
}
})
// 插件初始化代码
const plugin = requirePlugin('goodsDetailPlugin')
plugin.setPlaceOrderCallback((arg) => {
// 回调处理
})
// #endif