<template>
<view id="shareImg">
    <view class="img-box">
        <image mode="widthFix" :src="info.image" alt></image>
    </view>
    <view class="btn-box" v-if="info.image">
        <view class="btn" :style="'background: ' + config.mainColor" @tap="downImg">保存海报</view>
    </view>
    <!--用户拒绝下载图片授权弹出-->
    <alert :is_refused="is_refused" @:close="closeAlert"></alert>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
import alert from "@/components/alert/alert";

export default {
  data() {
    return {
      info: '',
      order_no: '',
      config: '',
      is_refused: false
    };
  },

  onLoad(e) {
    this.getShearImg();
  },

  components: {
    alert
  },
  props: {},
  methods: {
    getShearImg() {
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      var token = this.$cookieStorage.get('user_token');
      this.$http.get({
        api: 'api/free/goods/share/image',
        data: {
          pages: 'pages/other/vip/vip' // pages: 'pages/store/detail/detail',

        },
        header: {
          Authorization: token
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            this.setData({
              info: res.data
            });
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: '请求失败',
            showCancel: false
          });
        }

        wx.hideLoading();
      }).catch(() => {
        wx.hideLoading();
        wx.showModal({
          content: '请求失败',
          showCancel: false
        });
      });
    },

    closeAlert() {
      this.setData({
        is_refused: false
      });
    },

    // 下载图片
    downImg() {
      if (this.info.image) {
        wx.showLoading({
          title: '正在下载',
          mask: true
        });
        this.$http.dowloadFile({
          api: this.info.image
        }).then(res => {
          if (res.statusCode == 200) {
            wx.getSetting({
              success: ret => {
                // 如果之前没有授权
                if (!ret.authSetting['scope.writePhotosAlbum']) {
                  wx.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success: rej => {
                      this.saveImg(res.tempFilePath);
                    },
                    // 用户拒绝授权
                    fail: ret => {
                      this.setData({
                        is_refused: true
                      });
                      wx.hideLoading();
                    }
                  });
                } else {
                  this.saveImg(res.tempFilePath);
                }
              }
            });
          } else {
            wx.hideLoading();
            wx.showToast({
              title: '下载图片失败',
              icon: 'none'
            });
          }
        }, err => {});
      }
    },

    // 保存图片
    saveImg(path) {
      wx.saveImageToPhotosAlbum({
        filePath: path,
        success: res => {
          wx.hideLoading();
        },
        fail: rej => {
          wx.hideLoading();
          wx.showToast({
            title: '保存图片失败',
            icon: 'none'
          });
        }
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>

<style rel="stylesheet/less" lang="less">
    @import "shareImg";
</style>