<template>
  <view>
    <text> {{ code }}</text>
    <countDown :time="time"></countDown>
    <view class="menu">
      <button @click="copyCode" style="background: red">复制code</button>
      <button @click="freshCode" style="background: orange">
        重新获取code
      </button>
    </view>
    <view class="menu">
      <button
        @click="
          downloadPdf(
            'https://cos-admin.yizhiwechat.com/yizhiClassroom/saveLesson/material/20211201165322_rMiiDd.pdf'
          )
        "
        style="background: red"
      >
        下载pdf
      </button>
      <button
        @click="
          openPdf(
            'https://cos-admin.yizhiwechat.com/yizhiClassroom/saveLesson/material/20211201165322_rMiiDd.pdf'
          )
        "
        style="background: red"
      >
        打开pdf
      </button>
    </view>
  </view>
</template>

<script>
import countDown from "@/components/countDown/index";
export default {
  data() {
    return {
      code: "",
      time: 1000 * 60 * 45,
    };
  },
  methods: {
    downloadPdf(src) {
      tt.downloadFile({
        url: src,
        success: (res) => {
          console.log("临时文件下载成功,下载路径: " + res.tempFilePath);
          tt.saveFile({
            // 参数: 需传入文件临时路径tempFilePath
            tempFilePath: res.tempFilePath,
            success: (data) => {
              // 返回参数中的savedFilePath属性为保存的本地文件路径
              console.log("保存成功, 本地路径为: ", data.savedFilePath);
              tt.showModal({
                content: "临时文件保存成功",
                showCancel: false,
              });
            },
            fail: (res) => {
              // 当 API 执行失败后调用, 预定义返回消息格式为${API_NAME}:fail
              console.log("临时文件保存失败", res.errMsg);
              tt.showModal({
                content: "临时文件保存失败",
                showCancel: false,
              });
            },
            complete: (res) => {
              // 当 API 执行完成（无论成功或者失败）后都会调用, 预定义返回消息格式为${API_NAME}:ok / fail
              console.log("接口已调用", res.errMsg);
            },
          });
        },
        fail: (res) => {
          console.log("下载失败: ", res.errMsg);
        },
      });
    },
    openPdf(src) {
      tt.downloadFile({
        // 仅为示例 url，并非真实地址
        url: src,
        success: function (res) {
          const filePath = res.tempFilePath;
          tt.openDocument({
            filePath: filePath,
            success: function (res) {
              console.log("打开文档成功");
            },
          });
        },
      });
    },
    freshCode() {
      let that = this;
      uni.login({
        success(res) {
          that.code = res.code;
        },
      });
    },
    copyCode(e) {
      let that = this;
      uni.setClipboardData({
        data: that.code,
        success(res) {
          console.log(res);
        },
      });
    },
  },
  onLoad(op) {
    this.freshCode();
  },
  components: {
    countDown,
  },
};
</script>

<style lang="scss" scoped>
.menu {
  display: flex;
  justify-content: space-between;
  button {
    margin-top: 15px;
    width: 40%;
    color: #fff;
  }
}
</style>