login.html 3.53 KB
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <link rel="stylesheet" href="./css/index.css" />
  <link rel="stylesheet" href="./css/common.css">
  <link rel="stylesheet" href="./css/page.css">
  <script src="./js/browser.min.js"></script>
</head>

<body>
  <!-- <div id="loginApp" v-cloak> -->
  <div id="loginApp">
    <div class="login-container">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
        <div class="login-header">
          <h3 class="login-title">Login Form</h3>
        </div>
        <el-form-item prop="loginName">
          <el-input ref="username" v-model.trim="loginForm.loginName"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input ref="password" type="password" v-model.trim="loginForm.password">
          </el-input>
        </el-form-item>
        <el-button type="primary" style="width: 100%;" @click="submitForm">登 录</el-button>
      </el-form>
    </div>
  </div>

  <!-- import Vue before Element -->
  <script src="./js/vue.js"></script>
  <!-- import JavaScript -->
  <script src="./js/index.js"></script>
  <script src="./js/axios.js"></script>
  <script src="./js/common.js" type="text/javascript" charset="utf-8"></script>
  <script src="./js/cookie.js"></script>
  <script src="./js/request.js"></script>
  <script>
    new Vue({
      el: '#loginApp',
      data() {
        const validateUsername = (rule, value, callback) => {
          if (!value) {
            callback(new Error('请输入用户名'))
          } else {
            callback()
          }
        }
        const validatePassword = (rule, value, callback) => {
          const regexp = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{6,16}$/
          if (!value) {
            callback(new Error('请输入密码'))
          } else if (!regexp.test(value)) {
            callback(new Error('请输入不小于六位包含字母数字的密码!'))
          } else {
            callback()
          }
        }
        return {
          loginForm: {
            loginName: '',
            password: ''
          },
          // paswordType: 'password',
          rules: {
            loginName: [
              { required: true, trigger: 'blur', validator: validateUsername }
            ],
            password: [
              { required: true, trigger: 'blur', validator: validatePassword }
            ]
          }
        }
      },
      mounted() {
        // console.log({ get, post })
        // if (this.loginForm.name === '') {
        //   this.$refs.username.focus()
        // } else if (this.loginForm.password === '') {
        //   this.$refs.password.focus()
        // }
      },
      methods: {
        submitForm() {
          this.$refs.loginForm.validate(valid => {
            if (valid) {
              post(
                window._CONF_.reportApiUrl + '/users/login',
                this.loginForm
              ).then(res => {
                const { data, code } = res
                if (code === 200) {
                  Cookies.set('atoken', data.atoken)
                  const currentPath = window.location.href
                  window.location.replace(currentPath.substr(0, currentPath.lastIndexOf('/') + 1))
                }
              })
              .catch(err => {
                console.log(err)
              })
            }
          })
        },
        resetForm() {
          this.loginForm.name = ''
          this.loginForm.password = ''
          this.$refs.loginForm.resetFields()
        },
      }
    })
  </script>
</body>

</html>