- 使用uniapp编写代码(具体见插件接入文档https://xfpzl.yuque.com/dgw58m/apg2ie/gyovgcdyzpsbgfpy#n9cFk)
- 通过支付宝开发者上传版本
- 进入https://open.alipay.com/api/onlineAcceptance验收选择租赁商品交易、租赁快捷下单插件
- 支付宝扫码验收

- 发布:2025-02-19 18:20
- 更新:2025-02-19 18:20
- 阅读:26
产品分类: uniapp/小程序/阿里
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows 10 专业版 22H2
HBuilderX类型: 正式
HBuilderX版本号: 4.45
第三方开发者工具版本号: 3.9.41
基础库版本号: 2.9.30
项目创建方式: HBuilderX
操作步骤:
预期结果:
完成支付宝租赁快捷下单插件验收
完成支付宝租赁快捷下单插件验收
实际结果:
无法加载小程序的onLaunch生命周期导致无法触发插件回调
无法加载小程序的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 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 || '系统错误'
}
},
}
}
``` 该代码通过redirectTo跳转到插件的页面支付宝真机调试没有问题而通过支付宝的验收工具无法触发plugin.setRentOrderInitCallback等回调onLaunch都没有执行
```javascript
// 此为支付宝原生创建的小程序项目写的app.js没有问题能过验收
const plugin = requirePlugin('goodsDetailPlugin'); // 引入插件
App({
data() {
return {
productId: '',
agreementNeedData: {
},
checkBeforeAddOrderParam: {},
isNeedFace: false,
certificationRelated: {
"needIdCardPhoto": false,
"isRealName": "FINISH"
},
pluginOrderInit: {}
}
},
onLaunch(options) {
// 第一次打开
// options.query == {number:1}
// my.redirectTo({
// url: `plugin://goodsDetailPlugin/rentGoodsDetail?outItemId=${1728270749993}` //outItemId:必填,商品编码
// })
my.checkBeforeAddOrder({
success: (addOrderRes) => {
this.checkBeforeAddOrderParam = addOrderRes;
console.log("App.js==>onLaunch交易组件获取成功:", this.checkBeforeAddOrderParam)
}
})
console.info('App onLaunch', plugin);
plugin.setRentOrderInitCallback((params) => {
this.pluginOrderInit = JSON.parse(JSON.stringify(params || {}));
this.productId = params.outItemId
return this.$request('hzsx/liteUserOrders/orderInitialize', {
...params
}, 'post')
.then((res) => {
this.isNeedFace = res.data.needFace
const pageData = {
productId: this.productId,
type: 'order',
tempOrderId: res.data.orderId
}
res.data.bizParamData.serviceProtocolList = [{
protocolName: '租赁/授权及服务相关协议',
protocolUrl: `pages/agreement/agreement?pageData=${JSON.stringify(pageData)}`
}]
my.confirm({
content: JSON.stringify(res.data),
});
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'); // 生成临时协议
/* return {
freight: "0", // 商家实际计算出来的金额
success: true,
}; */
return this.$request('hzsx/liteUserOrders/shippingCalculations', params, 'post').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').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)
})
});
},
onShow(options) {
// 从后台被 scheme 重新打开
// options.query == {number:1}
},
$request(url, data = {}, method = "GET") {
const baseUrl = "http://192.168.254.82:9001/zyj-api-web/";
data.uid = '79f16b6b74c6ef5b4a088a0d393db3fa59da04d8ua3dryphn5xx'
data.channelId = '000'
data.uniPlatform = 'MP-ALIPAY'
return new Promise((resolve, reject) => {
my.request({
url: baseUrl + url,
method,
data,
success: (res) => {
if (res.data.errorMessage) {
return reject(res.data)
} else {
return resolve(res.data);
}
},
fail: (res) => {
uni.showToast({
title: res.data.message,
})
}
})
});
},
$onTo(url, switchType, useReplace = false) {
if (url.indexOf("alipays://") === 0) {
if (url.indexOf("http") !== -1) {
let paths = decodeURIComponent(url.replace(/.+(?=https)/, ""))
//h5链接跳转
my.ap.navigateToAlipayPage({
path: paths,
})
return
}
let decodeUrl = decodeURIComponent(url).replace(/.+(?=appId)/, "")
let urls = decodeUrl.split("&page=")
let appId = urls[0].split("=")[1]
let path = urls[1]
//小程序之间跳转
my.navigateToMiniProgram({
appId,
path,
})
} else if (switchType) {
my.switchTab({
url,
})
} else {
// 常规路径跳转
const jumpFun = useReplace ? my.redirectTo : my.navigateTo
jumpFun({
url
})
}
},
/**
* 检测实名成功并创建订单
*/
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 || '系统错误'
}
},
startAPVerify(data) {
console.log(data)
return new Promise((res, rej) => {
my.startAPVerify({
url: data.faceUrl,
certifyId: data.certifyId,
success: (data) => {
res(data)
},
fail: (data) => {
rej(data)
console.log('startAPVerify')
}
})
})
}
});
0 个回复