MicroFrontend.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * @Author: sjl
  3. * @Date: 2023-04-26 10:55:12
  4. * @Descripttion:
  5. */
  6. class MicroFrontend {
  7. constructor() {
  8. this.apps = {}; // 存储子应用的信息
  9. this.globalData = {}; // 存储全局数据
  10. this.init();
  11. }
  12. // 初始化方法
  13. init() {
  14. // 监听子应用的消息
  15. window.addEventListener('message', (event) => {
  16. // console.log("父消息:",event);
  17. const { data, source } = event;
  18. if (data.type === 'register') {
  19. // 子应用注册
  20. this.apps[data.name] = {
  21. source,
  22. loaded: false,
  23. };
  24. } else if (data.type === "logout") {
  25. if (globalEvent.wlwLogin) {
  26. globalEvent.wlwLogin();
  27. globalEvent.wlwLogin = null;
  28. }
  29. } else if (data.type === "js") {
  30. eval(data.message)
  31. } else if (data.type === 'loaded') {
  32. // 子应用加载完成
  33. this.apps[data.name].loaded = true;
  34. } else if (data.type === 'setGlobalData') {
  35. // 设置全局数据
  36. this.globalData[data.key] = data.value;
  37. } else if (data.type === 'getGlobalData') {
  38. // 获取全局数据
  39. const value = this.globalData[data.key];
  40. source.postMessage({
  41. type: 'getGlobalData',
  42. key: data.key,
  43. value,
  44. }, '*');
  45. }
  46. });
  47. return this;
  48. }
  49. // 注册子应用
  50. register(name, id, url, callback,token) {
  51. console.log('register',url);
  52. const iframe = document.createElement('iframe');
  53. iframe.src = url;
  54. iframe.token=token;
  55. iframe.id = id + '-iframe';
  56. iframe.style.width = "100%";
  57. iframe.style.height = "100%";
  58. iframe.style.opacity = "0.2";
  59. document.getElementById(id).appendChild(iframe);
  60. this.apps[name] = {
  61. source: iframe.contentWindow,
  62. loaded: false,
  63. };
  64. let app = this.apps[name]
  65. // 发送注册消息给子应用
  66. iframe.contentWindow.postMessage({
  67. type: 'register',
  68. name,
  69. }, '*');
  70. iframe.onload = function () {
  71. setTimeout(()=>{
  72. // app.postMessage(token, '*');
  73. },
  74. 3000)
  75. setTimeout(() => callback && callback(), 1000);
  76. setTimeout(() => iframe.style.opacity = "1", 4000);
  77. };
  78. }
  79. // 发送消息给子应用
  80. sendToApp(name, message) {
  81. console.log(message,'sendToApp');
  82. console.log(this.apps,'this.apps');
  83. console.log(name,'name');
  84. const app = this.apps[name];
  85. // if (app && app.loaded) {
  86. console.log(app,'app');
  87. if (app) {
  88. app.source.postMessage({
  89. type:'token',
  90. data:message.token,
  91. path:message.path
  92. }, '*');
  93. setTimeout(() => this.updateIframeDom(app.source), 1000);
  94. setTimeout(() => this.updateIframeDom(app.source), 2000);
  95. setTimeout(() => this.updateIframeDom(app.source), 3000);
  96. setTimeout(() => this.updateIframeDom(app.source), 4000);
  97. }
  98. }
  99. updateIframeDom(iframe) {
  100. var list = iframe.document.querySelectorAll('.jeecg-basic-table .ant-table .ant-table-row');
  101. if (list && list.length) {
  102. var dom = iframe.document.querySelectorAll('.jeecg-basic-table .ant-table');
  103. if (dom && dom.length) {
  104. dom.forEach(i => {
  105. const tr = i.querySelectorAll('tr');
  106. if(tr && tr.length > 9){
  107. i.style.height = "calc(100vh - 350px)";
  108. }
  109. })
  110. }
  111. }
  112. }
  113. // 设置全局数据
  114. setGlobalData(key, value) {
  115. this.globalData[key] = value;
  116. // 发送设置全局数据的消息给所有子应用
  117. Object.values(this.apps).forEach((app) => {
  118. if (app.loaded) {
  119. app.source.postMessage({
  120. type: 'setGlobalData',
  121. key,
  122. value,
  123. }, '*');
  124. }
  125. });
  126. }
  127. // 获取全局数据
  128. getGlobalData(key) {
  129. // 发送获取全局数据的消息给所有子应用
  130. Object.values(this.apps).forEach((app) => {
  131. if (app.loaded) {
  132. app.source.postMessage({
  133. type: 'getGlobalData',
  134. key,
  135. }, '*');
  136. }
  137. });
  138. }
  139. }