-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
253 lines (213 loc) · 5.92 KB
/
index.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
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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.WeexDowngrade = factory());
}(this, (function () { 'use strict';
var semver = {
satisfies: function satisfies(left, right) {
var regex = /(\W+)?([\d|.]+)/;
if (typeof left + typeof right !== 'stringstring')
{ return false }
// *匹配所有
if (right == '*') {
return true
}
var arr = right.match(regex);
var a = left.split('.');
var i = 0;
var b = arr[2].split('.');
var len = Math.max(a.length, b.length);
var flag = 0;
for (var i$1 = 0; i$1 < len; i$1++) {
if ((a[i$1] && !b[i$1] && parseInt(a[i$1]) > 0) || (parseInt(a[i$1]) > parseInt(b[i$1]))) {
flag = 1;
break
} else if ((b[i$1] && !a[i$1] && parseInt(b[i$1]) > 0) || (parseInt(a[i$1]) < parseInt(b[i$1]))) {
flag = -1;
break
}
}
switch (arr[1]) {
case '<':
if (flag === -1) {
return true
}
break
case '<=':
if (flag !== 1) {
return true
}
break
case '>':
if (flag === 1) {
return true
}
break
case '>=':
if (flag !== -1) {
return true
}
break
default:
if (flag === 0) {
return true
}
break
}
return false
}
};
/* global WXEnvironment, weex, callNative */
var MODULE_NAME = 'instanceWrap';
var DOWNGRADE_TYPE = 1;
var DOWNGRADE_ERROR_CODE = 1003;
var DOWNGRADE_MSG = 'Force downgrade to web';
/**
* @private
* Using async type to check environment
*/
function isWeex () {
return typeof(WXEnvironment) !== 'undefined'
&& WXEnvironment.platform
&& WXEnvironment.platform.toLowerCase() !== 'web'
}
/**
* @private
* Require module and fixed lagacy version require issues.
*/
function getInstanceWrap() {
var prefix = '@weex-module/';
try {
return weex.requireModule(MODULE_NAME)
}catch(e) {}
try {
return __weex_require__( prefix + MODULE_NAME)
}catch(e) {}
return { error: function() {
console && console.log && console.log(("Can not found module [" + MODULE_NAME + "]"));
} }
}
function force (type, errorCode, message) {
if(!isWeex()) { return false }
type = parseInt(type) || DOWNGRADE_TYPE;
errorCode = parseInt(errorCode) || DOWNGRADE_ERROR_CODE;
message = isString(message) ? message : DOWNGRADE_MSG;
getInstanceWrap()['error'](type, errorCode, message);
return true
}
/**
* config
*
* {
* ios: {
* osVersion: '>1.0.0' or '>=1.0.0' or '<1.0.0' or '<=1.0.0' or '1.0.0'
* appVersion: '>1.0.0' or '>=1.0.0' or '<1.0.0' or '<=1.0.0' or '1.0.0'
* weexVersion: '>1.0.0' or '>=1.0.0' or '<1.0.0' or '<=1.0.0' or '1.0.0'
* deviceModel: ['modelA', 'modelB', ...]
* },
* android: {
* osVersion: '>1.0.0' or '>=1.0.0' or '<1.0.0' or '<=1.0.0' or '1.0.0'
* appVersion: '>1.0.0' or '>=1.0.0' or '<1.0.0' or '<=1.0.0' or '1.0.0'
* weexVersion: '>1.0.0' or '>=1.0.0' or '<1.0.0' or '<=1.0.0' or '1.0.0'
* deviceModel: ['modelA', 'modelB', ...]
* }
* }
*
*/
function check(config) {
var result = {
isDowngrade: false
};
if (!isWeex()) {
return result
}
var deviceInfo = WXEnvironment || {};
var platform = deviceInfo.platform || 'unknow';
var dPlatform = platform.toLowerCase();
var cObj = config[dPlatform] || {};
for (var key in deviceInfo) {
var keyLower = key.toLowerCase();
var val = deviceInfo[key];
var isVersion = keyLower.indexOf('version') >= 0;
var isDeviceModel = keyLower.indexOf('devicemodel') >= 0;
var criteria = cObj[key];
if (criteria && isVersion) {
var c = normalizeVersion(criteria);
var d = normalizeVersion(deviceInfo[key]);
// app version support multiple app format
if(keyLower.indexOf('app') === 0 && isObject(criteria)) {
var aName = deviceInfo['appName'] || '';
c = normalizeVersion(criteria[aName]);
}
if (semver.satisfies(d, c)) {
result = getError(key, val, criteria);
break
}
} else if (isDeviceModel) {
var _criteria = Array.isArray(criteria) ? criteria : [criteria];
if (_criteria.indexOf(val) >= 0) {
result = getError(key, val, criteria);
break
}
}
}
return result
}
function isString(v) {
return typeof(v) === 'string'
}
function isObject(v) {
return Object.prototype.toString.call(v).slice(8, -1) === 'Object'
}
function normalizeVersion(v) {
if (v === '*') {
return v
}
v = isString(v) ? v : '';
var split = v.split('.');
var i = 0;
var result = [];
while (i < 3) {
var s = isString(split[i]) && split[i] ? split[i] : '0';
result.push(s);
i++;
}
return result.join('.')
}
function getError(key, val, criteria) {
var result = {
isDowngrade: true,
errorType: 1,
code: 1000
};
var getMsg = function(key, val, criteria) {
return ("Downgrade[" + key + "]: envInfo " + val + " matched criteria " + criteria)
};
var _key = key.toLowerCase();
if (_key.indexOf('osversion') >= 0) {
result.code = 1001;
} else if (_key.indexOf('appversion') >= 0) {
result.code = 1002;
} else if (_key.indexOf('weexversion') >= 0) {
result.code = 1003;
} else if (_key.indexOf('devicemodel') >= 0) {
result.code = 1004;
}
result.errorMessage = getMsg(key, val, criteria);
return result
}
function condition (config) {
var diagnose = check(config);
if(diagnose.isDowngrade) {
force(diagnose.errorType, diagnose.code, diagnose.errorMessage);
}
return diagnose.isDowngrade
}
var index = {
force: force,
check: check,
condition: condition,
semverLite: semver
};
return index;
})));