useIframe.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { postAction } from '/@/api/manage/manage';
  2. import { inject } from 'vue';
  3. // TODO 先兼容vue2代码迁移,后期替换为vue3的写法, 当前from src/mixins/iframeMixin.js
  4. export const useIframe = {
  5. data() {
  6. return {
  7. src: '',
  8. loading: true,
  9. isUsing: true,
  10. wordpressSwitch: '3',
  11. pageError: false,
  12. wpConfig: {},
  13. wordpressSetting: '',
  14. openSetting: [],
  15. };
  16. },
  17. mounted() {
  18. // 监听子页面的消息
  19. if (typeof window.addEventListener != 'undefined') {
  20. window.addEventListener('message', this.dealMessage, false);
  21. } else if (typeof window.attachEvent != 'undefined') {
  22. window.attachEvent('onmessage', this.dealMessage);
  23. }
  24. this.queryWordPressConfig();
  25. },
  26. methods: {
  27. dealMessage(e) {
  28. if (e.data.status == 2000) {
  29. if (!e.data.data['wp-auth-check']) {
  30. this.src = '';
  31. this.queryWordPressConfig();
  32. }
  33. }
  34. },
  35. // 调用方法
  36. iframeLoad() {
  37. this.$nextTick(() => {
  38. this.loading = true;
  39. const iframe = this.$refs.iframeCom.iframeRef;
  40. //表示页面加载错误
  41. this.pageError = true;
  42. iframe.onload = () => {
  43. this.loading = false;
  44. };
  45. });
  46. },
  47. async queryWordPressConfig() {
  48. let that = this;
  49. that.wordpressSwitch = '';
  50. that.pageError = false;
  51. let url = '';
  52. let params = {
  53. siteCode: localStorage.getItem('siteCode'),
  54. };
  55. await postAction('/adweb/adwebSiteManage/queryWordPressConfig', params)
  56. .then(function (res) {
  57. if (res.code === 200) {
  58. that.wordpressSwitch = res.result.wordpressSwitch ? res.result.wordpressSwitch : 2;
  59. if (res.result.wordpressSetting != null) {
  60. that.wordpressSetting = res.result.wordpressSetting;
  61. }
  62. if (res.result.openSetting != null) {
  63. that.openSetting = res.result.openSetting;
  64. }
  65. if (res.result.productType === 'woocommerce') {
  66. url = that.wooUrl ? that.wooUrl : that.url;
  67. } else {
  68. url = that.url;
  69. }
  70. let isIframe = 0;
  71. if (that.wordpressSetting === 'all' || that.openSetting.includes(that.iframePage)) {
  72. isIframe = 1;
  73. }
  74. if (that.parantEvent) {
  75. that.parantEvent(res.result.wordpressSwitch);
  76. }
  77. if ((that.wordpressSetting === 'all' || that.openSetting.includes('product') || that.openSetting.includes('article')) && isIframe === 1) {
  78. let domainUrl = res.result.domain;
  79. let token = res.result.wordpressToken;
  80. let username = res.result.wordpressName;
  81. let page_no_login = domainUrl + '/wp-html.php?token=' + token + '&username=' + username + '&jumpUrl=' + encodeURIComponent(url);
  82. // let page_ready = domainUrl + "/wp-admin/"+ that.loginUrl +""
  83. // let ssoResKey = res.result.ssoResKey
  84. // if(ssoResKey){
  85. // that.src = page_ready
  86. // }else{
  87. that.src = page_no_login;
  88. // }
  89. that.iframeLoad();
  90. }
  91. }
  92. })
  93. .catch((err) => {
  94. console.log(err);
  95. that.wordpressSwitch = '3';
  96. });
  97. },
  98. },
  99. };