小程序初始提交
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"minSdkVersion": "21"
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
// 导入Android相关类
|
||||
import Intent from "android.content.Intent"; // Android意图类,用于启动活动
|
||||
import Uri from "android.net.Uri"; // Android URI类,用于解析URL
|
||||
import Activity from "android.app.Activity"; // Android活动基类
|
||||
import Context from "android.content.Context"; // Android上下文类
|
||||
|
||||
/**
|
||||
* 打开网页URL函数
|
||||
*
|
||||
* @param url 要打开的网页地址,必须是有效的URL格式
|
||||
* @returns 返回操作结果,true表示成功,false表示失败
|
||||
*
|
||||
* 功能说明:
|
||||
* 1. 验证URL格式的合法性
|
||||
* 2. 创建Intent意图,指定ACTION_VIEW动作
|
||||
* 3. 获取当前Activity实例
|
||||
* 4. 启动系统默认浏览器打开链接
|
||||
* 5. 提供错误处理和异常捕获
|
||||
*
|
||||
* 注意事项:
|
||||
* - URL必须包含协议头(如http://或https://)
|
||||
* - 需要确保设备已安装浏览器应用
|
||||
* - 在某些情况下可能需要网络权限
|
||||
*/
|
||||
export function openWeb(url: string): boolean {
|
||||
try {
|
||||
// 1. 参数验证:检查URL是否为空或无效
|
||||
if (url.trim() == "") {
|
||||
console.error("[cool-openurl] URL不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. URL格式验证:确保包含协议头
|
||||
let trimmedUrl = url.trim();
|
||||
if (!trimmedUrl.startsWith("http://") && !trimmedUrl.startsWith("https://")) {
|
||||
console.error("[cool-openurl] URL必须包含协议头(http://或https://)");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. 解析URL:将字符串转换为Uri对象
|
||||
let uri: Uri | null;
|
||||
try {
|
||||
uri = Uri.parse(trimmedUrl);
|
||||
} catch (e: any) {
|
||||
console.error("[cool-openurl] URL格式无效:" + trimmedUrl, e);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 4. 验证URI是否解析成功
|
||||
if (uri == null) {
|
||||
console.error("[cool-openurl] URI解析失败:" + trimmedUrl);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 5. 创建Intent意图
|
||||
// ACTION_VIEW表示查看指定数据的通用动作,系统会选择合适的应用来处理
|
||||
let intent = new Intent(Intent.ACTION_VIEW, uri);
|
||||
|
||||
// 6. 设置Intent标志
|
||||
// FLAG_ACTIVITY_NEW_TASK:在新的任务栈中启动活动,确保浏览器在独立的任务中运行
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
// 7. 获取当前Activity实例
|
||||
let activity = UTSAndroid.getUniActivity();
|
||||
if (activity == null) {
|
||||
console.error("[cool-openurl] 无法获取当前Activity实例");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 8. 类型安全:将获取的对象转换为Activity类型
|
||||
let currentActivity = activity as Activity;
|
||||
|
||||
// 9. 验证系统中是否有能够处理该Intent的应用
|
||||
let packageManager = currentActivity.getPackageManager();
|
||||
let resolveInfos = packageManager.queryIntentActivities(intent, 0);
|
||||
|
||||
if (resolveInfos.size == 0) {
|
||||
console.error("[cool-openurl] 系统中没有可以打开URL的应用");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 10. 启动Intent,打开URL
|
||||
currentActivity.startActivity(intent);
|
||||
|
||||
// 11. 记录成功日志
|
||||
console.log("[cool-openurl] 成功打开URL:" + trimmedUrl);
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
// 12. 异常处理:捕获并记录所有可能的异常
|
||||
console.error("[cool-openurl] 打开URL时发生异常:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { OpenWebNative } from "./openWeb.ets";
|
||||
|
||||
/**
|
||||
* 在鸿蒙系统中打开指定的网页URL
|
||||
* @param url 要打开的网页地址,支持http、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();
|
||||
|
||||
// 基本URL格式验证
|
||||
if (!trimmedUrl.includes(".") || trimmedUrl.length < 4) {
|
||||
console.error("openWeb: 无效的URL格式 -", trimmedUrl);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果URL不包含协议,默认添加https://
|
||||
if (!trimmedUrl.startsWith("http://") && !trimmedUrl.startsWith("https://") && !trimmedUrl.startsWith("//")) {
|
||||
trimmedUrl = "https://" + trimmedUrl;
|
||||
}
|
||||
|
||||
// 调用鸿蒙原生实现
|
||||
return OpenWebNative.openUrl(trimmedUrl);
|
||||
} catch (e) {
|
||||
// 捕获可能的异常
|
||||
console.error("openWeb: 打开URL时发生错误 -", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { Want, common } from '@kit.AbilityKit';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
|
||||
/**
|
||||
* 原生打开网页控制类
|
||||
* 用于在鸿蒙系统中打开网页URL
|
||||
*/
|
||||
export class OpenWebNative {
|
||||
/**
|
||||
* 打开指定的网页URL
|
||||
* @param url 要打开的网页地址
|
||||
* @returns 返回操作结果,true表示成功,false表示失败
|
||||
*/
|
||||
static openUrl(url: string): boolean {
|
||||
try {
|
||||
// 获取应用上下文
|
||||
const context = getContext() as common.UIAbilityContext;
|
||||
|
||||
// 构建Want对象,用于启动浏览器
|
||||
const want: Want = {
|
||||
action: 'ohos.want.action.viewData', // 查看数据的标准动作
|
||||
entities: ['entity.system.browsable'], // 可浏览实体
|
||||
uri: url // 目标URL
|
||||
};
|
||||
|
||||
// 启动浏览器应用
|
||||
context.startAbility(want)
|
||||
.then(() => {
|
||||
console.info(`成功打开URL: ${url}`);
|
||||
})
|
||||
.catch((error: BusinessError) => {
|
||||
console.error(`打开URL失败: 错误码 ${error.code}, 错误信息 ${error.message}`);
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
// 捕获意外错误
|
||||
const error: BusinessError = err as BusinessError;
|
||||
console.error(
|
||||
`发生意外错误: 错误码 ${error.code}, 错误信息 ${error.message}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"deploymentTarget": "12"
|
||||
}
|
||||
57
cool-unix/uni_modules/cool-open-web/utssdk/app-ios/index.uts
Normal file
57
cool-unix/uni_modules/cool-open-web/utssdk/app-ios/index.uts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { URL } from "Foundation";
|
||||
import { UIApplication } from "UIKit";
|
||||
|
||||
/**
|
||||
* 在iOS设备上打开指定的网页URL
|
||||
* @param url 要打开的网页地址,支持http、https、tel、mailto等协议
|
||||
* @returns 返回操作结果,true表示成功,false表示失败
|
||||
*/
|
||||
export function openWeb(url: string): boolean {
|
||||
// 参数验证:检查URL是否为空或无效
|
||||
if (url == null || url.trim() == "") {
|
||||
console.error("openWeb: URL参数不能为空");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// 创建URL对象,用于验证URL格式的有效性
|
||||
let href = new URL((string = url.trim()));
|
||||
|
||||
// 检查URL对象是否创建成功
|
||||
if (href == null) {
|
||||
console.error("openWeb: 无效的URL格式 -", url);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查系统版本,iOS 16.0及以上版本使用新的API
|
||||
if (UTSiOS.available("iOS 16.0, *")) {
|
||||
// iOS 16.0+ 使用 open(_:options:completionHandler:) 方法
|
||||
// 先检查系统是否支持打开该URL
|
||||
if (UIApplication.shared.canOpenURL(href!)) {
|
||||
// 使用新API打开URL,传入空的options和completionHandler
|
||||
UIApplication.shared.open(href!, (options = new Map()), (completionHandler = nil));
|
||||
console.log("openWeb: 成功使用新API打开URL -", url);
|
||||
return true;
|
||||
} else {
|
||||
console.warn("openWeb: 系统不支持打开该URL协议 -", url);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// iOS 16.0以下版本使用已弃用但仍可用的 openURL 方法
|
||||
// 先检查系统是否支持打开该URL
|
||||
if (UIApplication.shared.canOpenURL(href!)) {
|
||||
// 使用传统API打开URL
|
||||
UIApplication.shared.openURL(href!);
|
||||
console.log("openWeb: 成功使用传统API打开URL -", url);
|
||||
return true;
|
||||
} else {
|
||||
console.warn("openWeb: 系统不支持打开该URL协议 -", url);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 捕获可能的异常,如URL格式错误等
|
||||
console.error("openWeb: 打开URL时发生错误 -", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 在微信小程序中打开指定的网页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;
|
||||
}
|
||||
}
|
||||
37
cool-unix/uni_modules/cool-open-web/utssdk/web/index.uts
Normal file
37
cool-unix/uni_modules/cool-open-web/utssdk/web/index.uts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* 在Web浏览器中打开指定的网页URL
|
||||
* @param url 要打开的网页地址,支持http、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();
|
||||
|
||||
// 基本URL格式验证
|
||||
if (!trimmedUrl.includes(".") || trimmedUrl.length < 4) {
|
||||
console.error("openWeb: 无效的URL格式 -", trimmedUrl);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果URL不包含协议,默认添加https://
|
||||
if (!trimmedUrl.startsWith("http://") && !trimmedUrl.startsWith("https://") && !trimmedUrl.startsWith("//")) {
|
||||
trimmedUrl = "https://" + trimmedUrl;
|
||||
}
|
||||
|
||||
// 在当前窗口中打开URL
|
||||
location.href = trimmedUrl;
|
||||
|
||||
console.log("openWeb: 成功打开URL -", trimmedUrl);
|
||||
return true;
|
||||
} catch (e) {
|
||||
// 捕获可能的异常
|
||||
console.error("openWeb: 打开URL时发生错误 -", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user