SDKTools.ts
8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { GAMEDATA } from "../wxsdk/base/SDKConst";
import DateUtils from "../wxsdk/utils/DateUtils";
import WXSDK from "../wxsdk/WXSDK";
/*
* SDK工具类库;
*/
export class SDKTools {
static get isWx() {
return typeof wx !== 'undefined';
}
static get isSupported() {
return typeof WXSDK === 'object';
}
static get envNum() {
return WXSDK.data.envEnum;
}
static get data() {
return WXSDK.data;
}
static get scene() {
return WXSDK.data.scene;
}
static get uid() {
return WXSDK.data.userId;
}
static get channel() {
return WXSDK.data.channelId;
}
static get openId() {
return WXSDK.data.openId;
}
// 是否当天注册用户
static get isnew() {
try {
let t = DateUtils.today;
return WXSDK.data.regTime === t;
} catch (error) {
return false;
}
}
// 是否首次登录
static get isFirstLogin() {
try {
return WXSDK.data.isnew === 1;
} catch (error) {
return false;
}
}
// 是否是某个时间点后注册的
static isAfterTime(t) {
try {
let t1 = new Date(t).getTime();
let t2 = new Date(`${WXSDK.data.regTime} 00:00:00`).getTime();
// console.log("t1t2", t1, t2)
return t2 >= t1;
} catch (error) {
return false;
}
}
/**
* banner
* @param adUnitId bannerID
* @param opts { //非必填 默认满屏居低
* type:默认1 banner
* bannerWidth:
* offsetY: 距离底部多远
* isOff:是否关闭默认显示banner
* }
*/
static createBanner(adUnitId: string = GAMEDATA.bannerId, opts?: { type?: number; bannerWidth?: number, offsetY?: number; adIntervals?: number, isOff?: boolean }) {
if (!this.isWx) return
return WXSDK.ad.createBanner(adUnitId, opts);
}
/**
* banner 显示 ps:创建默认显示
*/
static showBanner() {
if (!this.isWx) return
WXSDK.ad.showBanner()
}
/**
* banner 隐藏
*/
static hideBanner() {
if (!this.isWx) return
WXSDK.ad.hideBanner();
}
/**
* banner 销毁
*/
static destoryBanner() {
if (!this.isWx) return
WXSDK.ad.destoryBanner();
}
/**
* 插屏
*/
static createInterstitialAd(adUnitId: string = GAMEDATA.interstitialAdId) {
if (!this.isWx) 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
* @param opts { //非必填 默认满屏居低
* bannerWidth:
* offsetY: 距离底部多远
* }
*/
static createCustom(adUnitId: string = GAMEDATA.customId, opts: { top: number; left: number; adIntervals?: number }) {
if (!this.isWx) return
return WXSDK.ad.createCustom(adUnitId, opts);
}
/**
* 原生广告 显示 ps:创建默认显示
*/
static showCustom() {
if (!this.isWx) return
WXSDK.ad.showCustom()
}
/**
* 原生广告 隐藏
*/
static hideCustom() {
if (!this.isWx) return
WXSDK.ad.hideCustom()
}
/**
* 原生广告 销毁
*/
static destoryCustom() {
if (!this.isWx) return
WXSDK.ad.destoryCustom()
}
/**
*
* @param data 游戏数据
* @param type 游戏类型
* @param opts 扩展参数
*/
static navigateToMiniProgram(data: IGameList, type, opts?) {
if (!this.isWx) return
return WXSDK.ad.navigateToMiniProgram(data, type, opts)
}
/**
* 订阅
* @param template_ids 模板id eg['aaaaaaa','bbbbbbb']
* @param ids 对应后台的id eg:['1','2']
*/
static subScribe(template_ids: Array<string>, ids: Array<string>) {
if (!this.isWx) return
return WXSDK.game.subScribe(template_ids, ids)
}
/**
* 登录,sdk已实现弱登录和强登录的判断
*/
static login(isAuthorize?: boolean): Promise<any> {
return WXSDK.game.login(isAuthorize);
}
/**
* 保存数据
* @param key
* @param value
*/
static saveData(key: string, value: string) {
return WXSDK.game.saveData(key, value)
}
/**
* 获取数据
* @param key
*/
static getData(key: string) {
return WXSDK.game.getData(key)
}
/**
* 添加排行榜 2021-5-17 废弃新游戏勿用
*
* @param key 获取排行时掉对应key即可获取对应的排行
* @param value 值
*/
static rankAdd(key: string, value: number) {
return WXSDK.game.rankAdd(key, value)
}
/**
* 添加排行榜扩展数据
* @param value 对象 扩展字段 段位 等级等等...
*/
static updateRankData(value: Object = {}) {
let val = JSON.stringify(value)
return WXSDK.game.saveData('rankData', val)
}
/**
* 排行榜数据 废弃新游戏勿用
* type:1 日榜 2周榜 3月榜
* isRankData: 是否有扩展参数
*/
static rankList(key: string, type: number = 1, isRankData: boolean = false): Promise<IResult<any>> {
if (!this.isWx) return Promise.resolve({ code: -1 })
return WXSDK.game.rankList(key, type, isRankData);
}
/**
* 添加排行榜 2021-5-17 新增
*
* @param key 获取排行时掉对应key即可获取对应的排行
* @param value 值
* @param type 0永久排行1日排行2周排行3月排行,默认为0
* @param sort 1 大到小 2小到大
*/
static totalrankAdd(key: string, value: number, type: number = 0, sort: number = 1) {
return WXSDK.game.totalrankAdd(key, value, type, sort);
}
/**
* 排行榜数据
* type:0永久榜单 1 日榜 2周榜 3月榜
* isRankData: 是否有扩展参数
* @param sort 1 大到小 2小到大
*/
static totalrankList(key: string, type: number = 0, isRankData: boolean = false, sort: number = 1): Promise<IResult<any>> {
if (!this.isWx) return Promise.resolve({ code: -1 });
return WXSDK.game.totalrankList(key, type, isRankData, sort);
}
/**
* 互导数据
* 1抽屉广告2猜你喜欢3格子广告4试玩
*/
static adList(key: number): Promise<IResult<any>> {
if (!this.isWx) return Promise.resolve({ code: -1 })
return WXSDK.game.adList(key);
}
/**
* 添加曝光
* @param type 1,抽屉 2 banner(猜你喜欢)3格子4猜你喜欢
* @param arr
*/
static addExposure(type, arr: Array<IGameList>) {
if (!this.isWx) return Promise.resolve({ code: -1 })
return WXSDK.stat.addExposure(type, arr)
}
/**
* 初始化在线参数,初始化游戏调用
*/
static updateOnlineParams() {
return WXSDK.online.updateOnlineConfig();
}
static getParamsInt(key: OnlineKeys | string, defaultVal: number = 0) {
return WXSDK.online.getParamsInt(key.toString(), defaultVal)
}
static getParamsObj(key: OnlineKeys | string, defaultVal: any = {}) {
return WXSDK.online.getParamsObj(key.toString(), defaultVal)
}
static getParamsString(key: OnlineKeys | string, defaultVal: string = '') {
return WXSDK.online.getParamsString(key.toString(), defaultVal)
}
/**
* 支付
* @param params
* @param opts
* @returns
*/
static pay(params: {payid:string, goodid:string, money: number; orderid: string }, opts: any = {}) {
return WXSDK.game.pay(params, opts)
}
/**
* 检测支付是否成功
*/
static orderQuery(source: string) {
return WXSDK.game.orderQuery(source)
}
/**
* 获取支付方式
* @param goodid 商品id
* @param orderid 订单号
* @returns
*/
static preorder(goodid:string,orderid:string) {
return WXSDK.game.preorder(goodid,orderid)
}
/**
* 用户创角
* @param role_name 角色名称
* @param region 区服
*/
static role(role_name: string, region: string) {
return WXSDK.stat.role(role_name, region)
}
}
/**
* 中台自定义配置表
*/
export enum OnlineKeys {
isOpenInterstitialAdId = 'isOpenInterstitialAdId',
shareRandom = 'shareRandom',
switch_draw = 'switch_draw',
switch_banner = 'switch_banner',
switch_grid = 'switch_grid',
calendar_level_max = 'calendar_level_max',
over_show_ad = 'over_show_ad'
}