/** * 变态版电魂小游戏版本 sdk配置 * */ import Mint from "../utils/mintFilter/index"; import { gameMethod } from "../common/gameMethod"; import { PlayerLogin, PlayerLoginPram } from "../common/Xyc"; import { SevBack } from "../common/Xys"; import Config from "../Config"; import { GameEvent, LoadEvent } from "../data/const/EventConst"; import { GameErrCode, PayProductParam, ReportRoleType } from "../data/const/TypeConst"; import GameDataCenter from "../data/GameDataCenter"; import GameController from "../GameController"; import UIHelp from "../logic/ui/UIHelp"; import EventMng from "../manager/EventMng"; import PlatformBase from "./PlatformBase"; export default class PlatformDianHunWeChat extends PlatformBase { isGoPay: boolean mint: Mint; constructor() { super() // 屏幕常亮 wx.setKeepScreenOn({ keepScreenOn: true }) // 热启动,或唤醒时触发 wx.onShow(res => { if (this.isGoPay) { this.isGoPay = false if (Config.paySuccAdokId != null) { clearInterval(Config.paySuccAdokId) Config.paySuccAdokId = null } Config.paySuccAdokIndex = 0 Config.paySuccAdokId = setInterval(() => { Config.paySuccAdokIndex++ if (Config.paySuccAdokIndex >= 5) { clearInterval(Config.paySuccAdokId) Config.paySuccAdokId = null } GameDataCenter.time.sendAdok() }, 1000) } GameDataCenter.audio.resumeMusic() // IOS系统,在后台切回时似乎回失去常亮功能,这里再调用一次 wx.setKeepScreenOn({ keepScreenOn: true }) EventMng.emit(GameEvent.ON_SHOW_APP) console.log("-----wx.onShow 回调触发:", res) }) // 切到后台时触发 wx.onHide(res => { GameDataCenter.plat.instance.reportRole(ReportRoleType.offline) // this.dealQuery(res) GameDataCenter.audio.stopAllEffects() GameDataCenter.audio.pauseMusic() }) // cc.game.on(cc.game.EVENT_SHOW, () => { // if (this.isGoPay) { // this.isGoPay = false // if (Config.paySuccAdokId != null) { // clearInterval(Config.paySuccAdokId) // Config.paySuccAdokId = null // } // Config.paySuccAdokIndex = 0 // Config.paySuccAdokId = setInterval(() => { // Config.paySuccAdokIndex++ // if (Config.paySuccAdokIndex >= 5) { // clearInterval(Config.paySuccAdokId) // Config.paySuccAdokId = null // } // GameDataCenter.time.sendAdok() // }, 1000) // } // }) // 小程序更新机制 https://developers.weixin.qq.com/minigame/dev/guide/runtime/update-mechanism.html const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate((res) => { // 请求完新版本信息的回调 console.log("请求完新版本信息的回调", res.hasUpdate) }) updateManager.onUpdateReady(() => { wx.showModal({ title: '更新提示', content: '新版本已经准备好,是否重启应用?', success(res) { if (res.confirm) { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate() } } }) }) updateManager.onUpdateFailed(() => { // 新版本下载失败 }) //需要初始化广告 if (gameMethod.isEmpty(GameController.rewardedVideoAd)) { this.initRewardedVideoAd("adunit-7684634ced33efb2") } } showLoginView(): void { console.log("初始化&登录SDK => ", window['dhSDK'].system.platform) EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.25, "login") GameDataCenter.plat.instance.deviceOS = window['dhSDK'].system.platform try { setTimeout(() => { // console.log("----拉取登录----") window['dhSDK'].login({ success: response => { // console.log("用户信息如下:"); // console.log(response.data); // console.log("用户微信唯一标识openId如下:"); // console.log(response.data.openId) // console.log("login data=",JSON.stringify(response)) // EventMng.emit(LoadEvent.LOAD_MESSAGE, JSON.stringify(result)) let data = response.data EventMng.emit(LoadEvent.LOAD_PROGRESS, 0.75, "login") let params: PlayerLoginPram = { pid: Config.pid, param: [data.accountId, data.loginType, data.token, window['dhSDK'].deviceOS, window['dhSDK'].deviceId, window['dhSDK'].channel, window['dhSDK'].mainChannel, window['dhSDK'].secondChannel] } setTimeout(() => { //电魂sdk漏斗打点 if (window['dhSDK']) { // 事件ID let eventId = "420000::GAME_CLIENT_LOGIN_START"; // 表示SDK客户端上报的启动事件 // 事件参数 let eventParams = {}; // 必填参数 eventParams["logLevel"] = "d"; eventParams["logContent"] = "SDK启动"; // 执行上报 window['dhSDK'].trackEvent(eventId, eventParams); } GameController.network.send(PlayerLogin.url, params, (result: SevBack) => { console.log("后段验证返回") if (result.type != 1) { if (gameMethod.isEmpty(result.win) || gameMethod.isEmpty(result.win.msgOut)) { GameDataCenter.plat.showErrDialog(GameErrCode.errGamePlatLogin) } return } EventMng.emit(LoadEvent.LOAD_PROGRESS, 1, "login") if (result.type == 1 && result.platBack) { this.openId = result.platBack.openid this.loginParams = [result.platBack.token] console.log("后端验证成功", this.openId) this.login() //电魂sdk漏斗打点 if (window['dhSDK']) { // 事件ID let eventId = "450000::GAME_CLIENT_LOGIN_SUCCESS"; // 表示SDK客户端上报的启动事件 // 事件参数 let eventParams = {}; // 必填参数 eventParams["logLevel"] = "d"; eventParams["logContent"] = "游戏客户端登录成功"; // 执行上报 window['dhSDK'].trackEvent(eventId, eventParams); } } else { console.log("PlayerLogin.url 返回数据异常") if (gameMethod.isEmpty(result.win) || gameMethod.isEmpty(result.win.msgOut)) { GameDataCenter.plat.showErrDialog(GameErrCode.errGamePlatLogin) } } }) // 首先通过 sdk.getSetting 查询用户是否已授权获取昵称头像 window['dhSDK'].getSetting({ success: setting => { // 判断用户之前是否已同意授权获取其个人信息 // 若已经授权,可以直接调用 sdk.getUserInfo 获取昵称头像 if (setting.authSetting && setting.authSetting["scope.userInfo"]) { console.log("已授权") GameDataCenter.plat.instance.hasAuthUserInfo = true } else { wx.getUserInfo({ lang: "zh_CN", success: function (res) { GameDataCenter.plat.instance.reportUserInfo(res) }, fail: function () { } }) } // 整理订阅消息 if (setting.subscriptionsSetting && setting.subscriptionsSetting.itemSettings) { GameDataCenter.subscribe.recordSubscribeAlwaysSettings(setting.subscriptionsSetting.itemSettings) } } }); }, 1000) }, fail: error => { console.log(error); UIHelp.ShowTips(error["message"]) }, complete: () => { // do something here... } }); window["dhSDK"].setShare({ title: "", imageUrl: "https://mmocgame.qpic.cn/wechatgame/IPo1swnSw7jm37KrqskuianLoGjy7jJ0XyAFbowVKTNicA5TIibl71JQS5D9ZrJvbQZ/0", imageUrlId: "2+FmTyUWS9CB3UmprmJE7A==", imagePreviewUrl: "https://mmocgame.qpic.cn/wechatgame/IPo1swnSw7jm37KrqskuianLoGjy7jJ0XyAFbowVKTNicA5TIibl71JQS5D9ZrJvbQZ/0", imagePreviewUrlId: "2+FmTyUWS9CB3UmprmJE7A==", query: "" }); }, 1000) } catch (error) { UIHelp.ShowTips("----" + error) } } pay(orderId: string, order10cs: string, param: PayProductParam): void { let successFunc = () => { //拉取支付成功 console.log("电魂支付成功") window['dhSDK'].uploadRoleInfo({ type: "PAY_SUCCESS", // 登录游戏服时传LOGIN_GAME,创角时传CREATE_ROLE,升级时传LEVEL_UP,充值成功时传PAY_SUCCESS areaId: parseInt(GameDataCenter.sevBack.userInfo?.a?.sid), // 游戏大区ID,若游戏无大区概念,可不填,默认值为 1,表示初始大区编号 areaName: GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo?.a?.sid].name, // 游戏大区名称 roleId: GameDataCenter.sevBack.userInfo?.a?.uuid, // 角色ID,若游戏无角色概念,可不填,默认值同 accountId 账号ID roleName: GameDataCenter.sevBack.userInfo?.a?.name, // 角色名 roleLevel: GameDataCenter.sevBack.userInfo?.a?.level, // 角色等级,若游戏无角色及等级概念,可填关卡等级或不填,默认值为 1 roleVipLevel: 1, // 角色VIP等级 // 以下参数仅在 type 为 PAY_SUCCESS 时所需,以上为通用参数 itemId: param.productId, // 商品ID itemName: param.productName, // 商品名称 orderId: orderId, // 订单唯一编号 orderPrice: param.price * 100, // 商品总价,即实际支付价格,单位为分!!! currency: "CNY", // 货币类型 payType: "weixinpay" // 支付方式 }, (params: any) => { console.log(params); }); this.isGoPay = false; if (Config.paySuccAdokId != null) { clearInterval(Config.paySuccAdokId) Config.paySuccAdokId = null } Config.paySuccAdokIndex = 0 Config.paySuccAdokId = setInterval(() => { Config.paySuccAdokIndex++ if (Config.paySuccAdokIndex >= 5) { clearInterval(Config.paySuccAdokId) Config.paySuccAdokId = null } GameDataCenter.time.sendAdok() }, 1000) } let time = GameDataCenter.time.sevTime let orderInfo = { cardMessageTitle: param.productName, // 该选项仅在使用跳转H5网页支付时必填,一般此参数必填,因iOS设备仅支持跳转H5网页支付 cardMessageImage: "http://static.m3guo.com/om/minigame/wx/payment.png", // 该选项仅在使用跳转H5网页支付时必填,一般此参数必填,因iOS设备仅支持跳转H5网页支付 areaId: parseInt(GameDataCenter.sevBack.userInfo?.a?.sid), areaName: GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo?.a?.sid].name,//parseInt(GameDataCenter.sevBack.userInfo?.a?.sid), roleId: parseInt(GameDataCenter.sevBack.userInfo?.a?.uuid), roleName: GameDataCenter.sevBack.userInfo?.a?.name, roleLevel: GameDataCenter.sevBack.userInfo?.a?.level, itemId: param.productId, itemName: param.productName, itemNum: 1, // 一般固定传1 itemPrice: param.price * 100, // 商品单价单位为分,请注意换算!例如此示例中的600表示600分,即6元 currency: "CNY", rate: 10, memo: orderId,//JSON.stringify({ orderId: orderId }), // 透传参数,字符串类型;支付完成后,SDK服务器将通过支付回调原样返回给游戏服务器;这里演示透传游戏业务订单号 remark: Config.pid, // 备用透传参数 success: successFunc, fail: (error) => { // 此处返回接口调用失败的错误信息,error.code为cancel时,表示用户主动点击了取消按钮,此时可以不向用户弹出提示 if (error.code == "cancel") { return; } // 此处可向用户弹出接口返回的错误消息或统一弹出“支付失败” window['dhSDK'].modal.message(error.message); }, complete: (result) => { // do something here... } } console.log("调用电魂支付接口=>", JSON.stringify(orderInfo)) this.isGoPay = true; window['dhSDK'].pay(orderInfo) } // 上报角色数据 reportRole(event: ReportRoleType): void { // • 初始化成功后调⽤. • 进⼊区服、创⻆、⻆⾊等级更新、战⼒更新时调⽤. if (GameDataCenter.sevBack == null) { return } if (GameDataCenter.sevBack.userInfo == null) { return } console.log("上报角色数据:", event) let onlineTime = 0 if (GameDataCenter.time.onlineTime > 0) { onlineTime = GameDataCenter.time.sevTime - GameDataCenter.time.onlineTime } let isCreateRole = false switch (event) { case ReportRoleType.create: isCreateRole = true //电魂sdk漏斗打点 if (window['dhSDK']) { // 事件ID let eventId = "450001::GAME_CLIENT_CREATING_A_ROLE"; // 表示SDK客户端上报的启动事件 // 事件参数 let eventParams = {}; // 必填参数 eventParams["logLevel"] = "d"; eventParams["logContent"] = "游戏客户端创角成功"; // 执行上报 window['dhSDK'].trackEvent(eventId, eventParams); } window['dhSDK'].uploadRoleInfo({ type: "CREATE_ROLE", // 登录游戏服时传LOGIN_GAME,创角时传CREATE_ROLE,升级时传LEVEL_UP,充值成功时传PAY_SUCCESS areaId: parseInt(GameDataCenter.sevBack.userInfo?.a?.sid), // 游戏大区ID,若游戏无大区概念,可不填,默认值为 1,表示初始大区编号 areaName: GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo?.a?.sid].name, // 游戏大区名称 roleId: GameDataCenter.sevBack.userInfo?.a?.uuid, // 角色ID,若游戏无角色概念,可不填,默认值同 accountId 账号ID roleName: GameDataCenter.sevBack.userInfo?.a?.name, // 角色名 roleLevel: GameDataCenter.sevBack.userInfo?.a?.level, // 角色等级,若游戏无角色及等级概念,可填关卡等级或不填,默认值为 1 roleVipLevel: 1, // 角色VIP等级 }, (params: any) => { console.log(params); }); break; case ReportRoleType.levelup: window['dhSDK'].uploadRoleInfo({ type: "LEVEL_UP", // 登录游戏服时传LOGIN_GAME,创角时传CREATE_ROLE,升级时传LEVEL_UP,充值成功时传PAY_SUCCESS areaId: parseInt(GameDataCenter.sevBack.userInfo?.a?.sid), // 游戏大区ID,若游戏无大区概念,可不填,默认值为 1,表示初始大区编号 areaName: GameDataCenter.sevBack.qufuList[GameDataCenter.sevBack.userInfo?.a?.sid].name, // 游戏大区名称 roleId: GameDataCenter.sevBack.userInfo?.a?.uuid, // 角色ID,若游戏无角色概念,可不填,默认值同 accountId 账号ID roleName: GameDataCenter.sevBack.userInfo?.a?.name, // 角色名 roleLevel: GameDataCenter.sevBack.userInfo?.a?.level, // 角色等级,若游戏无角色及等级概念,可填关卡等级或不填,默认值为 1 roleVipLevel: 1, // 角色VIP等级 }, (params: any) => { console.log(params); }); break; case ReportRoleType.offline: break; case ReportRoleType.online: break; case ReportRoleType.other: break; default: break; } if (isCreateRole) { // window['dhSDK'].analytics.register({ // accountId: window['dhSDK'].userInfo.accountId, // extendParams: {} // }, () => { // // console.log(params); // }); } } openService() { window['dhSDK'].openCustomerService({ areaId: parseInt(GameDataCenter.sevBack.userInfo?.a?.sid), roleId: GameDataCenter.sevBack.userInfo?.a?.uuid, roleName: GameDataCenter.sevBack.userInfo?.a?.name, roleLevel: GameDataCenter.sevBack.userInfo?.a?.level, success: (response) => { console.log(response); }, fail: (error) => { console.log(error); }, complete: (result) => { // do something here... } }); } // 订阅消息 setSubscribe(tmplIds: string[], ids: string[]) { } initRewardedVideoAd(adkey: string) { console.log("初始化广告实例") //创建激励视频广告实例;建议提前创建好广告实例,在需要展示的时候直接调用实例的 show 方法展示即可 GameController.rewardedVideoAd = window['dhSDK'].createRewardedVideoAd({ adUnitId: adkey, // 请联系相关运营人员于微信公众平台后台创建并生成 adPlatform: "weixin", adPosition: "", onLoad: (response) => { console.log("initRewardedVideoAd:" + response.code); // 响应状态码,此处为0,表示加载成功 }, onClose: (response) => { console.log(response.code); // 响应状态码,此处为0 console.log(response.data); // 响应数据 console.log(response.data.isCompleted); // 广告任务是否已完成,true 表示已完成,false 表示未完成 // 用户已完成广告任务,即广告正常播放结束 if (response.data.isCompleted) { // 这里可以通知游戏服务器下发广告奖励 GameDataCenter.adVideo.onXiadanSucc() } // 用户未完成广告任务,即广告播放中途退出 else { // 这里可以提示用户尚未完成广告播放 } }, onError: (error) => { console.log(error.code); // 响应状态码,此处为非0状态码 console.log(error.message); // 错误消息 } }); } //视频广告 showRewardAd(adkey: string, kid: string, order11Id: string): void { //用于加载广告 const loadRewardedVideoAd = (options: any, times: number) => { // 先判断 rewardedVideoAd 是否存在再使用 // 因为当用户的客户端基础库版本低于 2.0.4 时,调用 sdkInstance.createRewardedVideoAd 接口将返回 undefined if (!GameController.rewardedVideoAd) { return window['dhSDK'].modal.message("暂不支持激励视频广告"); } // 手动重新加载广告 GameController.rewardedVideoAd.load((response: any) => { // 响应状态码 response.code 为 0 时表示加载成功,可再次调用 show 方法进行展示 if (response.code == 0) { showRewardedVideoAd(options, times); } // 其他非 0 状态码均表示加载失败,可向用户弹出错误消息 response.message,亦可自定义失败提示 else { window['dhSDK'].modal.message(response.message); } }); }; //用于展示广告 const showRewardedVideoAd = (options: any, times: number) => { // 先判断 rewardedVideoAd 是否存在再使用 // 因为当用户的客户端基础库版本低于 2.0.4 时,调用 sdkInstance.createRewardedVideoAd 接口将返回 undefined if (!GameController.rewardedVideoAd) { return window['dhSDK'].modal.message("暂不支持激励视频广告"); } // 广告实例的 show 方法用于展示广告,默认是隐藏的 GameController.rewardedVideoAd.show(options, (response: any) => { // 响应状态码 response.code 为 0 时表示展示成功 if (response.code == 0) { console.log("广告展示成功"); } // 其他非 0 状态码均表示展示失败,可尝试利用实例的 load 方法手动重新加载,并在加载成功后再次调用 show 方法进行展示 else { // 重试次数大于0时进行重试 if (times > 0) { loadRewardedVideoAd(options, times--); } // 反之弹出错误消息 response.message,亦可自定义失败提示 else { window['dhSDK'].modal.message(response.message); } } }); }; showRewardedVideoAd({ adPosition: "" }, 3); } checkMsg(str: string, cb: Function): void { // if (gameMethod.isEmpty(this.mint)) { // let cfg = Gamecfg.sensitiveInfo.pool // let sessionidInfo = Object.values(cfg) // let allSessionidStr = [] // for (let i = 0; i < sessionidInfo.length; i++) { // allSessionidStr.push(sessionidInfo[i].desc) // } // this.mint = new Mint(allSessionidStr) // } // try { // let tempStr = str.replace(" ","") // tempStr = tempStr.replace("&","") // const status = this.mint.verify(tempStr) // console.log(status) // false // if (status) { // cb() // } else { // UIHelp.ShowTips("输入文字带有敏感词,请重新输入") // } // } catch (error) { // UIHelp.ShowTips("输入文字带有敏感词,请重新输入") // } cb() } }