diff --git a/sdk/SDKTools.ts b/sdk/SDKTools.ts index f58853d..86eda05 100644 --- a/sdk/SDKTools.ts +++ b/sdk/SDKTools.ts @@ -110,47 +110,10 @@ export class SDKTools { * 插屏 */ static createInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { - if (!this.isWx) return - WXSDK.ad.createInterstitialAd(adUnitId); + if (!this.isWx) return Promise.resolve({ code: 1, msg: "暂无广告" }) + return WXSDK.ad.createInterstitialAd(adUnitId); } - - /** - * 格子广告 - * @param adUnitId 格子广告ID - * @param opts { //非必填 默认满屏居低 - * bannerWidth: - * offsetY: 距离底部多远 - * isOff:是否默认不显示 - * } - */ - static createGrid(key: string, adUnitId: string = GAMEDATA.gridId, opts?: { gridCount?: number; bannerWidth?: number, offsetY: number; adIntervals?: number, isOff?: boolean }) { - if (!this.isWx) return - return WXSDK.ad.createGrid(key, adUnitId, opts); - } - /** - * 格子广告 显示 ps:创建默认显示 - */ - static showGrid(key: string) { - if (!this.isWx) return - WXSDK.ad.showGrid(key) - } - /** - * 格子广告 隐藏 - */ - static hideGrid(key: string) { - if (!this.isWx) return - WXSDK.ad.hideGrid(key) - } - /** - * 格子广告 销毁 - */ - static destoryGrid(key: string) { - if (!this.isWx) return - WXSDK.ad.destoryGrid(key) - } - - /** * 原生模板广告 全局只能存在一个 并且创建的位置最好是同一个位置 通过show hide控制 如果要改变位置,调用destory再调用创建(不建议频繁销毁创建,会导致广告拉取不到) * @param adUnitId 格子广告ID diff --git a/wxsdk/WXSDK.ts b/wxsdk/WXSDK.ts index 21a507b..b159946 100644 --- a/wxsdk/WXSDK.ts +++ b/wxsdk/WXSDK.ts @@ -1,4 +1,3 @@ -import { __LOG__ } from "./base/SDKConst"; import DataService from "./service/DataService"; import SdkData from "./service/entity/SdkData"; import ShareVideoService from "./service/ShareVideoService"; diff --git a/wxsdk/base/SDKConst.ts b/wxsdk/base/SDKConst.ts index e1ec0db..eb52352 100644 --- a/wxsdk/base/SDKConst.ts +++ b/wxsdk/base/SDKConst.ts @@ -1,7 +1,7 @@ // 游戏配置数据 只需要改动这里的配置信息 // 游戏配置数据 只需要改动这里的配置信息 export const GAMEDATA = { - game_id: '10001',//游戏id + game_id: '10001',//游戏id channel_id: '10001',//渠道id 暂时和游戏id一致 version: '1.0.1', //版本号 中台控制正式还是测试服,尽量保持和小游戏版本一致 appkey: 'd959274a83ea3b95bd1d9b765683eff6',//中台appkey @@ -35,9 +35,15 @@ export const GAMEDATA = { // sdk版本 -export const SDKVersion = 'v1.0.2'; +export const SDKVersion = 'v1.0.3'; // 是否打印 export const __LOG__ = false; +// 是否mock +export var sdkEnv = { + isDebug: false +}; + + //游戏基础信息 export const VersionHost = 'https://wxsdk-ver.d3games.com/version'; @@ -62,6 +68,11 @@ export const OrderHost = { Prod: 'https://wxsdk-order.d3games.com/', Pre: 'https://wxsdk-order-pre.d3games.com/', }; +// gm接口 +export const GMHost = { + Prod: 'https://hermes.d3games.com/', + Pre: 'http://wxadmin.pre.d3games.com/', +}; export const HostKeys = { //打点服务器 @@ -96,11 +107,11 @@ export const HostKeys = { adList: 'api/adplan/list', //广告计划列表 behavior: 'api/douyin/behavior', //抖音投放关键行为 attribute: 'api/member/attribute/set', //修改用户属性 - //订单 orderReport: 'api/order/v2/mimas', //订单信息上报 orderQuery: 'api/order/query', //订单信息查询 - preorder: 'api/order/v2/preorder' //获取支付方式 + preorder: 'api/order/v2/preorder', //获取支付方式 + }; // 本地存储keys @@ -175,4 +186,4 @@ export const SceneCode = { // sdk系统默认分享id export const SDKDotType = { Share: 1001 // 会话进入,无渠道ID -}; \ No newline at end of file +}; diff --git a/wxsdk/http/SDKApi.ts b/wxsdk/http/SDKApi.ts index 9827a0b..5a25668 100644 --- a/wxsdk/http/SDKApi.ts +++ b/wxsdk/http/SDKApi.ts @@ -5,71 +5,70 @@ import DataService from "../service/DataService"; export class SDKApi { - public static Version = (...args) => SDKHttp.httpPost(`${VersionHost}`, ...args); + public static Version = (...args) => SDKHttp.httpPost(`${VersionHost}`, "", ...args); //GameApi - public static ShareList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.ShareList}`, ...args); + public static ShareList = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.ShareList, ...args); - public static getConfig = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.getConfig}`, ...args); + public static getConfig = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.getConfig, ...args); //订阅 - public static subscribe = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.subscribe}`, ...args); + public static subscribe = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.subscribe, ...args); // - public static saveData = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.saveData}`, ...args); - public static getData = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.getData}`, ...args); + public static saveData = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.saveData, ...args); + public static getData = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.getData, ...args); //排行榜添加分数 废弃 - public static rankAdd = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.rankAdd}`, ...args); + public static rankAdd = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.rankAdd, ...args); //排行榜添加分数 - public static totalrankAdd = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.totalrankAdd}`, ...args); + public static totalrankAdd = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.totalrankAdd, ...args); //排行榜 废弃 - public static rankList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.rankList}`, ...args); + public static rankList = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.rankList, ...args); //排行榜 - public static totalrankList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.totalrankList}`, ...args); + public static totalrankList = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.totalrankList, ...args); //广告计划列表 - public static adList = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.adList}`, ...args); + public static adList = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.adList, ...args); // - public static behavior = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.behavior}`, ...args); + public static behavior = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.behavior, ...args); //修改用户属性 - public static attribute = (...args) => SDKHttp.httpPost(`${DataService.I.GameApi}${HostKeys.attribute}`, ...args); - + public static attribute = (...args) => SDKHttp.httpPost(DataService.I.GameApi, HostKeys.attribute, ...args); //LoginApi - public static Login = (...args) => SDKHttp.httpPost(`${DataService.I.LoginApi}${HostKeys.Login}`, ...args); + public static Login = (...args) => SDKHttp.httpPost(DataService.I.LoginApi, HostKeys.Login, ...args); - public static reftoken = (...args) => SDKHttp.httpPost(`${DataService.I.LoginApi}${HostKeys.Reftoken}`, ...args); + public static reftoken = (...args) => SDKHttp.httpPost(DataService.I.LoginApi, HostKeys.Reftoken, ...args); - public static weakLogin = (...args) => SDKHttp.httpPost(`${DataService.I.LoginApi}${HostKeys.weakLogin}`, ...args); + public static weakLogin = (...args) => SDKHttp.httpPost(DataService.I.LoginApi, HostKeys.weakLogin, ...args); //DotApi - // public static dot = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.Dot}`, ...args); + // public static dot = (...args) => SDKHttp.httpGet(DataService.I.DotApi,HostKeys.Dot, ...args); + + public static logOut = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.logOut, ...args); + + public static loadingFinish = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.loadingFinish, ...args); - public static logOut = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.logOut}`, ...args); + public static active = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.Active, ...args); - public static loadingFinish = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.loadingFinish}`, ...args); + public static share = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.Share, ...args); - public static active = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.Active}`, ...args); + public static adStat = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.AdStat, ...args); - public static share = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.Share}`, ...args); + public static stay = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.stay, ...args); - public static adStat = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.AdStat}`, ...args); + public static behaviors = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.behaviors, ...args); - public static stay = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.stay}`, ...args); + public static dot = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.dot, ...args); - public static behaviors = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.behaviors}`, ...args); + public static jumps = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.jumps, ...args); - public static dot = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.dot}`, ...args); + public static level = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.level, ...args); - public static jumps = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.jumps}`, ...args); + public static role = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.role, ...args); - public static level = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.level}`, ...args); - - public static role = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.role}`, ...args); - - public static client_log = (...args) => SDKHttp.httpGet(`${DataService.I.DotApi}${HostKeys.client_log}`, ...args); + public static client_log = (...args) => SDKHttp.httpGet(DataService.I.DotApi, HostKeys.client_log, ...args); // OrderApi - public static pay = (...args) => SDKHttp.httpPost(`${DataService.I.OrderApi}${HostKeys.orderReport}`, ...args); - public static orderQuery = (...args) => SDKHttp.httpPost(`${DataService.I.OrderApi}${HostKeys.orderQuery}`, ...args); - public static preorder = (...args) => SDKHttp.httpPost(`${DataService.I.OrderApi}${HostKeys.preorder}`, ...args); + public static pay = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.orderReport, ...args); + public static orderQuery = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.orderQuery, ...args); + public static preorder = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.preorder, ...args); } diff --git a/wxsdk/http/SDKHttp.ts b/wxsdk/http/SDKHttp.ts index f44b3e6..9184c69 100644 --- a/wxsdk/http/SDKHttp.ts +++ b/wxsdk/http/SDKHttp.ts @@ -1,4 +1,4 @@ -import { GAMEDATA } from "../base/SDKConst"; +import { GAMEDATA, sdkEnv } from "../base/SDKConst"; import SignUtils from "../utils/SignUtils"; export default class SDKHttp { @@ -91,17 +91,32 @@ export default class SDKHttp { } xhr.timeout = 3000; xhr.send(data); - - - }); } - public static async httpGet(url: string, data?: any, dataType: "json" | "string" = "json") { + public static withMock(url: string) { + return sdkEnv.isDebug && window["MOCK"] && window["MOCK"][url]; + } + public static mockData(url: string): Promise> { + let responseText = window["MOCK"][url] + return Promise.resolve({ code: +responseText.code, data: responseText.data, msg: responseText.msg }); + } + + public static async httpGet(baseUrl: string, url: string, data?: any, dataType: "json" | "string" = "json") { + if (this.withMock(url)) { + return this.mockData(url); + } + + url = baseUrl + url; return this.httpRequest(url, "GET", data, dataType); } - public static httpPost(url: string, data?: any, dataType: "json" | "string" = "json") { + public static httpPost(baseUrl: string, url: string, data?: any, dataType: "json" | "string" = "json") { + if (this.withMock(url)) { + return this.mockData(url); + } + + url = baseUrl + url; return this.httpRequest(url, "POST", data, dataType); } } \ No newline at end of file diff --git a/wxsdk/service/AdService.ts b/wxsdk/service/AdService.ts index 6425397..b3720de 100644 --- a/wxsdk/service/AdService.ts +++ b/wxsdk/service/AdService.ts @@ -1,6 +1,5 @@ import WxApi from "../wx/WxApi"; import WxBanner from "../wx/WxBanner"; -import WxGrid from "../wx/WxGrid"; import WxCustom from "../wx/WxCustom"; import WxInterstitial from "../wx/WxInterstitial"; import { GAMEDATA } from "../base/SDKConst"; @@ -44,36 +43,7 @@ export default class AdService { * @param adUnitId */ createInterstitialAd(adUnitId: string) { - WxInterstitial.showInterstitialAd(adUnitId) - // let interstitialAd = WxApi.I.createInterstitialAd(adUnitId); - // interstitialAd.show(); - } - - /** - * 创建格子广告 - * @param adUnitId - * @param opts - */ - createGrid(key: string, adUnitId: string, opts?: { gridCount?: number; bannerWidth?: number, offsetY: number; adIntervals?: number }) { - return WxGrid.I.create(key, adUnitId, opts); - } - /** - * 格子广告 显示 ps:创建默认显示 - */ - showGrid(key: string) { - WxGrid.I.show(key) - } - /** - * 格子广告 隐藏 - */ - hideGrid(key: string) { - WxGrid.I.hide(key); - } - /** - * 格子广告 销毁 - */ - destoryGrid(key: string) { - WxGrid.I.destory(key); + return WxInterstitial.showInterstitialAd(adUnitId) } /** diff --git a/wxsdk/service/DataService.ts b/wxsdk/service/DataService.ts index e00bd9d..90c39b4 100644 --- a/wxsdk/service/DataService.ts +++ b/wxsdk/service/DataService.ts @@ -1,7 +1,7 @@ import SdkData from "./entity/SdkData"; import SDKUtils from "../utils/SDKUtils"; import StorageUtils from "../utils/StorageUtils"; -import { StorageKeys, GAMEDATA, LoginHost, GameHost, DotHost, OrderHost } from "../base/SDKConst"; +import { StorageKeys, GAMEDATA, LoginHost, GameHost, DotHost, OrderHost, GMHost } from "../base/SDKConst"; import { Gender, NetworkType } from "../base/SDKEnum"; import WxLaunch from "../wx/WxLaunch"; @@ -15,7 +15,7 @@ export default class DataService { this._data = new SdkData; } - private setValue(key: string, val: any) { + setValue(key: string, val: any) { // console.error(key,val) if (SDKUtils.isUndefined(val)) return; @@ -33,7 +33,6 @@ export default class DataService { setLoginData(data: _LoginInnerData) { if (!data || Object.keys(data).length === 0) return; - if (data.hasOwnProperty('openId')) this.setOpenId(data.openId); @@ -232,10 +231,6 @@ export default class DataService { return this; } - // get NetworkType(): NetworkType { - // return this._data.networkType; - // } - setNetworkType(networkType: NetworkType) { this.setValue('networkType', networkType); return this; @@ -411,6 +406,13 @@ export default class DataService { return path; } + get GMApi() { + let path = GMHost.Prod; + if (this.EnvEnum === 1) { + path = GMHost.Pre; + } + return path; + } get GameId() { return GAMEDATA.game_id; @@ -502,22 +504,6 @@ export default class DataService { return this; } - // get ShareTotalNum(): number { - // return IntegralService.I.getFinishShareNum(); - // } - - // get VideoTotalNum(): number { - // return IntegralService.I.getFinishVideoNum(); - // } - - // get ShareDayNum(): number { - // return LocalService.I.getDayAllShareNum(); - // } - - // get VideoDayNum(): number { - // return LocalService.I.getDayAllVideoNum(); - // } - private timeInterval public updateTodayOnlineTime() { let dot = StorageUtils.I.get("pcsdk_today_online_time_300") || 0; diff --git a/wxsdk/service/GameService.ts b/wxsdk/service/GameService.ts index 5b9c2eb..bda2027 100644 --- a/wxsdk/service/GameService.ts +++ b/wxsdk/service/GameService.ts @@ -8,7 +8,7 @@ import WxSystem from "../wx/WxSystem"; export default class GameService { - private constructor() { + public constructor() { } @@ -84,18 +84,12 @@ export default class GameService { status: 1, id, }) + resolve({ code: 0, msg: '订阅成功!' }) } - // GameService.I.subScribe(acceptKeys) - // .then(() => { - // resolve(ret); - // DebugUtils.I.dynamic('====> PCSDK subScribe订阅消息成功', ret); - // }) - // .catch((err) => reject(err || { errCode: 0, errMsg: 'GameApi subScribe请求错误' })); }) .catch(err => reject(err)); }); - // return WxApi.I.subscribeMessage(template_ids); } /** @@ -130,7 +124,7 @@ export default class GameService { /** * 构建登录/弱登录公用参数 */ - private buildParams2() { + public buildParams2() { let gameid = GAMEDATA.game_id; let channel = DataService.I.ChannelId; let brand = WxSystem.I.brand; @@ -174,13 +168,13 @@ export default class GameService { token }) } - + /** * 构建登录/弱登录公用参数 */ - private buildParams() { + public buildParams() { let gameid = GAMEDATA.game_id; let channel = DataService.I.ChannelId; let uid = DataService.I.UserId; @@ -195,7 +189,7 @@ export default class GameService { }; } - private static instance: GameService; + public static instance: GameService; static get I(): GameService { return this.instance || (this.instance = new GameService()); } diff --git a/wxsdk/service/LogService.ts b/wxsdk/service/LogService.ts index 3ef6ab9..cd89008 100644 --- a/wxsdk/service/LogService.ts +++ b/wxsdk/service/LogService.ts @@ -465,8 +465,25 @@ export default class LogService { cacheUserId: StorageUtils.I.get("userId") || "0", level: LogLevel[level], content - // level: }) + switch (level) { + case LogLevel.error: + console.error(content); + break; + case LogLevel.debug: + console.log(content); + break; + case LogLevel.info: + console.info(content); + break; + case LogLevel.warning: + console.warn(content); + break; + default: + console.log(content); + break; + } + } diff --git a/wxsdk/service/entity/SdkData.ts b/wxsdk/service/entity/SdkData.ts index 12a6d1d..5271be4 100644 --- a/wxsdk/service/entity/SdkData.ts +++ b/wxsdk/service/entity/SdkData.ts @@ -39,7 +39,7 @@ export default class SdkData { public logindays: number; // 登录天数 public amount: number; // 充值金额 public pon: number; // 用户类型:1微信投放2抖音3自然量0未知 - public ptm:number; // 当天是否300s + public ptm: number; // 当天是否300s public cule_token: string; // 用户通过广告调起微信小游戏的唯一编码 public creative_id: string; // 创意ID public advertister_id: string; // 广告账户ID @@ -85,10 +85,10 @@ export default class SdkData { this.amount = 0; this.pon = 0; this.ptm = 0; - this.cule_token =''; - this.creative_id =''; - this.advertister_id =''; - this.request_id =''; + this.cule_token = ''; + this.creative_id = ''; + this.advertister_id = ''; + this.request_id = ''; } } diff --git a/wxsdk/utils/SignUtils.ts b/wxsdk/utils/SignUtils.ts index dbeea21..59a0bb0 100644 --- a/wxsdk/utils/SignUtils.ts +++ b/wxsdk/utils/SignUtils.ts @@ -71,12 +71,12 @@ export default class SignUtils { let query = ''; for (let i = 0, len = keys.length; i < len; i++) { // 为空,为 0的参数不参与签名,参数名为ver,pkv的参数不参与签名, 字符集为 utf-8 - if (params[keys[i]] === '' || params[keys[i]] === '0' || params[keys[i]] === 0 || keys[i] === 'ver'|| keys[i] === 'pkv') continue + if (params[keys[i]] === '' || params[keys[i]] === '0' || params[keys[i]] === 0 || keys[i] === 'ver' || keys[i] === 'pkv') continue key = keys[i]; i && (query += ''); - if(SDKUtils.isArray(params[key])){ + if (SDKUtils.isArray(params[key])) { query += `${key}=${JSON.stringify(params[key])}`; - }else{ + } else { query += `${key}=${params[key]}`; } } diff --git a/wxsdk/wx/WxApi.ts b/wxsdk/wx/WxApi.ts index 60324f6..beb0f7a 100644 --- a/wxsdk/wx/WxApi.ts +++ b/wxsdk/wx/WxApi.ts @@ -41,7 +41,7 @@ export default class WxApi { subscribeMessage(tmplIds: Array) { let version = Version.I.getVSubscribeMessage(); if (!this.canIUse(version)) return Promise.reject(this.getVersionError(version)); - console.log("tmplIds",tmplIds) + // console.log("tmplIds", tmplIds) return new Promise((resolve, reject) => { wx.requestSubscribeMessage({ tmplIds, @@ -412,4 +412,5 @@ export default class WxApi { static get I(): WxApi { return this._instance || (this._instance = new WxApi); } -} \ No newline at end of file + +} diff --git a/wxsdk/wx/WxCustom.ts b/wxsdk/wx/WxCustom.ts index b4039af..da628bb 100644 --- a/wxsdk/wx/WxCustom.ts +++ b/wxsdk/wx/WxCustom.ts @@ -86,7 +86,7 @@ export default class WxCustom { // console.log("that.isEnd", this.isEnd, this.customAd) if (this.customAd) { LogService.I.adStat('custom', this.adUnitId, DOT_AD_TYPE.custom, DOT_AD_STATUS.show) - this.customAd.show().then(res=>{ + this.customAd.show().then(res => { WxCustom.I.handleQueue(); }).catch((err: any) => this.handleShowError(err)); __LOG__ && console.error('WxCustom - show: ' + this.adUnitId, this.customAd); @@ -166,7 +166,7 @@ export default class WxCustom { let that = WxCustom.I; that.unbind(); that._isErrored = true; - if(that.customAd){ + if (that.customAd) { that.customAd.destroy() } that.customAd = null diff --git a/wxsdk/wx/WxGrid.ts b/wxsdk/wx/WxGrid.ts deleted file mode 100644 index 536cf75..0000000 --- a/wxsdk/wx/WxGrid.ts +++ /dev/null @@ -1,233 +0,0 @@ -import WxSystem from "./WxSystem"; -import SDKUtils from "../utils/SDKUtils"; -import { BannerError, __LOG__ } from "../base/SDKConst"; -import LogService from "../service/LogService"; -import { DOT_AD_STATUS, DOT_AD_TYPE } from "../base/SDKEnum"; - -/* -* banner -*/ -export default class WxGrid { - private static instance: WxGrid; - static get I(): WxGrid { - return this.instance || (this.instance = new WxGrid(750, 750)); - } - private designWidth: number; - private bannerWidth: number; - private bannerHeight: number; - private bannerScale: number; - private bannerParams: any; - private _isErrored: boolean; - private adUnitId: string; - private gridAd: any; - private resolve: any; - private reject: any; - private AdList: object - - constructor(designWidth: number, bannerWidth: number) { - designWidth = designWidth || 750; - bannerWidth = bannerWidth || 750; - - this.AdList = {} - this.adUnitId = ''; - this._isErrored = false; - this.designWidth = designWidth; - this.bannerScale = WxSystem.I.winWidth / this.designWidth; - this.bannerWidth = Math.max(this.bannerScale * bannerWidth, 300); - this.bannerHeight = WxSystem.I.winHeight / this.bannerScale; - } - - get isErrored() { - return this._isErrored; - } - - create(key: string, adUnitId: string, opts?: { gridCount?: number; bannerWidth?: number, offsetY: number; adIntervals?: number }) { - this.bannerParams = opts || {}; - this.AdList[key] = { - ...opts, - adUnitId, - key, - isEnd: false, - queue: [] - } - if (opts && opts.bannerWidth) { - this.bannerWidth = opts.bannerWidth; - } - this.bannerParams.type = this.bannerParams.type || 1; - this.bannerParams.gridCount = this.bannerParams.gridCount || 5; - this.bannerParams.offsetY = -this.bannerParams.offsetY || 0; - this.bannerParams.adIntervals = this.bannerParams.adIntervals || 60; - return new Promise((resolve, reject) => { - this.resolve = resolve; - this.reject = reject; - this._isErrored = false; - this.adUnitId = adUnitId; - - if (SDKUtils.isEmpty(adUnitId)) - return this.reject({ ...BannerError.BannerInvalid, adUnitId: this.adUnitId }); - - // 设置样式(hack:修复qq版本) - this.bannerParams.type === 2 && (this.bannerWidth = WxSystem.I.winWidth); - let style = { top: 0, left: (WxSystem.I.winWidth - this.bannerWidth) / 2, width: this.bannerWidth }; - style = { - ...style, - top: 0 + this.bannerParams.offsetY - }; - if (this.AdList[key].gridAd) { - if (this.AdList[key].isOff) { - this.show(key) - } - resolve() - return - } - // 创建并判断是否存在 - this.gridAd = wx.createGridAd({ adUnitId, style: { left: style.left, top: this.bannerHeight }, gridCount: this.bannerParams.gridCount, }); - LogService.I.adStat('grid', adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.request) - if (!this.gridAd) - return this.reject({ ...BannerError.BannerNotOpen, adUnitId: this.adUnitId }); - this.AdList[key].gridAd = this.gridAd; - this.gridAd.onLoad(this.onLoad); - this.gridAd.onError(this.onError); - this.gridAd.onResize(this.onResize); - if (!this.AdList[key].isOff) { - this.show(key); - } - }); - } - - show(key: string): boolean { - if (this.AdList[key].gridAd) { - if (this.AdList[key].isEnd) { - let fun = (key) => { - WxGrid.I.show(key) - } - this.AdList[key].queue.push(fun); - return - } - this.AdList[key].isEnd = true; - - if (this.AdList[key].gridAd.style.realHeight) - this.AdList[key].gridAd.style.top = WxSystem.I.winHeight - this.AdList[key].gridAd.style.realHeight + this.AdList[key].offsetY; - if (this.bannerParams.type === 2) { - if (this.AdList[key].gridAd.style.width) - this.AdList[key].gridAd.style.left = (WxSystem.I.winWidth - this.AdList[key].gridAd.style.width) / 2; - else - this.AdList[key].gridAd.style.left = (WxSystem.I.winWidth - this.bannerWidth) / 2; - } else - this.AdList[key].gridAd.style.left = (WxSystem.I.winWidth - this.bannerWidth) / 2; - - LogService.I.adStat('grid', this.AdList[key].adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.show) - this.AdList[key].gridAd.show().catch((err: any) => this.handleShowError(err)); - this.onResize(); - __LOG__ && console.error('WxGrid - show: ' + this.adUnitId, this.AdList[key].gridAd.style); - WxGrid.I.handleQueue(key) - return true; - } - WxGrid.I.handleQueue(key) - return false; - } - - hide(key: string) { - if (this.AdList[key].gridAd) { - if (this.AdList[key].isEnd) { - let fun = (key) => { - WxGrid.I.hide(key) - } - this.AdList[key].queue.push(fun); - return - } - this.AdList[key].isEnd = true; - this.AdList[key].gridAd.style.left = -9999; - this.AdList[key].gridAd.hide(); - LogService.I.adStat('grid', this.AdList[key].adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.interrupt); - __LOG__ && console.error('WxGrid - hide: ' + this.adUnitId); - - WxGrid.I.handleQueue(key) - } - } - - destory(key: string) { - if (this.AdList[key] && this.AdList[key].gridAd) { - this.AdList[key].gridAd.style.left = -9999; - this.AdList[key].gridAd.destroy(); - this.AdList[key].gridAd = null; - LogService.I.adStat('grid', this.AdList[key].adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.interrupt) - __LOG__ && console.error('WxGrid - destory: ' + this.AdList[key].adUnitId); - } - } - - private onLoad() { - let that = WxGrid.I; - let gridAd = that.gridAd; - if (!gridAd) return; - // Platform.IsQQ && that.show(); - __LOG__ && console.error('WxGrid - onLoad: ' + that.adUnitId); - if (gridAd.style.realHeight) { - gridAd.style.top = WxSystem.I.winHeight - gridAd.style.realHeight + that.bannerParams.offsetY; - gridAd.style.left = (WxSystem.I.winWidth - gridAd.style.realWidth) / 2; - } - - that.unbind(); - that.resolve && that.resolve({ - adUnitId: that.adUnitId, - scale: that.bannerScale, - width: gridAd.style.realWidth / that.bannerScale, - height: gridAd.style.realHeight / that.bannerScale - }); - } - - private onError(err: any) { - __LOG__ && console.error('WxGrid - onError', err); - let that = WxGrid.I; - !that._isErrored && that.handleError(err, { ...BannerError.BannerFail, adUnitId: that.adUnitId }); - } - - private onResize() { - let that = WxGrid.I; - let gridAd = that.gridAd; - if (!gridAd) return; - gridAd.style.top = WxSystem.I.winHeight - gridAd.style.realHeight + that.bannerParams.offsetY; - gridAd.style.left = (WxSystem.I.winWidth - gridAd.style.realWidth) / 2; - } - - private handleShowError(ret: { errCode: number; errMsg: string }) { - __LOG__ && console.error('WxGrid - handleShowError', ret); - let that = WxGrid.I; - let { errCode, errMsg } = ret; - !that.isErrored && that.handleError(ret, { code: errCode, msg: errMsg }); - } - - private handleError(ret: any, err: any) { - let that = WxGrid.I; - that.unbind(); - if (that.gridAd) { - that.gridAd.destroy() - } - that.gridAd = null; - that._isErrored = true; - that.reject && that.reject({ ...err, adUnitId: that.adUnitId }); - LogService.I.adStat('grid', that.adUnitId, DOT_AD_TYPE.grid, DOT_AD_STATUS.fail) - __LOG__ && console.error('WxGrid - onError: ' + that.adUnitId); - } - - private unbind() { - if (this.gridAd) { - this.gridAd.offLoad(this.onLoad); - this.gridAd.offError(this.onError); - this.gridAd.offResize(this.onResize); - } - } - /** - * 处队列 - */ - handleQueue(key) { - let that = WxGrid.I; - if (that.AdList[key].queue.length > 0) { - that.AdList[key].isEnd = false; - let fn = that.AdList[key].queue.shift(); - fn(); - } else { - that.AdList[key].isEnd = false; - } - } -} \ No newline at end of file diff --git a/wxsdk/wx/WxInit.ts b/wxsdk/wx/WxInit.ts index 0ddac9b..0cd1c76 100644 --- a/wxsdk/wx/WxInit.ts +++ b/wxsdk/wx/WxInit.ts @@ -55,7 +55,7 @@ export default class WxInit { fromChannel = infoArr[0]; } // 设置抖音投放转化跟踪 - if(ad_id){ + if (ad_id) { fromChannel = ad_id; } @@ -85,7 +85,7 @@ export default class WxInit { .setCreativeId(creative_id) .setAdvertisterId(advertister_id) .setRequestId(request_id) - //cule_token=cule_token123&creative_id=creative_id213&ad_id=ad_id9999&request_id=request_id456&advertister_id=advertister_id9876 + //cule_token=cule_token123&creative_id=creative_id213&ad_id=ad_id9999&request_id=request_id456&advertister_id=advertister_id9876 // 设置网络类型 WxApi.I.getNetworkType() .then((ret: _NetworkTypeSuccessObject) => this.setNetworkType(ret)) diff --git a/wxsdk/wx/WxInterstitial.ts b/wxsdk/wx/WxInterstitial.ts index 8162f09..ab59ddd 100644 --- a/wxsdk/wx/WxInterstitial.ts +++ b/wxsdk/wx/WxInterstitial.ts @@ -18,33 +18,49 @@ export default class WxInterstitial { /** 显示插屏广告 */ public static showInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { - this.interstitialAdId = adUnitId; - if (!this.interstitialSuccell) { - this.createInterstitial(true); - return; - } - this.interstitialAd.show(); - LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.show) - setTimeout(() => { - this.createInterstitial(false); - }, 5000); + return new Promise(async (resolve, reject) => { + this.interstitialAdId = adUnitId; + if (!this.interstitialSuccell) { + this.createInterstitial(true, resolve); + return; + } + this.interstitialAd.show().then(suc => { + resolve && resolve({ code: 0, msg: "展示成功!" }); + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.show) + }).catch((err) => { + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.fail) + resolve && resolve({ code: err.errCode, msg: err.errMsg }); + }) + }) } /** 创建插屏广告 */ - private static createInterstitial(isShow: boolean = false) { + private static createInterstitial(isShow: boolean = false, resolve?) { LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.request) this.interstitialAd = WxApi.I.createInterstitialAd(this.interstitialAdId); - this.interstitialAd.onLoad(() => { + this.interstitialAd.onLoad(async () => { WxInterstitial.interstitialSuccell = true; LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.rt) if (isShow) { - this.interstitialAd.show(); - LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.show) + this.interstitialAd.show().then(suc => { + resolve && resolve({ code: 0, msg: "展示成功!" }); + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.show) + }).catch((err) => { + WxInterstitial.interstitialSuccell = false; + LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.fail) + resolve && resolve({ code: err.errCode, msg: err.errMsg }); + }) } }) this.interstitialAd.onError(err => { LogService.I.adStat('InterstitialAd', this.interstitialAdId, DOT_AD_TYPE.interstitial, DOT_AD_STATUS.fail) WxInterstitial.interstitialSuccell = false; + resolve && resolve({ code: err.errCode, msg: err.errMsg }); }) + + this.interstitialAd.onClose(call => { + this.createInterstitial(false); + }) + } } \ No newline at end of file diff --git a/wxsdk/wx/WxLogin.ts b/wxsdk/wx/WxLogin.ts index 9c269d8..15b04cd 100644 --- a/wxsdk/wx/WxLogin.ts +++ b/wxsdk/wx/WxLogin.ts @@ -16,7 +16,7 @@ export default class WxLogin { * tip1:如果是重新登录,需要把之前的token清空 * tip2:必须先调用wxLogin才能使用getUserInfo */ - async login(isAuthorize: boolean): Promise { + async login(isAuthorize: boolean): Promise { return new Promise(async (resolve, reject) => { let code = await WxApi.I.login(); if (isAuthorize) { @@ -94,7 +94,7 @@ export default class WxLogin { if (data) { // 设置登录信息 if (data.data) { - let { channel, uid, firstlogin, token, reftoken, openid, expire, isnew, gameconfig,logindays,amount,pon} = data.data; + let { channel, uid, firstlogin, token, reftoken, openid, expire, isnew, gameconfig, logindays, amount, pon } = data.data; LogService.I.setLogind({ channel, userId: uid, @@ -114,7 +114,11 @@ export default class WxLogin { this.isFirst = true; LogService.I.share(DataService.I.ShareKey, DataService.I.ShareId, DOT_SHARE_TYPE.click); } - this.handleExpire(expire) + try { //修改时间后会报错,未找到原因 + this.handleExpire(expire) + } catch (error) { + console.log("handleExpire_error", error) + } ShareVideoService.I.forward() } } diff --git a/wxsdk/wx/WxPay.ts b/wxsdk/wx/WxPay.ts index 0e58014..de30733 100644 --- a/wxsdk/wx/WxPay.ts +++ b/wxsdk/wx/WxPay.ts @@ -22,7 +22,7 @@ export default class WxPay { platform, offerId: OfferId, currencyType: CurrencyType, - buyQuantity: params.money/10, + buyQuantity: params.money / 10, zoneId: ZoneId }; console.log(pms); -- libgit2 0.21.0