element ui实现全局警告动图并播放警报音乐

先看效果图

需求:

        每五分钟一次查询是否存在逾期数据,存在则弹出全局警报(遮罩)并播放警报音乐,点击可关闭,无点击则在20秒后自动关闭,关闭后弹出弹框展示逾期的数据

(本人纯后端狗一枚,公司前端实在没空,才自己写)

-------------------------

        起初使用element ui的loading事件加载了遮罩并设置图片弹出层级给解决了,但是后来发现停留在其他页面时这个警报只加载了弹框,并没有加载图片,是因为这个的图片只在本页面也就是index.vue起作用无法在其他地方看到,在index2.vue 或者index3.vue 就不起作用了。       

        此时就想想,觉得应该做个全局组件使用,奈何本人技艺不高,不会!!!就想了很多办法,后来灵光一现突然想起来element ui里面的 el-dialog,有遮罩,并且还是全局的!!!想到了就用,通过自己改了改,功能轻轻松松实现!!,上代码!!!

<style>
/* 加这一块是因为,用过el-dialog的都知道,弹框是有弹框头和弹框体的边框的,那样加载出来就很难看
 同时也是为了不影响页面其他地方的弹框,给dialog上了一个id ,通过选择器来定位*/
#errDia .el-dialog .el-dialog__header{
  display:none
}
#errDia .el-dialog .el-dialog__body{
  padding:0px;
}
</style>


<template>
  <div class="app-container">
    <!-- 
       *****
       -->
    
    <!-- 警报-->
    <audio :src="src" autoplay="autoplay" controls="controls" loop="loop" ref="audio" v-show="false" id="jbMusic"/>
    
    <!--定义父遮罩-->
    <el-dialog :visible.sync="overdueAlertDiaFlag" width="70%" append-to-body :close-on-click-modal="false"
               @click="playStop" id="errDia">
      <img src="@/utils/img/jbBag.gif"  style="width: 100%" @click="playStop(this)">
    </el-dialog>

    <!--  逾期弹框  -->
    <el-dialog title="即将逾期数据列表" :visible.sync="overdueFlag" width="50%" append-to-body :close-on-click-modal="false">
      <div style="width: 100%;height: 600px;overflow-y:scroll">
              <!--   xxxxxxxxxx    就是个展示数据 -->
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeOverdueDia">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { getArchiveAndUpOverdue, checkShDept } from "@/api/business/circulation";
import mp3 from "@/utils/music/jb.mp3";

var timeOut = null;
export default {
  name: "Circulation",
  data() {
    return {
      src:null,
      audioSrc:mp3,
      //即将逾期数据
      overdueList:[],
      overdueFlag:false,
      //逾期弹框
      overdueAlertDiaFlag:false
    };
  },
  created() {
    /** 页面加载时去判断是否是逾期数据*/
    this.checkUser();
  },
  methods: {
    checkUser(){
      checkShDept().then((rep)=>{
        if(true == rep){
          setInterval(this.getOverArchive,60000 );
        }
      })
    },
    /** 定时去获取逾期数据*/
    getOverArchive() {
      this.overdueList = [];
      getArchiveAndUpOverdue().then((rep) => {
        if(rep.length > 0){
          this.overdueList = rep;
          this.playAudio();
        }
      })
    },
    /** 播放音乐*/
    playAudio() {
      this.overdueFlag = false;
      this.src = this.audioSrc;
      this.$refs.audio.currentTime = 0;
      this.$refs.audio.loop = true;
      this.$refs.audio.play();
      this.overdueAlertDiaFlag = true;
      /** 20秒后自动停止*/
      let thi = this;
      timeOut = setTimeout(function () {
        thi.playStop(thi);
      }, 20000);
    },
    playStop(thi) {
      let th = this || thi;
      th.src = null;
      th.overdueFlag = true;
      th.$refs.audio.pause();
      th.overdueAlertDiaFlag = false;
      window.clearTimeout(timeOut)
    },
    /** 关闭逾期弹框*/
    closeOverdueDia(){
      this.overdueFlag = false;
      this.overdueList = [];
    }
  }
};
</script>

写出来真是太不容易了,自己思考的很复杂,没想到最后灵光一闪真的太重要了,简简单单就给实现了

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值