<template>
  <view class="uni-popup-post">
    <image
      class="uni_post_img"
      :src="postUrl"
    />
    <view
      @tap="saveAndClose"
      class="uni_post_button_box"
    >
      保存图片
    </view>
  </view>
</template>

<script>
export default {
  name: 'UniPopupPost',
  props: {
    postUrl: {
      type: String,
      default: 'https://api.glass.xiuyetang.com/adv_pic/428_0_7.png',
    },
  },
  inject: ['popup'],
  data() {
    return {}
  },
  created() {
    console.log('dada', this)
  },
  methods: {
    // 保存图片并关闭窗口
    saveAndClose() {
      // 获取用户保存相册权限
      const scope = 'scope.writePhotosAlbum'
      const that = this
      uni.getSetting({
        success(res) {
          console.log('获取用户保存相册权限', res)
          if (!res.authSetting[scope]) {
            uni.authorize({
              scope,
              success() {
                console.log('获取用户保存相册权限---->', '授权成功')
                // 保存到相册
                uni.showLoading({
                  title: '保存中',
                })
                that.saveImage()
              },
              fail() {
                console.log('获取用户保存相册权限---->', '授权失败')
                that.secondGetPhoteAuthor()
              },
            })
          } else {
            console.log('拥有授权权限')
            // 保存到相册
            uni.showLoading({
              title: '保存中',
            })
            that.saveImage()
          }

          that.popup.close()
        },
        fail(res) {
          console.log('授权失败------>', res)
          uni.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 2000,
          })
        },
      })
    },
    // 保存到相册
    saveImage() {
      const that = this
      uni.getImageInfo({
        src: that.postUrl,
        success(res) {
          console.log('图片读取是否可行', res)
          uni.saveImageToPhotosAlbum({
            filePath: res.path,
            success() {
              uni.showToast({
                title: '已保存到相册',
                icon: 'none',
                duration: 2000,
              })
              uni.hideLoading()
            },
            fail(err) {
              console.log('用户拒绝', err)
              if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                console.log('当用户拒绝，再次发起授权')
                that.secondGetPhoteAuthor()
              } else if (err.errMsg === 'saveImageToPhotosAlbum:fail cancel') {
                uni.showToast({
                  title: '已取消保存',
                  icon: 'none',
                  duration: 2000,
                })
                uni.hideLoading()
              } else {
                uni.showToast({
                  title: '请截屏保存分享',
                  icon: 'none',
                  duration: 2000,
                })
                uni.hideLoading()
              }
            },
          })
        },
        fail(res) {
          console.log('授权失败----->', res)
          uni.hideLoading()
          uni.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 2000,
          })
        },
      })
    },
    // 二次请求授权
    secondGetPhoteAuthor() {
      const that = this
      uni.showModal({
        title: '保存海报',
        content: '需要您提供保存相册权限',
        success: (res) => {
          if (res.confirm) {
            uni.openSetting({
              success(settingdata) {
                console.log('settingdata 二次弹窗获取', settingdata)
                if (settingdata.authSetting['scope.writePhotosAlbum']) {
                  console.log('二次弹窗获取---->', '获取 相册 权限成功，给出再次点击图片保存到相册的提示。')
                  uni.showLoading({
                    title: '保存中',
                  })
                  that.saveImage()
                } else {
                  uni.showToast({
                    title: '保存失败',
                    icon: 'none',
                    duration: 2000,
                  })
                  console.log('二次弹窗获取', '获取 相册 权限失败，给出不给权限就无法正常使用的提示')
                }
              },
            })
          } else {
            uni.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 2000,
            })
          }
        },
        fail(err) {
          uni.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 2000,
          })
          console.log('再次失败', err)
        },
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.uni-popup-post {
  border-radius: 8px 8px 0px 0px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

.uni_post_img {
  width: 690rpx;
  height: 466.667px;
  margin-top: -180rpx;
}

.uni_post_button_box {
  height: 112rpx;
  line-height: 112rpx;
  background-color: #ff6b4a;
  border-radius: 4px;
  width: 350px;
  color: #fff;
  text-align: center;
  margin-top: 44rpx;
  font-family: PingFangSC-Medium;
  font-size: 14px;
  color: #ffffff;
  letter-spacing: -0.26px;
  text-align: center;
}
</style>
