<!-- 账号登录 -->
<template>
  <div id="accountLogin">
    <img class="login-max-big" :src="loginBanner">
    <div class="login-box">
      <div class="tabTitle">
        <div class="tabTitle_li" :key="index" v-for="(item,index) in tabText">
          <p :class="{on:curTab==index}" class="tabTitle_text">{{item}}</p>
        </div>
      </div>
      <div class="input-max-box-one">
        <div class="input-box">
          <img :src="imagesUrl('commonality/img_log_in_account.png')">
          <input type="text" v-model="username" @confirm="login()"
                 placeholder="请输入账号" maxlength="20">
        </div>
      </div>
      <div class="input-max-box-two">
        <div class="input-box">
          <img :src="imagesUrl('commonality/img_log_in_password.png')">
          <input type="password" v-model="password" @confirm="login()"
                 placeholder="请输入密码" maxlength="20">
        </div>
      </div>
      <div class="check-box" @click="checkboxChange()">
        <img v-if="checkedType" :src="imagesUrl('commonality/icon_13.png')">
        <img v-if="!checkedType" :src="imagesUrl('commonality/icon_12.png')"/>
        <p>记住我</p>
      </div>
      <p class="button-box" @click="login()">登录</p>
      <!--<p class="passwrod-button-box" @click="forgotPasswordButton()">忘记密码?</p>-->
    </div>
    <!-- <view class="loginButton" @click="backButton()">统一身份认证登录</view> -->
  </div>
</template>

<script>
  import { Toast } from 'vant';
  import {judgmentNetworkReturnAddress} from "@/utils/ruoyi";
  import md5 from 'js-md5';
  import {
    login,
    configInfo,
    getConfigByType,
    systemSubjectCheckIsAdminOrSafeUser,
    securityDataStatisticsGetUserIdentity
  } from '@/api/index.js'
  import { Encrypt,Decrypt} from '@/utils/secret'
  export default {
    data() {
      return {
        identityStatus: 1,
        username: "",
        password: "",
        checkedType: false,
        loginBanner: localStorage.getItem('loginBanner'),
        infoList: [], //模板消息Id
        tabText: ['登录'],
        curTab: 0,
        pageType: 0,
        supplierType: false,
      }
    },

    created() {
      if (localStorage.getItem('userName') && localStorage.getItem('password')) {
        this.username = localStorage.getItem('userName');
        this.password = localStorage.getItem('password');
        this.checkedType = true;
      }
    },
    mounted() {
      this.getConfigInfo();
    },
    methods: {
      imagesUrl(imgUrl) {
        return 'https://zj-wechat.oss-cn-beijing.aliyuncs.com/xcx_images/xcx_v3/' + imgUrl
      },
      //登录
      login(){
        let obj = {
          account: this.username,
          password: md5(this.password),
        }
        login(obj).then(response => {
          if (response.data.userType == 0 || response.data.userType == 1 || response.data.userType == 2) {
            localStorage.setItem('dataBoardType', false);
            localStorage.setItem('touken', response.data.token);
            localStorage.setItem('userId', response.data.userId);
            localStorage.setItem('isInitPwd', true);
            // userType 0-系统 1-教职工 2-学生 3-大屏
            localStorage.setItem('userType', response.data.userType == 0 || response.data.userType == 1 ? '1' :
              (response.data.userType == 2 ? '2' : (response.data.userType == 3 ? '3' : 'none')));
            if (this.checkedType) {
              localStorage.setItem('userName', this.username)
              localStorage.setItem('password', this.password)
            } else {
              localStorage.removeItem('userName')
              localStorage.removeItem('password')
            }
            //等待配置与字段获取到后跳转
            Promise.all([
              //获取开发配置
              this.getConfigByType(),
            ]).then((result) => {
              //查询是否是管理员/安全员
              this.systemSubjectCheckIsAdminOrSafeUser()
            }).catch((error) => {
              Toast.fail('数据异常,请稍候再试!');
            })
          } else {
            Toast.fail('非教职工/学生账号,无法登录本系统.');
          }
        })
      },
      //查询公共配置
      getConfigInfo() {
        let urlText = window.location.href.split('://')[0] + '://' + judgmentNetworkReturnAddress();
        configInfo({type: '1,2,4'}).then(response => {
          let list = JSON.parse(response.data)
          let newData = {};
          list.forEach((item) => {
            let obj = JSON.parse(item.configValue)
            newData = {
              ...newData,
              ...obj
            }
          });
          localStorage.setItem('circularLogo', urlText + newData.circularLogo)
          localStorage.setItem('rectangleLogo', urlText + newData.rectangleLogo)
          localStorage.setItem('videoCover', urlText + newData.videoCover)
          this.$set(this, 'loginBanner', urlText + newData.loginBanner);
          localStorage.setItem('loginBanner', urlText + newData.loginBanner)
          this.$set(this, 'supplierType', newData.supplier);
          localStorage.setItem('supplierType', newData.supplier)
          localStorage.setItem('homepageBanner', urlText + newData.homepageBanner)
        })
      },
      //获取开发配置
      getConfigByType() {
        getConfigByType({category: 2, configType: 5}).then(response => {
          let obj = JSON.parse(response.data.configValue)
          //文件预览地址
          localStorage.setItem('filePreviewUrl', 'https://' + obj.fileExtranetUrl)
          //小程序视频地址
          localStorage.setItem('cameraExtranetAgent', 'https://' + obj.cameraExtranetAgent)
          //MQTT地址
          localStorage.setItem('mqttUrl', Decrypt(obj.mqttExtranetUrl))
          //MQTT地址-内网
          localStorage.setItem('mqttIntranetUrl', Decrypt(obj.mqttIntranetUrl))
          //MQTT账号
          localStorage.setItem('mqttUser', Decrypt(obj.mqttExtranetUser))
          //MQTT密码
          localStorage.setItem('mqttPassword', Decrypt(obj.mqttExtranetPassword))
          //文件浏览环境
          localStorage.setItem('fileBrowseEnvironment', 'http://' + Decrypt(obj.fileBrowseEnvironment))
          localStorage.setItem('fileBrowseEnvironmentExtranet', 'http://' + Decrypt(obj.fileBrowseEnvironmentExtranet))
        })
      },
      //查询是否是管理员/安全员
      systemSubjectCheckIsAdminOrSafeUser(){
        systemSubjectCheckIsAdminOrSafeUser({}).then(response => {
          if (response.isSafe || response.isAdmin) {
            window.location.href = localStorage.getItem('alarmUrl');
          }else{
            this.securityDataStatisticsGetUserIdentity();
          }
        })
      },
      //查询是否是校级管理员/院级管理员
      securityDataStatisticsGetUserIdentity(){
        securityDataStatisticsGetUserIdentity({}).then(response => {
          if(response.data.schoolAdmin || response.data.collegeAdmin){
            window.location.href = localStorage.getItem('alarmUrl');
          }else{
            Toast.fail('您无权查看报警信息,请联系管理员.');
          }
        })
      },
      checkboxChange() {
        this.checkedType = !this.checkedType;
      },

    },

  }
