Commit 3219d96596d726685cc77542a7aec37dd981c6af
1 parent
c2712cce
Exists in
master
and in
3 other branches
1.0.10。兼容抖音部分功能
Showing
9 changed files
with
263 additions
and
20 deletions
Show diff stats
wxsdk/WXSDK.ts
| @@ -36,13 +36,16 @@ export default class WXSDK { | @@ -36,13 +36,16 @@ export default class WXSDK { | ||
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | public static async init() { | 38 | public static async init() { |
| 39 | + // await platf | ||
| 39 | if (this.isWx) { | 40 | if (this.isWx) { |
| 40 | WxInit.I.init(); | 41 | WxInit.I.init(); |
| 41 | //视频预加载 启动预加载视频会闪屏 | 42 | //视频预加载 启动预加载视频会闪屏 |
| 42 | - setTimeout(() => { | ||
| 43 | - WxInterstitial.initInterstitialAd();//插屏预加载 | ||
| 44 | - ShareVideoService.I.preloadVideo(); | ||
| 45 | - }, 2000); | 43 | + if (cc.sys.platform === cc.sys.WECHAT_GAME) { |
| 44 | + setTimeout(() => { | ||
| 45 | + WxInterstitial.initInterstitialAd();//插屏预加载 | ||
| 46 | + ShareVideoService.I.preloadVideo(); | ||
| 47 | + }, 2000); | ||
| 48 | + } | ||
| 46 | } | 49 | } |
| 47 | DataService.I.updateTodayOnlineTime(); | 50 | DataService.I.updateTodayOnlineTime(); |
| 48 | await this.game.env(); | 51 | await this.game.env(); |
wxsdk/base/SDKConst.ts
| @@ -32,8 +32,10 @@ export const GAMEDATA = { | @@ -32,8 +32,10 @@ export const GAMEDATA = { | ||
| 32 | } | 32 | } |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | + | ||
| 36 | + | ||
| 35 | // sdk版本 | 37 | // sdk版本 |
| 36 | -export const SDKVersion = 'v1.0.9'; | 38 | +export const SDKVersion = 'v1.0.10'; |
| 37 | // 是否打印 | 39 | // 是否打印 |
| 38 | export const __LOG__ = false; | 40 | export const __LOG__ = false; |
| 39 | // 是否mock | 41 | // 是否mock |
| @@ -108,9 +110,10 @@ export const HostKeys = { | @@ -108,9 +110,10 @@ export const HostKeys = { | ||
| 108 | behavior: 'api/douyin/behavior', //抖音投放关键行为 | 110 | behavior: 'api/douyin/behavior', //抖音投放关键行为 |
| 109 | attribute: 'api/member/attribute/set', //修改用户属性 | 111 | attribute: 'api/member/attribute/set', //修改用户属性 |
| 110 | //订单 | 112 | //订单 |
| 111 | - orderReport: 'api/order/v2/mimas/new', //订单信息上报 | ||
| 112 | - orderQuery: 'api/order/query', //订单信息查询 | ||
| 113 | - preorder: 'api/order/v2/preorder', //获取支付方式 | 113 | + orderReport: 'api/order/v2/mimas/new', //订单信息上报 |
| 114 | + orderQuery: 'api/order/query', //订单信息查询 | ||
| 115 | + preorder: 'api/order/v2/preorder', //获取支付方式 | ||
| 116 | + bytedanceOrderReport: 'api/order/v2/bytedance', //字节订单信息上报 | ||
| 114 | // | 117 | // |
| 115 | goodslist: 'api/v2/goodslist', //商品列表 | 118 | goodslist: 'api/v2/goodslist', //商品列表 |
| 116 | }; | 119 | }; |
| @@ -0,0 +1,43 @@ | @@ -0,0 +1,43 @@ | ||
| 1 | +import { GAMEDATA } from "../base/SDKConst"; | ||
| 2 | + | ||
| 3 | +export default class DouyinInterstitial { | ||
| 4 | + /** 插屏广告ID */ | ||
| 5 | + private static interstitialAdId: string = GAMEDATA.interstitialAdId; | ||
| 6 | + /** 插屏广告实例 */ | ||
| 7 | + public static interstitialAd: any; | ||
| 8 | + /** 插屏广告是否加载完成 */ | ||
| 9 | + private static interstitialSuccell: boolean = false; | ||
| 10 | + /** 初始化插屏广告 */ | ||
| 11 | + public static initInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { | ||
| 12 | + // this.interstitialAdId = adUnitId; | ||
| 13 | + // this.createInterstitial(); | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + private static canReTry = false; | ||
| 17 | + /** 显示插屏广告 */ | ||
| 18 | + public static showInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) { | ||
| 19 | + const interstitiaAd = tt.createInterstitialAd({ adUnitId: this.interstitialAdId }); | ||
| 20 | + this.canReTry = true; | ||
| 21 | + let onLoadHandle = ()=> { | ||
| 22 | + console.log("插屏广告onload成功") | ||
| 23 | + interstitiaAd.show().then(() => { | ||
| 24 | + console.log("插屏广告展示成功"); | ||
| 25 | + }); | ||
| 26 | + } | ||
| 27 | + interstitiaAd.onLoad(onLoadHandle); //创建会自动load | ||
| 28 | + | ||
| 29 | + let onErrorHandle = (err)=>{ | ||
| 30 | + // 这里要等待一定时间后,或者等待下次需要展示的时候,参考频控,尝试一次,或者几次,不能一直尝试。 | ||
| 31 | + if (this.canReTry) { | ||
| 32 | + this.canReTry = false; | ||
| 33 | + interstitiaAd.load(); //如果需要,这里等待一定时间后,或者等待下次需要展示的时候,再次 load->onLoad-> show。 | ||
| 34 | + } else { | ||
| 35 | + tt.showToast({ | ||
| 36 | + title: "暂无广告", | ||
| 37 | + icon: "none", | ||
| 38 | + }); | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + interstitiaAd.onError(onErrorHandle); // 自动load 的失败会走到这里 | ||
| 42 | + } | ||
| 43 | +} | ||
| 0 | \ No newline at end of file | 44 | \ No newline at end of file |
| @@ -0,0 +1,175 @@ | @@ -0,0 +1,175 @@ | ||
| 1 | +import { GAMEDATA } from '../base/SDKConst'; | ||
| 2 | +import { EnvCode } from '../base/SDKEnum'; | ||
| 3 | +import { SDKApi } from '../http/SDKApi'; | ||
| 4 | +import DataService from '../service/DataService'; | ||
| 5 | +import WxSystem from '../wx/WxSystem'; | ||
| 6 | + | ||
| 7 | +export default class DouyinPay { | ||
| 8 | + /** | ||
| 9 | + * 发起支付 | ||
| 10 | + */ | ||
| 11 | + pay(params: { payid: string, goodid: string, money: number; orderid: string }, opts: any = {}): Promise<any> { | ||
| 12 | + return new Promise((resolve, reject) => { | ||
| 13 | + let { Mode, OfferId, ZoneId, CurrencyType, Platform } = GAMEDATA.MidasPay; | ||
| 14 | + // 环境配置,0:米大师正式环境 1:米大师沙箱环境 | ||
| 15 | + let env = DataService.I.EnvEnum === EnvCode.Prod ? 0 : 1; | ||
| 16 | + // 平台类型,android or ios,config.js有配置走配置,否则判断系统id | ||
| 17 | + let platform = DataService.I.Platform; | ||
| 18 | + let pms = { | ||
| 19 | + mode: Mode, | ||
| 20 | + env, | ||
| 21 | + platform, | ||
| 22 | + offerId: OfferId, | ||
| 23 | + currencyType: CurrencyType, | ||
| 24 | + buyQuantity: params.money / 10, | ||
| 25 | + zoneId: ZoneId | ||
| 26 | + }; | ||
| 27 | + let extend = ""; | ||
| 28 | + for (let key in opts) { | ||
| 29 | + extend += (`${key}=${opts[key]}&`) | ||
| 30 | + } | ||
| 31 | + extend = extend.substring(0, extend.length - 1); | ||
| 32 | + extend = encodeURIComponent(extend); | ||
| 33 | + | ||
| 34 | + let payParams = { | ||
| 35 | + ...this.buildParams(), | ||
| 36 | + extend, | ||
| 37 | + orderid: params.orderid, | ||
| 38 | + // payid: params.payid, | ||
| 39 | + goodid: params.goodid, | ||
| 40 | + money: params.money, | ||
| 41 | + platform | ||
| 42 | + }; | ||
| 43 | + console.log("payParams", payParams); | ||
| 44 | + SDKApi.bytedanceOrderReport(payParams).then(res => { | ||
| 45 | + console.log("res", res); | ||
| 46 | + if (!res.code) { | ||
| 47 | + let { mode, env, currencyType, platform, buyQuantity, zoneId, customId, extraInfo } = res.data; | ||
| 48 | + tt.requestGamePayment({ | ||
| 49 | + mode, // 支付类型 | ||
| 50 | + env, //支付环境 | ||
| 51 | + currencyType, // 币种:目前仅为 "CNY" | ||
| 52 | + platform, // 申请接入时的平台:目前仅为"android" | ||
| 53 | + buyQuantity, // 购买数量,必须满足:金币数量*金币单价 = 限定价格等级(详见金币限定等级) | ||
| 54 | + zoneId, | ||
| 55 | + customId,//开发者自定义唯一订单号。如不填,支付结果回调将不包含此字段,将导致游戏开发者无法发放游戏道具, 基础库版本低于1.55.0没有此字段 | ||
| 56 | + extraInfo,//extraInfo要转成字符串 | ||
| 57 | + success(res) { | ||
| 58 | + // console.log("调用函数成功", res); | ||
| 59 | + resolve({ code: 0, msg: '支付成功!' }) | ||
| 60 | + }, | ||
| 61 | + fail(err) { | ||
| 62 | + // console.log("调用函数失败", res); | ||
| 63 | + resolve({ code: err.errCode, msg: '支付失败' }); | ||
| 64 | + }, | ||
| 65 | + complete(res) { | ||
| 66 | + // console.log("调用完成", res); | ||
| 67 | + }, | ||
| 68 | + }) | ||
| 69 | + // tt.requestGamePayment(pms) | ||
| 70 | + // .then(() => this.handlePaySuccess({ ...params, platform }, opts, resolve, reject)) | ||
| 71 | + // .catch(err => this.handlePayError({ ...params, platform }, opts, err, reject)); | ||
| 72 | + } else { | ||
| 73 | + reject(res); | ||
| 74 | + } | ||
| 75 | + }) | ||
| 76 | + }); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + private async handlePaySuccess(data: { payid: string, goodid: string, money: number; orderid: string, platform: string }, opts: any, resolve: any, reject: any, t = 1000) { | ||
| 80 | + // let code = await WxApi.I.login(); | ||
| 81 | + | ||
| 82 | + let extend = ""; | ||
| 83 | + for (let key in opts) { | ||
| 84 | + extend += (`${key}=${opts[key]}&`) | ||
| 85 | + } | ||
| 86 | + extend = extend.substring(0, extend.length - 1); | ||
| 87 | + extend = encodeURIComponent(extend); | ||
| 88 | + let params = { | ||
| 89 | + ...this.buildParams(), | ||
| 90 | + extend, | ||
| 91 | + midasenv: DataService.I.EnvEnum === EnvCode.Prod ? 0 : 1, | ||
| 92 | + orderid: data.orderid, | ||
| 93 | + payid: data.payid, goodid: data.goodid, | ||
| 94 | + type: 1, | ||
| 95 | + money: data.money, | ||
| 96 | + platform: data.platform | ||
| 97 | + }; | ||
| 98 | + var that = this; | ||
| 99 | + SDKApi.bytedanceOrderReport(params) | ||
| 100 | + .then(res => { | ||
| 101 | + if (!res.code) { | ||
| 102 | + resolve(res); | ||
| 103 | + } else { | ||
| 104 | + t = Math.min(t * 2, 60000) | ||
| 105 | + // console.error("上报失败,重新上报", t) | ||
| 106 | + setTimeout(() => { | ||
| 107 | + that.handlePaySuccess(data, opts, resolve, reject, t) | ||
| 108 | + }, t); | ||
| 109 | + } | ||
| 110 | + }) | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + private handlePayError(data: { payid: string, goodid: string, money: number; orderid: string, platform: string }, opts, err: any, reject: any) { | ||
| 114 | + console.log("支付失败", JSON.stringify(err)); | ||
| 115 | + let type = 2; | ||
| 116 | + if (err.errCode + '' == '1') { | ||
| 117 | + type = 3; | ||
| 118 | + } | ||
| 119 | + let extend = ""; | ||
| 120 | + for (let key in opts) { | ||
| 121 | + extend += (`${key}=${opts[key]}&`) | ||
| 122 | + } | ||
| 123 | + extend = extend.substring(0, extend.length - 1); | ||
| 124 | + extend = encodeURIComponent(extend); | ||
| 125 | + let params = { | ||
| 126 | + ...this.buildParams(), | ||
| 127 | + extend, | ||
| 128 | + midasenv: DataService.I.EnvEnum === EnvCode.Prod ? 0 : 1, | ||
| 129 | + orderid: data.orderid, | ||
| 130 | + payid: data.payid, goodid: data.goodid, | ||
| 131 | + type, | ||
| 132 | + money: data.money, | ||
| 133 | + platform: data.platform | ||
| 134 | + }; | ||
| 135 | + SDKApi.bytedanceOrderReport(params) | ||
| 136 | + reject(err); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * 构建支付公用参数 | ||
| 141 | + */ | ||
| 142 | + private buildParams() { | ||
| 143 | + let gameid = GAMEDATA.game_id; | ||
| 144 | + let channel = DataService.I.ChannelId; | ||
| 145 | + let openid = DataService.I.OpenId; | ||
| 146 | + let brand = WxSystem.I.brand; | ||
| 147 | + let model = WxSystem.I.model; | ||
| 148 | + let version = WxSystem.I.version; | ||
| 149 | + let system = WxSystem.I.system; | ||
| 150 | + let sdkversion = WxSystem.I.SDKVersion; | ||
| 151 | + let scene = DataService.I.Scene + ''; | ||
| 152 | + let uid = DataService.I.UserId; | ||
| 153 | + let token = DataService.I.Token; | ||
| 154 | + let env = DataService.I.EnvEnum === 1 ? 'pre' : 'prod'; | ||
| 155 | + return { | ||
| 156 | + gameid, | ||
| 157 | + openid, | ||
| 158 | + channel, | ||
| 159 | + brand, | ||
| 160 | + model, | ||
| 161 | + version, | ||
| 162 | + system, | ||
| 163 | + sdkversion, | ||
| 164 | + scene, | ||
| 165 | + uid, | ||
| 166 | + token, | ||
| 167 | + env | ||
| 168 | + }; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + private static _instance: DouyinPay; | ||
| 172 | + static get I(): DouyinPay { | ||
| 173 | + return this._instance || (this._instance = new DouyinPay); | ||
| 174 | + } | ||
| 175 | +} | ||
| 0 | \ No newline at end of file | 176 | \ No newline at end of file |
wxsdk/http/SDKApi.ts
| @@ -73,6 +73,7 @@ export class SDKApi { | @@ -73,6 +73,7 @@ export class SDKApi { | ||
| 73 | 73 | ||
| 74 | // OrderApi | 74 | // OrderApi |
| 75 | public static pay = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.orderReport, ...args); | 75 | public static pay = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.orderReport, ...args); |
| 76 | + public static bytedanceOrderReport = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.bytedanceOrderReport, ...args); | ||
| 76 | public static orderQuery = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.orderQuery, ...args); | 77 | public static orderQuery = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.orderQuery, ...args); |
| 77 | public static preorder = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.preorder, ...args); | 78 | public static preorder = (...args) => SDKHttp.httpPost(DataService.I.OrderApi, HostKeys.preorder, ...args); |
| 78 | 79 |
wxsdk/service/AdService.ts
| @@ -4,6 +4,7 @@ import WxCustom from "../wx/WxCustom"; | @@ -4,6 +4,7 @@ import WxCustom from "../wx/WxCustom"; | ||
| 4 | import WxInterstitial from "../wx/WxInterstitial"; | 4 | import WxInterstitial from "../wx/WxInterstitial"; |
| 5 | import { GAMEDATA } from "../base/SDKConst"; | 5 | import { GAMEDATA } from "../base/SDKConst"; |
| 6 | import LogService from "./LogService"; | 6 | import LogService from "./LogService"; |
| 7 | +import DouyinInterstitial from "../douyin/DouyinInterstitial"; | ||
| 7 | 8 | ||
| 8 | 9 | ||
| 9 | 10 | ||
| @@ -43,7 +44,11 @@ export default class AdService { | @@ -43,7 +44,11 @@ export default class AdService { | ||
| 43 | * @param adUnitId | 44 | * @param adUnitId |
| 44 | */ | 45 | */ |
| 45 | createInterstitialAd(adUnitId: string) { | 46 | createInterstitialAd(adUnitId: string) { |
| 46 | - return WxInterstitial.showInterstitialAd(adUnitId) | 47 | + if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) { |
| 48 | + return DouyinInterstitial.showInterstitialAd(adUnitId); | ||
| 49 | + } else { | ||
| 50 | + return WxInterstitial.showInterstitialAd(adUnitId) | ||
| 51 | + } | ||
| 47 | } | 52 | } |
| 48 | 53 | ||
| 49 | /** | 54 | /** |
wxsdk/service/GameService.ts
| @@ -5,6 +5,7 @@ import WxLogin from "../wx/WxLogin"; | @@ -5,6 +5,7 @@ import WxLogin from "../wx/WxLogin"; | ||
| 5 | import WxApi from "../wx/WxApi"; | 5 | import WxApi from "../wx/WxApi"; |
| 6 | import WxPay from "../wx/WxPay"; | 6 | import WxPay from "../wx/WxPay"; |
| 7 | import WxSystem from "../wx/WxSystem"; | 7 | import WxSystem from "../wx/WxSystem"; |
| 8 | +import DouyinPay from "../douyin/DouyinPay"; | ||
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | export default class GameService { | 11 | export default class GameService { |
| @@ -99,7 +100,11 @@ export default class GameService { | @@ -99,7 +100,11 @@ export default class GameService { | ||
| 99 | * @returns | 100 | * @returns |
| 100 | */ | 101 | */ |
| 101 | pay(params: { payid: string, goodid: string, money: number; orderid: string }, opts: any = {}) { | 102 | pay(params: { payid: string, goodid: string, money: number; orderid: string }, opts: any = {}) { |
| 102 | - return WxPay.I.pay(params, opts); | 103 | + if (cc.sys.platform === cc.sys.BYTEDANCE_GAME) { |
| 104 | + return DouyinPay.I.pay(params, opts); | ||
| 105 | + } else { | ||
| 106 | + return WxPay.I.pay(params, opts); | ||
| 107 | + } | ||
| 103 | } | 108 | } |
| 104 | /** | 109 | /** |
| 105 | * 订单查询 | 110 | * 订单查询 |
wxsdk/service/ShareVideoService.ts
| @@ -33,9 +33,12 @@ export default class ShareVideoService { | @@ -33,9 +33,12 @@ export default class ShareVideoService { | ||
| 33 | // console.log(JSON.stringify(data)) | 33 | // console.log(JSON.stringify(data)) |
| 34 | SDKShare.I.updateShareMenu(true);//打开群分享 | 34 | SDKShare.I.updateShareMenu(true);//打开群分享 |
| 35 | 35 | ||
| 36 | - if (GAMEDATA.shareMessageToFriend.scene > 0) { | ||
| 37 | - wx.setMessageToFriendQuery({ shareMessageToFriendScene: GAMEDATA.shareMessageToFriend.scene }) | 36 | + if (cc.sys.platform === cc.sys.WECHAT_GAME) { |
| 37 | + if (GAMEDATA.shareMessageToFriend.scene > 0) { | ||
| 38 | + wx.setMessageToFriendQuery({ shareMessageToFriendScene: GAMEDATA.shareMessageToFriend.scene }) | ||
| 39 | + } | ||
| 38 | } | 40 | } |
| 41 | + | ||
| 39 | if (this.forwardKey) this.forward(this.forwardKey); | 42 | if (this.forwardKey) this.forward(this.forwardKey); |
| 40 | } | 43 | } |
| 41 | 44 |
wxsdk/share/SDKVideo.ts
| @@ -55,7 +55,7 @@ export default class SDKVideo { | @@ -55,7 +55,7 @@ export default class SDKVideo { | ||
| 55 | that.isPreload = false | 55 | that.isPreload = false |
| 56 | __LOG__ && console.warn("视频预加载成功", that.preloadVideoAd) | 56 | __LOG__ && console.warn("视频预加载成功", that.preloadVideoAd) |
| 57 | // that.preloadVideoAd.ttttttt = '111111' | 57 | // that.preloadVideoAd.ttttttt = '111111' |
| 58 | - console.warn(that.preloadVideoAd.isReady()); | 58 | + // console.warn(that.preloadVideoAd.isReady()); |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | offPreload() { | 61 | offPreload() { |
| @@ -137,17 +137,22 @@ export default class SDKVideo { | @@ -137,17 +137,22 @@ export default class SDKVideo { | ||
| 137 | private async handleLoaded() { | 137 | private async handleLoaded() { |
| 138 | let that = SDKVideo.I; | 138 | let that = SDKVideo.I; |
| 139 | try { | 139 | try { |
| 140 | - __LOG__ && console.warn("handleLoaded2", that.videoAd, that.videoAd.isReady()); | 140 | + // __LOG__ && console.warn("handleLoaded2", that.videoAd, that.videoAd.isReady()); |
| 141 | LogService.I.adStat(this.videoKey, this.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.show) | 141 | LogService.I.adStat(this.videoKey, this.adUnitId, DOT_AD_TYPE.video, DOT_AD_STATUS.show) |
| 142 | 142 | ||
| 143 | - if (that.videoAd.isReady()) { | 143 | + if (cc.sys.platform === cc.sys.WECHAT_GAME) { |
| 144 | + if (that.videoAd.isReady()) {//抖音没有 isReady | ||
| 145 | + that.show_time = Date.now(); | ||
| 146 | + await that.videoAd.show(); | ||
| 147 | + } else { | ||
| 148 | + that.videoAd.load().then(() => { | ||
| 149 | + that.show_time = Date.now(); | ||
| 150 | + that.videoAd.show(); | ||
| 151 | + }).catch(that.handleError) | ||
| 152 | + } | ||
| 153 | + } else { | ||
| 144 | that.show_time = Date.now(); | 154 | that.show_time = Date.now(); |
| 145 | await that.videoAd.show(); | 155 | await that.videoAd.show(); |
| 146 | - } else { | ||
| 147 | - that.videoAd.load().then(() => { | ||
| 148 | - that.show_time = Date.now(); | ||
| 149 | - that.videoAd.show(); | ||
| 150 | - }).catch(that.handleError) | ||
| 151 | } | 156 | } |
| 152 | __LOG__ && console.log("handleLoaded_show") | 157 | __LOG__ && console.log("handleLoaded_show") |
| 153 | //打点 | 158 | //打点 |