shipManage.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var _this, dataObj = [], baseTok = null;
  2. var timeId = null;
  3. var map;
  4. new Vue({
  5. el: '#app',
  6. data: function() {
  7. return {
  8. shipLi: [],
  9. nowTab: {},
  10. userForm: {}
  11. }
  12. },
  13. created() {
  14. _this = this;
  15. baseTok = getCookie("Admin-Token") != "null" && getCookie("Admin-Token") != null ? getCookie("Admin-Token") : "";
  16. _this.userForm = JSON.parse(getCookie("Admin-User"));
  17. _this.initAjax();
  18. clearInterval(timeId);
  19. timeId = setInterval(()=>{
  20. _this.initAjax();
  21. },15*1000);
  22. },
  23. mounted(){
  24. map = new AMap.Map("areaMap", {
  25. center: [113.5556, 22.9422],
  26. zoom: 14
  27. });
  28. map.addControl(new AMap.Scale({ visible: true }) );
  29. window.addEventListener('message',(data)=>{
  30. if(data.data != _this.nowTab.sn){
  31. _this.changTab(data.data);
  32. }
  33. })
  34. },
  35. methods: {
  36. initAjax(){
  37. const loading = this.$loading({ background: 'rgba(0, 0, 0, 0.4)' }) let _opts = {
  38. url: baseUrl + "/SelCctvListByUserId",
  39. type:"GET"
  40. };
  41. let _data = {
  42. "user_id": _this.userForm.userId
  43. };
  44. let _hea = {
  45. "Authorization": "Bearer " + baseTok
  46. }; getAxios(_opts,_data,_hea).then((res) => { if(res.code == 200&&res.data.length){
  47. let _xji = res.data;
  48. let sipList = JSON.parse(localStorage.getItem("sipLists")) || [];
  49. let _noShip = [], _hasShip = [];
  50. _xji.forEach(item => {
  51. if(item.mmsi.length>15){
  52. let _lonlat = [], _time = "";
  53. if(sipList.length){
  54. let _anm = sipList.filter(sipitem => sipitem.user_info.device_id == item.sn);
  55. if(_anm.length){
  56. _lonlat = [_anm[0].location_info.y_point, _anm[0].location_info.x_point];
  57. _time = _anm[0].location_info.ctime;
  58. }else{
  59. _lonlat = item.mmsi.split(",");
  60. }
  61. }else{
  62. _lonlat = item.mmsi.split(",");
  63. }
  64. // let _newpo = GpsToGd(Number(_lonlat[1]), Number(_lonlat[0]));
  65. _noShip.push({
  66. icon: staticUrl + "/img/ship.png",
  67. position: [_lonlat[0], _lonlat[1]],
  68. lon: _lonlat[0],
  69. lat: _lonlat[1],
  70. prod: item.dev_name,
  71. sn: item.sn,
  72. mmsi: item.mmsi,
  73. speed: 0,
  74. time: _time
  75. })
  76. }else{
  77. _hasShip.push(item)
  78. }
  79. })
  80. if(!_hasShip.length){
  81. _this.setMapAfter(_noShip)
  82. }else{
  83. let _bji = _hasShip.map(item => item.mmsi);
  84. getCctvAis(_bji.toString(),function(_res){
  85. let _arr = [];
  86. _hasShip.forEach(item => {
  87. let sd = _res.filter(subitem => subitem[0] == item.mmsi)[0];
  88. let _newpo = GpsToGd(Number(sd[13]), Number(sd[12]));
  89. _noShip.push({
  90. icon: staticUrl + "/img/ship.png",
  91. position: [_newpo.lon,_newpo.lat],
  92. lon: sd[12],
  93. lat: sd[13],
  94. prod: item.dev_name,
  95. sn: item.sn,
  96. mmsi: item.mmsi,
  97. speed: sd[15],
  98. time: sd[23]
  99. })
  100. });
  101. _this.setMapAfter(_noShip);
  102. });
  103. } }else{ _this.shipLi = []; } loading.close(); }) },
  104. setMapAfter(_arr){
  105. _this.shipLi = _arr;
  106. dataObj = _arr;
  107. let _sn = JSON.parse(localStorage.getItem("nowShip"));
  108. if(_sn !=null && _sn != "null"){
  109. let _ind = _this.shipLi.findIndex((i,ind) => {return i.sn == _sn.sn});
  110. if(_ind != -1){
  111. _sn = _this.shipLi[_ind];
  112. }else{
  113. _sn = _this.shipLi[0];
  114. }
  115. }else{
  116. _sn = _this.shipLi[0];
  117. }
  118. _this.nowTab = _sn;
  119. _this.setMap(_this.shipLi);
  120. },
  121. changTab(id){
  122. let ind = _this.shipLi.findIndex((it,ind) => {return id == it.sn});
  123. if(ind != -1){
  124. _this.nowTab = _this.shipLi[ind].sn;
  125. window.parent.postMessage({id: _this.nowTab},staticUrl+ "/");
  126. _this.openInfo(_this.nowTab);
  127. }
  128. },
  129. setMap(_obj){
  130. map.clearMap();
  131. _obj.forEach(function(_obj) {
  132. var marker = new AMap.Marker({
  133. map: map,
  134. icon: new AMap.Icon({
  135. size: new AMap.Size(50, 50),
  136. image: _obj.icon,
  137. imageSize: new AMap.Size(50, 50)
  138. }),
  139. position: _obj.position,
  140. offset: new AMap.Pixel(-13, -30),
  141. extData: _obj.sn
  142. });
  143. marker.setLabel({
  144. offset: new AMap.Pixel(0, 0),
  145. content: "<div style='color:#000;font-size:12px;'>" + _obj.prod + "</div>",
  146. direction: 'bottom'
  147. });
  148. marker.on('click', markerClick);
  149. });
  150. _this.openInfo(_this.nowTab.sn);
  151. function markerClick(e) {
  152. let _ind = e.target.getExtData();
  153. window.parent.postMessage({id: _ind}, staticUrl+ "/");
  154. _this.openInfo(_ind);
  155. }
  156. },
  157. openInfo(_sn){
  158. let _cmk = _this.shipLi;
  159. for (var i = 0; i < _cmk.length; i++) {
  160. if (_sn == _cmk[i].sn) {
  161. let _str = _cmk[i];
  162. _this.nowTab = _str;
  163. var info = [];
  164. // getFormattedAddress(_str.position, function(_pos) {
  165. info.push(`<div class="nowShip">
  166. <div class="mapTitle">${_str.prod}</div>
  167. <div class="padding-sm mapInfo">
  168. <div><div>经度:</div><div>${duToGpsDM(_str.lon,'E')}</div></div>
  169. <div><div>纬度:</div><div>${duToGpsDM(_str.lat,'N')}</div></div>
  170. <div><div>航速:</div><div>${_str.speed} 节</div></div>
  171. <div><div>时间:</div><div>${ parseTime(_str.time, '{y}-{m}-{d} {h}:{d}:{s}')}</div></div>
  172. </div>
  173. </div>`);
  174. infoWindow = new AMap.InfoWindow({
  175. offset: new AMap.Pixel(6, -30),
  176. content: info.join("")
  177. });
  178. infoWindow.open(map, _str.position);
  179. map.setCenter(_str.position);
  180. return;
  181. // });
  182. }
  183. }
  184. }
  185. }
  186. })