</script>

<style scoped lang="scss">
  #accountLogin {
    height: 100%;
    width: 100%;
    background: #f5f5f5;
    position: relative;
    .login-max-big {
      width: 750px;
      height: 1177px;
      z-index: 0;
    }

    .login-box {
      z-index: 3;
      position: absolute;
      top: 430px;
      left: 46px;
      width: 658px;
      height: 700px;

      // background #fff
      /* 切换按钮 */
      .tabTitle {
        display: flex;
        width: 100%;
        height: 100px;
        position: absolute;
        // top: 50px;
        justify-content: center;

        .tabTitle_li {
          width: 168px;
          text-align: center;

          .tabTitle_text {
            display: inline-block;
            font-size: 32px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #333333;
            line-height: 90px;

            &.on {
              color: #0183FA;
            }
          }

          .tabTitle_across {
            width: 100px;
            height: 4px;
            background: #0183FA;
            border-radius: 2px;
            margin-left: 30px;
            display: none;

            &.on {
              display: block;
            }
          }

        }
      }

      border-radius: 20px;

      .input-max-box-one {
        overflow: hidden;
        // margin-top: 68px;

        .input-box {
          display: flex;
          width: 600px;
          height: 80px;
          border: 1px solid #e0e0e0;
          border-radius: 40px;
          margin: 147px auto 0;

          img {
            width: 28px;
            height: 32px;
            margin: 24px 31px;
          }

          input {
            flex: 1;
            font-size: 24px;
            height: 80px;
            line-height: 80px;
            margin-right: 31px;
            padding:0 20px;
          }
        }

        .text-box {
          height: 59px;
          line-height: 59px;
          color: #DC1616;
          font-size: 24px;
          margin-left: 102px;
        }
      }

      .input-max-box-two {
        margin-top: 40px;

        .input-box {
          display: flex;
          width: 600px;
          height: 80px;
          border: 1px solid #e0e0e0;
          border-radius: 40px;
          margin: 0 auto 0;

          img {
            width: 30px;
            height: 32px;
            margin: 24px 30px;
          }

          input {
            flex: 1;
            font-size: 24px;
            height: 80px;
            line-height: 80px;
            margin-right: 31px;
            padding:0 20px;
          }
        }

        .text-box {
          height: 59px;
          line-height: 59px;
          color: #DC1616;
          font-size: 24px;
          margin-left: 102px;
        }
      }

      .check-box {
        margin: 30px 0 30px 104px;
        width: 300px;
        height: 50px;
        display: flex;

        img {
          margin-top: 10px;
          width: 32px;
          height: 32px;
          margin-right: 10px;
        }
        p {
          font-size: 24px;
          line-height: 50px;
        }
      }

      .button-box {
        width: 600px;
        line-height: 80px;
        background: #0183FA;
        border-radius: 40px;
        font-size: 36px;
        color: #fff;
        text-align: center;
        margin: 0 auto 0;
      }
      .passwrod-button-box {
        line-height: 60px;
        width: 120px;
        margin: 20px 0 0 40px;
        color: #333;
        font-size: 24px;
      }
      /* 供应商注册 */
      .supplier {
        display: flex;
        justify-content: space-between;
        margin: 30px 40px 0;

        .supplier_l {
          font-size: 24px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #333333;
          line-height: 24px;
        }

        .supplier_r {
          font-size: 24px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #333333;
          line-height: 24px;

          > text {
            color: #0183FA;
          }
        }
      }

      /* 供应商切换 */
      .switch_btn {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 24px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #0183FA;
        line-height: 24px;
        margin-top: 60px;

        > img {
          width: 24px;
          height: 24px;
          margin-left: 12px;
        }
      }
    }
    .loginButton {
      position: absolute;
      width: 100%;
      bottom: 40px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      color: #0183fa;
      font-size: 28px;
    }
    .top-back {
      z-index: 2;
      position: absolute;
      top: 261px;
      left: 375px;
      height: 296px;
      width: 366px;
    }
  }
</style>
