neo-cmp-cli 1.8.11 → 1.8.12

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.
Files changed (3) hide show
  1. package/README.md +12 -0
  2. package/package.json +1 -1
  3. package/test/demo6.js +90 -0
package/README.md CHANGED
@@ -120,6 +120,18 @@ neo push cmp
120
120
  | `neo pull cmp` | 拉取线上自定义组件(NeoCRM 平台)至当前项目 | `--name` 指定组件名称 |
121
121
  | `neo delete cmp` | 删除线上自定义组件(NeoCRM 平台) | `--name` 指定组件名称 |
122
122
 
123
+ ## 常见问题
124
+
125
+ **Q1: 在 Windows VSCode 控制台执行 neo 相关命令出现报错**
126
+ A: 如果提示“在此系统上禁止运行脚本”,这个可能是 Windows 客户端的默认安全策略影响,可尝试在控制台输入以下命令解决:
127
+
128
+ ```bash
129
+ Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
130
+ ```
131
+
132
+ 说明:以上命令用于设置 允许当前用户在此 Windows 客户端执行所有脚本。
133
+
134
+
123
135
  ---
124
136
 
125
137
  ## 🔐 授权配置
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neo-cmp-cli",
3
- "version": "1.8.11",
3
+ "version": "1.8.12",
4
4
  "description": "Neo 自定义组件开发工具,支持react 和 vue2.0技术栈。",
5
5
  "keywords": [
6
6
  "neo-cli",
package/test/demo6.js ADDED
@@ -0,0 +1,90 @@
1
+ /**
2
+ * 基于XMLHttpRequest的通用请求方法
3
+ * @param {string} url - 请求地址
4
+ * @param {object} options - 请求配置
5
+ * @param {string} [options.method='GET'] - 请求方法
6
+ * @param {object} [options.params={}] - URL查询参数(GET/DELETE)
7
+ * @param {object} [options.data={}] - 请求体数据(POST/PUT)
8
+ * @param {object} [options.headers={}] - 请求头
9
+ * @param {number} [options.timeout=5000] - 超时时间(毫秒)
10
+ * @returns {Promise} - 返回Promise对象
11
+ */
12
+ function requestXHR(url, options = {}) {
13
+ const {
14
+ method = 'GET',
15
+ params = {},
16
+ data = {},
17
+ headers = {},
18
+ timeout = 5000
19
+ } = options;
20
+
21
+ return new Promise((resolve, reject) => {
22
+ // 1. 创建XHR对象
23
+ const xhr = new XMLHttpRequest();
24
+
25
+ // 2. 处理GET参数,拼接到URL
26
+ const queryString = Object.entries(params)
27
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
28
+ .join('&');
29
+ const fullUrl = queryString ? `${url}?${queryString}` : url;
30
+
31
+ // 3. 初始化请求
32
+ xhr.open(method.toUpperCase(), fullUrl, true);
33
+
34
+ // 4. 设置请求头
35
+ // 默认JSON请求头(POST/PUT)
36
+ if (['POST', 'PUT'].includes(method.toUpperCase())) {
37
+ xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
38
+ }
39
+ // 自定义请求头
40
+ Object.entries(headers).forEach(([key, value]) => {
41
+ xhr.setRequestHeader(key, value);
42
+ });
43
+
44
+ // 5. 设置超时
45
+ xhr.timeout = timeout;
46
+
47
+ // 6. 处理响应
48
+ xhr.onload = function () {
49
+ if (xhr.status >= 200 && xhr.status < 300) {
50
+ // 解析响应数据(优先JSON,其次文本)
51
+ let responseData;
52
+ try {
53
+ responseData = JSON.parse(xhr.responseText);
54
+ } catch (e) {
55
+ responseData = xhr.responseText;
56
+ }
57
+ resolve({
58
+ data: responseData,
59
+ status: xhr.status,
60
+ statusText: xhr.statusText
61
+ });
62
+ } else {
63
+ reject(new Error(`请求失败:${xhr.status} ${xhr.statusText}`));
64
+ }
65
+ };
66
+
67
+ // 7. 处理网络错误、超时
68
+ xhr.onerror = function () {
69
+ reject(new Error('网络错误,请检查网络连接'));
70
+ };
71
+ xhr.ontimeout = function () {
72
+ reject(new Error(`请求超时(${timeout}ms)`));
73
+ };
74
+
75
+ // 8. 发送请求(POST/PUT需传请求体)
76
+ if (['POST', 'PUT'].includes(method.toUpperCase())) {
77
+ xhr.send(JSON.stringify(data));
78
+ } else {
79
+ xhr.send(null);
80
+ }
81
+ });
82
+ }
83
+
84
+ // GET请求
85
+ requestXHR('https://jsonplaceholder.typicode.com/posts', {
86
+ params: { userId: 1 },
87
+ timeout: 10000
88
+ })
89
+ .then(res => console.log('GET请求成功:', res))
90
+ .catch(err => console.error('GET请求失败:', err));