mm_os 2.8.7 → 2.8.9

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 (53) hide show
  1. package/core/base/index.js +28 -28
  2. package/core/base/mqtt/index.js +314 -314
  3. package/core/base/mqtt/lib.js +39 -39
  4. package/core/base/web/index.js +109 -109
  5. package/core/com/mqtt/index.js +2 -6
  6. package/core/com/nav/tpl/admin_pc/page_channel.vue +137 -131
  7. package/core/com/nav/tpl/admin_pc/page_config.vue +280 -279
  8. package/core/com/nav/tpl/admin_pc/page_config_form.vue +194 -194
  9. package/core/com/nav/tpl/admin_pc/page_default.vue +47 -87
  10. package/core/com/nav/tpl/admin_pc/page_form.vue +19 -19
  11. package/core/com/nav/tpl/admin_pc/page_lang.vue +44 -45
  12. package/core/com/nav/tpl/admin_pc/page_nav.vue +140 -132
  13. package/core/com/nav/tpl/admin_pc/page_table.vue +135 -124
  14. package/core/com/nav/tpl/admin_pc/page_type.vue +139 -131
  15. package/core/com/nav/tpl/admin_pc/page_view.vue +124 -0
  16. package/core/com/nav/tpl/dev_pc/page_channel.vue +34 -34
  17. package/core/com/nav/tpl/dev_pc/page_config.vue +26 -26
  18. package/core/com/nav/tpl/dev_pc/page_default.vue +4 -4
  19. package/core/com/nav/tpl/dev_pc/page_form.vue +2 -2
  20. package/core/com/nav/tpl/dev_pc/page_nav.vue +34 -34
  21. package/core/com/nav/tpl/dev_pc/page_table.vue +34 -34
  22. package/core/com/nav/tpl/dev_pc/page_type.vue +131 -71
  23. package/core/com/nav/tpl/home_pc/page_default.vue +2 -2
  24. package/core/com/nav/tpl/home_pc/page_form.vue +2 -2
  25. package/core/com/nav/tpl/home_pc/page_view.vue +2 -2
  26. package/core/com/nav/tpl/home_phone/page_form.vue +2 -2
  27. package/core/com/nav/tpl/home_phone/page_list.vue +2 -2
  28. package/core/com/nav/tpl/home_phone/page_view.vue +2 -2
  29. package/demo/index.js +28 -0
  30. package/middleware/cors/index.js +84 -84
  31. package/middleware/cors/middleware.json +6 -6
  32. package/middleware/log/index.js +20 -20
  33. package/middleware/log/middleware.json +8 -8
  34. package/middleware/mqtt_base/index.js +10 -10
  35. package/middleware/mqtt_base/middleware.json +7 -7
  36. package/middleware/waf/index.js +64 -64
  37. package/middleware/waf/middleware.json +8 -8
  38. package/middleware/waf_ip/index.js +115 -115
  39. package/middleware/waf_ip/middleware.json +9 -9
  40. package/middleware/web_base/index.js +65 -65
  41. package/middleware/web_base/middleware.json +8 -8
  42. package/middleware/web_event/index.js +412 -412
  43. package/middleware/web_event/middleware.json +9 -9
  44. package/middleware/web_proxy/index.js +23 -23
  45. package/middleware/web_proxy/middleware.json +8 -8
  46. package/middleware/web_router/index.js +33 -33
  47. package/middleware/web_router/middleware.json +9 -9
  48. package/middleware/web_socket/index.js +21 -21
  49. package/middleware/web_socket/middleware.json +8 -8
  50. package/middleware/web_static/index.js +25 -25
  51. package/middleware/web_static/middleware.json +8 -8
  52. package/package.json +8 -8
  53. package/static/file/image/user/42/2025_01_02_10_58_15.png +0 -0
@@ -1,71 +1,85 @@
1
1
  <template>
2
2
  <main id="${id}">
