<template>
  <div id="app">
    <router-view />
    <!-- 语音播报 -->
    <iframe id="iframeDom" allow="autoplay" style="display:none" :src="src" sandbox=""></iframe>
    <audio class="audio" ref="audio" :src="url" controlsList="nodownload" controls autoplay hidden>
    </audio>
    
  </div>
</template>

<script>
import { getSoundMsg } from "./api/setting.js";
import bus from "./common/bus.js";
export default {
  name: "App",
  data() {
    return {
      url: "https://pc.liebaoai.cn/audio/test.mp3",
      audioList: [
        "https://pc.liebaoai.cn/audio/new-order.mp3",
        "https://pc.liebaoai.cn/audio/lower-than.mp3",
        "https://pc.liebaoai.cn/audio/missed-orders.mp3",
        "https://pc.liebaoai.cn/audio/not-enough.mp3",
        "https://pc.liebaoai.cn/audio/order-received.mp3",
        "https://pc.liebaoai.cn/audio/rider-cancel.mp3",
        "https://pc.liebaoai.cn/audio/rider-go-cancel.mp3",
        "https://pc.liebaoai.cn/audio/user-go-cancel.mp3",
        "https://pc.liebaoai.cn/audio/over-time.mp3",
      ],
      src: "https://pc.liebaoai.cn/audio/alone.mp3",
      timer: null,
      openMsg: false,
    };
  },
  created() {},
  mounted() {
    bus.$on("closeGetSoundMsg", () => {
      console.log("进来准备关闭新订单提醒声音了");
      clearInterval(this.timer);
      this.timer = null;
    });
    bus.$on("openGetSoundMsg", () => {
      console.log("进来准备开启新订单提醒声音了");
      this.timer = setInterval(() => {
        this.getSoundMsg();
      }, 5000);
    });
    // 诱导用户触发点击动作，否则页面刷新后不会自动播放音频
    let userInfo = localStorage.getItem("userInfo");
    if (userInfo) {
      let nickname = JSON.parse(userInfo).nickname;
      this.$confirm(`当前登录账号：${nickname}`, "提示", {
        confirmButtonText: "确定",
        showCancelButton: false,
        type: "warning",
      }).then(() => {});
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
    this.timer = null;
  },
  destroyed() {},
  methods: {
    
    getSoundMsg() {
      getSoundMsg().then((res) => {
        if (res.code === 200) {
          if (res.data) {
            let name = res.data.slice(0, res.data.indexOf("."));
            let url = this.audioList.filter((v) => {
              return v.includes(name);
            })[0];
            this.url = url + "?t=" + new Date().getTime();
          }
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
  },
};
</script>

<style>
body {
  margin: 0;
  padding: 0;
}
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  margin: 0;
  padding: 0;
}
</style>
