Files
esengine/packages/platform-wechat/src/utils.ts

47 lines
1.0 KiB
TypeScript
Raw Normal View History

/**
*
*/
/**
*
*/
export function getWx(): WechatMinigame.Wx {
if (typeof wx === 'undefined') {
throw new Error('当前环境不是微信小游戏环境');
}
return wx;
}
/**
*
*/
export function isWeChatMiniGame(): boolean {
try {
if (typeof wx === 'undefined') {
return false;
}
const wxObj = wx as WechatMinigame.Wx;
return typeof wxObj.getWindowInfo === 'function' &&
typeof wxObj.createCanvas === 'function' &&
typeof wxObj.createImage === 'function';
} catch {
return false;
}
}
/**
* API Promise
*/
export function promisify<T>(
fn: (options: any) => void,
options: any = {}
): Promise<T> {
return new Promise((resolve, reject) => {
fn({
...options,
success: (res: T) => resolve(res),
fail: (err: any) => reject(err)
});
});
}