3
- <mm_warp>
4
- <mm_container>
5
- <mm_row>
6
- <mm_col class="col-12">
7
- <mm_card>
8
- <div class="card_head arrow">
3
+ <div class="mm_warp">
4
+ <div class="mm_container">
5
+ <div class="mm_row">
6
+ <div class="mm_col col-12">
7
+ <div class="mm_card" :class="{ 'hide_filter': !show_filter }">
8
+ <div class="card_head arrow" @click="show_filter = !show_filter">
9
9
  <h5>${api.title}</h5>
10
10
  </div>
11
11
  <div class="card_body">
12
- <mm_form class="bar_filter">
12
+ <div class="mm_form" class="bar_filter">
13
13
  <div class="title">
14
14
  <h5><span>筛选条件</span></h5>
15
15
  </div>
16
- <mm_list :col="3">
17
- <!--{if(param.list)}-->
16
+ <div class="mm_list list-3">
18
17
  <!--{loop param.list v idx}-->
19
- <!--{if(v.name == 'keyword')}-->
20
- <mm_item>
18
+ <!--{if(v.name === 'keyword')}-->
19
+ <div class="mm_item">
21
20
  <control_input v-model="query.keyword" title="${v.title}" desc="${v.description.replace(/\([0-9A-Za-z_]+\)/g, '').replace('用于搜索', '').replace(/、/g, ' / ')}"
22
- @blur="search()" />
23
- </mm_item>
21
+ />
22
+ </div>
24
23
  <!--{/if}-->
25
24
  <!--{/loop}-->
26
- <!--{/if}-->
27
25
  <!--{loop field v idx}-->
28
- <!--{if(v.format)}-->
26
+ <!--{if(v.show.search)}-->
27
+ <!--{if(v.show.search === "input")}-->
28
+ <div class="mm_item">
29
+ <control_input v-model="query.${v.name}" title="${v.title}" @blur="search()" />
30
+ </div>
31
+ <!--{else if(v.show.search === "select")}-->
29
32
  <!--{if(v.format.table)}-->
30
- <mm_item>
33
+ <div class="mm_item">
34
+ <!--{if(v.format.key.endWith('_id') !== -1)}-->
35
+ <control_select type="list" v-model="query.${v.format.key}" title="${v.title}" :options="$to_kv(${v.label}, '${v.format.id || v.format.key}', '${v.format.name}')" @change="search()" />
36
+ <!--{else}-->
31
37
  <control_select v-model="query.${v.format.key}" title="${v.title}" :options="$to_kv(${v.label}, '${v.format.id || v.format.key}', '${v.format.name}')"
32
38
  @change="search()" />
33
- </mm_item>
39
+ <!--{/if}-->
40
+ </div>
41
+ <!--{else if(v.format.key.indexOf('user_id') !== -1)}-->
42
+ <div class="mm_item">
43
+ <control_select v-model="query.${v.format.key}" title="${v.title}" :options="$to_kv(${v.label})" @change="search()" />
44
+ </div>
34
45
  <!--{else}-->
35
- <mm_item>
46
+ <div class="mm_item">
36
47
  <control_select v-model="query.${v.format.key}" title="${v.title}" :options="$to_kv(${v.label})" @change="search()" />
37
- </mm_item>
48
+ </div>
49
+ <!--{/if}-->
38
50
  <!--{/if}-->
39
51
  <!--{/if}-->
40
52
  <!--{/loop}-->
41
- <mm_item>
42
- <mm_btn class="btn_primary-x" type="reset" @click.native="reset();search()">重置</mm_btn>
43
- </mm_item>
44
- </mm_list>
45
- </mm_form>
53
+ <div class="mm_item">
54
+ <button class="btn_primary-x" type="reset" @click.native="reset();">重置</button>
55
+ </div>
56
+ </div>
57
+ </div>
46
58
  <div class="bar_action">
47
59
  <h5><span>操作</span></h5>
48
60
  <div class="btns">
