<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'
import { checkUpdate } from '@/apis/commonApi.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
    this.createPushEvent()
    // #endif
    // app更新
    //#ifdef APP-PLUS
    checkUpdate().then((version) => {
      let curVersion = plus.runtime.version
      if (version != curVersion) {
        let client = uni.getSystemInfoSync().platform
        if (client === 'ios') {
          uni.showModal({
            title: '更新提示',
            content: '有新的版本更新，请前往App Store更新'
          })
        } else {
          uni.showModal({
            title: '更新提示',
            content: '有新版本，请前往更新',
            success: (res) => {
              if (res.confirm) {
                plus.runtime.openURL(`https://www.pgyer.com/ST8i`)
              }
            }
          })
        }
      }
    })
    //#endif
  },
  onShow: function () {
    // 先默认进入应用就清除所有消息
    plus.runtime.setBadgeNumber(0)
  },
  data() {
    return {
      timer: null
    }
  },
  methods: {
    createPushEvent() {
      let client = uni.getSystemInfoSync().platform
      setTimeout(() => {
        plus.push.addEventListener(
          'click',
          function (msg) {
            console.log('click msg', 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) {
                  this.enquiryVoice()
                }
              }
            }
            if (client === 'android') {
              console.log('receive msg', msg)
              let title = msg.title
              let content = msg.content
              plus.push.createMessage(content, JSON.stringify(msg.payload), {
                title: title
              })
              if (msg.payload.messageType == 4) {
                this.enquiryVoice()
              }
            }
            // 在线语音播放
          },
          false
        )
      }, 1000)
    },
    enquiryVoice() {
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
      this.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)
    }
  }
}
</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>
