-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk_tool.js
133 lines (108 loc) · 2.86 KB
/
sdk_tool.js
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
/*
* sdk的tool模块,提供一些通用的api
*
* create by zengxx on 2018-11-23
*/
let logManager = require('./sdk_log');
let config = require('./sdk_config');
let tool = {};
tool.getBase64Image = function (imageUrl, callback) {
imageUrl.indexOf('https') === -1 && (imageUrl = imageUrl.replace('http', 'https'));
let xhr = new XMLHttpRequest();
xhr.open('get', imageUrl, true);
xhr.responseType = 'blob';
xhr.onload = function () {
logManager.LOGD('getBase64Image... ' + xhr.status);
if (xhr.status === 200) {
let blob = xhr.response;
let fr = new FileReader();
fr.onload = function(ret) {
callback && callback(ret.target.result || '');
};
fr.readAsDataURL(blob);
}
};
xhr.send();
};
// 纯web环境可以从这里获取uuid
tool.getUuid = function () {
let s = [];
let hexDigits = '0123456789abcdefghijklmnopqrstuvwxyz';
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '';
let uuid = s.join('');
return uuid;
};
/*
* 保证uuid为32位
*/
tool.uuidTo32 = function (uuid) {
let s = [];
s[0] = uuid;
let len = 32 - uuid.toString().length;
for (let i = 1; i < len+1; i++) {
s[i] = '0';
}
return s.join('');
};
/*
* 获取发布平台
*
* 微信小游戏:wx
* facebook:fb
* cordova:cordova
* 浏览器测试:web
*/
tool.getPublishPlatform = function () {
if (window && window['cordova']) {
return 'cordova';
}
if (window && window['FBInstant']) {
return 'fb';
}
if (window && window['wx']) {
return 'wx';
}
return 'web';
};
/*
* 获取设备系统类型 ios android,其他系统暂不支持
*/
tool.getDevicePlatform = function () {
if (/(android)/i.test(window['navigator']['userAgent'])) {
return 'android';
} else if (/(ipod|iphone|ipad)/i.test(window['navigator']['userAgent'])) {
return 'ios';
} else {
return '';
}
};
/*
* 获取当前平台的clientId
*/
tool.getClientId = function () {
let clientId = '';
if (tool.getPublishPlatform() === 'cordova') {
clientId = config.SystemInfo.clientIds[tool.getPublishPlatform()][tool.getDevicePlatform()] || '';
} else {
clientId = config.SystemInfo.clientIds[tool.getPublishPlatform()] || '';
}
return clientId;
};
/*
* 获取当前平台的clientId的int值
*/
tool.getIntClientId = function () {
let intClientId = 0;
if (tool.getPublishPlatform() === 'cordova') {
intClientId = config.SystemInfo.intClientIds[tool.getPublishPlatform()][tool.getDevicePlatform()] || 0;
} else {
intClientId = config.SystemInfo.intClientIds[tool.getPublishPlatform()] || 0;
}
return intClientId;
};
module.exports = tool;