49
- <mm_btn class="btn_primary-x" url="./${name}_form?">添加</mm_btn>
50
- <mm_btn @click.native="show = true" class="btn_primary-x" v-bind:class="{ 'disabled': !selects }">批量修改</mm_btn>
61
+ <button class="btn_primary-x" url="./${name}_form?">添加</button>
62
+ <button @click.native="show = true" class="btn_primary-x" v-bind:class="{ 'disabled': !selects }">批量修改</button>
51
63
  </div>
52
64
  <div class="btn_small">
53
65
  <control_file class="btn_default-x" type="excel" :func="import_db" v-if="url_import"></control_file>
54
- <mm_btn class="btn_default-x" @click.native="export_db()" v-if="url_export">导出</mm_btn>
66
+ <button class="btn_default-x" @click.native="export_db()" v-if="url_export">导出</button>
55
67
  </div>
56
68
  </div>
57
- <mm_table type="3">
69
+ <div class="mm_table table-3">
58
70
  <thead class="table-sm">
59
71
  <tr>
60
72
  <th class="th_open"></th>
61
73
  <th class="th_selected"><input type="checkbox" :checked="select_state" @click="select_all()" /></th>
62
74
  <th class="th_id"><span>#</span></th>
63
75
  <!--{loop field v idx}-->
76
+ <!--{if(v.show.table)}-->
64
77
  <!--{if(v.name !== sql.key)}-->
65
- <th>
66
- <control_reverse title="${v.title}" v-model="query.orderby" field="${v.name}" :func="search"></control_reverse>
78
+ <th class="th_${v.name}">
79
+ <span>${v.title}</span>
67
80
  </th>
68
81
  <!--{/if}-->
82
+ <!--{/if}-->
69
83
  <!--{/loop}-->
70
84
  <th class="th_handle"><span>操作</span></th>
71
85
  </tr>
@@ -79,6 +93,7 @@
79
93
  <th class="th_selected"><input type="checkbox" :checked="select_has(o[field])" @click="select_change(o[field])" /></th>
80
94
  <td>{{ o[field] }}</td>
81
95
  <!--{loop field v idx}-->
96
+ <!--{if(v.show.table)}-->
82
97
  <!--{if(v.name !== sql.key)}-->
83
98
  <td>
84
99
  <!--{if(v.dataType === 'tinyint')}-->
@@ -106,51 +121,75 @@
106
121
  <!--{/if}-->
107
122
  </td>
108
123
  <!--{/if}-->
124
+ <!--{/if}-->
109
125
  <!--{/loop}-->
110
126
  <td>
111
- <mm_btn class="btn_primary" :url="'./${name}_form?${sql.key}=' + o[field]">修改</mm_btn>
112
- <mm_btn class="btn_warning" @click.native="del_show(o, field)">删除</mm_btn>
127
+ <button class="btn_primary" :url="'./${name}_form?${sql.key}=' + o[field]">修改</button>
128
+ <button class="btn_warning" @click.native="del_show(o, field)">删除</button>
113
129
  </td>
114
130
  </tr>
115
131
  </tbody>
116
132
  <!-- </draggable> -->
117
- </mm_table>
133
+
134
+ <template v-slot:right>
135
+ <thead class="table-sm">
136
+ <tr>
137
+ <th class="th_handle"><span>操作</span></th>
138
+ </tr>
139
+ </thead>
140
+ <tbody>
141
+ <tr v-for="(o, idx) in list_new" :key="idx" :class="{'active': select == idx, sub: o[father_id], open: opens_has(o[field]), no_sub: !opens_has_sub(o[field]) }"
142
+ @click="selected(idx)">
143
+ <td>
144
+ <button class="mm_btn btn_primary" :url="'./${name}_form?${sql.key}=' + o[field]">修改</button>
145
+ <button class="mm_btn btn_warning" @click.native="del_show(o, field)">删除</button>
146
+ </td>
147
+ </tr>
148
+ </tbody>
149
+ </template>
150
+ </div>
118
151
  </div>
