Files
18xchat/assets/Scripts/Main/Channel/platform/WebSDK.ts
T

95 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-10-13 15:39:35 +08:00
import { IPlatformSDK } from "../core/IPlatformSDK";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
export class WebSDK implements IPlatformSDK {
constructor() {}
init(cb?: Function): void {
logger.log("Web平台初始化");
cb && cb();
}
// 登录
login(cb?: Function): void {
cb && cb({ code: "Web" });
}
// 获取用户信息
getUserInfo(
cb: (info: { nickName: string; avatarUrl: string }) => void
): void {
cb({ nickName: "小明", avatarUrl: "avatar.png" });
}
// 检查平台权限
checkPermission(scope: string, cb: (granted: boolean) => void): void {
cb(true);
}
//region 复制到剪贴板
/**复制文本到剪贴板 */
copyToClipboard(text: string, cb?: Function) {
logger.log("web 复制到剪贴板:", text);
if (navigator.clipboard && window.isSecureContext) {
// 使用现代的 Clipboard API
navigator.clipboard
.writeText(text)
.then(() => {
logger.log("web 复制成功 (Clipboard API)");
cb && cb(true);
})
.catch((err) => {
logger.log("web 复制失败 (Clipboard API)", err);
this.fallbackCopyToClipboard(text, cb);
});
} else {
// 使用兼容性方案
this.fallbackCopyToClipboard(text, cb);
}
}
/**兼容性复制方案 */
private fallbackCopyToClipboard(text: string, cb?: Function) {
const textArea = document.createElement("textarea");
textArea.value = text;
// 避免滚动到底部
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
textArea.style.opacity = "0";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
const successful = document.execCommand("copy");
if (successful) {
logger.log("web 复制成功 (fallback)");
cb && cb(true);
} else {
logger.log("web 复制失败 (fallback)");
cb && cb(false);
}
} catch (err) {
logger.log("web 复制异常 (fallback)", err);
cb && cb(false);
}
document.body.removeChild(textArea);
}
// 客服功能是否可用
kefuSupport(): boolean {
return true;
}
// 打开客服会话
openKefu(): void {}
// 获取设备分辨率
getWindowSize(): { width: number; height: number } {
return { width: 720, height: 1280 };
}
}