widgetHtmlSlider.vue 12.1 KB
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
<template>
  <div class="fullscreen-carousel" :style="style">
    <!-- 进度指示器 -->
    <div class="progress-indicator" :style="{width: progressWidth}"></div>

    <!-- 控制按钮 -->
<!--    <div class="controls">-->
<!--      <button class="control-btn" @click="prevSlide">-->
<!--        <i class="el-icon-arrow-left"></i>-->
<!--      </button>-->
<!--      <button class="control-btn" @click="nextSlide">-->
<!--        <i class="fas el-icon-arrow-right"></i>-->
<!--      </button>-->
<!--    </div>-->

    <el-carousel
      :direction="carouselStyle.direction"
      :indicator-position="carouselStyle.indicatorPosition"
      :type="carouselStyle.type"

      :interval="intervalTime"
      :autoplay="autoPlay"
      :loop="true"
      @change="handleSlideChange"
      ref="carousel">
      <el-carousel-item v-for="(slide, index) in slides" :key="index">
        <div class="slide">
          <div class="content-container">
            <!-- 当前和下一个幻灯片预加载 -->
            <template v-if="index === currentSlide || index === nextSlideIndex">
              <!-- 尝试使用object标签作为备用方案 -->
              <object v-if="slide.useObject"
                      :data="slide.url"
                      type="text/html"
                      @load="onContentLoad(index)"
                      @error="onContentError(index)"
                      :class="{loaded: !slide.loading && !slide.error}">
                <div class="fallback-content">
                  <i class="fas fa-exclamation-triangle"></i>
                  <h4>内容加载失败</h4>
                  <p>无法加载报表内容,请检查网络连接</p>
                </div>
              </object>

              <!-- 主要使用iframe -->
              <iframe v-else
                      :src="slide.url"
                      frameborder="0"
                      allowfullscreen
                      @load="onContentLoad(index)"
                      @error="onContentError(index)"
                      referrerpolicy="no-referrer"
                      :class="{loaded: !slide.loading && !slide.error}"
              ></iframe>
            </template>

            <div class="loading" v-if="slide.loading">
              <div class="spinner"></div>
              <p>报表加载中...</p>
            </div>

            <div class="error" v-if="slide.error && !slide.loading">
              <i class="fas fa-exclamation-triangle"></i>
              <p>报表加载失败</p>
            </div>
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>

    <!-- 圆圈页码指示器 -->
    <div class="circle-indicator">
      <div
        v-for="(slide, index) in slides"
        :key="index"
        :class="['circle', { active: currentSlide === index }]"
        @click="goToSlide(index)"
      >
      </div>
    </div>

  </div>

</template>
<script>

