wangzhibo
2021-09-06 2efb5d2a02ce47a9fd6f1cec138496aba72f0815
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
"use script"; //开发环境建议开启严格模式
 
//对应widget.js中MyWidget实例化后的对象
var thisWidget;
 
//当前页面业务
function initWidgetView(_thisWidget) {
  thisWidget = _thisWidget;
 
  // $(window).resize(refHeight)
  // refHeight()
 
  //获取台风列表
  queryTyphoonList().then(function (arr) {
    //启动正在发送的台风
    arr.forEach((item) => {
      if (item.state == "start") {
        item.show = true;
        selectOneTyphoon(item);
      }
    });
    showTyphoonTable(arr);
  });
}
 
//显示台风列表
function showTyphoonTable(data) {
  $("#listTable").bootstrapTable({
    data: data,
    height: 390,
    pagination: false,
    singleSelect: false,
    checkboxHeader: false,
    columns: [
      {
        title: "是否显示",
        field: "show",
        align: "center",
        checkbox: true,
        with: 50,
      },
      {
        title: "台风编号",
        field: "typnumber",
        align: "center",
      },
      {
        title: "台风名(中文)",
        field: "name_cn",
        align: "center",
      },
      {
        title: "台风名(英文)",
        field: "name_en",
        align: "center",
      },
    ],
    onCheck: function (row) {
      selectOneTyphoon(row);
    },
    onUncheck: function (row) {
      unSelectOneTyphoon(row);
    },
    onClickRow: function (row) {
      if (typhoonListObj[row.id] && typhoonListObj[row.id].show) {
        selectOneTyphoon(row);
      }
    },
  });
 
  $("#btnPlay").click(function () {
    if (!selectTF) {
      return;
    }
 
    if (selectTF.playTyphoon?.isStart) {
      stopTyphoon();
    } else {
      startTyphoon();
    }
  });
}
 
function startTyphoon() {
  $("#btnPlay").html('<span class="fa fa-stop" aria-hidden="true"></span> 停止');
 
  thisWidget.startTyphoon(selectTF);
}
 
//停止播放
function stopTyphoon() {
  $("#btnPlay").html('<span class="fa fa-play" aria-hidden="true"></span> 播放');
  thisWidget.stopTyphoon(selectTF);
}
 
var typhoonListObj = {};
 
//取消勾选台风
function unSelectOneTyphoon(row) {
  let typhoon = typhoonListObj[row.id];
  if (typhoon) {
    typhoon.show = false;
  }
  if (typhoon == selectTF) {
    stopTyphoon();
    $("#typhoonPath").hide();
  }
}
 
//勾选了台风
function selectOneTyphoon(row) {
  $("#typhoonPath").show();
  $("#lblName").html("[" + row.typnumber + "]" + "" + row.name_cn);
  stopTyphoon();
 
  if (typhoonListObj[row.id]) {
    let typhoon = typhoonListObj[row.id];
    typhoon.show = true;
    typhoon.flyTo();
    showPathTable(typhoon); //已有的,直接展示
  } else {
    queryTyphoonItem(row.id).then(function (newData) {
      let typhoon = thisWidget.createTyphoon({
        ...row,
        ...newData,
      });
      typhoon.flyTo();
 
      showPathTable(typhoon);
 
      typhoonListObj[row.id] = typhoon; //绑定到数据中,方便使用
    });
  }
}
 
let selectTF;
 
//台风路径表格的显示
function showPathTable(typhoon) {
  selectTF = typhoon;
 
  $("#pathTable").bootstrapTable("destroy");
  $("#pathTable").bootstrapTable({
    height: getHeight(),
    pagination: false,
    singleSelect: true,
    data: typhoon.options.path,
    columns: [
      {
        title: "时间",
        field: "time_str",
        align: "center",
      },
 
      {
        title: "风速",
        field: "centerSpeed",
        align: "center",
        formatter: function (value, row, index) {
          return value + "m/s";
        },
      },
      {
        title: "移向",
        field: "moveTo_str",
        align: "center",
      },
      {
        title: "强度",
        field: "level_str",
        align: "center",
      },
    ],
    onClickRow: function (row) {
      typhoon.showPointFQ(row);
      let graphic = typhoon.getPointById(row.id);
      if (graphic) {
        graphic.flyTo({
          radius: 1600000,
          complete() {
            graphic.openTooltip();
          },
        });
      }
    },
  });
}
 
function getHeight() {
  return $(window).height() - 450;
}
 
//访问后端接口,取台风列表数据
function queryTyphoonList() {
  return new Promise(function (resolve, reject) {
    function typhoon_jsons_list_default(data) {
      var arr = [];
      data.typhoonList.forEach((item) => {
        arr.push({
          id: item[0],
          name_en: item[1],
          name_cn: item[2],
          typnumber: item[3],
          state: item[7],
        });
      });
      resolve(arr);
    }
    window.typhoon_jsons_list_default = typhoon_jsons_list_default;
 
    $.ajax({
      // url: './data/typhoon/list_2020.json',
      url: "http://typhoon.nmc.cn/weatherservice/typhoon/jsons/list_default", //在线实时接口
      type: "GET",
      dataType: "jsonp",
      data: {
        t: new Date().getTime(),
        callback: "typhoon_jsons_list_default",
      },
      success: typhoon_jsons_list_default,
      error: function (data) {
        console.log("台风列表数据获取失败", data);
        // haoutil.msg('台风列表数据获取失败!')
        reject(data);
      },
    });
  });
}
 
