<template>
	<view class="content">

	</view>
</template>

<script>
	import {getToken,getUserInfo} from "@/utils/auth";
  import request from '@/utils/request';
  let innerAudioContext ='';
    const keepAlivePlugin = uni.requireNativePlugin('Ba-KeepAlive')
	export default {
		data() {
			return {
        // 是否正在播放
        ifPlay:false,
				keepLive: {
					channelId: 'Ba-KeepAlive',
					channelName: "Ba-KeepAlive",
					title: "汽修小助手正在运行",
					content: "汽修小助手正在运行",
					dataResult: "",
					type: undefined
				}
			}
		},
    onLoad() {
      // #ifdef APP
      //保活技术
      this.register()
      // #endif
      if (getToken()) {
        this.getNoReadNum()
        //跳转首页
        //判断是否是仓管，仓管需要跳单独的首页
        let userInfo = getUserInfo()
        if (userInfo.roleCodes.includes('repair_warehouse')) {
          //仓管
          uni.navigateTo({
            url: '/pages-warehouse/home/home'
          })
        } else {
          uni.navigateTo({
            url: '/pages-home/home/home'
          })
        }
      } else {
        uni.navigateTo({
          url: '/pages/login/login'
        })
      }
    },
    onShow(){
      if (getToken()) {
        //判断是否是仓管，仓管需要跳单独的首页
        let userInfo = getUserInfo()
        if (userInfo.roleCodes.includes('repair_warehouse')) {
          //仓管
          uni.navigateTo({
            url: '/pages-warehouse/home/home'
          })
        } else {
          uni.navigateTo({
            url: '/pages-home/home/home'
          })
        }
      } else {
        uni.navigateTo({
          url: '/pages/login/login'
        })
      }
    },
		methods: {
			register() { //注册
				console.log(keepAlivePlugin, 'keepAlive');
				keepAlivePlugin.register({
						channelId: this.keepLive.channelId,
						channelName: this.keepLive.channelName,
						title: this.keepLive.title,
						content: this.keepLive.content,
					},
					(res) => {
						console.log('保活注册', res);
					});
			},
      /**
       * 获取未读消息数量
       * @returns {Promise<void>}
       */
      async getNoReadNum(){
        await request({
          url: "/admin-api/system/notify-message/get-unread-count",
          method: "GET"
        }).then((res) => {
          if(res.code==200 && res.data>0){
              this.dianyidain()
          }
        })
      },
      dianyidain() {
        if(!this.ifPlay){
          console.log('执行了，dianyidain');
          if(innerAudioContext!=""){
            try {
              console.log('调用前先销毁');
              innerAudioContext.stop();
              innerAudioContext.destroy();
              innerAudioContext="";
            }catch (e){
              console.log('销毁出错');
            }
          }
          innerAudioContext = uni.createInnerAudioContext();
          this.ifPlay=true
          // #ifdef APP-PLUS
          innerAudioContext.src = '../../static/msgV.mp3';
          // #endif
          // #ifndef APP-PLUS
          innerAudioContext.src = 'https://www.nuoyunr.com/lananRsc/rescue/msgV.mp3';
          // #endif
          // 设置播放次数和计数器
          const playCount = 4;
          let currentCount = 0;
          // 初次播放
          innerAudioContext.play();
          // 震动
          uni.vibrateLong({
            success: function () {
              console.log('success');
            }
          });
          innerAudioContext.onError((err) => {
            console.error('播放错误', err);
            this.ifPlay=false
            innerAudioContext.stop();
            innerAudioContext.destroy(); // 播放错误后释放实例
          });
          // 监听音频播放结束事件
          innerAudioContext.onEnded(() => {
            // 播放计数加一
            currentCount++;
            // 判断是否达到播放次数上限
            if (currentCount < playCount) {
              // 继续播放
              innerAudioContext.play();
              // 震动
              uni.vibrateLong({
                success: function () {
                  console.log('success');
                }
              });
            } else {
              // 播放完成，可以在这里添加额外的逻辑
              console.log('播放完成');
              this.ifPlay=false
              //及时释放资源
              innerAudioContext.stop();
              innerAudioContext.destroy();
            }
          });
        }else{
          console.log('正在播放音频，拒绝播放请求');
        }
      },
		}
	}
</script>

<style>

</style>
