index.ts
8.37 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
import { SDKBase, TimeManager } from "simba-sdk";
import { LaunchOptionType } from "simba-sdk/dist/SDKTypes";
import { setNoRewaredAd } from "simba-sdk/dist/SVConfig";
import { SDKConfig } from "simba-sdk/dist/SDKBase";
function compareVersion(v1: string, v2: string) {
let vv1 = v1.split('.');
let vv2 = v2.split('.');
var len = Math.max(vv1.length, vv2.length)
while (vv1.length < len) {
vv1.push('0');
}
while (vv2.length < len) {
vv2.push('0');
}
for (var i = 0; i < len; i++) {
var num1 = parseInt(vv1[i]);
var num2 = parseInt(vv2[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
export default class SDKImpl extends SDKBase {
static readonly channel: string = "android";
private _loginResolve: Function
private _loginReject: (reason?: any) => void;
private _showRewardedVideoAdresolve: Function;
getChannel(): string {
return SDKImpl.channel;
}
async init() {
globalThis.SDKImpl = this; //提供给java调用
let androidConfig = SDKConfig.channelConfig.android;
if (androidConfig) {
if (androidConfig.appId) {
this.initCSJADSDK(androidConfig.appId);
}
if (androidConfig.extraParam.appId) {
this.initYLHADSDK(androidConfig.extraParam.appId);
}
setTimeout(() => {
if (androidConfig.rewaredVideoAdId) {
this.loadCSJRewardVideoAd(androidConfig.rewaredVideoAdId);
}
if (androidConfig.bannerAdId) {
this.loadCSJBannerAd(androidConfig.bannerAdId);
}
if (androidConfig.interstitialAdId) {
this.loadCSJInterstitialAd(androidConfig.interstitialAdId);
}
if (androidConfig.extraParam.rewaredVideoAdId) {
this.loadYLHRewardVideoAd(androidConfig.extraParam.rewaredVideoAdId);
}
if (androidConfig.extraParam.bannerAdId) {
this.loadYLHBannerAd(androidConfig.extraParam.bannerAdId);
}
if (androidConfig.extraParam.rewaredVideoAdId) {
this.loadYLHInterstitialAd(androidConfig.extraParam.rewaredVideoAdId);
}
}, 5 * 1000);
} else {
setNoRewaredAd();
}
return super.init().then((value) => {
return value;
});
}
async login(extraParams, timeout = 5000) {
let result = await this._login2();
try {
let resultInfo = JSON.parse(result);
this.loginInfo = {
channelAccessToken: resultInfo.code,
localServerPublicServiceToken: resultInfo.commonToken,
localServerRecordServiceToken: resultInfo.recordToken ? resultInfo.recordToken : resultInfo.commonToken,
localServerRefreshToken: resultInfo.refreshToken,
localServerSubject: resultInfo.subject,
playerId: resultInfo.subject.split("_")[1]
};
await this.downloadRecord();
await this.timeManagerOnLogin();
return Object.assign({}, this.loginInfo);
} catch (error) {
console.error("loginLocalServer", error);
}
}
protected _login2(): Promise<string> {
return new Promise((resolve, reject) => {
this._loginResolve = resolve;
this._loginReject = reject;
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "login", "()V");
});
}
overLogin(str) {
if (str == "fail") {
this._loginReject(str);
} else {
this._loginResolve(str);
}
}
protected _login(): Promise<{ code: string }> {
return new Promise((resolve, reject) => {
let timeout = false;
let timer = setTimeout(() => {
timeout = true;
reject("timeout");
}, 2000);
});
}
exit(): void {
}
initCSJADSDK(appId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'initCSJADSDK', "(Ljava/lang/String;)V", appId);
}
loadCSJRewardVideoAd(rewardVideoAdId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'loadCSJRewardVideoAd', "(Ljava/lang/String;)V", rewardVideoAdId);
}
loadCSJBannerAd(bannerAdId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'loadCSJBannerAd', "(Ljava/lang/String;)V", bannerAdId);
}
loadCSJInterstitialAd(interstitialAdId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'loadCSJInterstitialAd', "(Ljava/lang/String;)V", interstitialAdId);
}
initYLHADSDK(appId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'initYLHADSDK', "(Ljava/lang/String;)V", appId);
}
loadYLHRewardVideoAd(rewardVideoAdId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'loadYLHRewardVideoAd', "(Ljava/lang/String;)V", rewardVideoAdId);
}
loadYLHBannerAd(bannerAdId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'loadYLHBannerAd', "(Ljava/lang/String;)V", bannerAdId);
}
loadYLHInterstitialAd(interstitialAdId: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'loadYLHInterstitialAd', "(Ljava/lang/String;)V", interstitialAdId);
}
showRewardedVideoAd(): Promise<{ result: "complete" | "cancel" | "fail" }> {
return new Promise((resolve) => {
this._showRewardedVideoAdresolve = resolve;
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'showRewardVideoAd', "()V");
});
}
overShowRewardedVideoAd(str) {
let result = JSON.parse(str);
this._showRewardedVideoAdresolve(result);
}
showInterstitialAd(): Promise<boolean> {
return new Promise((resolve) => {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'showInterstitialAd', "()V");
});
}
showBannerAd(style: { left: number; top: number; width: number; height: number; anchorX?: number, anchorY?: number }, refreshInterval?: number): Promise<boolean> {
return new Promise(async (resolve) => {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'showBannerAd', "()V");
});
}
hideBannerAd() {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'hideBannerAd', "()V");
}
shareAppMessage(info: { title?: string, imageUrl?: string, query?: string, imageUrlId?: string }): Promise<boolean> {
return new Promise((resolve) => {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'shareAppMessage', "(Ljava/lang/String;Ljava/lang/String;)V", info.title, info.imageUrl);
setTimeout(() => {
resolve(true);
}, 2 * 1000);
});
}
sharePicture(localUrl: string) {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'sharePicture', "(Ljava/lang/String;)V", localUrl);
}
setClipboardData(data: string) {
return new Promise<void>((resolve, reject) => {
jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'setClipboardData', "(Ljava/lang/String;)V", data);
resolve();
})
}
onBannerResize = (res: { width: number, height: number }) => { }
hideGameClubButton() { }
showGameClubButton() { }
createGameClubButton(obj): boolean { return false; }
destroyGameClubButton() { }
openProgram(appId: string, extraData?: object): Promise<void> {
return new Promise((resolve, reject) => { });
}
openCustomerServiceConversation(param: {
sessionFrom?: string,
showMessageCard?: boolean,
sendMessageTitle?: string,
sendMessagePath?: string,
sendMessageImg?: string
}): Promise<boolean> {
return new Promise((resolve) => { });
}
getLaunchOptions(): LaunchOptionType {
return {};
}
checkSession(): Promise<boolean> {
return Promise.resolve(true);
}
}