AppNew.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div id="app">
  3. <Map id="map" />
  4. </div>
  5. </template>
  6. <script>
  7. import Map from "@/components/MapNew";
  8. import $ from 'jquery';
  9. export default {
  10. name: 'App',
  11. components: {
  12. Map,
  13. },
  14. mounted(){
  15. window.CityGIS = (($Core) => {
  16. var app = {
  17. debug: false,
  18. Invoke: (options) => {
  19. // console.log(options);
  20. if(!Array.isArray(options))
  21. options = [options];
  22. options.forEach(function(item)
  23. {
  24. window.CityGIS.InvokeAPI(item.ActionName,item.Parameters);
  25. });
  26. },
  27. InvokeAPI: (methodName, params) => {
  28. this.InvokeMethod(methodName, params);
  29. },
  30. InitInvokeType: () => {
  31. //如果被嵌入Iframe响应跨域消息
  32. if (window.CityGIS.hasIframe()) {
  33. window.addEventListener("message", (evt) => {
  34. if(evt.data != undefined){
  35. var commands = JSON.parse(evt.data);
  36. if(!Array.isArray(commands))
  37. {
  38. commands = [commands]
  39. }
  40. commands.forEach(function(command){
  41. var Parameters = command.Parameters;
  42. if (typeof (Parameters) == "string")
  43. {
  44. Parameters = eval('(' + command.Parameters + ')');
  45. }
  46. window.CityGIS.InvokeAPI(command.ActionName, Parameters);
  47. })
  48. }
  49. });
  50. }
  51. },
  52. SendOutMessage: (msg) => {
  53. msg = JSON.stringify(msg);
  54. console.log("发送消息" + msg);
  55. //如果被嵌入Iframe响应跨域消息
  56. if (window.CityGIS.hasIframe()) {
  57. return $.when(window.parent.postMessage(msg, "*")).then(function(){
  58. return "ok";
  59. });
  60. }
  61. else {
  62. console.error("未嵌入页面,无法通讯");
  63. return $.when(true).then(function(){
  64. return "error";
  65. });
  66. }
  67. },
  68. hasIframe: () => {
  69. if (self.frameElement == null) {
  70. return self != top;
  71. }
  72. else {
  73. return self.frameElement.tagName == "IFRAME";
  74. }
  75. },
  76. AppStart: () => {
  77. window.CityGIS.InitInvokeType();
  78. //创建地图
  79. this.$bus.$emit('CreateMap');
  80. //监听回传信息
  81. this.$bus.$on('SendMessage', (data) => {
  82. // console.log(data);
  83. window.CityGIS.SendOutMessage(data).then((data) => {
  84. console.log("回传成功:" + JSON.stringify(data));
  85. },(err) => {
  86. console.log(err);
  87. });
  88. })
  89. }
  90. }
  91. return $.extend($Core, app);
  92. })(window.CityGIS || {});
  93. window.CityGIS.AppStart();
  94. },
  95. methods: {
  96. InvokeMethod(methodname, params){
  97. this.$bus.$emit('InvokeMethod',{
  98. "methodname": methodname,
  99. "params": params
  100. });
  101. }
  102. }
  103. }
  104. </script>
  105. <style>
  106. #app {
  107. width: 100%;
  108. height: 100%;
  109. padding: 0;
  110. margin: 0;
  111. overflow: hidden;
  112. }
  113. #map{
  114. position: absolute;
  115. width: 100%;
  116. height: 100%;
  117. padding: 0;
  118. margin: 0;
  119. overflow: hidden;
  120. }
  121. </style>