<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<script setup>
import { WKSDK, RecvackPacket, ChatManager } from 'wukongimjssdk'
import util from './utils/util'

onLaunch(() => {
  // eslint-disable-next-line no-console
  console.log('mall4j.v240506')
  // #ifdef H5
  // 判断是什么系统，用于微信验签时传递参数
  uni.getSystemInfo({
    success: function (res) {
    // 客户端平台，值域为：ios、android
      if (res.platform === 'ios') {
        uni.setStorageSync('bbcIosUrl', window.location.href.split('#')[0])
      }
    }
  })
  // #endif

  const payTypeParams = {
    url: '/sys/config/paySettlementType',
    method: 'get'
  }
  http.request(payTypeParams).then(({ data }) => {
    if (data) {
      const paySettlementType = JSON.parse(data).paySettlementType
      uni.setStorageSync('bbcPaySettlementType', paySettlementType)
    }
  })
  // 获取语言配置
  getLangs()

  nextTick(() => {
    getApp().globalData.connectSuccess = false
  })
})

// 获取语言配置
const getLangs = () => {
  const params = {
    url: '/sys/lang',
    method: 'GET'
  }
  http.request(params).then(({ data: res }) => {
    // 获取用户设置的语言
    const clientLang = uni.getStorageSync('bbcMsLang')

    // 本地没有缓存语言，使用后台默认语言
    if (!clientLang) {
      uni.setStorageSync('bbcMsLang', res.language)
      uni.setStorageSync('bbcMsLangKey', res.lang)
    }
  })
}

ChatManager.prototype.sendRecvackPacket = function (recvPacket) {
  const packet = new RecvackPacket()
  packet.noPersist = recvPacket.noPersist
  packet.syncOnce = recvPacket.syncOnce
  packet.reddot = recvPacket.reddot
  packet.messageID = recvPacket.messageID
  packet.messageSeq = recvPacket.messageSeq
  WKSDK.shared().connectManager.sendPacket(packet)
}

onShow((options) => {
  if (uni.getStorageSync('bbcToken_us')) {
    util.getWuKongIm()
  }
  // #ifdef MP-WEIXIN
  // 通联支付从通联微信小程序返回后接收的参数
  if (uni.getStorageSync('bbcPaySettlementType') !== 1) {
    // 非通联支付模式下不处理
    return
  }
  if (options.referrerInfo && options.referrerInfo.extraData) {
    let backMerchantUrl = options.referrerInfo.extraData.backMerchantUrl
    if (backMerchantUrl) {
      backMerchantUrl = decodeURIComponent(backMerchantUrl)
      const result = backMerchantUrl.split('?')[1].split('&')
      const query = {}
      for (let i = 0; i < result.length; i++) {
        const item = result[i].split('=')
        query[item[0]] = item[1]
      }
      if (Number(query.isSignSuccess) === 1 && query.bizContent) {
        // 获取内存中的签约编号
        const acctProtocolNo = uni.getStorageSync('bbcAllinpayAcctProtocolNo')
        const bizContent = JSON.parse(decodeURIComponent(query.bizContent))
        if (bizContent.acctProtocolNo !== acctProtocolNo) {
          uni.showToast({
            title: '签约成功',
            icon: 'none'
          })
          uni.setStorageSync('bbcAllinpayAcctProtocolNo', bizContent.acctProtocolNo)
        }
      } else if (Number(query.isSignSuccess) === 0) {
        uni.showToast({
          title: '签约失败',
          icon: 'none'
        })
      }
    }
  }
  // #endif
})

// 页面不存在时触发
onPageNotFound(() => {
  uni.reLaunch({
    url: '/pages/index/index'
  })
})

</script>
<!-- eslint-disable-next-line -->
<style lang="scss">
@import './app.scss';
@import url('@/static/iconfont/iconfont.css');
</style>
