forked from adsproo/txnews
-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather_pro.js
847 lines (824 loc) · 32.7 KB
/
weather_pro.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
可以自由定制显示的天气脚本,想怎样都随你,轻松修改轻松查看
https://github.com/sazs34/TaskConfig/blob/master/assets/weather_pro.md
*/
let config = {
darksky_api: "", //从https://darksky.net/dev/ 上申请key填入即可
aqicn_api: "", //从http://aqicn.org/data-platform/token/#/ 上申请key填入即可
huweather_apiKey: "", //和风天气APIkey,可自行前往 https://dev.heweather.com/ 进行获取
lat_lon: "", //请填写经纬度,直接从谷歌地图中获取即可
lang: "zh", //语言,随意切换为您想要的语言哦(zh/zh-tw/ja/en/fr/...)
log: 0, //调试日志,0为不开启,1为开启,2为开启精简日志
timeout: 0, //超时时间,单位毫秒(1000毫秒=1秒),一般不推荐修改[为0则不限制超时时间]
show: {
//普通每天的
template: {
title: `$[city]$[district] $[summary]`,
subtitle: `$[weatherIcon]$[weather] $[temperatureMin] ~ $[temperatureMax]°C ☔️降雨概率 $[precipProbability]%`,
detail: `$[aqiIcon]空气质量 $[aqi]($[aqiDesc]) 🏋🏻♀️气压$[atmosphere]pa
🌪风速$[windSpeed]km/h 💨$[windDir]$[windPower]级
👀紫外线指数 $[uv]($[uvDesc]) 💦湿度$[currentHumidity]%
🌡体感温度 $[apparentTemperatureMin] ~ $[apparentTemperatureMax]°C 🎚当前温度 $[currentTemperature]℃
[生活指数]
$[lifeStyle($[icon]:$[brf],$[txt])]
[天气周报] $[weeklySummary]
$[daily($[month]月$[day]日 $[temperatureLow]~$[temperatureHigh]°C $[weatherIcon]$[weather])]`
},
lifestyle: { //此处用于显示各项生活指数,可自行调整顺序,顺序越在前面则显示也会靠前,如果您不想查看某一指数,置为false即可,想看置为true即可
drsg: true, //穿衣指数,
flu: true, //感冒指数,
comf: true, //舒适度指数,
cw: false, //洗车指数,
sport: false, //运动指数,
trav: false, //旅游指数,
uv: false, //紫外线指数,
air: true, //空气污染扩散条件指数,
ac: false, //空调开启指数,
ag: false, //过敏指数,
gl: false, //太阳镜指数,
mu: false, //化妆指数,
airc: false, //晾晒指数,
ptfc: true, //交通指数,
fsh: false, //钓鱼指数,
spi: false, //防晒指数
}
}
}
const provider = {
heweather_now: {
api: `https://free-api.heweather.net/s6/weather/now?location=${config.lat_lon.replace(/\s/g, "").replace(",", ",")}&key=${config.huweather_apiKey}`,
progress: 0, //处理进度:0需处理1已处理2无需处理9报错
timeoutNumber: 0, //超时处理编号
data: {
basic: {},
now: {}
},
support: ['$[province]', '$[city]', '$[district]', '$[weatherIcon]', '$[weather]', '$[currentTemperature]', '$[currentWindSpeed]', '$[currentWindDir]', '$[currentWindPower]', '$[currentHumidity]', '$[currentAtmosphere]', '$[currentVisibility]']
},
heweather_daily: {
api: `https://free-api.heweather.net/s6/weather/forecast?location=${config.lat_lon.replace(/\s/g, "").replace(",", ",")}&key=${config.huweather_apiKey}`,
progress: 0,
timeoutNumber: 0,
data: {},
support: ['$[temperatureMin]', '$[temperatureMax]', '$[precipProbability]', '$[windSpeed]', '$[windDir]', '$[windPower]', '$[humidity]', '$[atmosphere]', '$[visibility]', '$[uv]', '$[uvDesc]', '$[sunrise]', '$[sunset]', '$[moonrise]', '$[moonset]']
},
heweather_air: {
api: `https://free-api.heweather.net/s6/air/now?location=auto_ip&key=${config.huweather_apiKey}`,
progress: 0,
timeoutNumber: 0,
data: {},
support: []
},
heweather_lifestyle: {
api: `https://free-api.heweather.net/s6/weather/lifestyle?location=${config.lat_lon.replace(/\s/g, "").replace(",", ",")}&key=${config.huweather_apiKey}`,
progress: 0,
timeoutNumber: 0,
data: [],
support: ['$[lifeStyle]']
},
darksky: {
api: `https://api.darksky.net/forecast/${config.darksky_api}/${config.lat_lon.replace(/\s/g, "").replace(",", ",")}?lang=${config.lang}&units=si`,
progress: 0,
timeoutNumber: 0,
data: {
daily: {
data: []
},
hourly: {
data: []
},
currently: {}
},
support: ['$[summary]', '$[weeklySummary]', '$[weatherIcon]', '$[weather]', '$[temperatureMin]', '$[temperatureMax]', '$[apparentTemperatureMin]', '$[apparentTemperatureMax]', '$[precipProbability]', '$[uv]', '$[uvDesc]']
},
aqicn: {
api: `https://api.waqi.info/feed/geo:${config.lat_lon.replace(/\s/g, "").replace(",", ",").replace(/,/, ";")}/?token=${config.aqicn_api}`,
progress: 0,
data: {},
support: ['$[aqiIcon]', '$[aqi]', '$[aqiDesc]', '$[aqiWarning]']
}
}
// #region 天气数据获取
function weather() {
support();
heweatherNow();
heweatherDaily();
darksky();
aqicn();
heweatherLifestyle();
}
//clear-day, partly-cloudy-day, cloudy, clear-night, rain, snow, sleet, wind, fog, or partly-cloudy-night
//☀️🌤⛅️🌥☁️🌦🌧⛈🌩🌨❄️💧💦🌫☔️☂️ ☃️⛄️
function darksky() {
if (provider.darksky.progress == 2) return;
start("darksky");
$task.fetch({
url: provider.darksky.api
}).then(response => {
try {
let darkObj = JSON.parse(response.body);
record(`天气数据获取-A1-${response.body}`);
if (darkObj.error) {
$notify("DarkApi", "出错啦", darkObj.error);
}
provider.darksky.data.daily = darkObj.daily;
provider.darksky.data.hourly = darkObj.hourly;
provider.darksky.data.currently = darkObj.currently;
record(`天气数据获取-A2`);
check('darksky', true)
} catch (e) {
console.log(`天气数据A获取报错${JSON.stringify(e)}`)
}
}, reason => {
record(`天气数据获取-A3-${reason.error}`);
check('darksky', false);
});
}
function aqicn() {
if (provider.aqicn.progress == 2) return;
start("aqicn");
$task.fetch({
url: provider.aqicn.api
}).then(response => {
try {
var waqiObj = JSON.parse(response.body);
if (waqiObj.status == 'error') {
$notify("Aqicn", "出错啦", waqiObj.data);
} else {
record(`天气数据获取-B1-${response.body}`);
provider.aqicn.data = {
...getAqiInfo(waqiObj.data.aqi)
};
}
check('aqicn', true)
} catch (e) {
console.log(`天气数据B获取报错${JSON.stringify(e)}`)
}
}, reason => {
record(`天气数据获取-B2-${reason.error}`);
//获取精确数据失败后,直接获取粗略信息即可
heweatherAir();
});
}
function heweatherNow() {
start("heweather_now");
$task.fetch({
url: provider.heweather_now.api
}).then(response => {
try {
record(`天气数据获取-C1-${response.body}`);
var heObj = JSON.parse(response.body);
provider.heweather_now.data.basic = heObj.HeWeather6[0].basic;
provider.heweather_now.data.now = heObj.HeWeather6[0].now;
check('heweather_now', true)
} catch (e) {
console.log(`天气数据C获取报错${JSON.stringify(e)}`)
}
}, reason => {
record(`天气数据获取-C2-${reason.error}`);
//因为此接口出错率还挺高的,所以即使报错我们也不处理,该返回什么就返回什么好了
check('heweather_now', false)
})
}
function heweatherDaily() {
if (provider.heweather_daily.progress == 2) return;
start("heweather_daily");
$task.fetch({
url: provider.heweather_daily.api
}).then(response => {
try {
record(`天气数据获取-D1-${response.body}`);
var heObj = JSON.parse(response.body);
provider.heweather_daily.data = heObj.HeWeather6[0].daily_forecast[0];
check('heweather_daily', true)
} catch (e) {
console.log(`天气数据D获取报错${JSON.stringify(e)}`)
}
}, reason => {
record(`天气数据获取-D2-${reason.error}`);
//因为此接口出错率还挺高的,所以即使报错我们也不处理,该返回什么就返回什么好了
check('heweather_daily', false)
})
}
function heweatherAir() {
if (provider.heweather_air.progress == 2) return;
start("heweather_air");
$task.fetch({
url: provider.heweather_air.api
}).then(response => {
try {
record(`天气数据获取F1-${response.body}`);
var heObj = JSON.parse(response.body);
provider.heweather_air.data = {
...getAqiInfo(heObj.HeWeather6[0].air_now_city.aqi)
};
check('heweather_air', true)
} catch (e) {
console.log(`天气数据F获取报错${JSON.stringify(e)}`)
}
}, reason => {
record(`天气数据获取-F2-${reason.error}`);
//因为此接口出错率还挺高的,所以即使报错我们也不处理,该返回什么就返回什么好了
check('heweather_air', false)
})
}
function heweatherLifestyle() {
if (provider.heweather_lifestyle.progress == 2) return;
start("heweather_lifestyle");
var needRequest = false;
//判断一下是否全部都是false,全false的话,则不需要请求此接口直接返回渲染的数据了
for (var item in config.show.lifestyle) {
if (config.show.lifestyle[item]) {
needRequest = true;
break;
}
}
if (needRequest) {
$task.fetch({
url: provider.heweather_lifestyle.api
}).then(response => {
try {
record(`天气数据获取-E1-${response.body}`);
var heObj = JSON.parse(response.body);
provider.heweather_lifestyle.data = heObj.HeWeather6[0].lifestyle;
check('heweather_lifestyle', true)
} catch (e) {
console.log(`天气数据E获取报错${JSON.stringify(e)}`)
}
}, reason => {
record(`天气数据获取-E2-${reason.error}`);
//因为此接口出错率还挺高的,所以即使报错我们也不处理,该返回什么就返回什么好了
check('heweather_lifestyle', false)
})
} else {
check('heweather_lifestyle', false)
}
}
//#endregion
// #region 提醒数据组装
function check(type, result) {
record(`check-${type}-${result}`);
//支持setTimeout居然不支持clearTimeout,有点难受
if (provider[type].progress == 1 || provider[type].progress == 9) return;
provider[type].progress = result ? 1 : 9;
var isAllChecked = provider.heweather_now.progress != 0 && provider.heweather_daily.progress && provider.darksky.progress != 0 && (provider.aqicn.progress != 0 || provider.heweather_air.progress != 0) && provider.heweather_lifestyle.progress != 0;
if (isAllChecked) {
record(`天气数据渲染中[template]`);
try {
renderTemplate();
} catch (e) {
record(`天气渲染出错-${JSON.stringify(e)}`);
}
}
}
var lineBreak = `
`;
function renderTemplate() {
execArrayTemplate();
const map = {
//省
province: provider.heweather_now.data.basic.admin_area,
//市
city: provider.heweather_now.data.basic.parent_city,
//区
district: provider.heweather_now.data.basic.location || "UNKNOW",
//全天气候变化概述
summary: `${provider.darksky.data.hourly.summary||""}`,
//一周气候变化概述
weeklySummary: `${provider.darksky.data.daily.summary||""}`,
//天气图标
weatherIcon: `${getHeweatherIcon(provider.heweather_now.data.now.cond_code)||getDarkskyWeatherIcon(provider.darksky.data.hourly.icon)}`,
//天气描述(晴/雨/雪等)
weather: `${provider.heweather_now.data.now.cond_txt||getDarkskyWeatherDesc(provider.darksky.data.hourly.icon)}`,
//当前温度
currentTemperature: `${provider.heweather_now.data.now.tmp}`,
//温度最低值
temperatureMin: `${Math.round(provider.heweather_daily.data.tmp_min||provider.darksky.data.daily.data.get(0).temperatureMin)}`,
//温度最高值
temperatureMax: `${Math.round(provider.heweather_daily.data.tmp_max||provider.darksky.data.daily.data.get(0).temperatureMax)}`,
//体感温度最低值
apparentTemperatureMin: `${Math.round(provider.darksky.data.daily.data.get(0).apparentTemperatureLow)}`,
//体感温度最高值
apparentTemperatureMax: `${Math.round(provider.darksky.data.daily.data.get(0).apparentTemperatureHigh)}`,
//降雨概率
precipProbability: `${provider.heweather_daily.data.pop||(Number(provider.darksky.data.daily.data.get(0).precipProbability) * 100).toFixed(0)}`,
//空气质量图标
aqiIcon: `${provider.aqicn.data.aqiIcon||provider.heweather_air.data.aqiIcon}`,
//空气质量
aqi: `${provider.aqicn.data.aqi||provider.heweather_air.data.aqi}`,
//空气质量描述
aqiDesc: `${provider.aqicn.data.aqiDesc||provider.heweather_air.data.aqiDesc}`,
//空气质量警告(提示)
aqiWarning: `${provider.aqicn.data.aqiWarning||provider.heweather_air.data.aqiWarning}`,
//全天风速
windSpeed: `${provider.heweather_daily.data.wind_spd}`,
//当前风速
currentWindSpeed: `${provider.heweather_now.data.now.wind_spd}`,
//全天风向
windDir: `${provider.heweather_daily.data.wind_dir}`,
//当前风向
currentWindDir: `${provider.heweather_now.data.now.wind_dir}`,
//全天风力
windPower: `${provider.heweather_daily.data.wind_sc}`,
//当前风力
currentWindPower: `${provider.heweather_now.data.now.wind_sc}`,
//全天相对湿度
humidity: `${provider.heweather_daily.data.hum}`,
//当前相对湿度
currentHumidity: `${provider.heweather_now.data.now.hum}`,
//全天大气压
atmosphere: `${provider.heweather_daily.data.pres}`,
//当前大气压
currentAtmosphere: `${provider.heweather_now.data.now.pres}`,
//全天能见度
visibility: `${provider.heweather_daily.data.vis}`,
//当前能见度
currentVisibility: `${provider.heweather_now.data.now.vis}`,
//紫外线等级
uv: `${provider.heweather_daily.data.uv_index||provider.darksky.data.daily.data.get(0).uvIndex}`,
//紫外线描述
uvDesc: `${getUVDesc(provider.heweather_daily.data.uv_index||provider.darksky.data.daily.data.get(0).uvIndex)}`,
//日出时间
sunrise: `${provider.heweather_daily.data.sr}`,
//日落时间
sunset: `${provider.heweather_daily.data.ss}`,
//月出时间
moonrise: `${provider.heweather_daily.data.mr}`,
//月落时间
moonset: `${provider.heweather_daily.data.ms}`,
}
var notifyInfo = {
title: execTemplate(config.show.template.title, map),
subtitle: execTemplate(config.show.template.subtitle, map),
detail: execTemplate(config.show.template.detail, map),
};
$notify(notifyInfo.title, notifyInfo.subtitle, notifyInfo.detail);
$done({});
}
// #endregion
// #region 数据处理方法
function getHeweatherIcon(code) {
var codeMap = {
_100: '☀️',
_101: '☁️',
_102: '☁️',
_103: '⛅️',
_104: '☁️',
_200: '💨',
_201: '🌬',
_202: '🌬',
_203: '🌬',
_204: '🌬',
_205: '🌬',
_206: '💨',
_207: '💨',
_208: '💨',
_209: '🌪',
_210: '🌪',
_211: '🌪',
_212: '🌪',
_213: '🌪',
_300: '🌨',
_301: '🌨',
_302: '⛈',
_303: '⛈',
_304: '⛈',
_305: '💧',
_306: '💦',
_307: '🌧',
_308: '🌧',
_309: '☔️',
_310: '🌧',
_311: '🌧',
_312: '🌧',
_313: '🌧❄️',
_314: '💧',
_315: '💦',
_316: '🌧',
_317: '🌧',
_318: '🌧',
_399: '🌧',
_400: '🌨',
_401: '🌨',
_402: '☃️',
_403: '❄️',
_404: '🌨',
_405: '🌨',
_406: '🌨',
_407: '🌨',
_408: '🌨',
_409: '🌨',
_410: '❄️',
_499: '⛄️',
_500: '🌫',
_501: '🌫',
_502: '🌫',
_503: '🌫',
_504: '🌫',
_505: '🌫',
_506: '🌫',
_507: '🌫',
_508: '🌫',
_509: '🌫',
_510: '🌫',
_511: '🌫',
_512: '🌫',
_513: '🌫',
_514: '🌫',
_515: '🌫',
_900: '🔥',
_901: '⛄️',
_999: '❓',
}
return codeMap[`_${code}`] ? codeMap[`_${code}`] : "";
}
function getDarkskyWeatherIcon(icon_text) {
let icon = "❓"
if (icon_text == "clear-day") icon = "☀️";
if (icon_text == "partly-cloudy-day") icon = "🌤";
if (icon_text == "cloudy") icon = "☁️";
if (icon_text == "rain") icon = "🌧";
if (icon_text == "snow") icon = "☃️";
if (icon_text == "sleet") icon = "🌨";
if (icon_text == "wind") "🌬";
if (icon_text == "fog") icon = "🌫";
if (icon_text == "partly-cloudy-night") icon = "🌑";
if (icon_text == "clear-night") icon = "🌑";
return icon;
}
function getDarkskyWeatherDesc(icon_text) {
let icon = "未知"
if (icon_text == "clear-day") icon = `晴`;
if (icon_text == "partly-cloudy-day") icon = `晴转多云`;
if (icon_text == "cloudy") icon = `多云`;
if (icon_text == "rain") icon = `雨`;
if (icon_text == "snow") icon = `雪`;
if (icon_text == "sleet") icon = `雨夹雪`;
if (icon_text == "wind") icon = `大风`;
if (icon_text == "fog") icon = `大雾`;
if (icon_text == "partly-cloudy-night") icon = `多云`;
if (icon_text == "clear-night") icon = `晴`;
return icon;
}
function getCityInfo(name) {
var loc;
try {
var locArr = name.split(/[(),,()]/)
if (locArr.length >= 4) {
loc = locArr[2] + " ";
} else if (locArr.length >= 2) {
loc = locArr[1] + " ";
} else {
loc = ""; //此时会很长,还不如不显示了
}
} catch (e) {
loc = '';
record(`获取城市名称失败-${JSON.stringify(e)}`);
}
return loc;
}
function getAqiInfo(aqi) {
var aqiDesc = "";
var aqiIcon = "";
var aqiWarning = "";
if (aqi > 300) {
aqiIcon = `🟤`;
aqiDesc = `严重污染`;
aqiWarning = "儿童、老人、呼吸系统等疾病患者及一般人群停止户外活动";
} else if (aqi > 200) {
aqiIcon = `🟣`;
aqiDesc = `重度污染`;
aqiWarning = "儿童、老人、呼吸系统等疾病患者及一般人群停止或减少户外运动";
} else if (aqi > 150) {
aqiIcon = `🔴`;
aqiDesc = `中度污染`;
aqiWarning = "儿童、老人、呼吸系统等疾病患者及一般人群减少户外活动";
} else if (aqi > 100) {
aqiIcon = `🟠`;
aqiDesc = `轻度污染`;
aqiWarning = "老人、儿童、呼吸系统等疾病患者减少长时间、高强度的户外活动";
} else if (aqi > 50) {
aqiIcon = `🟡`;
aqiDesc = `良好`;
aqiWarning = "极少数敏感人群应减少户外活动";
} else {
aqiIcon = `🟢`;
aqiDesc = `优`;
}
return {
aqi,
aqiIcon,
aqiDesc,
aqiWarning
};
}
function getUVDesc(daily_uvIndex) {
var uvDesc = "";
if (daily_uvIndex >= 10) {
uvDesc = "五级-特别强";
} else if (daily_uvIndex >= 7) {
uvDesc = "四级-很强";
} else if (daily_uvIndex >= 5) {
uvDesc = "三级-较强";
} else if (daily_uvIndex >= 3) {
uvDesc = "二级-较弱";
} else {
uvDesc = "一级-最弱";
}
return uvDesc;
}
// #endregion
// #region 模板相关
/**
* 用于标识该接口已执行,如果有使用超时设置则此操作是有意义的
* @param {String} type 具体的接口执行类型
*/
function start(type) {
if (config.timeout > 0) {
provider[type].timeoutNumber = setTimeout(() => {
check(type, false);
}, config.timeout);
}
}
/**
* 判断哪些接口需要进行处理,减少网络请求
*/
function support() {
let regex = /\$\[([a-z,A-Z,0-9]*)\]/g;
const template = `${config.show.template.title}${config.show.template.subtitle}${config.show.template.detail}`.match(regex);
provider.heweather_now.progress = template.filter((item, filter) => {
return provider.heweather_now.support.indexOf(item) != -1;
}).length > 0 ? 0 : 2;
provider.heweather_daily.progress = template.filter((item, filter) => {
return provider.heweather_daily.support.indexOf(item) != -1;
}).length > 0 ? 0 : 2;
// provider.heweather_air.progress = template.filter((item, filter) => {
// return provider.heweather_air.support.indexOf(item) != -1;
// }).length > 0 ? 0 : 2;
provider.heweather_lifestyle.progress = template.filter((item, filter) => {
let regexLifestyle = /\$\[(lifeStyle\()+([\s\S]+?)(\))+\]/g;
return regexLifestyle.test(config.show.lifestyle) ? 0 : 2;
}).length > 0 ? 0 : 2;
provider.aqicn.progress = template.filter((item, filter) => {
return provider.aqicn.support.indexOf(item) != -1;
}).length > 0 ? 0 : 2;
provider.darksky.progress = template.filter((item, filter) => {
return provider.darksky.support.indexOf(item) != -1;
}).length > 0 ? 0 : 2;
if (provider.darksky.progress == 2) {
//如果
let regexDaily = /\$\[(daily\()+([\s\S]+?)(\))+\]/g;
let regexHourly = /\$\[(hourly\()+([\s\S]+?)(\))+\]/g;
provider.darksky.progress = (regexDaily.test(config.show.template.detail) || regexHourly.test(config.show.template.detail)) ? 0 : 2;
}
record(`h_n:${provider.heweather_now.progress},h_d:${provider.heweather_daily.progress},h_a:${provider.heweather_air.progress},h_l:${provider.heweather_lifestyle.progress},aq:${provider.aqicn.progress},da:${provider.darksky.progress}`)
}
/**
* 用于普通模板的映射
* @param {String} template 模板内容
* @param {Object} map 映射内容
*/
function execTemplate(template, map) {
if (!template) return "";
let regex = /\$\[([a-z,A-Z,0-9]*)\]/g;
if (regex.test(template)) {
for (item of template.match(regex)) {
item.match(regex);
if (RegExp.$1 && map[RegExp.$1]) {
template = template.replace(item, map[RegExp.$1]);
} else {
template = template.replace(item, "");
}
}
}
return template;
}
function execArrayTemplate() {
try {
execTemplateLifestyle();
execTemplateDaily();
execTemplateHourly();
} catch (e) {
console.log(`${JSON.stringify(e)}`)
}
}
function execTemplateLifestyle() {
let regexLifestyle = /\$\[(lifeStyle\()+([\s\S]+?)(\))+\]/g;
if (provider.heweather_lifestyle.data <= 0) {
config.show.template.detail.replace(regexLifestyle, '')
}
let result = [];
if (regexLifestyle.test(config.show.template.detail)) {
let lsMap = { //此处用于显示各项生活指数,可自行调整顺序,顺序越在前面则显示也会靠前,如果您不想查看某一指数,置为false即可,想看置为true即可
drsg: {
icon: '👔穿衣指数',
type: '穿衣指数'
},
flu: {
icon: '🤧感冒等级💉',
type: '感冒指数'
},
comf: {
icon: '😊舒适度',
type: '舒适度指数'
},
cw: {
icon: '🚗洗车',
type: '洗车指数'
},
sport: {
icon: '🏃🏻运动',
type: '运动指数'
},
trav: {
icon: '🌴',
type: '旅游指数'
},
uv: {
icon: '☂️',
type: '紫外线指数'
},
air: {
icon: '🌫',
type: '空气污染扩散条件指数'
},
ac: {
icon: '❄️',
type: '空调开启指数'
},
ag: {
icon: '😷',
type: '过敏指数'
},
gl: {
icon: '🕶',
type: '太阳镜指数'
},
mu: {
icon: '💄',
type: '化妆指数'
},
airc: {
icon: '🧺',
type: '晾晒指数'
},
ptfc: {
icon: '🚥',
type: '交通指数'
},
fsh: {
icon: '🎣',
type: '钓鱼指数'
},
spi: {
icon: '🔆',
type: '防晒指数'
},
}
config.show.template.detail.match(regexLifestyle);
var rangeTemplate = RegExp.$2; //此处拿到的是要替换的列表显示部分了
let regex = /\$\[([a-z,A-Z,0-9]*)\]/g;
var template = rangeTemplate.match(regex);
for (life of provider.heweather_lifestyle.data) {
if (!config.show.lifestyle[life.type]) continue;
var singleInfo = rangeTemplate;
for (item of template) {
item.match(regex);
if (RegExp.$1 == "icon") {
singleInfo = singleInfo.replace(item, lsMap[life.type].icon)
} else if (RegExp.$1 == "type") {
singleInfo = singleInfo.replace(item, lsMap[life.type].type)
} else {
singleInfo = singleInfo.replace(item, life[RegExp.$1])
}
}
result.push(singleInfo);
}
config.show.template.detail = config.show.template.detail.replace(regexLifestyle, result.join(lineBreak));
}
}
function execTemplateDaily() {
let regexDaily = /\$\[(daily\()+([\s\S]+?)(\))+\]/g;
if (provider.darksky.data.daily.data.length <= 0) {
config.show.template.detail.replace(regexDaily, '')
}
let result = [];
if (regexDaily.test(config.show.template.detail)) {
config.show.template.detail.match(regexDaily);
var rangeTemplate = RegExp.$2; //此处拿到的是要替换的列表显示部分了
let regex = /\$\[([a-z,A-Z,0-9]*)\]/g;
var template = rangeTemplate.match(regex);
for (daily of provider.darksky.data.daily.data) {
var singleInfo = rangeTemplate;
for (item of template) {
item.match(regex);
if (RegExp.$1 == "month") {
singleInfo = singleInfo.replace(item, (`${daily["time"]}`).toDateTime().Format("MM"));
} else if (RegExp.$1 == "day") {
singleInfo = singleInfo.replace(item, (`${daily["time"]}`).toDateTime().Format("dd"));
} else if (RegExp.$1 == "weatherIcon") {
singleInfo = singleInfo.replace(item, getDarkskyWeatherIcon(daily.icon));
} else if (RegExp.$1 == "weather") {
singleInfo = singleInfo.replace(item, getDarkskyWeatherDesc(daily.icon));
} else if (RegExp.$1 == "uvDesc") {
singleInfo = singleInfo.replace(item, getUVDesc(daily.uvIndex));
} else if (RegExp.$1 == "cloudCover") {
singleInfo = singleInfo.replace(item, daily.cloudCover * 100);
} else if (RegExp.$1 == "temperatureHigh") {
singleInfo = singleInfo.replace(item, Math.round(daily.temperatureHigh));
} else if (RegExp.$1 == "temperatureLow") {
singleInfo = singleInfo.replace(item, Math.round(daily.temperatureLow));
} else if (RegExp.$1 == "apparentTemperatureMax") {
singleInfo = singleInfo.replace(item, Math.round(daily.apparentTemperatureMax));
} else if (RegExp.$1 == "apparentTemperatureMin") {
singleInfo = singleInfo.replace(item, Math.round(daily.apparentTemperatureMin));
} else if (RegExp.$1 && daily[RegExp.$1] != undefined) {
singleInfo = singleInfo.replace(item, daily[RegExp.$1]);
}
}
result.push(singleInfo);
}
config.show.template.detail = config.show.template.detail.replace(regexDaily, result.join(lineBreak));
}
}
function execTemplateHourly() {
let regexHourly = /\$\[(hourly\()+([\s\S]+?)(\))+\]/g;
if (provider.darksky.data.hourly.data.length <= 0) {
config.show.template.detail.replace(regexHourly, '')
}
let result = [];
if (regexHourly.test(config.show.template.detail)) {
config.show.template.detail.match(regexHourly);
var rangeTemplate = RegExp.$2; //此处拿到的是要替换的列表显示部分了
let regex = /\$\[([a-z,A-Z,0-9]*)\]/g;
var template = rangeTemplate.match(regex);
for (hourly of provider.darksky.data.hourly.data) {
var singleInfo = rangeTemplate;
for (item of template) {
item.match(regex);
if (RegExp.$1 == "month") {
singleInfo = singleInfo.replace(item, (`${hourly["time"]}`).toDateTime().Format("MM"));
} else if (RegExp.$1 == "day") {
singleInfo = singleInfo.replace(item, (`${hourly["time"]}`).toDateTime().Format("dd"));
} else if (RegExp.$1 == "hour") {
singleInfo = singleInfo.replace(item, (`${hourly["time"]}`).toDateTime().Format("hh"));
} else if (RegExp.$1 == "weatherIcon") {
singleInfo = singleInfo.replace(item, getDarkskyWeatherIcon(hourly.icon));
} else if (RegExp.$1 == "weather") {
singleInfo = singleInfo.replace(item, getDarkskyWeatherDesc(hourly.icon));
} else if (RegExp.$1 == "uvDesc") {
singleInfo = singleInfo.replace(item, getUVDesc(hourly.uvIndex));
} else if (RegExp.$1 == "cloudCover") {
singleInfo = singleInfo.replace(item, hourly.cloudCover * 100);
} else if (RegExp.$1 == "temperature") {
singleInfo = singleInfo.replace(item, Math.round(hourly.temperature));
} else if (RegExp.$1 == "apparentTemperature") {
singleInfo = singleInfo.replace(item, Math.round(hourly.apparentTemperature));
} else if (RegExp.$1 && hourly[RegExp.$1] != undefined) {
singleInfo = singleInfo.replace(item, hourly[RegExp.$1]);
}
}
result.push(singleInfo);
}
config.show.template.detail = config.show.template.detail.replace(regexHourly, result.join(lineBreak));
}
}
function record(log) {
if (config.log == 1) {
console.log(log);
} else if (config.log == 2) {
console.log(log.substring(0, 60));
}
}
// #endregion
// #region 扩展方法
Array.prototype.get = function (index, defaultValue = {}) {
if (index >= 0 && this.length > 0 && this.length >= index + 1) {
return this[index];
} else {
return defaultValue;
}
}
String.prototype.toDateTime = function () {
var time = parseInt(this + '000');
return new Date(time);
}
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
// #endregion
weather();