111
jihongshun
8 天以前 0c741cdda7ef9935a20d3090dfea97e1ce8ae754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<template>
  <div :style="{width:windowWidth+'px',height:conHeight+'px',backgroundColor: designCache.bgColor,
  backgroundImage: designCache.bgImg ? 'url('+fileUrl+designCache.bgImg+')':'none'}"
       style="background-size:100% 100%;background-color: #2b3340;overflow: hidden">
    <div style="position:relative;transform-origin:0 0;"
         :style="{width:windowWidth+'px',height:conHeight+'px',transform: 'scale('+containerScale+')'}">
      <transition-group appear name="bounce">
        <div v-for="item in designCache.components" :key="item.id"
             style="position: absolute;"
             :style="{width:Math.round(item.cptWidth)+'px',
                  height:Math.round(item.cptHeight)+'px',
                  top:Math.round(item.cptY)+'px',
                  left:Math.round(item.cptX)+'px',
                  zIndex:item.cptZ}">
 
          <component :is="item.cptKey" :width="Math.round(item.cptWidth)"
                   :height="Math.round(item.cptHeight)" @reload="loadCacheData"
                   :option="item.cptOption"/>
        </div>
      </transition-group>
    </div>
 
    <el-dialog title="请输入访问码" :visible.sync="authCodeDialogVisible" width="30%" center
               :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form>
        <el-form-item label="访问码">
          <el-input v-model="viewCode" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="authCode">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import {authViewCodeApi, getByIdApi} from "@/api/DesignerApi";
import {fileUrl} from "/env";
 
import CptThreeDom from "@/components/three/cpt-three-dom.vue";
export default {
  name: "preview_index",
  components: {CptThreeDom},
  data(){
    return{
      fileUrl:fileUrl,
      designCache:{},
      windowWidth: 0,
      windowHeight: 0,
      conHeight: 0,
      containerScale:1,
      authCodeDialogVisible:false,
      viewCode:''
    }
  },
  mounted() {
    const that = this;
    that.loadCacheData();
    window.onresize = () => {
      return (() => {
        that.loadSize()
      })();
    };
  },
  methods:{
    loadCacheData(){
      const path = this.$route.path;
      const that = this;
      const id = this.$route.query.id;
      if (path === '/preview'){
        let designCache = JSON.parse(localStorage.getItem('designCache'));
        this.loadDesign(designCache,false);
      }else if(path === '/view'){
        if (!id){
          this.$message.error('id错');
          return;
        }
        const viewCode = localStorage.getItem('code'+id);//如果已经输入过访问码就带着访问码一起请求
        getByIdApi(id,1, viewCode).then(res => {
          if (res.data === 'NEED_AUTH'){
            that.authCodeDialogVisible = true;
          }else{
            that.loadDesign(res.data, true);
          }
        })
      }
    },
    loadDesign(design, componentPares){
      if (componentPares){
        design.components = JSON.parse(design.components);
      }
      document.title = design.title;
      this.designCache = design;
      this.loadSize();
    },
    authCode(){
      if (!this.viewCode){
        this.$message.error('请输入访问码');
        return;
      }
      const id = this.$route.query.id;
      authViewCodeApi({id: id, viewCode:this.viewCode}).then(res => {
        this.authCodeDialogVisible = false;
        localStorage.setItem('code'+id, res.data.viewCode);//缓存访问码避免二次刷新需要再次输入
        this.loadDesign(res.data,true)
      })
    },
    loadSize(){
      this.windowWidth = document.documentElement.clientWidth;
      this.windowHeight = document.documentElement.clientHeight;
      this.containerScale = Math.round(this.windowWidth / this.designCache.scaleX * 100) / 100
      this.conHeight = this.designCache.scaleY
      console.log(this.windowWidth,this.containerScale)
    },
  }
}
</script>
 
<style scoped>
.bounce-enter-active{
  transition: all 1s;
  /*animation: bounce-in 1s;*/
}
.bounce-enter{
  opacity: 0;
  transform: scale(.5);
}
@keyframes bounce-in {
  0% {
    transform: scale(0);
  }
  25% {
    transform: scale(0.5);
  }
  50% {
    transform: scale(1);
  }
  75% {
    transform: scale(1.5);
  }
  100% {
    transform: scale(1);
  }
}
</style>