119
- </mm_card>
120
- </mm_col>
121
- </mm_row>
122
- </mm_container>
123
- </mm_warp>
152
+ </div>
153
+ </div>
154
+ </div>
155
+ </div>
156
+ </div>
124
157
  <mm_modal v-model="show" mask="true">
125
- <mm_card class="card">
158
+ <div class="mm_card" class="card">
126
159
  <div class="card_head">
127
160
  <h5>批量修改</h5>
128
161
  </div>
129
162
  <div class="card_body pa">
130
163
  <dl>
131
164
  <!--{loop field v idx}-->
132
- <!--{if(v.format)}-->
165
+ <!--{if(v.show.batch)}-->
133
166
  <dt>${v.title}</dt>
134
- <!--{if(v.format.table)}-->
135
- <dd>
136
- <control_select v-model="form.${v.format.key}" :options="$to_kv(${v.label}, '${v.format.id || v.format.key}', '${v.format.name}')" />
137
- </dd>
138
- <!--{else}-->
139
167
  <dd>
168
+ <!--{if(v.show.batch === "input")}-->
169
+ <control_input v-model="form.${v.name}" />
170
+ <!--{else if(v.show.batch === "select")}-->
171
+ <!--{if(v.format.table)}-->
172
+ <!--{if(v.format.key.endWith('_id') !== -1)}-->
173
+ <control_select type="list" v-model="form.${v.format.key}" :options="$to_kv(${v.label}, '${v.format.id || v.format.key}', '${v.format.name}')" />
174
+ <!--{else}-->
175
+ <control_select v-model="form.${v.format.key}" :options="$to_kv(${v.label}, '${v.format.id || v.format.key}', '${v.format.name}')" />
176
+ <!--{/if}-->
177
+ <!--{else}-->
140
178
  <control_select v-model="form.${v.format.key}" :options="$to_kv(${v.label})" />
141
- </dd>
179
+ <!--{/if}-->
142
180
  <!--{/if}-->
181
+ </dd>
143
182
  <!--{/if}-->
144
183
  <!--{/loop}-->
145
184
  </dl>
146
185
  </div>
147
186
  <div class="card_foot">
148
187
  <div class="mm_group">
149
- <button class="btn_default" type="reset" @click="show = false">取消</button>
150
- <button class="btn_primary" type="button" @click="batchSet()">提交</button>
188
+ <button class="mm_btn btn_default" type="reset" @click="show = false">取消</button>
189
+ <button class="mm_btn btn_primary" type="button" @click="batchSet()">提交</button>
151
190
  </div>
152
191
  </div>
153
- </mm_card>
192
+ </div>
154
193
  </mm_modal>
155
194
  </main>
156
195
  </template>
@@ -174,20 +213,12 @@
174
213
  },
175
214
  // 查询条件
176
215
  query: {
177
- //页码
178
- page: 0,
179
- //页面大小
180
- size: '0',
181
- /*[loop js.query v idx]*/
182
- // ${' ' + v.title}
183
- /*[if v.type === 'number' && !v.select]*/
184
- '${v.name}': 0,
185
- /*[else]*/
186
- '${v.name}': '',
187
- /*[/if]*/
188
- /*[/loop]*/
189
216
  //排序
190
- orderby: ""
217
+ orderby: "",
218
+ // 上级分类ID
219
+ father_id: '',
220
+ // 关键词
221
+ keyword: ''
191
222
  },
192
223
  form: {},
193
224
  //颜色
@@ -216,13 +247,22 @@
216
247
  }
217
248
  this.$get('~${v.path}', query, function(json) {
218
249
  if (json.result) {
219
- _this /*['.' + v.name]*/ .clear();
220
- _this /*['.' + v.name]*/ .addList(json.result.list)
250
+ _this/*['.' + v.name]*/.clear();
251
+ _this/*['.' + v.name]*/.addList(json.result.list)
221
252
  }
222
253
  });
