WxHelper.ts
15.1 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// import UserManager from "../manager/UserManager";
import { Node, UITransform, game, view } from "cc";
import { ALIPAY } from "cc/env";
/** WXapi */
export class WxHelper {
/** 手机设备信息 */
public static SystemInfo: SystemInfoSyncReturnValue;
/** 微信用户信息 */
public static userInfo: WxUserInfo;
static get isWx() {
return typeof wx !== 'undefined';
}
/** 获取设备信息 */
public static getSystemInfo() {
if (this.isWx) this.SystemInfo = wx.getSystemInfoSync();
}
/** 像开放数据域发送信息 */
public static postMessage(eventType: string, data?: any): void {
if (!this.isWx || !this.checkVerison('1.9.92')) return;
wx.getOpenDataContext().postMessage({
eventType: eventType,
data: data
});
}
/** 更新微信开放数据榜单数据 */
public static updateWxRank(key: string, value: number) {
if (typeof my != 'undefined') {
return;
}
if (!this.isWx || !this.checkVerison('1.9.92')) return;
let data = {
value,
}
wx.setUserCloudStorage({
KVDataList: [{
key,
value: JSON.stringify(data)
}]
})
}
/** 创建用户反馈按钮 */
public static createFeedbackButton(target: Node) {
let data = this.computWxPosition(target);
return wx.createFeedbackButton({
type: 'text',
text: ' ',
style: {
left: data.left,
top: data.top,
width: data.width,
height: data.height,
borderRadius: 5,
// backgroundColor: '#69c66d',
}
})
}
/** 获取用户信息按钮 */
public static userInfoButton: any;
/** 创建一个获取用户信息按钮 */
public static createUserInfoButton(target: Node) {
let data = this.computWxPosition(target);
// console.log("data", data)
return wx.createUserInfoButton({
type: 'text',
text: ' ',
style: {
left: data.left,
top: data.top,
width: data.width,
height: data.height,
borderRadius: 5,
// color: '#ffffff',
// backgroundColor: '#69c66d',
// textAlign: 'center',
// fontSize: 20,
// lineHeight: 45,
},
withCredentials: true
})
}
/** 计算微信坐标 */
public static computWxPosition(target: Node): { left: number, top: number, width: number, height: number } {
let data = { left: 0, top: 0, width: 0, height: 0 }
let pos = target.worldPosition;
let width = target.getComponent(UITransform).width;
let height = target.getComponent(UITransform).height;
let ratio = view.getDevicePixelRatio();
let scale = view.getScaleX();
let factor = scale / ratio;
// let point = cc.v2(rect.x, rect.y);
// point.mulSelf(scale);
data.left = (pos.x - width / 2) * factor;
data.top = wx.getSystemInfoSync().screenHeight - (pos.y + height / 2) * factor;
data.width = width * factor;
data.height = height * factor;
// let btnSize = cc.size(target.width + 10, target.height + 10);
// let frameSize = cc.view.getFrameSize();
// let winSize = cc.director.getWinSize();
// // cc.log("winSize: ",winSize);
// // cc.log("frameSize: ",frameSize);
// //适配不同机型来创建微信按钮
// data.left = (winSize.width * 0.5 + target.x - btnSize.width * 0.5) / winSize.width * frameSize.width;
// data.top = (winSize.height * 0.5 - target.y - btnSize.height * 0.5) / winSize.height * frameSize.height;
// data.width = btnSize.width / winSize.width * frameSize.width;
// data.height = btnSize.height / winSize.height * frameSize.height;
// cc.log("button pos: ",cc.v2(left,top));
// cc.log("button size: ",cc.size(width,height));
// cc.log(data)
return data;
}
/** 兼容方式 - 版本比较 */
public static compareVersion(versionA: any, versionB: any) {
versionA = versionA.split('.');
versionB = versionB.split('.');
let len = Math.max(versionA.length, versionB.length);
while (versionA.length < len) {
versionA.push('0');
}
while (versionB.length < len) {
versionB.push('0');
}
for (let i = 0; i < len; i++) {
let num1 = parseInt(versionA[i]);
let num2 = parseInt(versionB[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
/** 当前版本是否能使用接口 */
public static checkVerison(version: string): boolean {
return this.compareVersion(this.SystemInfo.SDKVersion, version) >= 0;
}
/** 显示一个对话框 */
public static showModal(data: ShowModalType) {
if (typeof my != 'undefined') {
return new Promise((resolve, reject) => {
wx.confirm({
...data,
success: (res: { confirm: boolean, cancel: boolean }) => {
if (res.confirm) {
resolve(true);
} else {
resolve(false);
}
},
fail: () => {
reject();
}
});
});
}
return new Promise((resolve, reject) => {
if (typeof wx !== 'undefined') {
wx.showModal({
...data,
success: (res: { confirm: boolean, cancel: boolean }) => {
if (res.confirm) {
resolve(true);
} else {
resolve(false);
}
},
fail: () => {
reject();
}
});
} else {
console.log(JSON.stringify(data));
}
});
}
/**
* 显示一个提示层
* @param msg 显示信息
* @param time 关闭时间
*/
public static showToast(msg: string, time: number = 1500) {
if (!this.isWx) return;
if (typeof my != 'undefined') {
my.showToast({
type: 'none',
content: msg,
duration: time,
});
return;
}
wx.showToast({
title: msg,
icon: 'none',
duration: time
})
}
/** 设置系统剪贴板的内容 */
public static copy(str: string) {
if (!this.checkVerison('1.1.0')) return Promise.reject('');
if (typeof my != 'undefined') {
return new Promise((resolve, reject) => {
wx.setClipboard({
text: str,
success: (res: any) => {
resolve(res);
},
fail: () => {
resolve('');
}
});
});
}
return new Promise((resolve, reject) => {
wx.setClipboardData({
data: str,
success: (res: any) => {
resolve(res);
},
fail: () => {
resolve('');
}
});
});
}
/** 手机发生较长时间的振动(400 ms) */
public static vibrateLong() {
if (!this.checkVerison('1.2.0')) return Promise.reject(null);
return new Promise((resolve, reject) => {
wx.vibrateLong({
success: () => {
resolve(1);
},
fail: () => {
resolve(0);
}
});
});
}
/** 手机发生较短时间的振动(15 ms) */
public static vibrateShort() {
if (!this.checkVerison('1.2.0')) return Promise.reject(null);
return new Promise<IResult<any>>((resolve, reject) => {
wx.vibrateShort({
success: () => {
resolve({ code: 0 });
},
fail: () => {
resolve({ code: 1 });
}
});
});
}
/**
* 检测更新
*/
public static checkUpdate(bol: boolean = true) {
if (!this.checkVerison('1.9.90')) return;
const updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
console.log('updateManager', res.hasUpdate);
});
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,请重启应用!',
showCancel: bol,
success: function (res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
});
});
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
});
}
/** 发送客服信息 */
public static openCustomerServiceConversation() {
return new Promise<IResult<any>>((resolve, reject) => {
if (!this.checkVerison('2.0.3')) resolve({ code: 1 });
wx.openCustomerServiceConversation({
showMessageCard: true,
sendMessageTitle: '我要更多好玩!',
sendMessageImg: 'http://dep.miso-lab.com/tetrischangeskin/bin/share/concat.png',
success: () => {
resolve({ code: 0 });
},
fail: () => {
resolve({ code: 1 });
}
});
});
}
/** 发送客服信息 */
public static openConversation() {
return new Promise<IResult<any>>((resolve, reject) => {
if (!this.checkVerison('2.0.3')) resolve({ code: 1 });
wx.openCustomerServiceConversation({
success: () => {
resolve({ code: 0 });
},
fail: () => {
resolve({ code: 1 });
}
});
});
}
/** 回到前台 */
public static onShow(callback: () => void) {
if (!this.isWx) return;
wx.onShow(callback);
// return new Promise<IResult<any>>((resolve, reject) => {
// wx.onShow((res: any) => {
// resolve({ code: 1 });
// });
// });
}
/** 切换到后台 */
public static onHide(callback: () => void) {
if (!this.isWx) return;
wx.onHide(callback);
}
public static updateShareMenu(value) {
if (!this.isWx) return;
wx.updateShareMenu({
withShareTicket: value
});
}
/**
* 检查授权
*/
public static getSetting(scope: string = 'scope.userInfo') {
if (!this.isWx) return Promise.resolve(1);
return new Promise((resolve, reject) => {
wx.getSetting({
success(res) {
if (res.authSetting[scope]) {
resolve(1)
} else {
reject()
}
}
})
})
}
/**
* 打开授权设置
*/
public static openSetting(scope: string = 'scope.userInfo') {
if (!this.isWx) return Promise.resolve(1);
return new Promise((resolve, reject) => {
wx.openSetting({
success(res) {
if (res.authSetting[scope]) {
resolve(1)
} else {
reject()
}
}
})
})
}
/**
* 打开授权窗口
*/
public static authorize(scope: string = 'scope.userInfo') {
if (!this.isWx) return;
wx.authorize({
scope: scope,
success(suc) {
console.log("suc", suc)
},
fail(err) {
console.log("err", err)
}
})
}
/**
* 获取用户信息
*/
public static getUserInfo(callback) {
wx.getUserInfo({
success: function (res) {
// console.log(res)
callback(res)
},
fail: function (err) {
callback(err)
}
})
}
/**
* 获取用户信息 自动弹出
*/
public static getUserProfile(callback) {
if (!this.isWx) {
callback({ userInfo: "" })
return
};
wx.getUserProfile({
desc: '补充用户资料',
success: function (res) {
// console.log(res)
callback(res)
},
fail: function (err) {
console.log("获取用户信息", err);
callback(err)
}
})
}
/** 游戏圈 */
public static createGameClubButton(target: Node) {
let data = this.computWxPosition(target);
//@ts-ignore
return wx.createGameClubButton({
icon: 'white',
style: {
left: data.left,
top: data.top,
width: data.width,
height: data.height,
}
})
}
/** 抖音创建客服按钮 */
public static createContactButton(target: Node) {
let data = this.computWxPosition(target);
return wx.createContactButton({
type: "text", // image | text
// image: "../resources/prefab/loading/mask",
text: ' ',
style: {
left: data.left,
top: data.top,
width: data.width,
height: data.height,
// lineHeight: 40,
backgroundColor: "#00000000",
// textAlign: "center",
// fontSize: 16,
// borderRadius: 5,
borderColor: "#00000000",
// borderWidth: 1,
// textColor: "#ffffff",
},
success(res) {
console.log("createContactButton success", res);
},
fail(res) {
console.log("createContactButton fail", res);
},
complete(res) {
console.log("createContactButton complete", res);
},
});
}
public static restartMiniProgram() {
if (typeof my != 'undefined') {
game.restart();
return;
}
if (!this.isWx) {
return
};
if (!wx.restartMiniProgram) {
this.showToast("自动重启失败,请手动重启!", 10000);
}
wx.restartMiniProgram();
}
}