feat:抖音支付

This commit is contained in:
2026-09-09 11:46:36 +08:00
parent 75060faa39
commit b350f1c07a
18 changed files with 2670 additions and 531 deletions
+249
View File
@@ -109,6 +109,255 @@ export function zfbPayFun(alipay, orderId, orderNum) {
// });
}
/**
* 抖音支付(兼容 iOS、Android App 端)
* @param {Object|String} dyPay 抖音支付参数 (支持 orderStr, schema, url, payUrl, orderInfo 等)
* @param {Object|String|Number} orderId 支付的订单ID
* @param {Object|String} orderNum 支付的订单编号
*/
export function douyinPayFun(dyPay, orderId, orderNum) {
console.log("抖音支付---入参:", dyPay, orderId, orderNum);
return new Promise((resolve, reject) => {
if (!dyPay || (typeof dyPay === "object" && Object.keys(dyPay).length === 0)) {
uni.showToast({
title: "支付参数错误",
icon: "none",
});
jumpWayError();
return reject(new Error("支付参数为空"));
}
const d = typeof dyPay === "object" ? dyPay : {};
const sub = (typeof d.orderInfo === "object" && d.orderInfo) ? d.orderInfo : {};
const type = typeof dyPay === "object" ? (dyPay.type || "app") : "app";
// 1. 提取或生成 Scheme / URL (注意:排除 sign,避免将签名误认为 URL)
let orderStr = "";
if (typeof dyPay === "string") {
orderStr = dyPay;
} else if (typeof dyPay === "object") {
orderStr =
dyPay.orderStr ||
dyPay.schema ||
dyPay.scheme ||
dyPay.url ||
dyPay.payUrl ||
dyPay.link ||
sub.orderStr ||
sub.schema ||
sub.scheme ||
sub.url ||
"";
// 如果未配置直接链接,则尝试通过 prepayId / pay_token 自动生成抖音收银台 Scheme 备用
const token = d.prepayId || d.prepayid || sub.prepayid || sub.prepayId;
if (!orderStr && token) {
orderStr = `snssdk1128://pay?pay_token=${encodeURIComponent(token)}`;
}
}
console.log("抖音支付---提取到的 Scheme / orderStr:", orderStr);
// 2. 深度合并/映射原生支付所需的 orderInfo(融合根节点与 orderInfo 子节点,同时提供驼峰与小写命名)
const appId = d.appId || d.appid || sub.appId || sub.appid || "";
const mchId = d.mchId || d.partnerid || d.mchid || sub.mchId || sub.partnerid || sub.mchid || "";
const prepayId = d.prepayId || d.prepayid || sub.prepayId || sub.prepayid || "";
const callbackScheme = d.callbackScheme || sub.callbackScheme || "trustbridgeapp";
const packageValue = d.packageValue || d.package || sub.packageValue || sub.package || "Sign=DYPay";
const nonceStr = d.nonceStr || d.noncestr || sub.nonceStr || sub.noncestr || "";
const timeStamp = String(d.timeStamp || d.timestamp || sub.timeStamp || sub.timestamp || "");
const sign = d.sign || sub.sign || "";
const orderInfo = {
// 驼峰命名(标准 uni.requestPayment / 抖音 SDK)
appId,
mchId,
prepayId,
callbackScheme,
packageValue,
nonceStr,
timeStamp,
sign,
// 小写命名(部分原生桥接 SDK 兼容)
appid: appId,
partnerid: mchId,
mchid: mchId,
prepayid: prepayId,
package: packageValue,
noncestr: nonceStr,
timestamp: timeStamp,
service: 5,
};
// #ifdef APP-PLUS
// 情形 A:如果显式指定为 H5 方式
if (
type === "h5" &&
orderStr &&
(orderStr.startsWith("http://") || orderStr.startsWith("https://"))
) {
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?link=${encodeURIComponent(
orderStr
)}&orderNum=` + orderNum,
});
resolve({ status: "processing" });
return;
}
// 情形 B:如果后端直接传入了协议 Scheme 串(如以 snssdk1128://, douyin:// 开头)
if (
typeof dyPay === "string" ||
(dyPay.orderStr && (dyPay.orderStr.startsWith("snssdk") || dyPay.orderStr.startsWith("douyin")))
) {
let openSuccess = false;
// iOS Native.js 尝试
if (plus.os.name === "iOS") {
try {
const UIApplication = plus.ios.importClass("UIApplication");
const NSURL = plus.ios.importClass("NSURL");
const app = UIApplication.sharedApplication();
const nsUrl = NSURL.URLWithString(orderStr);
if (app && nsUrl && app.openURL(nsUrl)) {
openSuccess = true;
}
} catch (nativeErr) {
console.warn("iOS Native.js openURL 尝试:", nativeErr);
}
}
// Android Native.js 尝试
if (plus.os.name === "Android") {
try {
const Intent = plus.android.importClass("android.content.Intent");
const Uri = plus.android.importClass("android.net.Uri");
const main = plus.android.runtimeMainActivity();
const intent = new Intent(Intent.ACTION_VIEW, Uri.parse(orderStr));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
main.startActivity(intent);
openSuccess = true;
} catch (androidErr) {
console.warn("Android Native.js Intent 尝试:", androidErr);
}
}
if (!openSuccess) {
plus.runtime.openURL(
orderStr,
() => {
if (orderNum) {
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?orderNum=` +
orderNum,
});
}
resolve({ status: "invoked" });
},
(err) => {
console.error("拉起抖音客户端失败:", err);
const isSimulator =
plus.navigator && plus.navigator.isSimulator
? plus.navigator.isSimulator()
: false;
let tip = "未检测到抖音客户端或拉起失败,请确认是否已安装抖音客户端";
if (isSimulator) {
tip = "iOS模拟器无法打开第三方应用Scheme,请在安装了抖音的真机上测试";
} else if (err && err.code === -3) {
tip = "未检测到抖音应用或当前基座未配置Scheme白名单,请确认真机已安装抖音";
}
uni.showToast({
title: tip,
icon: "none",
duration: 3000,
});
jumpWayError();
reject(err);
}
);
} else {
if (orderNum) {
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?orderNum=` +
orderNum,
});
}
resolve({ status: "invoked" });
}
return;
}
// 情形 C:使用 uni.requestPayment 调起原生支付,失败时通过生成 Scheme 降级拉起
uni.requestPayment({
provider: "toutiao",
orderInfo: orderInfo,
service: 5,
success: function (res) {
console.log("抖音支付成功:", res);
payFeedbackFun(orderId, orderNum, 1);
jumpWayOk();
resolve(res);
},
fail: function (err) {
console.error("uni.requestPayment 抖音支付失败/不兼容,尝试 Scheme 降级拉起:", err);
if (orderStr) {
plus.runtime.openURL(
orderStr,
() => {
if (orderNum) {
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?orderNum=` +
orderNum,
});
}
resolve({ status: "invoked_fallback" });
},
(openErr) => {
console.error("openURL 降级失败:", openErr);
jumpWayError();
reject(err);
}
);
} else {
jumpWayError();
reject(err);
}
},
});
return;
// #endif
// 兜底处理 (H5 及非 App 环境)
if (orderStr) {
uni.navigateTo({
url:
`/pages/other_package/payment_processing/payment_processing?link=${encodeURIComponent(
orderStr
)}&orderNum=` + orderNum,
});
resolve({ status: "processing" });
} else {
uni.showToast({
title: "抖音支付暂仅支持在 App 端调用",
icon: "none",
});
jumpWayError();
reject(new Error("抖音支付暂仅支持在 App 端调用"));
}
});
}
// 导出别名方便不同调用习惯
export const dyPayFun = douyinPayFun;
export const appDyPayFun = douyinPayFun;
export const appDouyinPayFun = douyinPayFun;
// 跳转到支付成功页面
export function jumpWayOk() {
uni.navigateTo({