39 lines
1021 B
TypeScript
39 lines
1021 B
TypeScript
/**
|
||
* 支付环境检测:PC / H5 / 微信浏览器
|
||
*/
|
||
|
||
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"; // 默认微信支付(PC/H5/微信内均优先)
|
||
}
|
||
|
||
export function mustUseWxPay(env: PayEnv): boolean {
|
||
return env === "wechat";
|
||
}
|