@talknote/create-talknote-h5 0.0.8 → 0.1.0
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/package.json +1 -1
- package/template/README.md +5 -5
- package/template/package.json +3 -3
- package/template/scripts/dev.js +45 -0
- package/template/src/app.tsx +30 -3
package/package.json
CHANGED
package/template/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
<h1 align="center">Alita Pro</h1>
|
|
2
2
|
|
|
3
|
-
帮助你快速使用 alita
|
|
3
|
+
帮助你快速使用 alita, 基于Umi 的移动端开发框架
|
|
4
4
|
|
|
5
5
|
```
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
yarn
|
|
7
|
+
yarn dev
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
### 首页
|
|
@@ -132,13 +132,13 @@ useEffect(() => {
|
|
|
132
132
|
### 格式化
|
|
133
133
|
|
|
134
134
|
```
|
|
135
|
-
|
|
135
|
+
yarn format
|
|
136
136
|
```
|
|
137
137
|
|
|
138
138
|
### 构建
|
|
139
139
|
|
|
140
140
|
```
|
|
141
|
-
|
|
141
|
+
yarn build
|
|
142
142
|
```
|
|
143
143
|
|
|
144
144
|
### 调试工具
|
package/template/package.json
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
"private": true,
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "alita build",
|
|
6
|
-
"dev": "
|
|
6
|
+
"dev": "node scripts/dev.js",
|
|
7
7
|
"format": "prettier --write .",
|
|
8
8
|
"lint-staged": "lint-staged",
|
|
9
9
|
"lint-staged:js": "eslint --ext .ts,.tsx",
|
|
10
10
|
"plugin": "alita plugin list",
|
|
11
|
-
"start": "
|
|
11
|
+
"start": "node scripts/dev.js",
|
|
12
12
|
"test": "jest"
|
|
13
13
|
},
|
|
14
14
|
"lint-staged": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
]
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@talknote/h5-bridge":"^0.0.
|
|
24
|
+
"@talknote/h5-bridge":"^0.0.4",
|
|
25
25
|
"@alita/flow": "^3.1.1",
|
|
26
26
|
"alita": "^3.2.27",
|
|
27
27
|
"antd-mobile": "^5.15.1",
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 开发启动脚本
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* yarn dev # 使用 proxy.ts 中的默认代理地址
|
|
7
|
+
* yarn dev http://1.2.3.4:10000 # 把 process.env.BASE_URL 注入为该地址,代理过去
|
|
8
|
+
* yarn dev http://1.2.3.4:10000 --port 8081 # 额外参数透传给 alita dev
|
|
9
|
+
*
|
|
10
|
+
* 说明:
|
|
11
|
+
* 第一个以 http:// 或 https:// 开头的参数会被当作代理地址,
|
|
12
|
+
* 注入 process.env.BASE_URL(proxy.ts 会读取它)。
|
|
13
|
+
* 其余参数原样透传给 alita dev。
|
|
14
|
+
*/
|
|
15
|
+
const { spawn } = require('child_process');
|
|
16
|
+
|
|
17
|
+
const args = process.argv.slice(2);
|
|
18
|
+
|
|
19
|
+
// 找出第一个 http/https 开头的参数作为代理地址
|
|
20
|
+
const proxyIndex = args.findIndex((a) => /^https?:\/\//i.test(a));
|
|
21
|
+
const proxyUrl = proxyIndex >= 0 ? args.splice(proxyIndex, 1)[0] : undefined;
|
|
22
|
+
|
|
23
|
+
if (proxyUrl) {
|
|
24
|
+
process.env.BASE_URL = proxyUrl;
|
|
25
|
+
console.log(`\n[dev] 代理地址已注入 BASE_URL = ${proxyUrl}\n`);
|
|
26
|
+
} else {
|
|
27
|
+
console.log('\n[dev] 未指定代理地址,使用 proxy.ts 中的默认值\n');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 启动 alita dev,剩余参数透传
|
|
31
|
+
// shell: true 仅为兼容 Windows 下的 .cmd 可执行文件
|
|
32
|
+
const child = spawn('alita', ['dev', ...args], {
|
|
33
|
+
stdio: 'inherit',
|
|
34
|
+
env: process.env,
|
|
35
|
+
shell: process.platform === 'win32',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
child.on('error', (err) => {
|
|
39
|
+
console.error('[dev] 启动 alita 失败:', err.message);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
child.on('close', (code) => {
|
|
44
|
+
process.exit(code ?? 0);
|
|
45
|
+
});
|
package/template/src/app.tsx
CHANGED
|
@@ -10,14 +10,41 @@ import type {
|
|
|
10
10
|
TabBarProps,
|
|
11
11
|
TitleListItem,
|
|
12
12
|
} from 'alita';
|
|
13
|
-
import TalkNoteH5Bridge from '@talknote/h5-bridge';
|
|
13
|
+
import TalkNoteH5Bridge, { type RequestBaseInfo } from '@talknote/h5-bridge';
|
|
14
|
+
|
|
15
|
+
// 缓存原生请求信息,避免每次请求都调用 bridge
|
|
16
|
+
let cachedRequestInfo: RequestBaseInfo | null = null;
|
|
17
|
+
|
|
18
|
+
async function getRequestInfo(): Promise<RequestBaseInfo | null> {
|
|
19
|
+
if (cachedRequestInfo) return cachedRequestInfo;
|
|
20
|
+
try {
|
|
21
|
+
cachedRequestInfo = await TalkNoteH5Bridge.getRequestBaseInfo();
|
|
22
|
+
return cachedRequestInfo;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error('获取原生请求信息失败:', error);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
14
28
|
|
|
15
29
|
export const request = {
|
|
16
|
-
prefix: '
|
|
30
|
+
prefix: '',
|
|
17
31
|
method: 'get',
|
|
32
|
+
requestInterceptors: [
|
|
33
|
+
async (url: string, options: any) => {
|
|
34
|
+
const info = await getRequestInfo();
|
|
35
|
+
if (info) {
|
|
36
|
+
const headers: Record<string, string> = { ...(info.headers || {}) };
|
|
37
|
+
if (!headers.token && info.token) {
|
|
38
|
+
headers.Authorization = info.token;
|
|
39
|
+
}
|
|
40
|
+
// 业务请求自带的 header 优先级更高
|
|
41
|
+
options.headers = { ...headers, ...(options.headers || {}) };
|
|
42
|
+
}
|
|
43
|
+
return { url, options };
|
|
44
|
+
},
|
|
45
|
+
],
|
|
18
46
|
errorHandler: (error: ResponseError) => {
|
|
19
47
|
// 集中处理错误
|
|
20
|
-
console.log(11111111);
|
|
21
48
|
console.log(error);
|
|
22
49
|
},
|
|
23
50
|
};
|