<template>
  <view class="Mall4j page-invitation-card">
    <view class="card-con">
      <view class="card-box">
        <view class="bg">
          <image src="@/package-activities/static/images/invitation-card.png" />
        </view>
        <view class="card">
          <view class="inviter">
            <image
              v-if="distInfo.pic"
              :src="util.addDomain(distInfo.pic)"
              @error="distInfo.pic=''"
            />
            <image
              v-else
              src="/static/images/head-sculpture.png"
            />
          </view>
          <view class="name">
            {{ distInfo.nickName }}
          </view>
          <view class="des">
            邀请你一起加入，推广赢奖励！
          </view>
          <view class="code">
            <image :src="shareWxCode" />
          </view>
        </view>
      </view>
      <view class="action">
        截图保存至相册
      </view>
    </view>
  </view>
</template>

<script setup>
import Qr from '@/utils/wxqrcode'
import util from '@/utils/util.js'

onLoad(() => {
  onGenWeixinCode()
  onGetDisInfo()
})

// 获取分销员信息
const distInfo = ref({})
const onGetDisInfo = () => {
  http.request({
    url: '/mall4cloud_marketing/distribution_user/distribution_user_info',
    method: 'GET'
  }).then(res => {
    distInfo.value = res
  })
}

// 生成小程序二维码
const shareWxCode = ref('') // 生成的二维码路径 微信端
const onGenWeixinCode = () => {
  const distributionUserId = uni.getStorageSync('cloudDistributionUserId')
  // #ifdef H5 || APP-PLUS
  const code = `${import.meta.env.VITE_APP_DOMAIN_ADDRESS}/package-activities/pages/apply-distribution/apply-distribution?distributionUserId=${distributionUserId}`
  shareWxCode.value = Qr.createQrCodeImg(code, { size: 250 })
  // #endif
  // #ifdef MP-WEIXIN
  http.request({
    url: '/mall4cloud_marketing/ua/distribution_qrcode/invitation',
    method: 'GET',
    responseType: 'arraybuffer',
    data: {
      page: 'package-activities/pages/apply-distribution/apply-distribution',
      scene: distributionUserId
    }
  }).then(res => {
    shareWxCode.value = 'data:image/jpg;base64,' + uni.arrayBufferToBase64(res)
  })
  // #endif
}
</script>

<style lang="scss" scoped>
@use "./invitation-card.scss";
</style>
