<template>
<view id="shareImg" v-if="init">
    <image mode="widthFix" :src="url" alt></image>
    <view class="save" @tap="down">
        保存图片
    </view>
    <view class="tips">
        保存图片并分享到朋友圈，让好友为你打Call
    </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 {
      url: '',
      init: false,
      is_refused: false
    };
  },

  onLoad(e) {
    this.queryImgUrl(e.task_id, e.type);
  },

  components: {
    alert
  },
  props: {},
  methods: {
    closeAlert() {
      this.setData({
        is_refused: false
      });
    },

    // 获取图片url
    queryImgUrl(id, type) {
      var token = this.$cookieStorage.get('user_token');
      var pages = 'pages/store/shareCall/shareCall';
      wx.showLoading({
        title: "正在生成图片",
        mask: true
      });
      this.$http.get({
        api: 'api/free_event/createShareImage',
        header: {
          Authorization: token
        },
        data: {
          page: pages,
          task_id: id
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

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

        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
        wx.showModal({
          content: '内部错误',
          showCancel: false
        });
      });
    },

    // 下载图片
    down() {
      if (this.url) {
        wx.showLoading({
          title: '正在下载',
          mask: true
        });
        this.$http.dowloadFile({
          api: this.url
        }).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>
#shareImg{padding:10px 15px}#shareImg image{display:block;width:100%;border-radius:6px;overflow:hidden}#shareImg .save{width:100%;height:44px;line-height:44px;color:#FFF;text-align:center;background:#ff2741;box-shadow:0 4px 8px 0 rgba(155,155,155,.7);border-radius:20px;margin:10px 0}#shareImg .tips{font-size:12px;color:#888;text-align:center}
</style>