Files
jindengchen-ai-report/cool-unix/uni_modules/cool-open-web/utssdk/mp-weixin/index.uts

49 lines
1.4 KiB
Plaintext
Raw Normal View History

2025-11-13 10:36:23 +08:00
/**
* 在微信小程序中打开指定的网页URL
* 使用微信小程序的wx.openUrl API
* @param url 要打开的网页地址必须是https协议
* @returns 返回操作结果true表示成功false表示失败
*/
export function openWeb(url: string): boolean {
// 参数验证检查URL是否为空或无效
if (url == null || url.trim() == "") {
console.error("openWeb: URL参数不能为空");
return false;
}
try {
let trimmedUrl = url.trim();
// 微信小程序要求必须是https协议
if (!trimmedUrl.startsWith("https://")) {
console.error("openWeb: 微信小程序只支持https协议的URL -", trimmedUrl);
return false;
}
// 基本URL格式验证
if (!trimmedUrl.includes(".") || trimmedUrl.length < 12) { // https:// 最少8个字符 + 域名最少4个字符
console.error("openWeb: 无效的URL格式 -", trimmedUrl);
return false;
}
// 使用微信小程序的API打开URL
wx.openUrl({
url: trimmedUrl,
success: (res: any) => {
console.log("openWeb: 成功打开URL -", trimmedUrl);
},
fail: (err: any) => {
console.error("openWeb: 打开URL失败 -", err);
},
complete: (res: any) => {
console.log("openWeb: 操作完成 -", res);
}
});
return true;
} catch (e) {
// 捕获可能的异常
console.error("openWeb: 打开URL时发生错误 -", e);
return false;
}
}