@microbt/environment 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +127 -11
- package/dist/esm/arms.d.ts +39 -0
- package/dist/esm/arms.js +66 -0
- package/dist/esm/cookie.d.ts +2 -0
- package/dist/esm/cookie.js +20 -0
- package/dist/esm/environment/factory.d.ts +5 -0
- package/dist/esm/environment/factory.js +31 -0
- package/dist/esm/environment/index.d.ts +4 -0
- package/dist/esm/environment/index.js +59 -0
- package/dist/esm/environment/strategies/cinmoore.d.ts +7 -0
- package/dist/esm/environment/strategies/cinmoore.js +42 -0
- package/dist/esm/environment/strategies/local.d.ts +7 -0
- package/dist/esm/environment/strategies/local.js +23 -0
- package/dist/esm/environment/strategies/safio365.d.ts +7 -0
- package/dist/esm/environment/strategies/safio365.js +42 -0
- package/dist/esm/environment/strategies/sensforge.d.ts +7 -0
- package/dist/esm/environment/strategies/sensforge.js +42 -0
- package/dist/esm/environment/strategies/superacme.d.ts +8 -0
- package/dist/esm/environment/strategies/superacme.js +45 -0
- package/dist/esm/gateway/factory.d.ts +4 -0
- package/dist/esm/gateway/factory.js +25 -0
- package/dist/esm/gateway/index.d.ts +33 -0
- package/dist/esm/gateway/index.js +44 -0
- package/dist/esm/gateway/strategies/index.d.ts +3 -0
- package/dist/esm/gateway/strategies/index.js +4 -0
- package/dist/esm/gateway/strategies/safio365.d.ts +4 -0
- package/dist/esm/gateway/strategies/safio365.js +19 -0
- package/dist/esm/gateway/strategies/sensforge.d.ts +4 -0
- package/dist/esm/gateway/strategies/sensforge.js +19 -0
- package/dist/esm/gateway/strategies/superacme.d.ts +4 -0
- package/dist/esm/gateway/strategies/superacme.js +27 -0
- package/dist/esm/global.d.ts +58 -0
- package/dist/esm/hybrid.js +89 -0
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.js +8 -0
- package/dist/esm/local.js +27 -0
- package/dist/esm/platform.js +14 -0
- package/package.json +6 -4
- package/dist/esm/environment.d.ts +0 -15
- package/dist/esm/gateway.d.ts +0 -1
- package/dist/umd/environment.min.js +0 -1
package/README.md
CHANGED
|
@@ -60,17 +60,28 @@ console.log(regionCode); // 输出: 'cn'
|
|
|
60
60
|
|
|
61
61
|
## 网关相关函数
|
|
62
62
|
|
|
63
|
-
### `baseUrl()`
|
|
63
|
+
### `baseUrl(params: { name: string; path?: string })`
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
根据当前域名生成对应的 API URL。
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
**参数**:
|
|
68
|
+
|
|
69
|
+
- `params.name`: `string` - **必填**,API 服务名称(如 'api'、'iot-api')
|
|
70
|
+
- `params.path`: `string` - **可选**,API 路径(如 '/saas')
|
|
71
|
+
|
|
72
|
+
**返回值**: `string` - 生成的 URL。如果生成失败则返回传入的 path 值
|
|
68
73
|
|
|
69
74
|
**示例**:
|
|
70
75
|
|
|
71
76
|
```typescript
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
// 中国区开发环境
|
|
78
|
+
// location.host: 'mall-cn-dev.superacme.com'
|
|
79
|
+
baseUrl({ name: 'api' }); // => '//api-cn-dev.superacme.com'
|
|
80
|
+
baseUrl({ name: 'iot-api', path: '/saas' }); // => '//iot-api-cn-dev.superacme.com/saas'
|
|
81
|
+
|
|
82
|
+
// 美区 AWS 生产环境
|
|
83
|
+
// location.host: 'mall-us-aws.superacme.com'
|
|
84
|
+
baseUrl({ name: 'api' }); // => '//api-us-aws.superacme.com'
|
|
74
85
|
```
|
|
75
86
|
|
|
76
87
|
## 混合应用相关函数
|
|
@@ -94,12 +105,12 @@ console.log(resp);
|
|
|
94
105
|
|
|
95
106
|
### `bridgeBootstrap()`
|
|
96
107
|
|
|
97
|
-
|
|
108
|
+
初始化桥接环境。
|
|
98
109
|
|
|
99
110
|
**示例**:
|
|
100
111
|
|
|
101
112
|
```typescript
|
|
102
|
-
bridgeBootstrap();
|
|
113
|
+
bridgeBootstrap();
|
|
103
114
|
```
|
|
104
115
|
|
|
105
116
|
### `inSuperAcme()`
|
|
@@ -117,15 +128,36 @@ console.log(isInApp); // 输出: true 或 false
|
|
|
117
128
|
|
|
118
129
|
### `superAcmeVersion()`
|
|
119
130
|
|
|
120
|
-
获取 SuperAcme
|
|
131
|
+
获取 SuperAcme 应用的版本信息。
|
|
132
|
+
|
|
133
|
+
**返回值**: 返回一个包含版本信息的对象:
|
|
134
|
+
|
|
135
|
+
- 如果在 SuperAcme 应用中且能够解析版本号:
|
|
121
136
|
|
|
122
|
-
|
|
137
|
+
```json
|
|
138
|
+
{
|
|
139
|
+
version: string; // 完整版本号,如 "1.2.3"
|
|
140
|
+
major: number; // 主版本号
|
|
141
|
+
minor: number; // 次版本号
|
|
142
|
+
patch: number; // 修订号
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
- 如果不在 SuperAcme 应用中或无法解析版本号:
|
|
147
|
+
|
|
148
|
+
```json
|
|
149
|
+
{
|
|
150
|
+
version: 'unknown';
|
|
151
|
+
}
|
|
152
|
+
```
|
|
123
153
|
|
|
124
154
|
**示例**:
|
|
125
155
|
|
|
126
156
|
```typescript
|
|
127
|
-
const
|
|
128
|
-
console.log(
|
|
157
|
+
const versionInfo = superAcmeVersion();
|
|
158
|
+
console.log(versionInfo);
|
|
159
|
+
// 在 SuperAcme 应用中可能输出:{ version: "1.2.3", major: 1, minor: 2, patch: 3 }
|
|
160
|
+
// 在非 SuperAcme 应用中输出:{ version: "unknown" }
|
|
129
161
|
```
|
|
130
162
|
|
|
131
163
|
## 平台相关函数
|
|
@@ -176,3 +208,87 @@ if (androidWebView) {
|
|
|
176
208
|
const locale = getLocale();
|
|
177
209
|
console.log(locale); // 输出: 'zh-CN' 或 'en-US'
|
|
178
210
|
```
|
|
211
|
+
|
|
212
|
+
## Cookie 相关函数
|
|
213
|
+
|
|
214
|
+
### `getCookie(name: string)`
|
|
215
|
+
|
|
216
|
+
获取指定名称的 Cookie 值。
|
|
217
|
+
|
|
218
|
+
**参数**:
|
|
219
|
+
|
|
220
|
+
- `name`: `string` - 要获取的 Cookie 名称
|
|
221
|
+
|
|
222
|
+
**返回值**: `string | null` - 返回指定名称的 Cookie 值,如果不存在或解析失败则返回 `null`
|
|
223
|
+
|
|
224
|
+
**功能描述**:
|
|
225
|
+
|
|
226
|
+
- 使用正则表达式匹配指定名称的 Cookie 值。
|
|
227
|
+
- 如果匹配成功,返回解码后的 Cookie 值。
|
|
228
|
+
- 如果匹配失败或解码过程中发生错误,返回 `null`。
|
|
229
|
+
|
|
230
|
+
**示例**:
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
const userToken = getCookie('token');
|
|
234
|
+
if (userToken) {
|
|
235
|
+
console.log(`User token: ${userToken}`);
|
|
236
|
+
} else {
|
|
237
|
+
console.log('Token not found or invalid');
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**注意事项**:
|
|
242
|
+
|
|
243
|
+
- 函数会对 Cookie 值进行解码,如果解码失败会捕获错误并返回 `null`。
|
|
244
|
+
- 确保传入的 `name` 参数是合法的 Cookie 名称。
|
|
245
|
+
|
|
246
|
+
### `getRUID()`
|
|
247
|
+
|
|
248
|
+
获取当前域名下的 `RUID` 值。
|
|
249
|
+
|
|
250
|
+
**返回值**: `string | null` - 返回当前域名下的 `RUID` 值,如果不存在或解析失败则返回 `null`
|
|
251
|
+
|
|
252
|
+
**功能描述**:
|
|
253
|
+
|
|
254
|
+
- 基于当前域名(`location.hostname`)动态生成 `RUID` 的 Cookie 名称。
|
|
255
|
+
- 调用 `getCookie` 函数获取对应的 Cookie 值。
|
|
256
|
+
|
|
257
|
+
**示例**:
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
const ruid = getRUID();
|
|
261
|
+
if (ruid) {
|
|
262
|
+
console.log(`RUID: ${ruid}`);
|
|
263
|
+
} else {
|
|
264
|
+
console.log('RUID not found or invalid');
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
**注意事项**:
|
|
269
|
+
|
|
270
|
+
- 确保 `RUID` 的 Cookie 名称格式为 `RUID_<当前域名>`。
|
|
271
|
+
- 函数依赖 `getCookie`,因此会继承其解码逻辑和错误处理机制。
|
|
272
|
+
|
|
273
|
+
## ARMS 模块
|
|
274
|
+
|
|
275
|
+
`arms` 模块封装了阿里云 ARMS(应用实时监控服务)的前端监控功能,提供了一系列 API 用于上报性能数据、错误信息、用户行为等。
|
|
276
|
+
|
|
277
|
+
### 可用 API
|
|
278
|
+
|
|
279
|
+
以下是 `arms` 模块中提供的 API 列表:
|
|
280
|
+
|
|
281
|
+
- **`api(params)`**: 上报 API 请求数据。
|
|
282
|
+
- **`error(e, pos)`**: 上报错误信息。
|
|
283
|
+
- **`sum(key, value)`**: 上报累加指标。
|
|
284
|
+
- **`avg(key, value)`**: 上报平均值指标。
|
|
285
|
+
- **`reportBehavior()`**: 上报用户行为数据。
|
|
286
|
+
- **`performance(params)`**: 上报性能数据。
|
|
287
|
+
- **`setConfig(params)`**: 设置 ARMS 配置。
|
|
288
|
+
- **`setPage(page, sendPv)`**: 设置当前页面信息,并可选择是否发送页面访问数据(PV)。
|
|
289
|
+
- **`setCommonInfo(params)`**: 设置通用信息。
|
|
290
|
+
- **`addBehavior(data, page)`**: 添加用户行为数据。
|
|
291
|
+
|
|
292
|
+
### 参考链接
|
|
293
|
+
|
|
294
|
+
- [阿里云 ARMS 文档 - API 参考](https://help.aliyun.com/zh/arms/browser-monitoring/developer-reference/api-reference)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
declare const api: (params: {
|
|
2
|
+
api: string;
|
|
3
|
+
success: boolean;
|
|
4
|
+
time: number;
|
|
5
|
+
code?: string | number;
|
|
6
|
+
msg?: string;
|
|
7
|
+
begin?: number;
|
|
8
|
+
traceId?: string;
|
|
9
|
+
sid?: string;
|
|
10
|
+
}) => void;
|
|
11
|
+
export declare const arms: {
|
|
12
|
+
api: (params: {
|
|
13
|
+
api: string;
|
|
14
|
+
success: boolean;
|
|
15
|
+
time: number;
|
|
16
|
+
code?: string | number;
|
|
17
|
+
msg?: string;
|
|
18
|
+
begin?: number;
|
|
19
|
+
traceId?: string;
|
|
20
|
+
sid?: string;
|
|
21
|
+
}) => void;
|
|
22
|
+
error: (e: Error, pos: {
|
|
23
|
+
filename: string;
|
|
24
|
+
lineno: number;
|
|
25
|
+
colno: number;
|
|
26
|
+
}) => void;
|
|
27
|
+
sum: (key: string, value: number) => void;
|
|
28
|
+
avg: (key: string, value: number) => void;
|
|
29
|
+
reportBehavior: () => void;
|
|
30
|
+
performance: (params: unknown) => void;
|
|
31
|
+
setConfig: (params: unknown) => void;
|
|
32
|
+
setPage: (page: string, sendPv: boolean) => void;
|
|
33
|
+
setCommonInfo: (params: unknown) => void;
|
|
34
|
+
addBehavior: (data: {
|
|
35
|
+
name: string;
|
|
36
|
+
message: string;
|
|
37
|
+
}, page: string) => void;
|
|
38
|
+
};
|
|
39
|
+
export {};
|
package/dist/esm/arms.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/arms.ts
|
|
2
|
+
var api = (params) => {
|
|
3
|
+
if (window.__bl) {
|
|
4
|
+
window.__bl.api(params);
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
var error = (e, pos) => {
|
|
8
|
+
if (window.__bl) {
|
|
9
|
+
window.__bl.error(e, pos);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var sum = (key, value) => {
|
|
13
|
+
if (window.__bl) {
|
|
14
|
+
window.__bl.sum(key, value);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var avg = (key, value) => {
|
|
18
|
+
if (window.__bl) {
|
|
19
|
+
window.__bl.avg(key, value);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var reportBehavior = () => {
|
|
23
|
+
if (window.__bl) {
|
|
24
|
+
window.__bl.reportBehavior();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var performance = (params) => {
|
|
28
|
+
if (window.__bl) {
|
|
29
|
+
window.__bl.performance(params);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var setConfig = (params) => {
|
|
33
|
+
if (window.__bl) {
|
|
34
|
+
window.__bl.setConfig(params);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var setPage = (page, sendPv) => {
|
|
38
|
+
if (window.__bl) {
|
|
39
|
+
window.__bl.setPage(page, sendPv);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var setCommonInfo = (params) => {
|
|
43
|
+
if (window.__bl) {
|
|
44
|
+
window.__bl.setCommonInfo(params);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var addBehavior = (data, page) => {
|
|
48
|
+
if (window.__bl) {
|
|
49
|
+
window.__bl.addBehavior(data, page);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var arms = {
|
|
53
|
+
api,
|
|
54
|
+
error,
|
|
55
|
+
sum,
|
|
56
|
+
avg,
|
|
57
|
+
reportBehavior,
|
|
58
|
+
performance,
|
|
59
|
+
setConfig,
|
|
60
|
+
setPage,
|
|
61
|
+
setCommonInfo,
|
|
62
|
+
addBehavior
|
|
63
|
+
};
|
|
64
|
+
export {
|
|
65
|
+
arms
|
|
66
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/cookie.ts
|
|
2
|
+
var COOKIE_REGEX = (name) => new RegExp("(^| )" + name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "=([^;]+)");
|
|
3
|
+
function getCookie(name) {
|
|
4
|
+
try {
|
|
5
|
+
const match = document.cookie.match(COOKIE_REGEX(name));
|
|
6
|
+
if (!match)
|
|
7
|
+
return null;
|
|
8
|
+
return decodeURIComponent(match[2]);
|
|
9
|
+
} catch (e) {
|
|
10
|
+
console.warn(`Error decoding cookie "${name}":`, e);
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function getRUID() {
|
|
15
|
+
return getCookie(`RUID_${location.hostname}`);
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
getCookie,
|
|
19
|
+
getRUID
|
|
20
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/environment/factory.ts
|
|
2
|
+
import { CinmooreEnvironmentStrategy } from "./strategies/cinmoore";
|
|
3
|
+
import { LocalEnvironmentStrategy } from "./strategies/local";
|
|
4
|
+
import { Safio365EnvironmentStrategy } from "./strategies/safio365";
|
|
5
|
+
import { SensforgeEnvironmentStrategy } from "./strategies/sensforge";
|
|
6
|
+
import { SuperAcmeEnvironmentStrategy } from "./strategies/superacme";
|
|
7
|
+
var _EnvironmentStrategyFactory = class {
|
|
8
|
+
static getStrategy(domain) {
|
|
9
|
+
if (this.localStrategy.getEnvironment(domain) === "local") {
|
|
10
|
+
return this.localStrategy;
|
|
11
|
+
}
|
|
12
|
+
const strategy = this.strategies.get(domain);
|
|
13
|
+
if (!strategy) {
|
|
14
|
+
throw new Error(`Unsupported domain: ${domain}`);
|
|
15
|
+
}
|
|
16
|
+
return strategy;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var EnvironmentStrategyFactory = _EnvironmentStrategyFactory;
|
|
20
|
+
EnvironmentStrategyFactory.strategies = /* @__PURE__ */ new Map();
|
|
21
|
+
EnvironmentStrategyFactory.localStrategy = new LocalEnvironmentStrategy();
|
|
22
|
+
(() => {
|
|
23
|
+
_EnvironmentStrategyFactory.strategies.set("superacme.com", new SuperAcmeEnvironmentStrategy());
|
|
24
|
+
_EnvironmentStrategyFactory.strategies.set("safio365.com", new Safio365EnvironmentStrategy());
|
|
25
|
+
_EnvironmentStrategyFactory.strategies.set("sensforge.com", new SensforgeEnvironmentStrategy());
|
|
26
|
+
_EnvironmentStrategyFactory.strategies.set("cinmoore.com", new CinmooreEnvironmentStrategy());
|
|
27
|
+
_EnvironmentStrategyFactory.strategies.set("cinmoore.cn", new CinmooreEnvironmentStrategy());
|
|
28
|
+
})();
|
|
29
|
+
export {
|
|
30
|
+
EnvironmentStrategyFactory
|
|
31
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/environment/index.ts
|
|
2
|
+
import { EnvironmentStrategyFactory } from "./factory";
|
|
3
|
+
var SUPPORTED_DOMAINS = [
|
|
4
|
+
"superacme.com",
|
|
5
|
+
"cinmoore.com",
|
|
6
|
+
"cinmoore.cn",
|
|
7
|
+
"safio365.com",
|
|
8
|
+
"sensforge.com",
|
|
9
|
+
"0.0.0.0",
|
|
10
|
+
"127.0.0.1",
|
|
11
|
+
"localhost"
|
|
12
|
+
];
|
|
13
|
+
function getCurrentDomain() {
|
|
14
|
+
if (typeof location === "undefined") {
|
|
15
|
+
throw new Error("location is not defined");
|
|
16
|
+
}
|
|
17
|
+
const host = location.host;
|
|
18
|
+
const domain = SUPPORTED_DOMAINS.find((d) => host.endsWith(d));
|
|
19
|
+
if (!domain) {
|
|
20
|
+
throw new Error(`Unsupported domain: ${host}`);
|
|
21
|
+
}
|
|
22
|
+
return domain;
|
|
23
|
+
}
|
|
24
|
+
function environment() {
|
|
25
|
+
try {
|
|
26
|
+
const domain = getCurrentDomain();
|
|
27
|
+
const strategy = EnvironmentStrategyFactory.getStrategy(domain);
|
|
28
|
+
return strategy.getEnvironment(location.host);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.warn("Failed to determine environment:", error);
|
|
31
|
+
return "unknown";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function region() {
|
|
35
|
+
try {
|
|
36
|
+
const domain = getCurrentDomain();
|
|
37
|
+
const strategy = EnvironmentStrategyFactory.getStrategy(domain);
|
|
38
|
+
return strategy.getRegion(location.host);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.warn("Failed to determine region:", error);
|
|
41
|
+
return "unknown";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function isAWSDomain() {
|
|
45
|
+
try {
|
|
46
|
+
const domain = getCurrentDomain();
|
|
47
|
+
const strategy = EnvironmentStrategyFactory.getStrategy(domain);
|
|
48
|
+
return strategy.isAWS(location.host);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.warn("Failed to determine AWS domain:", error);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
SUPPORTED_DOMAINS,
|
|
56
|
+
environment,
|
|
57
|
+
isAWSDomain,
|
|
58
|
+
region
|
|
59
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/environment/strategies/cinmoore.ts
|
|
2
|
+
var CinmooreEnvironmentStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: false,
|
|
7
|
+
supportAWS: false
|
|
8
|
+
};
|
|
9
|
+
this.ENV_PATTERNS = {
|
|
10
|
+
// 匹配 -dev、-dev1、-dev-1、-dev. 等
|
|
11
|
+
dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
|
|
12
|
+
// 匹配 -test、-test1、-test-1、-test. 等
|
|
13
|
+
test: /-test(?:\d*)?(?:-|\.|\b|$)/,
|
|
14
|
+
// 匹配 -pre、-pre1、-pre-1、-pre. 等
|
|
15
|
+
pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
|
|
16
|
+
// 匹配 -gray、-gray1、-gray-1、-gray. 等
|
|
17
|
+
gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
|
|
18
|
+
// 本地环境
|
|
19
|
+
local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
getEnvironment(host) {
|
|
23
|
+
if (this.ENV_PATTERNS.local.test(host)) {
|
|
24
|
+
return "local";
|
|
25
|
+
}
|
|
26
|
+
for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
|
|
27
|
+
if (pattern.test(host)) {
|
|
28
|
+
return env;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return "prod";
|
|
32
|
+
}
|
|
33
|
+
getRegion() {
|
|
34
|
+
return "unknown";
|
|
35
|
+
}
|
|
36
|
+
isAWS() {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
CinmooreEnvironmentStrategy
|
|
42
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/environment/strategies/local.ts
|
|
2
|
+
var LocalEnvironmentStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: false,
|
|
7
|
+
supportAWS: false
|
|
8
|
+
};
|
|
9
|
+
this.LOCAL_PATTERN = /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/;
|
|
10
|
+
}
|
|
11
|
+
getEnvironment(host) {
|
|
12
|
+
return this.LOCAL_PATTERN.test(host) ? "local" : "unknown";
|
|
13
|
+
}
|
|
14
|
+
getRegion() {
|
|
15
|
+
return "unknown";
|
|
16
|
+
}
|
|
17
|
+
isAWS() {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export {
|
|
22
|
+
LocalEnvironmentStrategy
|
|
23
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/environment/strategies/safio365.ts
|
|
2
|
+
var Safio365EnvironmentStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: false,
|
|
7
|
+
supportAWS: false
|
|
8
|
+
};
|
|
9
|
+
this.ENV_PATTERNS = {
|
|
10
|
+
// 匹配 -dev、-dev1、-dev-1、-dev. 等
|
|
11
|
+
dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
|
|
12
|
+
// 匹配 -test、-test1、-test-1、-test. 等
|
|
13
|
+
test: /-test(?:\d*)?(?:-|\.|\b|$)/,
|
|
14
|
+
// 匹配 -pre、-pre1、-pre-1、-pre. 等
|
|
15
|
+
pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
|
|
16
|
+
// 匹配 -gray、-gray1、-gray-1、-gray. 等
|
|
17
|
+
gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
|
|
18
|
+
// 本地环境
|
|
19
|
+
local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
getEnvironment(host) {
|
|
23
|
+
if (this.ENV_PATTERNS.local.test(host)) {
|
|
24
|
+
return "local";
|
|
25
|
+
}
|
|
26
|
+
for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
|
|
27
|
+
if (pattern.test(host)) {
|
|
28
|
+
return env;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return "prod";
|
|
32
|
+
}
|
|
33
|
+
getRegion() {
|
|
34
|
+
return "unknown";
|
|
35
|
+
}
|
|
36
|
+
isAWS() {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
Safio365EnvironmentStrategy
|
|
42
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/environment/strategies/sensforge.ts
|
|
2
|
+
var SensforgeEnvironmentStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: false,
|
|
7
|
+
supportAWS: false
|
|
8
|
+
};
|
|
9
|
+
this.ENV_PATTERNS = {
|
|
10
|
+
// 匹配 -dev、-dev1、-dev-1、-dev. 等
|
|
11
|
+
dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
|
|
12
|
+
// 匹配 -test、-test1、-test-1、-test. 等
|
|
13
|
+
test: /-test(?:\d*)?(?:-|\.|\b|$)/,
|
|
14
|
+
// 匹配 -pre、-pre1、-pre-1、-pre. 等
|
|
15
|
+
pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
|
|
16
|
+
// 匹配 -gray、-gray1、-gray-1、-gray. 等
|
|
17
|
+
gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
|
|
18
|
+
// 本地环境
|
|
19
|
+
local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
getEnvironment(host) {
|
|
23
|
+
if (this.ENV_PATTERNS.local.test(host)) {
|
|
24
|
+
return "local";
|
|
25
|
+
}
|
|
26
|
+
for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
|
|
27
|
+
if (pattern.test(host)) {
|
|
28
|
+
return env;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return "prod";
|
|
32
|
+
}
|
|
33
|
+
getRegion() {
|
|
34
|
+
return "unknown";
|
|
35
|
+
}
|
|
36
|
+
isAWS() {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
SensforgeEnvironmentStrategy
|
|
42
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare class SuperAcmeEnvironmentStrategy implements EnvironmentStrategy {
|
|
2
|
+
features: EnvironmentFeatures;
|
|
3
|
+
private readonly ENV_PATTERNS;
|
|
4
|
+
private readonly REGION_PATTERN;
|
|
5
|
+
getEnvironment(host: string): string;
|
|
6
|
+
getRegion(host: string): string;
|
|
7
|
+
isAWS(host: string): boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/environment/strategies/superacme.ts
|
|
2
|
+
var SuperAcmeEnvironmentStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: true,
|
|
7
|
+
supportAWS: true
|
|
8
|
+
};
|
|
9
|
+
this.ENV_PATTERNS = {
|
|
10
|
+
// 匹配 -dev、-dev1、-dev-1、-dev.、-dev-aws 等
|
|
11
|
+
dev: /-dev(?:\d*)?(?:-|\.|\b|$)/,
|
|
12
|
+
// 匹配 -test、-test1、-test-1、-test.、-test-aws 等
|
|
13
|
+
test: /-test(?:\d*)?(?:-|\.|\b|$)/,
|
|
14
|
+
// 匹配 -pre、-pre1、-pre-1、-pre.、-pre-aws 等
|
|
15
|
+
pre: /-pre(?:\d*)?(?:-|\.|\b|$)/,
|
|
16
|
+
// 匹配 -gray、-gray1、-gray-1、-gray.、-gray-aws 等
|
|
17
|
+
gray: /-gray(?:\d*)?(?:-|\.|\b|$)/,
|
|
18
|
+
// 本地环境保持不变
|
|
19
|
+
local: /^(localhost|127\.0\.0\.1|0\.0\.0\.0)(?::\d+)?$/
|
|
20
|
+
};
|
|
21
|
+
this.REGION_PATTERN = new RegExp("-(?<region>cn|us|usa|jpn|deu|gbr|fra|can|sgp|aus|kor|ind|bra|rus|zaf|mex|idn|tur|sau|che|twn|hkg|mac)(?=-|\\.|\\d|$)");
|
|
22
|
+
}
|
|
23
|
+
getEnvironment(host) {
|
|
24
|
+
if (this.ENV_PATTERNS.local.test(host)) {
|
|
25
|
+
return "local";
|
|
26
|
+
}
|
|
27
|
+
for (const [env, pattern] of Object.entries(this.ENV_PATTERNS)) {
|
|
28
|
+
if (pattern.test(host)) {
|
|
29
|
+
return env;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return "prod";
|
|
33
|
+
}
|
|
34
|
+
getRegion(host) {
|
|
35
|
+
var _a;
|
|
36
|
+
const match = host.match(this.REGION_PATTERN);
|
|
37
|
+
return ((_a = match == null ? void 0 : match.groups) == null ? void 0 : _a.region) || "unknown";
|
|
38
|
+
}
|
|
39
|
+
isAWS(host) {
|
|
40
|
+
return host.includes("-aws");
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
export {
|
|
44
|
+
SuperAcmeEnvironmentStrategy
|
|
45
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/gateway/factory.ts
|
|
2
|
+
import {
|
|
3
|
+
Safio365URLStrategy,
|
|
4
|
+
SensforgeURLStrategy,
|
|
5
|
+
SuperAcmeURLStrategy
|
|
6
|
+
} from "./strategies/index";
|
|
7
|
+
var _URLStrategyFactory = class {
|
|
8
|
+
static getStrategy(domain) {
|
|
9
|
+
const strategy = this.strategies.get(domain);
|
|
10
|
+
if (!strategy) {
|
|
11
|
+
throw new Error(`Unsupported domain: ${domain}`);
|
|
12
|
+
}
|
|
13
|
+
return strategy;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var URLStrategyFactory = _URLStrategyFactory;
|
|
17
|
+
URLStrategyFactory.strategies = /* @__PURE__ */ new Map();
|
|
18
|
+
(() => {
|
|
19
|
+
_URLStrategyFactory.strategies.set("superacme.com", new SuperAcmeURLStrategy());
|
|
20
|
+
_URLStrategyFactory.strategies.set("sensforge.com", new SensforgeURLStrategy());
|
|
21
|
+
_URLStrategyFactory.strategies.set("safio365.com", new Safio365URLStrategy());
|
|
22
|
+
})();
|
|
23
|
+
export {
|
|
24
|
+
URLStrategyFactory
|
|
25
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 根据当前域名生成基于 name 和 path 的 baseUrl
|
|
3
|
+
*
|
|
4
|
+
* @param params 配置参数
|
|
5
|
+
* @param params.name - 必填,API 服务名称(例如:'api', 'iot-api')
|
|
6
|
+
* @param params.path - 可选,API 路径(例如:'/saas')
|
|
7
|
+
* @returns 生成的 URL。如果生成失败则返回传入的 path 值
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // SuperAcme 域名系列
|
|
11
|
+
* // 中国区开发环境
|
|
12
|
+
* baseUrl({ name: 'api' }) // => '//api-cn-dev.superacme.com'
|
|
13
|
+
* baseUrl({ name: 'iot-api', path: '/saas' }) // => '//iot-api-cn-dev.superacme.com/saas'
|
|
14
|
+
*
|
|
15
|
+
* // 美区 AWS 环境
|
|
16
|
+
* // location.host: 'mall-us-aws.superacme.com'
|
|
17
|
+
* baseUrl({ name: 'api' }) // => '//api-us-aws.superacme.com'
|
|
18
|
+
*
|
|
19
|
+
* // Safio365 域名系列
|
|
20
|
+
* // location.host: 'iot-dev.safio365.com'
|
|
21
|
+
* baseUrl({ name: 'iot-api', path: '/saas' }) // => '//iot-api-dev.safio365.com/saas'
|
|
22
|
+
*
|
|
23
|
+
* // Sensforge 域名系列
|
|
24
|
+
* // location.host: 'api-dev.sensforge.com'
|
|
25
|
+
* baseUrl({ name: 'iot-api' }) // => '//iot-api-dev.sensforge.com'
|
|
26
|
+
*
|
|
27
|
+
* @throws 如果 name 参数为空,将抛出错误并返回 path
|
|
28
|
+
* @throws 如果域名不在支持列表中,将返回 path
|
|
29
|
+
*/
|
|
30
|
+
export declare function baseUrl(params: {
|
|
31
|
+
name: string;
|
|
32
|
+
path?: string;
|
|
33
|
+
}): string | undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/gateway/index.ts
|
|
2
|
+
import { environment, isAWSDomain, region, SUPPORTED_DOMAINS } from "../environment/index";
|
|
3
|
+
import { URLStrategyFactory } from "./factory";
|
|
4
|
+
function getCurrentDomain() {
|
|
5
|
+
const host = location.host;
|
|
6
|
+
const domain = SUPPORTED_DOMAINS.find((domain2) => host.endsWith(domain2));
|
|
7
|
+
if (!domain) {
|
|
8
|
+
throw new Error(`Unsupported domain: ${host}`);
|
|
9
|
+
}
|
|
10
|
+
return domain;
|
|
11
|
+
}
|
|
12
|
+
function baseUrl(params) {
|
|
13
|
+
try {
|
|
14
|
+
if (!params.name) {
|
|
15
|
+
throw new Error("name parameter is required");
|
|
16
|
+
}
|
|
17
|
+
const domain = getCurrentDomain();
|
|
18
|
+
const strategy = URLStrategyFactory.getStrategy(domain);
|
|
19
|
+
const urlParams = {
|
|
20
|
+
name: params.name,
|
|
21
|
+
path: params.path || ""
|
|
22
|
+
};
|
|
23
|
+
if (strategy.features.supportEnv) {
|
|
24
|
+
const env = environment();
|
|
25
|
+
if (env === "unknown") {
|
|
26
|
+
return params.path;
|
|
27
|
+
}
|
|
28
|
+
urlParams.env = env;
|
|
29
|
+
}
|
|
30
|
+
if (strategy.features.supportRegion) {
|
|
31
|
+
urlParams.region = region();
|
|
32
|
+
}
|
|
33
|
+
if (strategy.features.supportAWS) {
|
|
34
|
+
urlParams.isAWS = isAWSDomain();
|
|
35
|
+
}
|
|
36
|
+
return strategy.generateURL(urlParams);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.warn("Failed to generate base URL:", error);
|
|
39
|
+
return params.path;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
baseUrl
|
|
44
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/gateway/strategies/safio365.ts
|
|
2
|
+
var Safio365URLStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: false,
|
|
7
|
+
supportAWS: false
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
generateURL({ name, env = "prod", path = "" }) {
|
|
11
|
+
if (env === "prod" || !env) {
|
|
12
|
+
return `//${name}.safio365.com${path}`;
|
|
13
|
+
}
|
|
14
|
+
return `//${name}-${env}.safio365.com${path}`;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
Safio365URLStrategy
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/gateway/strategies/sensforge.ts
|
|
2
|
+
var SensforgeURLStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: false,
|
|
7
|
+
supportAWS: false
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
generateURL({ name, env = "prod", path = "" }) {
|
|
11
|
+
if (env === "prod" || !env) {
|
|
12
|
+
return `//${name}.sensforge.com${path}`;
|
|
13
|
+
}
|
|
14
|
+
return `//${name}-${env}.sensforge.com${path}`;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
SensforgeURLStrategy
|
|
19
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/gateway/strategies/superacme.ts
|
|
2
|
+
var SuperAcmeURLStrategy = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.features = {
|
|
5
|
+
supportEnv: true,
|
|
6
|
+
supportRegion: true,
|
|
7
|
+
supportAWS: true
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
generateURL({
|
|
11
|
+
name,
|
|
12
|
+
env = "prod",
|
|
13
|
+
region = "",
|
|
14
|
+
isAWS = false,
|
|
15
|
+
path = ""
|
|
16
|
+
}) {
|
|
17
|
+
const regionPart = region ? `-${region}` : "";
|
|
18
|
+
const awsPart = isAWS ? "-aws" : "";
|
|
19
|
+
if (env === "prod" || !env) {
|
|
20
|
+
return `//${name}${regionPart}${awsPart}.superacme.com${path}`;
|
|
21
|
+
}
|
|
22
|
+
return `//${name}${regionPart}-${env}${awsPart}.superacme.com${path}`;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
export {
|
|
26
|
+
SuperAcmeURLStrategy
|
|
27
|
+
};
|
package/dist/esm/global.d.ts
CHANGED
|
@@ -17,6 +17,64 @@ declare global {
|
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
19
|
};
|
|
20
|
+
__bl?: {
|
|
21
|
+
api(params: {
|
|
22
|
+
api: string;
|
|
23
|
+
success: boolean;
|
|
24
|
+
time: number;
|
|
25
|
+
code?: string | number;
|
|
26
|
+
msg?: string;
|
|
27
|
+
begin?: number;
|
|
28
|
+
traceId?: string;
|
|
29
|
+
sid?: string;
|
|
30
|
+
}): void;
|
|
31
|
+
error(e: Error, pos: { filename: string; lineno: number; colno: number }): void;
|
|
32
|
+
sum(key: string, value: number): void;
|
|
33
|
+
avg(key: string, value: number): void;
|
|
34
|
+
reportBehavior(): void;
|
|
35
|
+
performance(params: unknown): void;
|
|
36
|
+
setConfig(params: unknown): void;
|
|
37
|
+
setPage(page: string, sendPv: boolean): void;
|
|
38
|
+
setCommonInfo(params: unknown): void;
|
|
39
|
+
addBehavior(data: { name: string; message: string }, page: string): void;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type URLStrategyConfig = {
|
|
44
|
+
name: string;
|
|
45
|
+
env?: string;
|
|
46
|
+
region?: string;
|
|
47
|
+
isAWS?: boolean;
|
|
48
|
+
path?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
interface DomainFeatures {
|
|
52
|
+
supportEnv: boolean;
|
|
53
|
+
supportRegion: boolean;
|
|
54
|
+
supportAWS: boolean;
|
|
55
|
+
}
|
|
56
|
+
interface URLStrategy {
|
|
57
|
+
features: DomainFeatures;
|
|
58
|
+
generateURL(params: URLStrategyConfig): string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
interface EnvironmentFeatures {
|
|
62
|
+
supportEnv: boolean; // 是否支持环境判断
|
|
63
|
+
supportRegion: boolean; // 是否支持区域判断
|
|
64
|
+
supportAWS: boolean; // 是否支持 AWS 判断
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface EnvironmentStrategy {
|
|
68
|
+
features: EnvironmentFeatures;
|
|
69
|
+
getEnvironment(host: string): string;
|
|
70
|
+
getRegion(host: string): string;
|
|
71
|
+
isAWS(host: string): boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface EnvironmentConfig {
|
|
75
|
+
env?: string;
|
|
76
|
+
region?: string;
|
|
77
|
+
isAWS?: boolean;
|
|
20
78
|
}
|
|
21
79
|
}
|
|
22
80
|
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/hybrid.ts
|
|
2
|
+
import { iOSWebview } from "./platform";
|
|
3
|
+
var queue = [];
|
|
4
|
+
document.addEventListener("WebViewJavascriptBridgeReady", () => {
|
|
5
|
+
queue.forEach(({ method, params, callback }) => {
|
|
6
|
+
if (window.SuperAcme && typeof window.SuperAcme.call === "function") {
|
|
7
|
+
window.SuperAcme.call(method, params, callback);
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
queue.length = 0;
|
|
11
|
+
});
|
|
12
|
+
function bridgeCall(method, params, callback) {
|
|
13
|
+
var _a;
|
|
14
|
+
try {
|
|
15
|
+
if (((_a = window.SuperAcme) == null ? void 0 : _a.call) && typeof window.SuperAcme.call === "function") {
|
|
16
|
+
window.SuperAcme.call(method, params, callback);
|
|
17
|
+
} else {
|
|
18
|
+
queue.push({ method, params, callback });
|
|
19
|
+
}
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("Bridge call failed:", error);
|
|
22
|
+
const errorResponse = {
|
|
23
|
+
success: false,
|
|
24
|
+
error: {
|
|
25
|
+
code: "BRIDGE_CALL_ERROR",
|
|
26
|
+
message: error instanceof Error ? error.message : String(error),
|
|
27
|
+
method,
|
|
28
|
+
params,
|
|
29
|
+
stack: error instanceof Error ? error.stack : void 0,
|
|
30
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
callback(JSON.stringify(errorResponse));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function bridgeBootstrap() {
|
|
37
|
+
function bootstrap(callback) {
|
|
38
|
+
var _a, _b, _c;
|
|
39
|
+
if (window.SuperAcme && typeof window.SuperAcme.call === "function") {
|
|
40
|
+
return callback();
|
|
41
|
+
}
|
|
42
|
+
if (window.WKWVJBCallbacks) {
|
|
43
|
+
return window.WKWVJBCallbacks.push(callback);
|
|
44
|
+
}
|
|
45
|
+
window.WKWVJBCallbacks = [callback];
|
|
46
|
+
if (window.webkit) {
|
|
47
|
+
(_c = (_b = (_a = window.webkit.messageHandlers) == null ? void 0 : _a.iOS_Native_InjectJavascript) == null ? void 0 : _b.postMessage) == null ? void 0 : _c.call(_b, null);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (iOSWebview) {
|
|
51
|
+
bootstrap(() => {
|
|
52
|
+
const ready = new CustomEvent("WebViewJavascriptBridgeReady", {
|
|
53
|
+
detail: { platform: "ios" }
|
|
54
|
+
});
|
|
55
|
+
document.dispatchEvent(ready);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function inSuperAcme() {
|
|
60
|
+
return navigator.userAgent.toLowerCase().includes("superacme");
|
|
61
|
+
}
|
|
62
|
+
var VERSION_REGEX = /AppVersion\s([\d.]+)/;
|
|
63
|
+
function superAcmeVersion() {
|
|
64
|
+
const ua = navigator.userAgent;
|
|
65
|
+
const UNKNOWN = {
|
|
66
|
+
version: "unknown"
|
|
67
|
+
};
|
|
68
|
+
if (!inSuperAcme()) {
|
|
69
|
+
return UNKNOWN;
|
|
70
|
+
}
|
|
71
|
+
const versionMatch = ua.match(VERSION_REGEX);
|
|
72
|
+
if (versionMatch) {
|
|
73
|
+
const version = versionMatch[1];
|
|
74
|
+
const [major, minor, patch] = version.split(".").map(Number);
|
|
75
|
+
return {
|
|
76
|
+
version,
|
|
77
|
+
major,
|
|
78
|
+
minor,
|
|
79
|
+
patch
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return UNKNOWN;
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
bridgeBootstrap,
|
|
86
|
+
bridgeCall,
|
|
87
|
+
inSuperAcme,
|
|
88
|
+
superAcmeVersion
|
|
89
|
+
};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './arms';
|
|
2
|
+
export * from './cookie';
|
|
3
|
+
export * from './environment/index';
|
|
4
|
+
export * from './gateway/index';
|
|
3
5
|
export * from './hybrid';
|
|
4
6
|
export * from './local';
|
|
5
7
|
export * from './platform';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/local.ts
|
|
2
|
+
function getLocale(defaultLocal = "zh-CN") {
|
|
3
|
+
let lang = defaultLocal;
|
|
4
|
+
const { search } = window.location;
|
|
5
|
+
const { userAgent } = window.navigator;
|
|
6
|
+
if (userAgent.toLocaleLowerCase().indexOf("en-us") > -1) {
|
|
7
|
+
lang = "en-US";
|
|
8
|
+
}
|
|
9
|
+
if (search) {
|
|
10
|
+
const queryParams = {};
|
|
11
|
+
const searchString = search.substring(1);
|
|
12
|
+
const pairs = searchString.split("&");
|
|
13
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
14
|
+
const pair = pairs[i].split("=");
|
|
15
|
+
const key = decodeURIComponent(pair[0]);
|
|
16
|
+
const value = decodeURIComponent(pair[1] || "");
|
|
17
|
+
queryParams[key] = value;
|
|
18
|
+
}
|
|
19
|
+
if (queryParams.locale) {
|
|
20
|
+
lang = queryParams.locale;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return lang;
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
getLocale
|
|
27
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/platform.ts
|
|
2
|
+
var getUserAgent = () => window.navigator.userAgent.toLowerCase();
|
|
3
|
+
var iOSWebview = (() => {
|
|
4
|
+
const ua = getUserAgent();
|
|
5
|
+
return ua.includes("ios") || ua.includes("iphone") || ua.includes("ipad") || ua.includes("ipod") || ua.includes("mac") && navigator.maxTouchPoints > 0;
|
|
6
|
+
})();
|
|
7
|
+
var androidWebView = (() => {
|
|
8
|
+
const ua = getUserAgent();
|
|
9
|
+
return ua.includes("android");
|
|
10
|
+
})();
|
|
11
|
+
export {
|
|
12
|
+
androidWebView,
|
|
13
|
+
iOSWebview
|
|
14
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microbt/environment",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "microbt app library for environment",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"main": "dist/
|
|
7
|
+
"main": "dist/esm/index.js",
|
|
8
8
|
"types": "dist/esm/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "father build",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"format": "prettier --cache --write .",
|
|
14
14
|
"lint": "eslint . --fix ",
|
|
15
15
|
"prepare": "husky install",
|
|
16
|
-
"prepublishOnly": "father doctor && npm run
|
|
16
|
+
"prepublishOnly": "father doctor && npm run build",
|
|
17
17
|
"postpublish": "cnpm sync @microbt/environment",
|
|
18
18
|
"test": "jest"
|
|
19
19
|
},
|
|
@@ -30,7 +30,9 @@
|
|
|
30
30
|
"prettier --cache --parser=typescript --write"
|
|
31
31
|
]
|
|
32
32
|
},
|
|
33
|
-
"dependencies": {
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"alife-logger": "^1.8.30"
|
|
35
|
+
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"@commitlint/cli": "^17.4.4",
|
|
36
38
|
"@commitlint/config-conventional": "^17.4.4",
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 获取当前运行的环境
|
|
3
|
-
* @returns {string} 环境名称(local, dev, test, pre, gray, prod, unknown)
|
|
4
|
-
*/
|
|
5
|
-
export declare const environment: () => string;
|
|
6
|
-
/**
|
|
7
|
-
* 获取当前运行的区域
|
|
8
|
-
* @returns {string} 区域名称(如 cn, us 等)
|
|
9
|
-
*/
|
|
10
|
-
export declare const region: () => string;
|
|
11
|
-
/**
|
|
12
|
-
* 判断当前域名是否为 AWS 域名
|
|
13
|
-
* @returns {boolean} 是否为 AWS 域名
|
|
14
|
-
*/
|
|
15
|
-
export declare function isAWSDomain(): boolean;
|
package/dist/esm/gateway.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function baseUrl(): string | undefined;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var o in r)("object"==typeof exports?exports:e)[o]=r[o]}}(self,(function(){return function(){var e={803:function(e){e.exports=function(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,o=new Array(t);r<t;r++)o[r]=e[r];return o},e.exports.__esModule=!0,e.exports.default=e.exports},473:function(e){e.exports=function(e){if(Array.isArray(e))return e},e.exports.__esModule=!0,e.exports.default=e.exports},502:function(e,t,r){var o=r(582);e.exports=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&o(e,t)},e.exports.__esModule=!0,e.exports.default=e.exports},941:function(e){e.exports=function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var o,n,u,i,a=[],c=!0,s=!1;try{if(u=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(o=u.call(r)).done)&&(a.push(o.value),a.length!==t);c=!0);}catch(e){s=!0,n=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(s)throw n}}return a}},e.exports.__esModule=!0,e.exports.default=e.exports},551:function(e){e.exports=function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")},e.exports.__esModule=!0,e.exports.default=e.exports},582:function(e){function t(r,o){return e.exports=t=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},e.exports.__esModule=!0,e.exports.default=e.exports,t(r,o)}e.exports=t,e.exports.__esModule=!0,e.exports.default=e.exports},216:function(e,t,r){var o=r(473),n=r(941),u=r(317),i=r(551);e.exports=function(e,t){return o(e)||n(e,t)||u(e,t)||i()},e.exports.__esModule=!0,e.exports.default=e.exports},71:function(e){function t(r){return e.exports=t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e.exports.__esModule=!0,e.exports.default=e.exports,t(r)}e.exports=t,e.exports.__esModule=!0,e.exports.default=e.exports},317:function(e,t,r){var o=r(803);e.exports=function(e,t){if(e){if("string"==typeof e)return o(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(e,t):void 0}},e.exports.__esModule=!0,e.exports.default=e.exports},583:function(e,t,r){var o=r(71).default,n=r(582),u=r(502);function i(){e.exports=i=function(e,t){return new a(e,void 0,t)},e.exports.__esModule=!0,e.exports.default=e.exports;var t=RegExp.prototype,r=new WeakMap;function a(e,t,o){var u=new RegExp(e,t);return r.set(u,o||r.get(e)),n(u,a.prototype)}function c(e,t){var o=r.get(t);return Object.keys(o).reduce((function(t,r){var n=o[r];if("number"==typeof n)t[r]=e[n];else{for(var u=0;void 0===e[n[u]]&&u+1<n.length;)u++;t[r]=e[n[u]]}return t}),Object.create(null))}return u(a,RegExp),a.prototype.exec=function(e){var r=t.exec.call(this,e);if(r){r.groups=c(r,this);var o=r.indices;o&&(o.groups=c(o,this))}return r},a.prototype[Symbol.replace]=function(e,n){if("string"==typeof n){var u=r.get(this);return t[Symbol.replace].call(this,e,n.replace(/\$<([^>]+)>/g,(function(e,t){var r=u[t];return"$"+(Array.isArray(r)?r.join("$"):r)})))}if("function"==typeof n){var i=this;return t[Symbol.replace].call(this,e,(function(){var e=arguments;return"object"!=o(e[e.length-1])&&(e=[].slice.call(e)).push(c(e,i)),n.apply(this,e)}))}return t[Symbol.replace].call(this,e,n)},i.apply(this,arguments)}e.exports=i,e.exports.__esModule=!0,e.exports.default=e.exports}},t={};function r(o){var n=t[o];if(void 0!==n)return n.exports;var u=t[o]={exports:{}};return e[o](u,u.exports,r),u.exports}r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,{a:t}),t},r.d=function(e,t){for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return function(){"use strict";r.r(o),r.d(o,{androidWebView:function(){return x},baseUrl:function(){return f},bridgeBootstrap:function(){return g},bridgeCall:function(){return b},environment:function(){return s},getLocale:function(){return S},iOSWebview:function(){return y},inSuperAcme:function(){return h},isAWSDomain:function(){return l},region:function(){return p},superAcmeVersion:function(){return _}});var e=r(583),t=r.n(e)()(/(dev|test|pre|gray)/,{env:1}),n=["us","usa","cn","chn","jp","jpn","de","deu","uk","gbr","fr","fra","ca","can","sg","sgp","au","aus","kr","kor","in","ind","br","bra","ru","rus","za","zaf","mx","mex","id","idn","tr","tur","sa","sau","ch","che","tw","twn","hk","hkg","mo","mac"],u=new RegExp("-(?<region>".concat(n.join("|"),")(-|$|\\.)")),i=/-aws\./,a=/^(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d+)?$/,c=new RegExp("^(?:[a-z0-9-]+\\.)*(?:".concat(["superacme.com","cinmoore.com","cinmoore.cn","safio365.com","sensforge.com"].join("|"),")$")),s=function(){var e=location.host;if(a.test(e))return"local";if(!c.test(e))return"unknown";var r=e.match(t);return r&&r.groups&&r.groups.env?r.groups.env:"prod"},p=function(){var e=location.host;if(!c.test(e))return"unknown";var t=e.match(new RegExp("-(".concat(n.join("|"),")-(dev|test|pre|gray)(?=-|\\.|$)")));if(t)return t[1];var r=e.match(u);return r&&r.groups&&r.groups.region?r.groups.region:"unknown"};function l(){var e=location.host;return!!c.test(e)&&i.test(e)}function f(){var e=s(),t=p();return"dev"===e||"test"===e||"pre"===e?"//api-".concat(t,"-").concat(e).concat(l()?"-aws":"",".superacme.com").concat(""):"prod"===e?"//api-".concat(t).concat(l()?"-aws":"",".superacme.com").concat(""):void 0}var d,v=r(216),m=r.n(v),y=(d=window.navigator.userAgent.toLocaleLowerCase()).includes("ios")||d.includes("iphone")||d.includes("ipad"),x=window.navigator.userAgent.toLocaleLowerCase().includes("android"),w=[];function b(e,t,r){window.SuperAcme&&"function"==typeof window.SuperAcme.call?window.SuperAcme.call(e,t,r):w.push({method:e,params:t,callback:r})}function g(){y&&function(e){if(window.SuperAcme&&"function"==typeof window.SuperAcme.call)return e();if(window.WKWVJBCallbacks)return window.WKWVJBCallbacks.push(e);var t,r;window.WKWVJBCallbacks=[e],window.webkit&&(null===(t=window.webkit.messageHandlers)||void 0===t||null===(t=t.iOS_Native_InjectJavascript)||void 0===t||null===(r=t.postMessage)||void 0===r||r.call(t,null))}((function(){var e=new CustomEvent("WebViewJavascriptBridgeReady",{detail:{platform:"ios"}});document.dispatchEvent(e)}))}function h(){return navigator.userAgent.toLowerCase().includes("superacme")}function _(){var e=navigator.userAgent,t={version:"unknown"};if(!h())return t;var r=e.match(/AppVersion\s([\d.]+)/);if(r){var o=r[1],n=o.split(".").map(Number),u=m()(n,3);return{version:o,major:u[0],minor:u[1],patch:u[2]}}return t}function S(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"zh-CN",t=e,r=window.location.search,o=window.navigator.userAgent;if(o.toLocaleLowerCase().indexOf("en-us")>-1&&(t="en-US"),r){for(var n={},u=r.substring(1),i=u.split("&"),a=0;a<i.length;a++){var c=i[a].split("="),s=decodeURIComponent(c[0]),p=decodeURIComponent(c[1]||"");n[s]=p}n.locale&&(t=n.locale)}return t}document.addEventListener("WebViewJavascriptBridgeReady",(function(){w.forEach((function(e){var t=e.method,r=e.params,o=e.callback;window.SuperAcme&&"function"==typeof window.SuperAcme.call&&window.SuperAcme.call(t,r,o)})),w.length=0}))}(),o}()}));
|