declare var wx: any; import { find, Node, Size, UITransform, v3, Vec3 } from "cc"; import BaseSdk from "./BaseSdk"; import { MgrScope } from "./SdkInterface"; const Scope = ({ [MgrScope.FriendInteraction]: 'scope.WxFriendInteraction', [MgrScope.UserInfo]: 'scope.userInfo' }) export default class WxSdk extends BaseSdk { getScope(str): string { return Scope[str]; } getMenuButtonBoundingClientRect() { return wx.getMenuButtonBoundingClientRect() } openSetting(res){ wx.openSetting(res) } getWindowInfo(){ return wx.getWindowInfo() } recorderResume() { } recorderPause() { } private videoAd: any; private callback: any; private target: any; private bannerAd: any; private interstitialAd: any; private customAd: any; /** * 初始化 * @param callback 初始化成功后回调 * @param target 监听对象 */ public init(callback: Function, target: Object): void { console.log("WxSdk init"); //转发 wx.showShareMenu({ withShareTicket: true, menus: ["shareAppMessage", "shareTimeline"] }); } /** * 登录 * @param callback 登录成功后回调 * @param target 监听对象 */ public login(callback: Function, target: Object): void { console.log("WxSdk login"); } /** * 登出 */ public logout(): void { console.log("WxSdk logout"); } /** * 退出 */ public exit(): void { console.log("WxSdk exit"); } /** * 切换账号 */ public switchLogin(): void { console.log("WxSdk switchLogin"); } public setOpenScore(score: number): void { wx.postMessage({ type: 'engine', event: 'setScore', score: score, }); } public getOpenRank(): void { wx.postMessage({ type: 'engine', event: 'getRank' }); } /** * 数据上报 * @param param 参数 */ public report(...param: any[]): void { console.log("WxSdk report"); } /** * 数据上报 * @param param 参数 */ public pay(...param: any[]): void { console.log("WxSdk pay"); } showLoading(param={ title: '加载中', }){ wx.showLoading(param) } hideLoading(){ wx.hideLoading() } /** * 播放广告 */ playRewardAd(callback: Function, target: any): void { this.callback = callback; this.target = target; if (this.adUnitId == 'adunit-') { this.callback.call(this.target, true); return } this.createRewardAd(); // 用户触发广告后,显示激励视频广告 this.videoAd.show().then(()=>{ wx.hideLoading() }).catch(() => { // 失败重试 this.videoAd.load() .then(() =>{ this.videoAd.show().then(()=>{ wx.hideLoading() }).catch(()=>{ console.log('激励视频 广告显示失败') this.onRewardAdCallback(false); }) } ) .catch(err => { console.log('激励视频 广告显示失败') wx.hideLoading() this.onRewardAdCallback(false); }) }) } adUnitId = 'adunit-68f5c230e5dae73c' createRewardAd() { if (!this.videoAd) { this.videoAd = wx.createRewardedVideoAd({ adUnitId: this.adUnitId }) // this.videoAd.onLoad((res)=>{ // wx.hideLoading() // }) this.videoAd.onClose(res => { // 用户点击了【关闭广告】按钮 // 小于 2.1.0 的基础库版本,res 是一个 undefined wx.hideLoading() if (res && res.isEnded || res === undefined) { // 正常播放结束,可以下发游戏奖励 // callback.call(target, true); this.onRewardAdCallback(true); } else { // 播放中途退出,不下发游戏奖励 // callback.call(target, false); this.onRewardAdCallback(false); } }) this.videoAd.onError(err => { // callback.call(target, false); // console.log(err, "1"); wx.hideLoading() this.onRewardAdCallback(false); }) } } onRewardAdCallback(success) { if (this.callback) { this.callback.call(this.target, success); if (success) { this.videoAd.load(); } this.callback = null; } } /** * 显示banner广告 */ showBannerAd(show: boolean): void { if (show) { // 在适合的场景显示 Banner 广告 this.createBannerAd(); this.bannerAd.show(); } else { this.bannerAd.hide(); this.bannerAd.destroy(); this.bannerAd = null; this.createBannerAd(); } } createBannerAd() { if (!this.bannerAd) { let sysInfo = wx.getSystemInfoSync(); // 创建 Banner 广告实例,提前初始化 var width = 320; this.bannerAd = wx.createBannerAd({ adUnitId: 'adunit-b03f9dc72e3592bd', adIntervals: 30, // 自动刷新频率不能小于30秒 style: { left: sysInfo.screenWidth / 2 - width / 2, top: sysInfo.screenHeight, width: width } }) this.bannerAd.onResize(res => { this.bannerAd.style.top = sysInfo.screenHeight - this.bannerAd.style.realHeight; }) this.bannerAd.onError(err => { console.log(err) }) } } createInterstitialAd() { // 创建插屏广告实例,提前初始化 if (wx.createInterstitialAd && !this.interstitialAd) { this.interstitialAd = wx.createInterstitialAd({ adUnitId: 'adunit-b03f9dc72e3592bd' }) this.interstitialAd.onClose(res => { this.interstitialAd = null; this.createInterstitialAd(); }) } } showInterstitialAd() { // 在适合的场景显示插屏广告 this.createInterstitialAd(); if (this.interstitialAd) { this.interstitialAd.show().catch((err) => { console.error(err) }) } } /** * 显示自定义广告 */ showCustomAd(show, top?, left?): void { // console.log(top, left); if (!this.customAd) { this.customAd = wx.createCustomAd({ adIntervals: 30, adUnitId: 'adunit-b03f9dc72e3592bd', style: { fixed: true, left: left, top: top, }, }) } if (show) { this.customAd.show(); } else { this.customAd.destroy(); this.customAd = null; } } /** * 主动拉起转发 */ shareAppMessage(title: string = "", imageUrlId: string = "", imageUrl: string = "", query: any = null, shareCallback: any = null): void { title = title; imageUrlId = imageUrlId; imageUrl = imageUrl; var data = { title: title, imageUrlId: imageUrlId, imageUrl: imageUrl, query: "" }; wx.shareAppMessage(data); } recorderStart(duration: number = 30): void { } recorderStop(): void { } shareRecord(title: string, desc: string, path: string, topics: string[]): void { } public goToSidebar(): void { } public checkHasSidebar(): boolean { return false; } public checkFromSidebar(): boolean { return false; } vibrateShort(param: any) { wx.vibrateShort({ type: param.type, success: param.success, fail: param.fail, complete: param.complete }); } posMessge(messge?) { let mess = { event: 'getRank', type: 'engine' } if (messge) { messge = { ...mess, ...messge } } messge = messge || mess console.log("messge") console.log(messge) console.log(wx.postMessage) console.log("messge__end") wx.postMessage(messge) // let env = wx // if (env) { // console.log('Message posted'); // env.getOpenDataContext().postMessage({ // event:'setValue',type:'engine' // }); // } } authorize(str?: any, cb?) { wx.getSetting({ success(res) { if (!res.authSetting[str]) { console.log("success_res ==>", str) wx.authorize({ scope: str, success() { console.log("已经授权,直接获取用户信息==>", str) } }) cb&&cb() } else { // if(str == 'scope.userInfo') } }, }); } getUserInfo(reportFun:any, node?: Node,btnCb?) { wx.getSetting({ success(res) { if (res.authSetting['scope.userInfo'] === true) { wx.getUserInfo({ success: (res) => { // 已经授权,直接获取用户信息 // var userInfo = res.userInfo // var nickName = userInfo.nickName // var avatarUrl = userInfo.avatarUrl // var gender = userInfo.gender //性别 0:未知、1:男、2:女 // var province = userInfo.province // var city = userInfo.city // var country = userInfo.country console.log("res=====>",res) reportFun(res) // export function reportUserInfo(avatar: string, nickname: string, gender: number): void; }, }); } else { let buttonPosition = { left: 0, top: 0, width:0, height:0 } if(node){ let uit = node?.getComponent(UITransform) const canvasNode = find('Canvas'); // 获取 Canvas 节点 const ret = uit.getBoundingBoxToWorld(); // 转换为世界坐标 let canvasSize = canvasNode.getComponent(UITransform).contentSize const systemInfo = wx.getSystemInfoSync(); console.log("systemInfo==>",systemInfo) console.log("canvasSize==>",canvasSize) let k_x = systemInfo.windowWidth/canvasSize.width let k_y = systemInfo.windowHeight/canvasSize.height buttonPosition = { left: ret.x *k_x, top: systemInfo.windowHeight - (ret.y * k_y) - ret.height *k_y , width:ret.width*k_x, height:ret.height * k_y, }; } console.log("buttonPosition==>",buttonPosition) const button = wx.createUserInfoButton({ type: "image", style: { left: buttonPosition.left, top: buttonPosition.top, width: buttonPosition.width, height: buttonPosition.height, // backgroundColor: "rgba(255, 255, 255, 0.5)", }, }); btnCb&&btnCb(button) button.onTap((res) => { if (res.errMsg.indexOf(':ok') > -1 && !!res.rawData) { // 获取用户信息 // var userInfo = res.userInfo // var nickName = userInfo.nickName // var avatarUrl = userInfo.avatarUrl // var gender = userInfo.gender //性别 0:未知、1:男、2:女 // var province = userInfo.province // var city = userInfo.city // var country = userInfo.country reportFun(res) } button.destroy(); }); } }, }); } }