//访问后端接口,取单个台风路径数据
function queryTyphoonItem(id) {
  return new Promise(function (resolve, reject) {
    function typhoon_jsons_view(res) {
      var newData = conversionPathData(res.typhoon); //在Typhoon.js中
      // console.log('台风数据==>', newData)
      resolve(newData);
    }
    window["typhoon_jsons_view_" + id] = typhoon_jsons_view;
 
    $.ajax({
      // url: 'data/typhoon/view_' + typId + '.json',
      url: "http://typhoon.nmc.cn/weatherservice/typhoon/jsons/view_" + id, //在线实时接口
      type: "GET",
      dataType: "jsonp",
      data: {
        t: new Date().getTime(),
        callback: "typhoon_jsons_view",
      },
      success: function (res) {},
      error: function (data) {
        console.log("获取台风单个数据失败");
        reject(data);
      },
    });
  });
}
 
//转换数据,将后端接口数据转换为需要的格式
function conversionPathData(oldData) {
  var path = [];
  oldData[8].forEach((message) => {
    let circle7;
    let circle10;
    let circle12;
    message[10].forEach((level) => {
      let radiusObj = {
        speed: level[0],
        radius1: level[1],
        radius2: level[2],
        radius3: level[3],
        radius4: level[4],
      };
 
      if (level[0] == "30KTS") {
        circle7 = radiusObj;
      } else if (level[0] == "50KTS") {
        circle10 = radiusObj;
      } else if (level[0] == "64KTS") {
        circle12 = radiusObj;
      } else {
        console.log("未处理风圈", radiusObj);
      }
    });
 
    //预测路径
    let babj = message[11]?.BABJ;
    let arrForecast;
    if (babj) {
      arrForecast = [];
      babj.forEach((element) => {
        let newArr = {
          time: element[0], //几小时预报
          time_str: element[1],
          lon: element[2], //预报经度
          lat: element[3], //预报纬度
          strength: element[4], //中心气压
          centerSpeed: element[5], //最大风速  m/s
          level: element[7], //预报台风等级, 代码
          color: getColor(element[7]), //对应等级的颜色
        };
        arrForecast.push(newArr);
      });
    }
 
    let time = new Date(message[2]); //时间
 
    path.push({
      id: message[0], //唯一标识
      time: new Date(message[2]), //时间
      time_str: time.format("MM-dd HH:mm"), //时间格式化字符串
 
      level: message[3], //台风等级, 代码
      level_str: getLevelStr(message[3]),
      color: getColor(message[3]), //对应等级的颜色
      lon: message[4], //经度
      lat: message[5], //纬度
      strength: message[6], //中心气压,百帕
      centerSpeed: message[7], //最大风速,米/秒
      moveTo: message[8], //移动方向, 代码
      moveTo_str: getMoveToStr(message[8]),
      windSpeed: message[9], //移动速度,公里/小时
 
      circle7: circle7, //7级风圈, 对象
      circle10: circle10, //10级风圈, 对象
      circle12: circle12, //12级风圈, 对象
      forecast: arrForecast, //预测路径, 数组
    });
  });
 
  return {
    id: oldData[0],
    name_en: oldData[1], //台风名字,英文
    name_cn: oldData[2], //台风名字
    typnumber: oldData[3], //台风编号
    state: oldData[7],
    path: path,
  };
}
 
function getLevelStr(value) {
  switch (value) {
    default:
    case "TD":
      return "热带低压";
    case "TS":
      return "热带风暴";
    case "STS":
      return "强热带风暴";
    case "TY":
      return "台风";
    case "STY":
      return "强台风";
    case "SuperTY":
      return "超强台风";
  }
}
 
function getMoveToStr(value) {
  switch (value) {
    default:
    case "N":
      return "北";
    case "NNE":
      return "北东北";
    case "NE":
      return "东北";
    case "ENE":
      return "东东北";
    case "E":
      return "东";
    case "ESE":
      return "东东南";
    case "ES":
      return "东南";
    case "SSE":
      return "南东南";
    case "S":
      return "南";
    case "SSW":
      return "南西南";
    case "SW":
      return "西南";
    case "WSW":
      return "西西南";
    case "W":
      return "西";
    case "WNW":
      return "西北西";
    case "NW":
      return "北西";
    case "NNW":
      return "北西北";
  }
}
 
//不同等级的台风对应不同的颜色
function getColor(level) {
  switch (level) {
    default:
    case "TD": //热带低压
      return "rgb(238,209,57)";
    case "TS": //热带风暴
      return "rgb(0,0,255)";
    case "STS": //强热带风暴
      return "rgb(15,128,0)";
    case "TY": //台风
      return "rgb(254,156,69)";
    case "STY": //强台风
      return "rgb(254,0,254)";
    case "SuperTY": //超强台风
      return "rgb(254,0,0)";
  }
}