<template>
  <div class="all">
    <div ref="qrElement"></div>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
import { Toast } from "mint-ui";
import axios from "axios";

export default {
  name: "qrcode",
  data() {
    return {
      qrcode: null,
      appid: 'wx6aae31801700533f',
      redirect_uri: 'http://test.jiangtang360.com/signin?nowTime=',
    };
  },
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.createQRCode(1);

      setInterval(() => {
        this.createQRCode(2);
      }, 1000 * 60 * 5);
    });
  },
  methods: {
    createQRCode(num) {
      axios.get("https://adminssl.jiangtang360.com/api/Card/getTime").then(
        (res) => {
          if (num === 1) {
            var redirect_uri = encodeURIComponent(this.redirect_uri)+res.data;
            this.qrcode = new QRCode(this.$refs.qrElement, {
              text:  'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+this.appid+'&redirect_uri='+redirect_uri+'&response_type=code&scope=snsapi_base&state=1234567#wechat_redirect',
              width: 400,
              height: 400,
              correctLevel: 3,//QRCode.CorrectLevel.H,
            });
          } else if (num === 2) {
            this.qrcode.clear();
            this.qrcode.makeCode('https://open.weixin.qq.com/connect/oauth2/authorize?appid='+this.appid+'&redirect_uri='+redirect_uri+'&response_type=code&scope=snsapi_base&state=1234567#wechat_redirect');
          }
        },
        (error) => {
          Toast("获取当时时间失败，请重新扫码签到");
        }
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.all {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
</style>