export default {
  name: 'WidgetHtmlSlider',
  components: {},
  props: {
    value: Object,
    ispreview: Boolean
  },
  data() {
    return {
      options: {},
      optionsSetUp:{},
      carouselKey: 0, // 重新渲染组件用
      flagInter: null,

      autoPlay: true,
      intervalTime: 5000, // 5秒切换一次
      currentSlide: 0,
      nextSlideIndex: 1,
      progressWidth: '0%',
      progressInterval: null,

      slides: [{
        title: "",
        url: "#",
        loading: true,
        error: false,
        useObject: false
      }]
    };
  },
  computed: {
    transStyle() {
      const obj = this.objToOne(this.options);
      return obj;
    },
    style() {
      //整屏显示
      return {
        position: this.ispreview ? "absolute" : "static",
        width: "100%",
        height: "100%",
        left:"0px",
        top:"0px",
      };
    },
    carouselStyle() {
      this.initData(this.transStyle);
      return {
        direction: !this.transStyle.tabType ? this.transStyle.tabDirection : 'horizontal',
        indicatorPosition: this.transStyle.tabSelector,
        // intervalTime: this.transStyle.tabTime,
        type: this.transStyle.tabType
      }
    }
  },
  watch: {
    value: {
      handler(val) {
        if(!this.ispreview){
          this.options = val;
          this.optionsSetUp = val.setup;
          this.handlerUrlData();
        }
      },
      deep: true,
    },
    carouselStyle: {
      handler(newValue, oldValue) {
        if (JSON.stringify(newValue) !== JSON.stringify(oldValue)
        && !this.ispreview
        ) {
          this.carouselKey++;
        }
      },
    },
  },
  created() {
    this.options = this.value;
  },
  mounted() {
    // 初始加载第一个幻灯片
    this.slides[0].loading = true;
    this.startProgressBar();
    // 设置超时检查
    setTimeout(() => {
        // console.log("12313",this.slides);
      for(let index=0;index<this.slides.length;index++){
        let slide = this.slides[index];
        if (slide.loading && !slide.useObject) {
          this.slides[index].useObject = true;
        } else if (slide.loading && slide.useObject) {
          this.slides[index].loading = false;
          this.slides[index].error = true;
        }
      }
      // _this.slides.forEach((slide, index) => {
      //   if (slide.loading && !slide.useObject) {
      //     this.slides[index].useObject = true;
      //   } else if (slide.loading && slide.useObject) {
      //     this.slides[index].loading = false;
      //     this.slides[index].error = true;
      //   }
      // });
    }, this.intervalTime);
  },
  beforeDestroy() {
    clearInterval(this.progressInterval);
  },
  methods: {
    // reload(){
    //   this.isRouterAlive=false;
    //   this.$nextTick(function(){
    //     this.isRouterAlive=true;
    //   })
    // },
    initData(data) {
      let urlUp =[];
      if(this.hasOwn(data,"urls")){
        urlUp =  data['urls'];
      }
      let slides = [];
      if(this.isNotBlankArray(urlUp)){
        for(let i=0;i<urlUp.length;i++){
          const url = urlUp[i].url;
          const slidesOne = {};
          slidesOne.title="";
          slidesOne.url=url;
          slidesOne.loading=true;
          slidesOne.error=false;
          slidesOne.useObject=false;
          slides.push(slidesOne);
        }
      }
      this.slides = slides;
      this.intervalTime = this.isNotBlank(this.transStyle.tabTime)? this.transStyle.tabTime:5000;
      // this.slides={...this.slides};
    },
    handlerUrlData(){
      const data = this.optionsSetUp;
      let urlUp =[];
      if(this.hasOwn(data,"urls")){
        urlUp =  data['urls'];
      }else if(this.hasOwn(data,"dynamicHtmlSlider")){
        urlUp =  data['dynamicHtmlSlider'];
      }
      let slides = [];
      if(this.isNotBlankArray(urlUp)){
        for(let i=0;i<urlUp.length;i++){
          const url = urlUp[i].url;
          const slidesOne = {};
          slidesOne.title="";
          slidesOne.url=url;
          slidesOne.loading=true;
          slidesOne.error=false;
          slidesOne.useObject=false;
          slides.push(slidesOne);
        }
      }
      this.slides = slides;
      // this.slides={...this.slides};
    },

    goToSlide(index) {
      this.$refs.carousel.setActiveItem(index);
    },
    prevSlide() {
      this.$refs.carousel.prev();
    },
    nextSlide() {
      this.$refs.carousel.next();
    },
    onContentLoad(index) {
      this.slides[index].loading = false;
      this.slides[index].error = false;
    },
    onContentError(index) {
      if (!this.slides[index].useObject) {
        // 尝试使用object标签作为备用
        this.slides[index].useObject = true;
        this.slides[index].loading = true;
        this.slides[index].error = false;
      } else {
        this.slides[index].loading = false;
        this.slides[index].error = true;
      }
    },
    handleSlideChange(currentIndex, prevIndex) {
      this.currentSlide = currentIndex;
      // 计算下一个幻灯片索引(用于预加载)
      this.nextSlideIndex = (currentIndex + 1) % this.slides.length;

      // 启动当前幻灯片的加载(如果尚未加载)
      if (this.slides[currentIndex].loading === false &&
        this.slides[currentIndex].error === false) {
        // 内容已加载,无需操作
      } else {
        this.slides[currentIndex].loading = true;
        this.slides[currentIndex].error = false;
      }

      // 启动进度条
      this.startProgressBar();
    },
    startProgressBar() {
      clearInterval(this.progressInterval);
      this.progressWidth = '0%';

      if (!this.autoPlay) return;

      const startTime = Date.now();
      const duration = this.intervalTime;

      this.progressInterval = setInterval(() => {
        const elapsed = Date.now() - startTime;
        const percent = Math.min(100, (elapsed / duration) * 100);
        this.progressWidth = percent + '%';

        if (percent >= 100) {
          clearInterval(this.progressInterval);
        }
      }, this.intervalTime);
    }
  },
}
</script>
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: #000;
}

.fullscreen-carousel {
  width: 100%;
  height: 100%;
  position: relative;
  background: #000;
}

.el-carousel {
  width: 100%;
  height: 100%;
}

.el-carousel__container {
  height: 100% !important;
}

.slide {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #000;
}

.content-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}

.content-container iframe,
.content-container object {
  width: 100%;
  height: 100%;
  border: none;
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
}

.content-container iframe.loaded,
.content-container object.loaded {
  opacity: 1;
}

.fallback-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #2c3e50, #4ca1af);
  color: white;
  text-align: center;
  padding: 20px;
}

.fallback-content i {
  font-size: 3rem;
  margin-bottom: 20px;
  color: #ffc107;
}

.fallback-content h4 {
  font-size: 1.5rem;
  margin-bottom: 10px;
}

.loading, .error {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  z-index: 20;
}

.loading i, .error i {
  font-size: 2rem;
  margin-bottom: 15px;
}

/* 隐藏轮播指示器 */
.el-carousel__indicators {
  display: none;
}

/* 加载动画 */
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 圆圈页码指示器 */
.circle-indicator {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 100;
}

.circle {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.4);
  transition: all 0.3s ease;
  cursor: pointer;
}

.circle.active {
  background: #fff;
  transform: scale(1.2);
  box-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}

.circle:hover {
  background: rgba(255, 255, 255, 0.7);
}

/* 控制按钮 */
.controls {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.fullscreen-carousel:hover .controls {
  opacity: 0.7;
}

.controls:hover {
  opacity: 1;
}
//el-icon-arrow-left
.control-btn {
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  font-size: 1.5rem;
  transition: all 0.3s ease;
}

.control-btn:hover {
  background: rgba(0, 0, 0, 0.8);
  transform: scale(1.1);
}

/* 进度指示器 */
.progress-indicator {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, #4ca1af, #2c3e50);
  z-index: 101;
  transition: width 0.3s ease;
}
</style>