<template>
  <div class="login">
    <div class="login-form">
      <h3 class="title">寻驾招生管理系统</h3>
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="微信扫码" name="wx" style="height:200px">
          <wxlogin v-if="!code && activeName === 'wx'" appid="wx203f734baa9c9845" :scope="'snsapi_login'" :theme="'black'" redirect_uri="https://xueche.ahduima.com/login" :href="href">
          </wxlogin>
          <!-- <div id="wxcode" v-if="!code && activeName === 'wx'"></div> -->
          <!-- 绑定手机号框 -->
          <div v-else v-loading="loading">
            <WxCode v-if="codeShow" :code="code" :openId="openId" />
          </div>
        </el-tab-pane>
        <el-tab-pane label="账号密码" name="password" style="height:200px">
          <Password v-if="activeName === 'password'" />
        </el-tab-pane>
      </el-tabs>

    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login';
import Cookies from 'js-cookie';
import { encrypt, decrypt } from '@/utils/jsencrypt';
import wxlogin from 'vue-wxlogin'
import Password from './password.vue';
import WxCode from './wxCode.vue';

export default {
  name: 'Login',
  components: {
    wxlogin, Password, WxCode
  },
  data() {
    return {
      codeUrl: '',
      loading: false,
      redirect: undefined,
      activeName: 'wx',
      code: undefined,
      openId: undefined,
      codeShow: false,
      href: "data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDE4MHB4OyBtYXJnaW4tdG9wOjBweH0KLmltcG93ZXJCb3ggLnRpdGxlIHtkaXNwbGF5OiBub25lO30KLmltcG93ZXJCb3ggLmluZm8ge3dpZHRoOiAxODBweDt9Ci5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZX0KLmltcG93ZXJCb3ggLnN0YXR1cyB7dGV4dC1hbGlnbjogY2VudGVyO30g"

    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.code = this.$route.query.code;
    if (this.code) {
      this.loading = true
      this.wxLogin(this.code);
    }
  },
  mounted() {
    // this.createWxQrcode();
  },
  methods: {
    async wxLogin(code) {
      //根据code和openId查询是否关联了手机号，如果没有关联 需先关联手机号
      this.$store.dispatch('WXLogin', { code: code, type: 1 }).then(resp => {
        if (resp && resp.token != undefined) {
          this.$router.push({ path: this.redirect || '/' }).catch(() => { });
        } else if (resp.openId) {
          //如果返回openId则是没有绑定手机号
          this.openId = resp.openId;
          this.loading = false
          this.codeShow = true
        } else {
          this.$message.error("二维码失效，请刷新！");
        }
      });

    },
    createWxQrcode() {
      // 生成微信登录二维码
      var obj = new WxLogin({
        self_redirect: true,
        id: "wxcode", // 页面显示二维码的容器id
        appid: "wx203f734baa9c9845", // 微信官方提供的测试id
        scope: "snsapi_login",
        redirect_uri: "https://xueche.ahduima.com/login", // 微信官方中的测试地址
        state: "bind",
        style: "black",
        href: this.href,
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('../assets/images/login-background.jpg');
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  display: inline;
  border-radius: 6px;
  background: #ffffff;
  width: 350px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

.impowerBox .qrcode {
  width: 200px !important;
}
</style>
