Login.vue 5.0 KB
Newer Older
yma16's avatar
yma16 已提交
1
<template>
Y
yma16 已提交
2 3 4 5 6 7 8 9 10 11 12 13
  <div class="login" style="text-align: center">
    <el-card class="box-card" style="text-align: center">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="text-align: center; margin: 0 auto"
      >
        <el-form-item><p style="font-size: 30px">登录</p></el-form-item>
yma16's avatar
yma16 已提交
14

Y
yma16 已提交
15 16 17
        <el-form-item label="账号" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
yma16's avatar
yma16 已提交
18

Y
yma16 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">清空</el-button>
        </el-form-item>
        <el-form-item>
          <el-link target="_blank" @click="$router.push({ path: '/register' })"
            >没有账号?</el-link
          >
          <el-link type="primary" @click="$router.push({ path: '/register' })"
            >去注册</el-link
          >
        </el-form-item>
      </el-form>
    </el-card>
yma16's avatar
yma16 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  </div>
</template>

<script>
import axios from 'axios'
import store from '@/store'
export default {
  name: 'Login',
  data () {
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'))
      }
      setTimeout(() => {
        // if (!Number.isInteger(value)) {
        //   callback(new Error('请输入密码'));
        // } if {
        if (value.length < 2) {
          callback(new Error('名字至少两位'))
        } else {
          callback()
        }
      }, 1000)
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        // if (this.ruleForm.checkPass !== '') {
        //   this.$refs.ruleForm.validateField('checkPass');
        // }
        callback()
      }
    }
    // var validatePass2 = (rule, value, callback) => {
    //   if (value === '') {
    //     callback(new Error('请再次输入密码'));
    //   } else if (value !== this.ruleForm.pass) {
    //     callback(new Error('两次输入密码不一致!'));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      // baseurl:"http://127.0.0.1/user/login/",
Y
yma16 已提交
87 88
      baseurl: 'http://114.116.52.53/',
      // baseurl: 'http://yongma16.xyz/user/login/',
yma16's avatar
yma16 已提交
89 90 91 92 93 94
      ruleForm: {
        pass: '',
        // checkPass: '',
        name: ''
      },
      rules: {
Y
yma16 已提交
95 96 97
        pass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass, trigger: 'blur' }],
        name: [{ validator: checkName, trigger: 'blur' }]
yma16's avatar
yma16 已提交
98 99 100 101 102
      }
    }
  },
  methods: {
    submitForm (formName) {
Y
yma16 已提交
103
      const that = this // this指向
yma16's avatar
yma16 已提交
104 105 106
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 提交数据
Y
yma16 已提交
107
          console.log(that.ruleForm.name, that.ruleForm.pass)
yma16's avatar
yma16 已提交
108
          // axios提交数据 post
Y
yma16 已提交
109 110
          const loginUrl = that.baseurl + 'user/login/'
          const userInfo = {
yma16's avatar
yma16 已提交
111 112 113
            name: that.ruleForm.name,
            password: that.ruleForm.pass
          }
Y
yma16 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
          axios
            .post(loginUrl, userInfo)
            .then((res) => {
              // alter('提交中');
              console.log(res.data)
              if (res.data.code == 1) {
                localStorage.setItem('yma16siteUserInfoName', userInfo.name)
                localStorage.setItem('yma16siteUserInfoPwd', userInfo.password)
                store.commit('setUserInfo', userInfo) // store信息
                console.log('return login success!')
                document.cookie = `user=${that.ruleForm.name}`
                that.$router.push({ path: '/' })
                // 添加cookie
              } else if (res.data.code == 0) {
                // alter("失败!")
                console.log('失败', res)
              }
              // that.$cookies.set('')
            })
            .catch((res) => {
              console.log('post失败')
              console.log(res)
            })
yma16's avatar
yma16 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        } else {
          alert('请输入账号密码')
          console.log('请输入账号密码')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
Y
yma16 已提交
152
.login {
yma16's avatar
yma16 已提交
153
  position: relative;
Y
yma16 已提交
154
  width: 100%;
yma16's avatar
yma16 已提交
155 156
  height: auto;
}
Y
yma16 已提交
157
.left {
yma16's avatar
yma16 已提交
158
  position: relative;
Y
yma16 已提交
159 160 161 162 163 164 165 166 167 168
  margin-top: 50px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.text {
  align-content: center;
  display: flex;
  margin: 0 auto;
  font-size: 14px;
yma16's avatar
yma16 已提交
169 170
}

Y
yma16 已提交
171 172 173 174 175
.item {
  display: flex;
  margin: 0 auto;
  /* width: 50%; */
}
yma16's avatar
yma16 已提交
176

Y
yma16 已提交
177 178 179 180 181 182 183 184 185 186 187
.box-card {
  align-self: center;
  align-content: center;
  display: flex;
  width: 450px;
  /* padding-left:25%; */
  margin: 0 auto;
  opacity: 1;
  margin-top: 80px;
  background-color: #ffffff;
}
yma16's avatar
yma16 已提交
188
</style>