Files
gitlab-instance-0a899031_di…/app/lib/payEnv.ts
2026-03-13 04:41:51 -05:00

50 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 支付环境检测PC / H5 / 微信浏览器
* 用于 ZPAY 等聚合支付,需区分环境以选择正确的支付通道
* 参考https://juejin.cn/post/7122720360683798542
*/
export type PayEnv = "pc" | "h5" | "wechat";
export type PayChannel = "alipay" | "wxpay";
/** 是否在微信内置浏览器 */
export function isWeChat(): boolean {
if (typeof navigator === "undefined") return false;
return /MicroMessenger/i.test(navigator.userAgent);
}
/** 是否在支付宝内置浏览器 */
export function isAlipay(): boolean {
if (typeof navigator === "undefined") return false;
return /AlipayClient|Alipay/i.test(navigator.userAgent);
}
/** 是否移动设备(含平板) */
export function isMobile(): boolean {
if (typeof navigator === "undefined") return false;
return /AppleWebKit.*Mobile|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
}
/** 当前支付环境 */
export function getPayEnv(): PayEnv {
if (isWeChat()) return "wechat";
if (isMobile()) return "h5";
return "pc";
}
/**
* 根据环境推荐支付通道
* 所有支付默认微信支付,支付宝暂不提供
*/
export function getRecommendedChannel(_env: PayEnv): PayChannel {
return "wxpay";
}
/** 微信内是否只能选微信支付 */
export function mustUseWxPay(env: PayEnv): boolean {
return env === "wechat";
}