<template>
  <div class="login" :style="{ backgroundImage: `url(${bg})` }">
    <div class="title">
      <div class="login-logo">
        <van-image :src="require('@/assets/login/login-logo.png')" />
      </div>
      <div class="login-name">首开集团安全风险和隐患排查治理体系</div>
    </div>

    <div class="con" :style="{ backgroundImage: `url(${conBg})` }">
      <div class="hello">Hello!</div>
      <div class="welcome">
        欢迎登录<span>首开集团安全风险和隐患排查治理体系</span>!
      </div>
      <div class="login-form">
        <van-form @submit="onSubmit" :show-error-message="false" >
          <div class="username-wrap">
            <div class="username-icon">
              <van-image :src="require('@/assets/login/login-username.png')" />
            </div>
            <van-field
              v-model="username"
              name="username"
              label=""
              placeholder="账号"
              :rules="[{ required: true, message: '请填写账号' }]"
            />
          </div>
          <div class="passworld-wrap">
            <div class="passworld-icon">
              <van-image :src="require('@/assets/login/login-passworld.png')" />
            </div>
            <van-field
              v-model="password"
              type="password"
              name="password"
              placeholder="密码"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
          </div>

          <div style="margin: 16px;">
            <van-button round block type="info" native-type="submit"
              >登录</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getFun,postFun } from '@/service/table'
import { setToken } from '@/utils/auth'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { crypto } from '@/utils/crypto'
import { setUserInfo ,getUserInfo} from '@/utils/userInfo'
export default {
  data() {
    return {
      bg: require("@/assets/login/login-bg.png"),
      conBg: require("@/assets/login/login-center.png"),
      username: "",
      password: "",
      obj:{}
    };
  },
  mounted() {
    if(localStorage.getItem('pwd')){
        this.password=decrypt(localStorage.getItem('pwd'))
        this.username=localStorage.getItem('username')
    }
  },
  created() {
    // 扩展API是否准备好，如果没有则监听“plusready"事件
      if (window.plus) {
        this.plusReady()
      } else {
        document.addEventListener('plusready', this.plusReady, false) 
      }
  },
  methods: {
    // 扩展API准备完成后要执行的操作
      plusReady () {
        // var ws = plus.webview.currentWebview(); //pw回车可输出plus.webview
        var barcode = plus.barcode.create('barcode',"aaa", {
        top: '-9999px', //改为-9999px隐藏该页面
        left: '0',
        width: '100%',
        height: '500px',
        position: 'static',
      });
      plus.webview.currentWebview().append(barcode);
      //barcode.start();开始扫码识别（我们把这句代码注释了，因为我们不需要扫描任何东西）
      },


     onSubmit(values) {
        var obj={
            username:this.username,
            password:this.password,
        }
        this.$toast.loading({
            message:'登录中...',
            forbidClick: true,
            loadingType: 'spinner',
            duration: 0
        })
        getFun('check/token',obj).then((Response)=>{
            if(Response.code==0){
                this.$toast.clear()
                postFun('mobile/user').then((Response2)=>{
                    var userInfo={
                        avatar:Response2.data.avatar,
                        userName:Response2.data.userName,
                        deptName:Response2.data.dept.deptName,
                        organizationId:Response2.data.organizationId,
                        organizationName:Response2.data.organizationName,
                        userId:Response2.data.userId,
                        roles:Response2.data.roles
                    }
                    /* 存储用户信息 */
                    setUserInfo(userInfo)
                })
                localStorage.setItem('pwd',encrypt(this.password))
                localStorage.setItem('username',this.username)
                setToken(Response.data)
                this.$router.push('/save-workbench')
            }else if(Response.code==301){
                this.$toast.clear()
                this.$toast.fail({
                    message:Response.msg,
                    duration: 2000
                })
            }
        })
    }
  }
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
.login {
  background-size: cover;
  background-repeat: no-repeat;
  // background-position: center;
  background-attachment: fixed;
  .login-logo {
    width: 120px;
    margin: 22% auto 0px;
  }
  .login-name {
    width: 100%;
    text-align: center;
    margin-top: 20px;
    color: #fafbfe;
    font-size: 18px;
    font-weight: none;
  }
  .con {
    width: 345px;
    height: 340px;
    margin: 62px auto 0;
    padding: 35px 25px;
    box-sizing: border-box;
    background-size: cover;
    background-repeat: no-repeat;
    .hello {
      color: #004fb1;
      font-size: 20px;
      font-weight: none;
    }
    .welcome {
      margin: 15px 0;
      color: #7795e0;
      span {
        color: #71aaf2;
      }
    }
    .login-form {
      .username-wrap {
        position: relative;
        border-bottom: 1px solid #d2dcf5;
        .username-icon {
          width: 20px;
          position: absolute;
          left: 5px;
          top: 9px;
          z-index: 100;
        }
        /deep/ .van-field__body {
          padding-left: 20px;
          box-sizing: border-box;
        }
        /deep/ .van-field__error-message {
          padding-left: 20px;
        }
      }
      .passworld-wrap {
        position: relative;
        border-bottom: 1px solid #d2dcf5;
        .passworld-icon {
          width: 20px;
          position: absolute;
          left: 5px;
          top: 9px;
          z-index: 100;
        }
        /deep/ .van-field__body {
          padding-left: 20px;
          box-sizing: border-box;
        }
        /deep/ .van-field__error-message {
          padding-left: 20px;
        }
      }
    }
  }
}
</style>
