<script>
import { go2 } from '@/utils/hook.js'
import { isString } from '@/utils/is.js'
import store from '@/store/index.js'
import handlePushMsg from '@/utils/handlePushMsg.js'
export default {
  onLaunch: function () {
    const token = store.state.qnToken
    let flag = uni.getStorageSync('hasLaunch')
    if (flag && !token) {
      go2('login')
    }
    if (flag && token) {
      go2('client')
    }
    // #ifdef APP-PLUS

    let client = uni.getSystemInfoSync().platform
    setTimeout(() => {
      plus.push.addEventListener(
        'click',
        function (msg) {
          handlePushMsg(msg)
        },
        false
      )
      // 监听在线消息事件
      plus.push.addEventListener(
        'receive',
        function (msg) {
          if (isString(msg.payload)) {
            msg.payload = JSON.parse(msg.payload)
          }
          if (client === 'ios') {
            if (msg.aps == null && msg.type == 'receive') {
              let content = msg.payload.content
              let title = msg.payload.title
              let payload = JSON.stringify(msg.payload)
              console.log('ios receive:', title, payload, content)
              plus.push.createMessage(content, payload, {
                title: title,
              })
              if (msg.payload.messageType == 4) {
                enquiryVoice()
              }
            }
          }
          if (client === 'android') {
            let title = msg.title
            let content = msg.content
            plus.push.createMessage(content, JSON.stringify(msg.payload), {
              title: title,
            })
            if (msg.payload.messageType == 4) {
              enquiryVoice()
            }
          }
          // 在线语音播放
        },
        false
      )
    }, 1000)
    // #endif
    let timer = null
    function enquiryVoice() {
      if (timer) {
        clearTimeout(timer)
        timer = null
      }
      timer = setTimeout(() => {
        const innerAudioContext = uni.createInnerAudioContext()
        innerAudioContext.autoplay = true
        innerAudioContext.src = 'https://qncloud.oss-cn-shenzhen.aliyuncs.com/paper_shopkeeper/BasePaperInquiryVoice.mp3'
        innerAudioContext.onPlay(() => {
          console.log('开始播放')
        })
        innerAudioContext.onError((res) => {
          console.log(res.errMsg)
          console.log(res.errCode)
        })
      }, 3000)
    }
  },
  onShow: function () {
    // 先默认进入应用就清除所有消息
    plus.runtime.setBadgeNumber(0)
  },
  onHide: function () {},
}
</script>

<style>
/*每个页面公共css */
@import url('./common/css/reset.scss');
@import '@/static/icon/iconfont.css';
page {
  height: 100%;
}
/* uni-dialog标题样式 */
.uni-dialog-title-text {
  font-size: 32rpx !important;
  color: #323233 !important;
  font-weight: 500 !important;
}
</style>
