zhangzengfei
2022-07-20 4a800a8fc83c6bd1f86a8e847b079a51a7532c09
src/pages/desktop/index/components/DFrame.vue
@@ -1,34 +1,57 @@
<template>
  <div
    v-show="data.isShow"
    :class="['d-frame', {'d-frame-full': fullScreen}]"
    :class="['d-frame', { 'd-frame-full': fullScreen }]"
    ref="dFrame"
    :data-id="data.id"
    v-bind:style="{left: data.leftOffset + 'px', top: data.topOffset + 'px', 'z-index': 125 + data.order, width: width + 'px', height: height + 'px'}"
    @contextmenu.prevent="openMenuList($event.offsetY, $event.offsetX, data)"
    v-bind:style="{
      left: data.leftOffset + 'px',
      top: data.topOffset + 'px',
      'z-index': 125 + data.order,
      width: width + 'px',
      height: height + 'px',
    }"
  >
    <div class="d-frame-title" v-drag @click="frameClick">
    <div class="d-frame-title" v-drag="fullScreen" @click="frameClick">
      <div class="icon iconfont back" @click="back" v-if="isShowBack">
        &#xe718;
      </div>
      <div class="d-frame-title-operation">
        <i class="icon-minus d-frame-operation-minus" @click="minFrame(data,$event)">
        <i
          class="icon-minus d-frame-operation-minus"
          @click="minFrame(data, $event)"
        >
          <img :src="`${publicPath}images/desktop/header-icon/min.png`" alt />
        </i>
        <i class="icon-screen-toggle d-frame-operation-full" @click="changeFullScreen($event)">
        <i
          class="icon-screen-toggle d-frame-operation-full"
          @click="changeFullScreen($event)"
        >
          <img
            :src="fullScreen?`${publicPath}images/desktop/header-icon/mini.png`:`${publicPath}images/desktop/header-icon/max.png`"
            :src="
              fullScreen
                ? `${publicPath}images/desktop/header-icon/mini.png`
                : `${publicPath}images/desktop/header-icon/max.png`
            "
            alt
          />
        </i>
        <i class="icon-close d-frame-operation-close" @click="closeFrame($event)"></i>
        <i
          class="icon-close d-frame-operation-close"
          @click="closeFrame($event)"
        ></i>
      </div>
      <slot name="d-frame-title-content">
        <div class="d-frame-title-content">
          <img :src="data.icon" />
          <span>{{data.title}}</span>
          <span>{{ data.title }}</span>
        </div>
      </slot>
    </div>
    <div class="d-frame-content">
      <div class="iframe-mask" v-if="data.order!=1" @click="frameClick"></div>
      <iframe :src="data.url" v-if="data.url"></iframe>
      <div class="iframe-mask" v-if="data.order != 1" @click="frameClick"></div>
      <iframe :src="data.url" v-if="data.url" :name="data.name"></iframe>
      <div v-html="data.html" v-if="data.html"></div>
      <!-- <span class="d-frame-operation-resize" v-resize></span> -->
      <span
@@ -38,20 +61,37 @@
        @mouseup="mouseup"
      ></span>
    </div>
    <div
      class="menuList"
      v-show="showMenu"
      :style="{ top: menuTop + 'px', left: menuLeft + 'px' }"
    >
      <ul>
        <li @click="refreshApp">重新加载</li>
        <li @click="changeFullScreen">最大化</li>
        <li @click="minFrame">最小化</li>
        <li @click="closeFrame">关闭</li>
      </ul>
    </div>
  </div>
