import { registerCanvas, registerMiniGame, dispatchPointerDown, dispatchPointerMove, dispatchPointerUp, dispatchPointerOut } from "@oasis-engine/miniprogram-adapter"; /** * 小游戏的 SDK * my 的接口写到外面标红太丑 */ export class PaladinAdapter { // onShow 与 onHide private static _onShowMap: Record = {}; private static _onHideMap: Record = {}; // 画布 static canvas: HTMLCanvasElement; // 是否苹果 static isIOS: boolean = false; // 手机型号 static phoneModel: string; /** * 初始化 * @param designWidth - 设计宽度 * @param designHeight - 设计高度 */ static init(designWidth: number, designHeight: number): void { // 获取系统信息 const info = this.getSystemInfoSync(); const { windowWidth, windowHeight, pixelRatio } = info; let scale = Math.max(designWidth / pixelRatio / windowWidth, designHeight / pixelRatio / windowHeight); scale = Math.max(0.4, scale); scale = Math.min(1.0, scale); // @ts-ignore var canvas = (this.canvas = my.createCanvas(windowWidth * pixelRatio * scale, windowHeight * pixelRatio * scale)); registerCanvas(canvas); canvas.clientWidth = windowWidth; canvas.clientHeight = windowHeight; registerMiniGame(); // 将 paladin 的 touch 事件转换成 pointer 事件 // @ts-ignore my.onTouchStart(dispatchPointerDown); // @ts-ignore my.onTouchMove(dispatchPointerMove); // @ts-ignore my.onTouchEnd(dispatchPointerUp); // @ts-ignore my.onTouchCancel(dispatchPointerOut); // Wether it is IOS this.isIOS = info.platform === "iOS"; // Get phoneModel this.phoneModel = info.model; // 屏幕常亮 // @ts-ignore my.setKeepScreenOn({ keepScreenOn: true }); // 控制帧率 30 帧 // @ts-ignore my.setPreferredFramesPerSecond({ fps: 30 }); // @ts-ignore my.onShow(() => { for (const key in this._onShowMap) { this._onShowMap[key](); } }); // @ts-ignore my.onHide(() => { for (const key in this._onHideMap) { this._onHideMap[key](); } }); } static addOnShowListener(key: string, fun: Function) { this._onShowMap[key] = fun; } static addOnHideListener(key: string, fun: Function) { this._onHideMap[key] = fun; } static removeOnShowListener(key: string) { delete this._onShowMap[key]; } static removeOnHideListener(key: string) { delete this._onHideMap[key]; } /** * 获取所有的缓存信息 * @param key 缓存key * @param succFun 成功回调 * @param failFun 失败回调 */ static getStorage(key: string, succFun: Function, failFun?: Function) { // @ts-ignore my.getStorage({ key: key, success: function (res) { console.log("请求缓存数据success:", res); succFun && succFun(res.data); }, fail: function (res) { console.log("请求缓存数据fail:", res); failFun && failFun(res); } }); } /** * 设置缓存信息 * @param key 缓存key * @param data 缓存的值 * @param succFun 成功回调 * @param failFun 失败回调 */ static setStorage(key: string, data: any, success?: Function, fail?: Function) { // @ts-ignore my.setStorage({ key: key, data: data, success: success, fail: fail }); } /** * 小游戏的 toast 提示 * @param content */ static toast(content: string, succ?: Function) { // @ts-ignore my.alert({ content: content, success: succ }); } /** * 小游戏环境下调用命令 * @param cmd 命令的key * @param parms 携带的参数 * @param success 成功回调 * @param fail 失败回调 */ static callCommand(cmd: string, parms: any, success?: Function, fail?: Function) { // @ts-ignore my.call(cmd, parms, success, fail); } /** * 像服务端发送 rpc * @param operationType * @param requestData * @param success * @param fail */ static tos(operationType: string, requestData: any, success?: Function, fail?: Function) { // @ts-ignore my.call("rpc", { operationType: operationType, requestData: requestData, success: success, fail: fail, disableLimitView: true, timeout: 3 }); } /** * 加载插件 * @param pligin 插件名 */ static loadPlugin(pligin: string) { // @ts-ignore my.LoadPlugin(pligin); } /** * 获取系统信息 */ static getSystemInfoSync() { // @ts-ignore return my.getSystemInfoSync(); } /** * 获取授权码 */ static getUserInfo(callBack: Function) { // @ts-ignore my.call("getUserInfo", callBack); } /** * 获取相机授权状态 * @param authType * @param successCB * @param failCB */ static getAuthStatus(authType: string, successCB: Function, failCB: Function, guideCB?: Function) { // @ts-ignore my.call("getAuthStatus", { authType: authType, bizType: "airFu", success: (res) => { try { if (parseInt(res.authStatus) === 0) { // @ts-ignore my.call("showAuthGuide", { authType: authType, bizType: "airFu", success: guideCB, fail: guideCB }); } else { // authStatus为1时 表示已经有权限 successCB && successCB(); } } catch (err) { // 获取权限错误 console.log("获取相机权限有误:x02"); failCB && failCB(); } }, fail: (err) => { const errMsg = err ? JSON.stringify(err) : "获取相机权限有误:x01"; // 获取权限错误 console.log(errMsg); failCB && failCB(); } }); } /** * 获取全局缓存数据 */ static getAPDataStorage(key: string, finishCB: Function) { // @ts-ignore my.call( "getAPDataStorage", { type: "user", business: "77700308", key: key }, finishCB ); } /** * 设置全局缓存数据 */ static setAPDataStorage(key: string, value: string, type: string = "user", finishCB: Function = null) { // @ts-ignore my.call( "setAPDataStorage", { type: type, business: "77700308", key: key, value: value }, finishCB ); } /** * 退出小游戏 */ static exit() { // @ts-ignore my.call("exitApp"); } } /** * 小游戏的 userInfo 数据结构 */ export class UserInfo { userId: string; userName: string; nick: string; loginId: string; iconUrl: string; }