<template>
  <div class="mianBox" >
    <van-row class="headerBox">
      运营中心
    </van-row>
    <van-row class="contentBox" v-if="loadingErp">
      <weather alt="天气" />
      <record alt="记录" />
    </van-row>
    <van-row class="mask" v-if="!loadingErp">
      <loadingSZ />
    </van-row>
  </div>
</template>

<script>
import axios from "axios";
import { Toast } from "vant";
import Cookies from "js-cookie";
import { encrypt,  decrypt } from "../../utils/jsencrypt";
import weather from "@/components/weather";
import record from "@/components/record";
import loadingSZ from "@/components/loadingSZ";
export default {
  components: {
    weather,
    record,
    loadingSZ
  },
  data() {
    return {
      loadingErp:false
    };
  },
  created() {
    var url = window.location.href;
    if (url.indexOf("userName") > 1) {
      //("公众号进入")
      this.userQuery();
    }else if (url.indexOf("token") > 1) {
      //("app进入")
      this.userQuery();
    }else if(Cookies.get("tokenMT")){
      this.userQuery()
    }else{
      this.$router.push({ path: "/login"})
    }
  },
  mounted() {},
  methods: {
    //用户查询
    userQuery(){
      if(JSON.stringify(this.$route.query) != "{}"){
        if(this.$route.query.passWord  && this.$route.query.userName ){
          let userName = decrypt(this.$route.query.userName)
          let passWord = decrypt(this.$route.query.passWord)
          axios.get(`https://smartpark.caih.com/zkxt/api/thirdparty/v1/user/appLogin?name=${userName}&pass=${passWord}`).then(res => {
            if (res?.data?.data?.data?.cipher) {
              let data = res.data.data.data
              //人员种类(00-正式人员、01-外协单位、02-租户)
              if(data.user.dmUser.type == "00"){
                //Toast('请使用"中国东信APP查看功能"')
                  Cookies.set("usernameMT", userName, { expires: 300 });
                  Cookies.set("passwordMT", encrypt(passWord), { expires: 300 });
                  Cookies.set("userIdMT", data.user.id, { expires: 300 });
                  Cookies.set("tokenMT", data.cipher, { expires: 300 });
                  Cookies.set("nonceMT", data.nonce, { expires: 300 });
                  Cookies.set("timestampMT", data.timestamp, { expires: 300 });
                  this.loadingErp = true
              }else if(data.user.dmUser.type == "01"){
                if(data.user.dmUser.status == false){
                  Toast("离职人员,暂无权限")
                  this.$router.push({ path: "/login"})
                  return
                }else{
                  Cookies.set("usernameMT", userName, { expires: 300 });
                  Cookies.set("passwordMT", encrypt(passWord), { expires: 300 });
                  Cookies.set("userIdMT", data.user.id, { expires: 300 });
                  Cookies.set("tokenMT", data.token, { expires: 300 });
                  Cookies.set("nonceMT", data.nonce, { expires: 300 });
                  Cookies.set("timestampMT", data.timestamp, { expires: 300 });
                  this.loadingErp = true
                }
              }else if(data.user.dmUser.type == "02"){
                Toast("暂无权限")
                this.$router.push({ path: "/login"})
              }else{
                this.$router.push({ path: "/login"})
              }
            } 
          }).catch(err =>{
            Toast(err.response.data.message)
          })
        }else{
          Toast('参数异常')
          this.$router.push({ path: "/login"})
        }
       
      }else{
        let token = Cookies.get("tokenMT")
        axios({//查询信息
          method: 'get',
          url: 'https://smartpark.caih.com/zkxt/api/thirdparty/v1/user/getZkUserByCipher?cipher=' + token,
          timeout: 10000,
        }).then(res =>{
          if(res.data?.data?.id){
            this.loadingErp = true
          }
        }).catch(err =>{
          Cookies.remove("usernameMT");
          Cookies.remove("rememberMeMT");
          Cookies.remove("passwordMT");
          Cookies.remove("userIdMT");
          Cookies.remove("tokenMT");
          Cookies.remove("nonceMT");
          Cookies.remove("timestampMT");
          this.loadingErp = false
          this.userNoAlert(err.response.data.message)
          this.$router.push({ path: "/login"})
        });
      }
      
    },
    userNoAlert(msg){
      Toast(msg)
    },
  },
};
</script>

<style lang="scss" scoped>
html,
body {
  color: #fff;
}
.mask{
  width:100%;
  height:100%;
  position: fixed;
  z-index: 1;
}
.mianBox {
  height: 100%;
  width: 100%;
  background-color: rgba(0, 0, 0, 0.01);
  padding-top: 40px;
  // box-sizing: border-box;
  position: fixed;
  overflow-y: scroll;
  padding-bottom: 40px;

  .contentBox {
    width: 100%;
    height: auto;
    .record {
      .recordList {
        margin-top: 10px;
        padding: 10px;
        box-sizing: border-box;
        background-color: #fff;
        .listTitle {
          font-size: 14px;
          color: #000;
          span {
            width: 4px;
            height: 14px;
            display: inline-block;
            background-color: #f22e3c;
            margin-right: 4px;
          }
        }
      }
    }
  }
}
.el-table th > .cell {
  font-size: 11px;
}
</style>
<style>
.el-table td,
.el-table th {
  padding: 4px 0 !important;
}
.el-loading-mask {
  background-color: transparent !important;
}

.el-table .cell {
  padding-right: 0;
}
.tableScroll {
  overflow-x: scroll;
}
</style>