223
254
  },
224
255
  /*[/if]*/
225
256
  /*[/loop]*/
257
+ /**
258
+ * 获取列表之前
259
+ * @param {Object} param 参数
260
+ */
261
+ get_list_before(param){
262
+ delete param.page;
263
+ param.size = "0";
264
+ return param;
265
+ }
226
266
  },
227
267
  created() {
228
268
  /*[loop js.data v idx]*/
@@ -234,16 +274,36 @@
234
274
  },
235
275
  computed: {
236
276
  list_new() {
237
- var lt = this.list.toTree(this.field).toList();
238
277
  var list = [];
239
- var arr = this.opens;
240
- for (var i = 0; i < lt.length; i++) {
241
- var o = lt[i];
242
- if (this.opens.indexOf(o[this.father_id]) !== -1) {
243
- list.push(o);
278
+ var {
279
+ keyword,
280
+ father_id
281
+ } = this.query;
282
+
283
+ if (keyword && father_id) {
284
+ return this.list.filter(function(o) {
285
+ return o.father_id == father_id && (o.title.indexOf(keyword) !== -1 || o.name.indexOf(
286
+ keyword) !== -1);
287
+ });
288
+ } else if (father_id) {
289
+ return this.list.filter(function(o) {
290
+ return o.father_id == father_id;
291
+ });
292
+ } else if (keyword) {
293
+ return this.list.filter(function(o) {
294
+ return o.title.indexOf(keyword) !== -1 || o.name.indexOf(keyword) !== -1;
295
+ });
296
+ } else {
297
+ var lt = this.list.toTree(this.field).toList();
298
+ var arr = this.opens;
299
+ for (var i = 0; i < lt.length; i++) {
300
+ var o = lt[i];
301
+ if (this.opens.indexOf(o[this.father_id]) !== -1) {
302
+ list.push(o);
303
+ }
244
304
  }
305
+ return list;
245
306
  }
246
- return list;
247
307
  }
248
308
  }
249
309
  }
@@ -143,8 +143,8 @@
143
143
  </mm_body>
144
144
  <footer>
145
145
  <div class="mm_group">
146
- <button class="btn_default" type="reset" @click="show = false">取消</button>
147
- <button class="btn_primary" type="button" @click="batchSet()">提交</button>
146
+ <button class="mm_btn btn_default" type="reset" @click="show = false">取消</button>
147
+ <button class="mm_btn btn_primary" type="button" @click="batchSet()">提交</button>
148
148
  </div>
149
149
  </footer>
150
150
  </mm_view>
@@ -58,8 +58,8 @@
58
58
  </dl>
59
59
  <footer>
60
60
  <div class="mm_group">
61
- <button class="btn_default" type="button" @click="cancel">返回</button>
62
- <button class="btn_primary" type="button" @click="submit()">提交</button>
61
+ <button class="mm_btn btn_default" type="button" @click="cancel">返回</button>
62
+ <button class="mm_btn btn_primary" type="button" @click="submit()">提交</button>
63
63
  </div>
64
64
  </footer>
65
65
  </mm_form>
@@ -46,8 +46,8 @@
46
46
  </dl>
47
47
  <footer>
48
48
  <div class="mm_group">
49
- <button class="btn_default" type="button" @click="cancel">返回</button>
50
- <button class="btn_primary" type="button" @click="submit()">提交</button>
49
+ <button class="mm_btn btn_default" type="button" @click="cancel">返回</button>
50
+ <button class="mm_btn btn_primary" type="button" @click="submit()">提交</button>
51
51
  </div>
52
52
  </footer>
53
53
  </mm_form>
@@ -58,8 +58,8 @@
58
58
  </dl>
59
59
  <footer>
60
60
  <div class="mm_group">
61
- <button class="btn_default" type="button" @click="cancel">返回</button>
62
- <button class="btn_primary" type="button" @click="submit()">提交</button>
61
+ <button class="mm_btn btn_default" type="button" @click="cancel">返回</button>
62
+ <button class="mm_btn btn_primary" type="button" @click="submit()">提交</button>
63
63
  </div>
64
64
  </footer>
65
65
  </mm_form>
@@ -143,8 +143,8 @@
143
143
  </mm_body>
144
144
  <footer>
145
145
  <div class="mm_group">
146
- <button class="btn_default" type="reset" @click="show = false">取消</button>
147
- <button class="btn_primary" type="button" @click="batchSet()">提交</button>
146
+ <button class="mm_btn btn_default" type="reset" @click="show = false">取消</button>
147
+ <button class="mm_btn btn_primary" type="button" @click="batchSet()">提交</button>
148
148
  </div>
149
149
  </footer>
150
150
  </mm_view>
@@ -46,8 +46,8 @@
46
46
  </dl>
47
47
  <footer>
48
48
  <div class="mm_group">
49
- <button class="btn_default" type="button" @click="cancel">返回</button>
50
- <button class="btn_primary" type="button" @click="submit()">提交</button>
49
+ <button class="mm_btn btn_default" type="button" @click="cancel">返回</button>
50
+ <button class="mm_btn btn_primary" type="button" @click="submit()">提交</button>
51
51
  </div>
52
52
  </footer>
53
53
  </mm_form>
package/demo/index.js ADDED
@@ -0,0 +1,28 @@
1
+ require("mm_expand");
2
+ $.runPath = __dirname + $.slash;
3
+ const OS = require("../index.js");
4
+
5
+ var NODE_ENV = process.env.NODE_ENV || 'local';
6
+ // 避免掉线
7
+ if (NODE_ENV == 'local') {
8
+ process.on('uncaughtException', (err) => {
9
+ console.error('捕获到未处理的异常:', err);
10
+ });
11
+ }
12
+
13
+ var config = `/config/${NODE_ENV}.json`.loadJson();
14
+ if (config) {
15
+ var package = "/package.json".loadJson();
16
+ config.name = "mm_os";
17
+ config.version = package.dependencies["mm_os"];
18
+ }
19
+ var os = new OS(config);
20
+
21
+ if ("/config/lock.cache".hasFile()) {
22
+ $.isReady = true;
23
+ } else {
24
+ $.isReady = false;
25
+ console.log("初次访问");
26
+ }
27
+
28
+ os.run();
@@ -1,85 +1,85 @@
1
- /**
2
- * 应用
3
- * @param {Object} server 服务
4
- * @param {Object} config 配置参数
5
- */
6
- module.exports = function(server, config) {
7
- var cos = config.cos;
8
- if (cos && cos.status) {
9
- if (cos.headers && cos.origin !== "*") {
10
- /* 跨域限制(web防火墙) */
11
- server.use(async (ctx, next) => {
12
- var req = ctx.request;
13
-
14
- // 允许来自所有域名请求
15
- ctx.set('Access-Control-Allow-Origin', cos.origin);
16
- // 这样就能只允许 http://localhost:8080 这个域名的请求了
17
- // ctx.set("Access-Control-Allow-Origin", "http://localhost:8080");
18
-
19
- // 字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段.
20
- if (req.method == "OPTIONS") {
21
- ctx.set('Access-Control-Allow-Headers', cos.headers);
22
-
23
- // 服务器收到请求以后,检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后,确认允许跨源请求,就可以做出回应。
24
- // Content-Type表示具体请求中的媒体类型信息
25
- // ctx.set("Content-Type", "application/json;charset=utf-8");
26
- // 设置所允许的HTTP请求方法PUT,POST,GET,DELETE,HEAD,OPTIONS
27
- // ctx.set('Access-Control-Allow-Methods', 'GET,POST');
28
-
29
- // 该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。
30
- // 当设置成允许请求携带cookie时,需要保证"Access-Control-Allow-Origin"是服务器有的域名,而不能是"*";
31
- // ctx.set("Access-Control-Allow-Credentials", 'false');
32
-
33
- // 该字段可选,用来指定本次预检请求的有效期,单位为秒。
34
- // 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时,服务器会提前发送一次请求进行验证
35
- // 下面的的设置只本次验证的有效时间,即在该时间段内服务端可以不用进行验证
36
- // ctx.set("Access-Control-Max-Age", '3600');
37
- /*
38
- CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:
39
- Cache-Control、
40
- Content-Language、
41
- Content-Type、
42
- Expires、
43
- Last-Modified、
44
- Pragma
45
- */
46
- // 需要获取其他字段时,使用Access-Control-Expose-Headers,
47
- // getResponseHeader('myData')可以返回我们所需的值
48
- // ctx.set("Access-Control-Expose-Headers", "myData");
49
- ctx.status = 204;
50
- } else {
51
- await next();
52
- }
53
- });
54
- } else if (cos.headers && cos.headers !== "*") {
55
- /* 跨域限制(web防火墙) */
56
- server.use(async (ctx, next) => {
57
- var req = ctx.request;
58
- // 允许来自所有域名请求
59
- ctx.set('Access-Control-Allow-Origin', '*');
60
- // ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
61
- if (req.method === 'OPTIONS') {
62
- ctx.set('Access-Control-Allow-Headers', cos.headers);
63
- ctx.status = 204;
64
- } else {
65
- await next();
66
- }
67
- });
68
- } else {
69
- /* 跨域限制(web防火墙) */
70
- server.use(async (ctx, next) => {
71
- var req = ctx.request;
72
- // 允许来自所有域名请求
73
- ctx.set('Access-Control-Allow-Origin', '*');
74
- // ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
75
- if (req.method === 'OPTIONS') {
76
- ctx.set('Access-Control-Allow-Headers', '*');
77
- ctx.status = 204;
78
- } else {
79
- await next();
80
- }
81
- });
82
- }
83
- }
84
- return server;
1
+ /**
2
+ * 应用
3
+ * @param {Object} server 服务
4
+ * @param {Object} config 配置参数
5
+ */
6
+ module.exports = function(server, config) {
7
+ var cos = config.cos;
8
+ if (cos && cos.status) {
9
+ if (cos.headers && cos.origin !== "*") {
10
+ /* 跨域限制(web防火墙) */
11
+ server.use(async (ctx, next) => {
12
+ var req = ctx.request;
13
+
14
+ // 允许来自所有域名请求
15
+ ctx.set('Access-Control-Allow-Origin', cos.origin);
16
+ // 这样就能只允许 http://localhost:8080 这个域名的请求了
17
+ // ctx.set("Access-Control-Allow-Origin", "http://localhost:8080");
18
+
19
+ // 字段是必需的。它也是一个逗号分隔的字符串,表明服务器支持的所有头信息字段.
20
+ if (req.method == "OPTIONS") {
21
+ ctx.set('Access-Control-Allow-Headers', cos.headers);
22
+
23
+ // 服务器收到请求以后,检查了Origin、Access-Control-Request-Method和Access-Control-Request-Headers字段以后,确认允许跨源请求,就可以做出回应。
24
+ // Content-Type表示具体请求中的媒体类型信息
25
+ // ctx.set("Content-Type", "application/json;charset=utf-8");
26
+ // 设置所允许的HTTP请求方法PUT,POST,GET,DELETE,HEAD,OPTIONS
27
+ // ctx.set('Access-Control-Allow-Methods', 'GET,POST');
28
+
29
+ // 该字段可选。它的值是一个布尔值,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中。
30
+ // 当设置成允许请求携带cookie时,需要保证"Access-Control-Allow-Origin"是服务器有的域名,而不能是"*";
31
+ // ctx.set("Access-Control-Allow-Credentials", 'false');
32
+
33
+ // 该字段可选,用来指定本次预检请求的有效期,单位为秒。
34
+ // 当请求方法是PUT或DELETE等特殊方法或者Content-Type字段的类型是application/json时,服务器会提前发送一次请求进行验证
35
+ // 下面的的设置只本次验证的有效时间,即在该时间段内服务端可以不用进行验证
36
+ // ctx.set("Access-Control-Max-Age", '3600');
37
+ /*
38
+ CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:
39
+ Cache-Control、
40
+ Content-Language、
41
+ Content-Type、
42
+ Expires、
43
+ Last-Modified、
44
+ Pragma
45
+ */
46
+ // 需要获取其他字段时,使用Access-Control-Expose-Headers,
47
+ // getResponseHeader('myData')可以返回我们所需的值
48
+ // ctx.set("Access-Control-Expose-Headers", "myData");
49
+ ctx.status = 204;
50
+ } else {
51
+ await next();
52
+ }
53
+ });
54
+ } else if (cos.headers && cos.headers !== "*") {
55
+ /* 跨域限制(web防火墙) */
56
+ server.use(async (ctx, next) => {
57
+ var req = ctx.request;
58
+ // 允许来自所有域名请求
59
+ ctx.set('Access-Control-Allow-Origin', '*');
60
+ // ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
61
+ if (req.method === 'OPTIONS') {
62
+ ctx.set('Access-Control-Allow-Headers', cos.headers);
63
+ ctx.status = 204;
64
+ } else {
65
+ await next();
66
+ }
67
+ });
68
+ } else {
69
+ /* 跨域限制(web防火墙) */
70
+ server.use(async (ctx, next) => {
71
+ var req = ctx.request;
72
+ // 允许来自所有域名请求
73
+ ctx.set('Access-Control-Allow-Origin', '*');
74
+ // ctx.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
75
+ if (req.method === 'OPTIONS') {
76
+ ctx.set('Access-Control-Allow-Headers', '*');
77
+ ctx.status = 204;
78
+ } else {
79
+ await next();
80
+ }
81
+ });
82
+ }
83
+ }
84
+ return server;
85
85
  };
@@ -1,7 +1,7 @@
1
- {
2
- "name": "web_cors",
3
- "title": "web跨域请求",
4
- "description": "用于跨域请求限制",
5
- "version": "1.0",
6
- "sort": 4
1
+ {
2
+ "name": "web_cors",
3
+ "title": "web跨域请求",
4
+ "description": "用于跨域请求限制",
5
+ "version": "1.0",
6
+ "sort": 4
7
7
  }
@@ -1,21 +1,21 @@
1
- /**
2
- * 应用
3
- * @param {Object} server 服务
4
- * @param {Object} config 配置参数
5
- */
6
- module.exports = function(server, config) {
7
- /* 跨域限制(web防火墙) */
8
- server.use(async (ctx, next) => {
9
- var url = ctx.path + ctx.querystring;
10
- var body = "";
11
- if (ctx.request.body) {
12
- body = JSON.stringify(ctx.request.body);
13
- if (body.length > 1024) {
14
- body = body.substring(0, 1024) + "..."
15
- }
16
- }
17
- $.log.http(`${ctx.method} ${url} ${body}`);
18
- await next();
19
- });
20
- return server;
1
+ /**
2
+ * 应用
3
+ * @param {Object} server 服务
4
+ * @param {Object} config 配置参数
5
+ */
6
+ module.exports = function(server, config) {
7
+ /* 跨域限制(web防火墙) */
8
+ server.use(async (ctx, next) => {
9
+ var url = ctx.path + ctx.querystring;
10
+ var body = "";
11
+ if (ctx.request.body) {
12
+ body = JSON.stringify(ctx.request.body);
13
+ if (body.length > 1024) {
14
+ body = body.substring(0, 1024) + "..."
15
+ }
16
+ }
17
+ $.log.http(`${ctx.method} ${url} ${body}`);
18
+ await next();
19
+ });
20
+ return server;
21
21
  };