</template>
<script>
import html2canvas from 'html2canvas';
import html2canvas from "html2canvas";
export default {
  name: "DFrame",
  props: {
    data: Object
    data: Object,
  },
  created() {
    this.addBackListener();
  },
  data() {
    return {
      publicPath: process.env.BASE_URL,
      showMenu: false,
      menuTop: 0,
      menuLeft: 0,
      fullScreen: false,
      width: this.data.width || 1024,
      height: this.data.height || 512,
@@ -60,12 +100,37 @@
        startH: 0,
        mouX: 0,
        mouY: 0,
        resizeLock: false
      }
    }
        resizeLock: false,
      },
      isShowBack: false,
    };
  },
  watch: {
    showMenu(val) {
      if (val) {
        document.body.addEventListener("click", this.closeMenuList);
      } else {
        document.body.removeEventListener("click", this.closeMenuList);
      }
    },
  },
  mounted() {
    window.addEventListener("message", (d) => {
      let { source, trigger, menuT, menuL } = d.data;
      if (trigger == "contextmenu") {
        this.openMenuList(menuT, menuL);
      }
    });
  },
  methods: {
    openMenuList(t, l, frame) {
      this.showMenu = true;
      this.menuTop = t;
      this.menuLeft = l;
    },
    closeMenuList() {
      this.showMenu = false;
    },
    //resize
    mousedown(e) {
      this.resizeObj.mouX = e.clientX;
@@ -77,103 +142,150 @@
    mousemove(e) {
      document.onmousemove = function (e) {
        if (!this.resizeObj.resizeLock) {
          return
          return;
        }
        e = e || window.event;
        e.preventDefault();
        let curWidth = this.resizeObj.startW + e.clientX - this.resizeObj.mouX;
        let curHeight = this.resizeObj.startH + e.clientY - this.resizeObj.mouY;
        this.width = curWidth;
        this.height = curHeight;
      }.bind(this)
      }.bind(this);
    },
    mouseup() {
      this.resizeObj.resizeLock = false;
      document.onmousemove = null;
    },
    //resize end
    //重新加载应用
    refreshApp() {
      window.frames[this.data.name].location.reload();
    },
    closeFrame: function () {
      this.$store.dispatch('desktop/closeFrame', this.data);
      this.refreshDock();
      this.$store.dispatch("desktop/closeFrame", this.data);
      //this.refreshDock();
      this.$store.commit("desktop/highlight");
    },
    changeFullScreen: function () {
      this.fullScreen = !this.fullScreen;
    },
    frameClick(e) {
      this.$store.commit('desktop/refreshFrame', this.data);
      this.$store.commit("desktop/refreshFrame", this.data);
    },
    minFrame(data, e) {
      //找到当前的iframe
      let curIframe = Array.from(document.querySelectorAll('iframe')).find(iframe => iframe.src.indexOf(data.url) >= 0);
      //保存当前应用快照
      html2canvas(curIframe.contentWindow.document.body, {
        dpi: window.devicePixelRatio * 4,
        //logging: true, //查看html2canvas内部执行流程
        removeContainer: true,
        imageTimeout: 0,
        //useCORS: true //开启跨域配置
      }).then(canvas => {
        let shotSrc = canvas.toDataURL();
        this.$store.commit('desktop/addMinDock', {
          id: this.data.id,
          src: this.data.icon,
          alt: this.data.title,
          type: "3",
          screenshot: shotSrc
        });
      //if(data.name=='cameraVideo'||data.name=='search'||data.name=='library'||data.name=='cameraAccess'||data.name=='dataStack'){
      //let shotSrc = canvas.toDataURL();
      this.$store.commit("desktop/addMinDock", {
        id: this.data.id,
        src: this.data.icon,
        alt: this.data.title,
        type: "3",
        //screenshot: shotSrc
      });
      return false;
      //this.refreshDock();
      //}
      //找到当前的iframe
      //let curIframe = Array.from(document.querySelectorAll('iframe')).find(iframe => iframe.src.indexOf(data.url) >= 0);
      //保存当前应用快照
      // html2canvas(curIframe.contentWindow.document.body, {
      //   dpi: window.devicePixelRatio * 4,
      //   logging: true, //查看html2canvas内部执行流程
      //   removeContainer: true,
      //   imageTimeout: 0,
      //   useCORS: true, //开启跨域配置
      //   //allowTaint: true
      // }).then(canvas => {
      //   let shotSrc = canvas.toDataURL();
      //   this.$store.commit('desktop/addMinDock', {
      //     id: this.data.id,
      //     src: this.data.icon,
      //     alt: this.data.title,
      //     type: "3",
      //     screenshot: shotSrc
      //   });
      // }).catch(e=>{
      //   console.log(e);
      //   this.$store.commit('desktop/addMinDock', {
      //     id: this.data.id,
      //     src: this.data.icon,
      //     alt: this.data.title,
      //     type: "3",
      //     screenshot: ''
      //   });
      // });
      //return false;
    },
    refreshDock: function () {
      setTimeout(function () {
        const dockItems = document.getElementsByClassName('dock-item');
        const dockMask = document.getElementsByClassName('dock-mask')[0];
        for (let i = 0; i < dockItems.length; i++) {
          dockItems[i].width = 60;
    // 菜单栏返回
    back() {
      let active = this.$store.state.desktop.minDocks;
      active = active.filter((item) => item.highlight);
      console.log(active);
      const iframeArr = document.querySelectorAll("iframe");
      iframeArr.forEach((item) => {
        item.contentWindow.postMessage({ msg: `返回${active[0].alt}` }, "*");
      });
    },
    // 添加返回按钮相关监听
    addBackListener() {
      //显示返回按钮
      window.addEventListener("message", (e) => {
        if (e.data.msg === "showBack") {
          this.isShowBack = true;
        }
        //dockMask.style.width = dockItems.length * 60 + 40 + 'px';
      }, 10);
    }
      });
      //隐藏返回按钮
      window.addEventListener("message", (e) => {
        if (e.data.msg === "hiddenBack") {
          this.isShowBack = false;
        }
      });
    },
  },
  directives: {
    drag(el) {
      if (el.dragLock) {
        return
    drag(el, binding) {
      if (el.dragLock || binding.arg) {
        return;
      }
      el.onmousedown = function (e) {
        console.log("binding", binding);
        if (binding.value) return;
        el.dragLock = true;
        let disx = e.clientX - el.parentElement.offsetLeft;
        let disy = e.clientY - el.parentElement.offsetTop;
        el.style.cursor = 'move';
        el.style.cursor = "move";
        document.onmousemove = function (e) {
          e.preventDefault();
          window.getSelection().removeAllRanges();
          let toLeft = e.clientX - disx;
          let toTop = e.clientY - disy;
          //边界处理
          if (toLeft <= -(el.parentElement.offsetWidth-46*3)) {
            toLeft = -(el.parentElement.offsetWidth-46*3)
          } else if (toLeft >= document.body.getBoundingClientRect().width - 46*3) {
            toLeft = document.body.getBoundingClientRect().width - 46*3;
          if (toLeft <= -(el.parentElement.offsetWidth - 46 * 3)) {
            toLeft = -(el.parentElement.offsetWidth - 46 * 3);
          } else if (
            toLeft >=
            document.body.getBoundingClientRect().width - 46 * 3
          ) {
            toLeft = document.body.getBoundingClientRect().width - 46 * 3;
          }
          el.parentElement.style.left = toLeft + 'px';
          el.parentElement.style.left = toLeft + "px";
          if (toTop <= 40) {
            toTop = 40
          } else if (toTop >= document.body.getBoundingClientRect().height - 31) {
            toTop = 40;
          } else if (
            toTop >=
            document.body.getBoundingClientRect().height - 31
          ) {
            toTop = document.body.getBoundingClientRect().height - 31;
          }
          el.parentElement.style.top = toTop + 'px';
          el.parentElement.style.top = toTop + "px";
        };
        document.onmouseup = function () {
          el.dragLock = false;
          document.onmousemove = document.onmouseup = null;
          el.style.cursor = 'unset';
          el.style.cursor = "unset";
        };
      }
      };
    },
    resize(el, binding, vnode) {
      if (el.resizeLock) {
@@ -185,7 +297,7 @@
        let disy = e.clientY;
        let disw = vnode.context.width;
        let dish = vnode.context.height;
        el.style.cursor = 'nw-resize';
        el.style.cursor = "nw-resize";
        document.onmousemove = function (e) {
          window.getSelection().removeAllRanges();
          vnode.context.width = disw + e.clientX - disx;
@@ -194,12 +306,12 @@
        document.onmouseup = function () {
          el.resizeLock = false;
          document.onmousemove = document.onmouseup = null;
          el.style.cursor = 'unset';
          el.style.cursor = "unset";
        };
      }
    }
  }
}
      };
    },
  },
};
</script>
<style scoped>
@@ -209,7 +321,7 @@
}
.d-frame {
  position: fixed;
  box-shadow: 0 5px 10px rgba(0,0,0,.5);
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.5);
}
.d-frame.d-frame-full {
@@ -220,6 +332,7 @@
}
.d-frame .d-frame-title {
  /* min-width: 378px; */
  height: 30px;
  background-color: #e0e0e0;
  border-radius: 4px 4px 0 0;
@@ -227,7 +340,7 @@
  border-left: 1px solid #d0d0d0;
  border-right: 1px solid #d0d0d0;
}
.iframe-mask{
.iframe-mask {
  position: absolute;
  top: 30px;
  left: 0;
@@ -245,6 +358,13 @@
  right: 0;
  height: 30px;
  z-index: 2;
}
.back {
  position: absolute;
  cursor: pointer;
  top: 8px;
  left: 10px;
}
.d-frame-title-operation i {
@@ -274,22 +394,14 @@
  color: #fbb450;
}
/* .d-frame-title-operation:hover .d-frame-operation-minus::before {
  content: "\f056";
} */
.d-frame-operation-full {
  color: #00d800;
}
/* .d-frame-title-operation:hover .d-frame-operation-full::before {
  content: "\f055";
} */
.d-frame-title-content {
  line-height: 30px;
  text-align: center;
  background: #dee1e6;
  background: #ffffff;
}
.d-frame-title-content img {
@@ -304,8 +416,8 @@
  position: relative;
  top: -1px;
  font-size: 15px;
  color: #777;
  /* font-family: '黑体'; */
  color: #000000;
  font-family: "PingFang SC";
}
.d-frame-content {
@@ -314,7 +426,9 @@
  border-radius: 0 0 4px 4px;
  background-color: #fff;
}
.d-frame-full .d-frame-content {
  height: calc(100% - 71px);
}
.d-frame-content iframe {
  width: 100%;
  height: 100%;
@@ -331,4 +445,21 @@
  cursor: nwse-resize;
  background: url("/images/desktop/resize.png") no-repeat;
}
.menuList {
  background: #fff;
  position: absolute;
  z-index: 100;
  width: 180px;
  text-align: left;
  border: 1px solid #ccc;
}
.menuList li {
  line-height: 28px;
  height: 28px;
  padding-left: 24px;
  background: #fff;
}
.menuList li:hover {
  background: rgba(152, 170, 190, 0.7);
}
</style>