// import UserManager from "../manager/UserManager"; import DataService from "../wxsdk/service/DataService"; import AlipayApi from "../wxsdk/alipay/AlipayApi"; import { SDKTools } from "./SDKTools"; /** WXapi */ export class WxHelper { /** 手机设备信息 */ public static SystemInfo: SystemInfoSyncReturnValue; /** 微信用户信息 */ public static userInfo: WxUserInfo; public static get isOperable() { return (typeof wx !== 'undefined' || typeof my != 'undefined') } /** 获取设备信息 */ public static getSystemInfo() { if (typeof my !== 'undefined') this.SystemInfo = my.getSystemInfoSync(); } /** 像开放数据域发送信息 */ public static postMessage(eventType: string, data?: any): void { if (typeof my === 'undefined' || !this.checkVerison('1.9.92')) return; my.getOpenDataContext().postMessage({ eventType: eventType, data: data }); } /** 更新微信开放数据榜单数据 */ public static updateWxRank(score: number) { if (typeof wx === 'undefined' || !this.checkVerison('1.9.92')) return; let data = { score: score } wx.setUserCloudStorage({ KVDataList: [{ key: 'score', value: JSON.stringify(data) }] }) } /** 创建用户反馈按钮 */ public static createFeedbackButton(target: cc.Node) { let data = this.computWxPosition(target); return my.createFeedbackButton({ type: 'text', text: ' ', style: { left: data.left, top: data.top, width: data.width, height: data.height, borderRadius: 5, // backgroundColor: '#69c66d', } }) } /** 获取用户信息按钮 */ public static userInfoButton: any; /** 创建一个获取用户信息按钮 */ public static createUserInfoButton(target: cc.Node) { let data = this.computWxPosition(target); // console.log("data", data) return my.createUserInfoButton({ type: 'text', text: ' ', style: { left: data.left, top: data.top, width: data.width, height: data.height, borderRadius: 5, // color: '#ffffff', // backgroundColor: '#69c66d', // textAlign: 'center', // fontSize: 20, // lineHeight: 45, }, withCredentials: true }) } /** 计算微信坐标 */ public static computWxPosition(target: cc.Node): { left: number, top: number, width: number, height: number } { let data = { left: 0, top: 0, width: 0, height: 0 } let rect = target.getBoundingBoxToWorld(); let ratio = cc.view.getDevicePixelRatio(); let scale = cc.view.getScaleX(); let factor = scale / ratio; // let point = v2(rect.x, rect.y); // point.mulSelf(scale); data.left = rect.x * factor; data.top = this.SystemInfo.screenHeight - (rect.y + rect.height) * factor; data.width = rect.width * factor; data.height = rect.height * factor; // let btnSize = size(target.width + 10, target.height + 10); // let frameSize = view.getFrameSize(); // let winSize = director.getWinSize(); // // log("winSize: ",winSize); // // log("frameSize: ",frameSize); // //适配不同机型来创建微信按钮 // data.left = (winSize.width * 0.5 + target.x - btnSize.width * 0.5) / winSize.width * frameSize.width; // data.top = (winSize.height * 0.5 - target.y - btnSize.height * 0.5) / winSize.height * frameSize.height; // data.width = btnSize.width / winSize.width * frameSize.width; // data.height = btnSize.height / winSize.height * frameSize.height; // log("button pos: ",v2(left,top)); // log("button size: ",size(width,height)); return data; } /** 兼容方式 - 版本比较 */ public static compareVersion(versionA: any, versionB: any) { versionA = versionA.split('.'); versionB = versionB.split('.'); let len = Math.max(versionA.length, versionB.length); while (versionA.length < len) { versionA.push('0'); } while (versionB.length < len) { versionB.push('0'); } for (let i = 0; i < len; i++) { let num1 = parseInt(versionA[i]); let num2 = parseInt(versionB[i]); if (num1 > num2) { return 1; } else if (num1 < num2) { return -1; } } return 0; } /** 当前版本是否能使用接口 */ public static checkVerison(version: string): boolean { return this.compareVersion(this.SystemInfo.SDKVersion, version) >= 0; } /** 显示一个对话框 */ public static showModal(data: ShowModalType) { return new Promise((resolve, reject) => { if (typeof my !== 'undefined') { my.showModal({ ...data, success: (res: { confirm: boolean, cancel: boolean }) => { if (res.confirm) { resolve(true); } else { resolve(false); } }, fail: () => { reject(); } }); } else { } }); } /** * 显示一个提示层 * @param msg 显示信息 * @param time 关闭时间 */ public static showToast(msg: string, time: number = 1500) { if (typeof wx !== 'undefined') wx.showToast({ title: msg, icon: 'none', duration: time }) else if (typeof my != 'undefined') { my.showToast({ type: 'none', content: msg, duration: time, }); } else { console.log(msg) } } /** 设置系统剪贴板的内容 */ public static copy(str: string) { if (!this.checkVerison('1.1.0')) return Promise.reject(''); return new Promise((resolve, reject) => { my.setClipboardData({ data: str, success: (res: any) => { resolve(res); }, fail: () => { resolve(''); } }); }); } /** 手机发生较长时间的振动(400 ms) */ public static vibrateLong() { // if (!this.checkVerison('1.2.0')) return Promise.reject(null); return new Promise((resolve, reject) => { my.vibrateLong({ success: () => { resolve(1); }, fail: () => { resolve(0); } }); }); } /** 手机发生较短时间的振动(15 ms) */ public static vibrateShort() { // if (!this.checkVerison('1.2.0')) return Promise.reject(null); return new Promise>((resolve, reject) => { my.vibrateShort({ success: () => { resolve({ code: 0 }); }, fail: () => { resolve({ code: 1 }); } }); }); } /** * 检测更新 */ public static checkUpdate(bol: boolean = true) { if (!this.checkVerison('1.9.90')) return; const updateManager = my.getUpdateManager(); updateManager.onCheckForUpdate(function (res) { console.log('updateManager', res.hasUpdate); }); updateManager.onUpdateReady(function () { my.showModal({ title: '更新提示', content: '新版本已经准备好,请重启应用!', showCancel: bol, success: function (res) { if (res.confirm) { updateManager.applyUpdate() } } }); }); updateManager.onUpdateFailed(function () { // 新的版本下载失败 }); } /** 发送客服信息 */ public static openCustomerServiceConversation() { return new Promise>((resolve, reject) => { if (!this.checkVerison('2.0.3')) resolve({ code: 1 }); my.openCustomerServiceConversation({ showMessageCard: true, sendMessageTitle: '我要更多好玩!', sendMessageImg: 'http://dep.miso-lab.com/tetrischangeskin/bin/share/concat.png', success: () => { resolve({ code: 0 }); }, fail: () => { resolve({ code: 1 }); } }); }); } /** 发送客服信息 */ public static openConversation() { return new Promise>((resolve, reject) => { if (!this.checkVerison('2.0.3')) resolve({ code: 1 }); my.openCustomerServiceConversation({ success: () => { resolve({ code: 0 }); }, fail: () => { resolve({ code: 1 }); } }); }); } /** 回到前台 */ public static onShow(callback: () => void) { if (typeof my === 'undefined') return; my.onShow(callback); // return new Promise>((resolve, reject) => { // my.onShow((res: any) => { // resolve({ code: 1 }); // }); // }); } /** 切换到后台 */ public static onHide(callback: () => void) { if (typeof my === 'undefined') return; my.onHide(callback); } public static updateShareMenu(value) { if (typeof my === 'undefined') return; my.updateShareMenu({ withShareTicket: value }); } /** * 检查授权 */ public static authorize(scope: string = 'scope.userInfo') { if (typeof my === 'undefined') return; my.getSetting({ success(res) { if (!res.authSetting[scope]) { my.authorize({ scope: scope, success() { } }) } } }) } /** * 获取用户信息 */ public static getUserInfo(callback) { my.getUserInfo({ success: function (res) { // console.log(res) callback(res) }, fail: function (err) { callback(err) } }) } /** 游戏圈 */ public static createGameClubButton(target: cc.Node) { let data = this.computWxPosition(target); return my.createGameClubButton({ icon: 'green', style: { left: data.left, top: data.top, width: data.width, height: data.height, } }) } public static setUserWxCloudStorage(data: object) { if (typeof wx == 'undefined' || !DataService.I.Authorize) return null; wx.setUserCloudStorage({ KVDataList: [{ key: 'data', value: JSON.stringify(data) }], success: res => { console.log("分数上传 成功", data); }, fail: res => { console.log("分数上传 失败"); } }); } public static showRankEnter() { return WxHelper.isOperable && AlipayApi.I.canUseRank() } public static getRank(key: string) { if (typeof my != 'undefined') { AlipayApi.I.getRankList(key).then(() => { console.log('success 关闭banner') // BannerManager.I.hideAll(); }).catch((msg) => { console.log('fail', JSON.stringify(msg)) // WxHelper.showToast(msg.errMsg || msg.errorMessage || '排行榜获取失败') }); } } public static setRankScore(key: string, score: number, updateType?: number) { if (typeof my != 'undefined') AlipayApi.I.setRankScoreData(key, score, updateType); } public static async loginWx(isAuthorize: boolean, failFunction?: Function, count?: number, success?: Function): Promise { count = count != undefined ? count : 3; let data = await SDKTools.login(isAuthorize); // console.error(count, 'loginWx', data) if (data.code == 0) { success && success(); return data; } else if (count >= 0) { count--; return await this.loginWx(isAuthorize, failFunction, count); } else if (failFunction) { console.warn('--wxlogin--fail', data) failFunction(); return null; } } public static goToGameCenter(success: Function, fail: Function) { //每个游戏跳转游戏中心的url 不一样 let url = "alipays://platformapi/startapp?appId=2021003125685383&url=https%3A%2F%2Frender.alipay.com%2Fp%2Fyuyan%2F180020010001206617%2Findex.html%3FcaprMode%3Dsync&chInfo=gamesetlattice&sms=YES&appClearTop=false"; if (typeof my != 'undefined') { AlipayApi.I.navigateToMiniProgramByScheme(url, success, fail) } } }