<template>
  <div class="my-promote">
    <img :src="$filters.urlToHttps(imgUrl)" class="my-code">
    <div class="download-code" @click="downloadCode">保存小程序推广码</div>
    <div class="descript">邀请他人注册并下单后您将获得一笔赏金</div>
    <div class="price">
      已获得赏金: <span class="money">￥{{$filters.toFixed2(selfInfo.profitSum)}}</span>
    </div>
    <div class="price" v-if="selfInfo.arrears">
      由于用户退款所欠赏金: <span class="money2">￥{{$filters.toFixed2(selfInfo.arrears)}}</span>
    </div>
    <!-- loading -->
    <zy-loading></zy-loading>
  </div>
</template>

<script>
import ZyLoading from '@/components/Loading/loading'
import {errToast, toast, modal} from '@/utils/uniapi.js'
import {mapGetters} from 'vuex'

// #ifdef APP-PLUS || APP
// import permision from "@/utils/permission.js"
// #endif

import loginApi from '@/apis/login'
export default {
  components: {
    ZyLoading
  },
  data () {
    return {
      imgUrl: null
    }
  },
  computed: {
    // store.getters['login/selfInfo'].mobile
    ...mapGetters({
      selfInfo: 'login/selfInfo'
    })
  },
  async onShow () {
    try {
      this.showLoad()
      await this.reqGetWxcode()
    } catch (e) {
      errToast(e)
    } finally {
      this.hideLoad()
    }
  },
  methods: {
    async reqGetWxcode () {
      try {
        // this.imgUrl = await getWxcode()
        let mobile = this.selfInfo && this.selfInfo.mobile ? {scene:'promote=' + this.selfInfo.mobile}:'brandID=1'
        let res =await loginApi.getTiktokOrWxCode(mobile)

        // #ifdef MP-WEIXIN
        this.imgUrl = res.wxcode
        // #endif

        // #ifdef MP-TOUTIAO
        this.imgUrl = res.tiktok
        // #endif

      } catch (e) {
        throw e
      }
    },
    // 下载二维码
    async downloadCode () {
      try {
        // this.showLoad()
        console.log('保存推广码-------',this.imgUrl)
        const [err, res] = await uni.downloadFile({ url: this.imgUrl })
        console.log('查看信息,,err',err)
        console.log('查看信息,,,res',res)
        if (err) throw err
		
        // #ifndef APP-PLUS || APP
        console.log('你不应该走这里')
        const [err2, res2] = await uni.authorize({
          scope: 'scope.writePhotosAlbum'
        })

        if (err2) {
          let confirm = await modal('', '需要打开相册使用权限', {
            confirmText: '前往开启',
            showCancel: false
          })
          if (confirm) {
            uni.openSetting()
          }
          return
        }
        // #endif

        // #ifdef APP-PLUS || APP
        console.log('你走这里了嘛99999999999999')
          // const result = await permision.requestAndroidPermission('android.permission.WRITE_EXTERNAL_STORAGE')
          const result = ''
          let err2 = ''
          if (result == 1) {
            // res2 = "已获得授权"
            console.log('已经获得授权')
            err2 = ''
          } else if (result == 0) {
            err2 = "未获得授权"
          } else {
            err2 = "被永久拒绝权限"
          }
          console.log(result,'打印权限信息',err2)
        // #endif

        if (err2) {
          // #ifndef APP-PLUS || APP
          let confirm = await modal('', '需要打开相册使用权限', {
            confirmText: '前往开启',
            showCancel: false
          })
          if (confirm) {
            uni.openSetting()
          }
          // #endif

          // #ifdef APP-PLUS || APP
           toast(err2)
          // #endif

        } else {
          const [err3, res3] = await uni.saveImageToPhotosAlbum({filePath: res.tempFilePath})
          if (err3) throw(err3)
          toast('已成功下载到相册')
        }
      } catch (e) {
        console.error(e)
        errToast(e)
      } finally {
        this.hideLoad()
      }
    }
  }
}
</script>

<style lang="scss">
@import "./my-promote.scss"
</style>