<template>
  <div class="login-box-parent">
    <div class="all" style="">
      <div class="data-all" style="text-align:center;margin-top:50px;">
        <transition name="fade">
          <img
            src="static/images/login300.png"
            v-if="logImage"
            style="margin:48px auto;width:104px;height:104px;"
            alt=""
          />
          <img
            src="static/images/init.gif"
            v-if="!logImage"
            style="margin:0 auto;"
            alt=""
          />
        </transition>
        <div>管理执行难，就用功道云</div>
        <transition name="fade">
          <van-button
            class="refresh"
            type="info"
            @click="LoginBut"
            v-show="loginButton"
            style="border-radius:5px;margin-top:10px;"
            >{{ logButText }}</van-button
          >
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Divider } from "vant";
import Cookies from 'js-cookie'

import { getToken, setToken } from "@/utils/auth";

import { wxAuth } from "@/utils/wx-auth-two.js";
import Cookies from "js-cookie";
import axios from "axios";
Vue.use(Divider);
export default {
  name: "login1",
  data() {
    return {
      logImage: true,
      logButText: "登录",
      loginButton: true
    };
  },
  activated() {//activated()每次进入页面都会执行,代替created
    var url = window.location.href; //获取url中"?"符后的字串
    let codesif = false;
    let codes = "";
    if (url) {
      if (url.split("?")[1]) {//判断有无？参数
        var strs = url.split("?")[1].split("&");
        for (var i = 0; i < strs.length; i++) {
          if (strs[i].split("=")[0] == "code") {
            this.loginButton = false;
            this.logImage = false;
            codesif = true;
            codes = strs[i].split("=")[1].split("&")[0];
          }
        }
      } else {//没有就清空缓存并放出登录按钮
        this.clone();
        this.loginButton = true;
        this.logImage = true;
      }
    }

    if (!codesif) {
      this.logImage = true;
      this.loginButton = true;
    } else {
        this.clone();
      let params = { code: codes };
      axios.get(process.env.BASE_API + "/api/wechat/minAuth", { params: params })
        .then(res => {
          if (res.data.code == 1) {
            if (res.data.data.user.is_official == 1) {
              setToken(res.data.data.token);
              this.$store.commit("SET_USERINFO", res.data.data.user); // 设置员工信息
              window.sessionStorage.setItem('__VCKEEPALIVE__', JSON.stringify(['/']))
              window.sessionStorage.setItem('routers', '/')
              console.log(getToken());
              wxAuth(true);
            } else if (res.data.data.user.is_official == 0) {
              this.clone();
              this.$router.push({name: 'noAccess',params: { status: 0 }})
            }
          } else if (res.data.code == 4444){
            this.$router.push({name: 'noAccess',params: { status: 4444 }})
          } else {
            this.logButText = "重新登录";
            this.loginButton = true;
            this.logImage = true;
          }
        }).catch((err)=>{
        });
    }
  },
  mounted() {
    console.log('登录页mounted')
  },
  watch: {},
  methods: {
    LoginBut() {
      // if(this.logButText == "重新登录"){
      //   window.location.href = 'http://wechatdimgs.vaiwan.com'
      // }else{
        console.log(process.env.APPID);
        console.log(process.env.REDIRECT_URI);
        var appid, redirect_uri, response_type, scope, state, wechat, agentid;
        appid = 'ww4065f0d4bb232a6a';
        redirect_uri = 'http%3A%2F%2Fwechatdings.vaiwan.com';
        response_type = "code";
        scope = "snsapi_privateinfo";
        state = "a";
        wechat = "#wechat_redirect";
        let link =
          "appid=" +
          appid +
          "&redirect_uri=" +
          redirect_uri +
          "&response_type=" +
          response_type +
          "&scope=" +
          scope +
          "&state=" +
          state +
          wechat;
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?" + link;
      // }
    },

    clone() {
      if (window.plus) {
        plus.storage.clear()
	      Cookies.remove('Admin-Token')
      } else {
        window.localStorage.clear()
	      Cookies.remove('Admin-Token')
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.all {
  margin: 0 auto;
  position: relative;
  top: 50%;
  margin-top: -325px;
}
.login-box-parent{
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: auto;
  cursor: default;
  /* background-color: #f5f6f9; */
  background-color: #FFF;
}
</style>
