<template>
  <div class="qr-code">
    <img id="image" src alt />
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "index",
  data() {
    return {
      accessToken: "",
      nowUrl: "",
      activeId: "",
      voucherId: "",
      unionId: ""
    };
  },
  mounted() {
    // alert('--->arCode: mounted: IN.')
    this.cleanUrl()
    // this.getAcessToken();
    this.createCode();
  },
  methods: {
    cleanUrl() {
      // alert('--->arCode: cleanUrl: IN.')
      this.nowUrl = this.$route.query.Url
      this.activeId = this.$route.query.activeId
      this.voucherId = this.$route.query.voucherId
      this.unionId = this.$route.query.unionId
    },
    getAcessToken() {
      let _this = this;
      axios({
        url: "http://139.155.48.151:8085/workWx/auth/oauth2/getAccessToken",
        method: "get"
      }).then(res => {
        _this.accessToken = res.data.data;
        _this.createCode();
      });
    },
    // createCode() {
    //   let _this = this;
    //   let pageUrl = `/wxx/cgi-bin/wxaapp/createwxaqrcode?access_token=${_this.accessToken}`;
    //   $.ajax({
    //     type: "POST",
    //     url: pageUrl,
    //     data: JSON.stringify({ path: "pages/signIn/signIn" }),
    //     // data: JSON.stringify({ path: `${_this.nowUrl}&detailid=${_this.voucherId}&actionid=${_this.activeId}&unionid=${_this.unionId}` }),
    //     xhrFields: { responseType: "arraybuffer" },
    //     success: function(res) {
    //       alert('success' + JSON.stringify(res))
    //       let imgUrl =
    //         "data:image/png;base64," +
    //         btoa(
    //           new Uint8Array(res).reduce(
    //             (data, byte) => data + String.fromCharCode(byte),
    //             ""
    //           )
    //         );
    //       $("#image").attr("src", imgUrl);
    //     },
    //     error: function(data) {
    //       alert('error' + JSON.stringify(data))
    //       console.log(data);
    //     }
    //   });
    // },
    createCode() {
      let _this = this;
      let nowUrl = _this.nowUrl? _this.nowUrl : ""
      let voucherId = _this.voucherId? _this.voucherId : ""
      let activeId = _this.activeId? _this.activeId : ""
      let unionId = _this.unionId? _this.unionId : ""
      let pageUri = `${nowUrl}&detailid=${voucherId}&actionid=${activeId}&unionid=${unionId}`;
      let pageUrl = `http://139.155.48.151:8085/workWx/auth/oauth2/wxMiniQrCode?pageUri=${pageUri}`;
      $.ajax({
        type: "POST",
        url: pageUrl,
        // data: JSON.stringify({ path: "pages/signIn/signIn" }),
        data: {},
        success: function(res) {
          console.log(res)
          $('#image').attr('src',res.data)
        },
        error: function(data) {
          alert('error' + JSON.stringify(data))
          console.log(data);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.qr-code {
  text-align: center;
  padding-top: 50px;
  #image {
    display: inline-block;
    width: 60%;
  }
}
</style>
