login 登录

qianlishi 3 years ago
parent dc4cced74f
commit c98ddf8de6

@ -49,7 +49,7 @@ service.interceptors.response.use(
type: 'error',
duration: 5 * 1000
})
return Promise.reject('error')
return response.data
} else {
return response.data
}

@ -8,20 +8,19 @@
<div class="login_container">
<!-- 顶部logo -->
<div class="login_title">
<img src="@/assets/images/home-logo.png"
alt="logo" />
<img src="@/assets/images/home-logo.png" alt="logo" />
</div>
<div class="login_contant">
<img src="@/assets/images/login.png"
alt="image"
class="login_img" />
<el-form ref="loginForm"
<img src="@/assets/images/login.png" alt="image" class="login_img" />
<el-form
ref="loginForm"
:model="loginForm"
:rules="loginRules"
class="login_form"
autocomplete="on"
label-position="left"
@keyup.enter.native="handleLogin">
@keyup.enter.native="handleLogin"
>
<div class="title_container">
<h3 class="title">
HELLO,
@ -37,7 +36,8 @@
<div>
<p>用户名</p>
<el-form-item prop="loginName">
<el-input ref="loginName"
<el-input
ref="loginName"
v-model="loginForm.loginName"
placeholder="用户名"
name="loginName"
@ -45,21 +45,27 @@
tabindex="1"
autocomplete="on"
@focus="setTop('0')"
@change="getPsw" />
@change="getPsw"
/>
</el-form-item>
</div>
<div>
<p>密码</p>
<input name="password"
<input
name="password"
type="password"
autocomplete="off"
class="take" />
<el-tooltip v-model="capsTooltip"
class="take"
/>
<el-tooltip
v-model="capsTooltip"
content="Caps lock is On"
placement="right"
manual>
manual
>
<el-form-item prop="password">
<el-input :key="passwordType"
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
@ -69,9 +75,9 @@
autocomplete="on"
@blur="capsTooltip = false"
@focus="setTop('50')"
@keyup.native="checkCapslock" />
<span class="show_pwd"
@click="showPwd">
@keyup.native="checkCapslock"
/>
<span class="show_pwd" @click="showPwd">
<i class="el-icon-view" />
</span>
</el-form-item>
@ -80,180 +86,181 @@
</div>
<div class="control">
<div class="remember">
<input v-model="rememberPsw"
type="checkbox" />
<input v-model="rememberPsw" type="checkbox" />
<p>记住密码</p>
</div>
</div>
<el-button :loading="loading"
<el-button
:loading="loading"
type="primary"
class="login_btn"
@click.native.prevent="handleLogin">登录</el-button>
@click.native.prevent="handleLogin"
>登录</el-button
>
</el-form>
</div>
<!-- 验证码 -->
<Verify v-if="needCaptcha"
<Verify
v-if="needCaptcha"
ref="verify"
:captcha-type="'blockPuzzle'"
:img-size="{ width: '400px', height: '200px' }"
@success="verifylogin" />
@success="verifylogin"
/>
</div>
</template>
<script>
import Verify from '@/components/verifition/Verify'
import cookies from 'js-cookie'
import { Decrypt, Encrypt } from '@/utils/index'
import { login } from '@/api/login'
import { transPsw } from '@/utils/encrypted'
import Verify from "@/components/verifition/Verify";
import cookies from "js-cookie";
import { Decrypt, Encrypt } from "@/utils/index";
import { login } from "@/api/login";
import { transPsw } from "@/utils/encrypted";
import { setToken, setAccessUser } from "@/utils/auth";
export default {
name: 'Login',
name: "Login",
components: {
Verify,
Verify
},
data () {
data() {
return {
activeTop: '-50%', //
activeTop: "-50%", //
rememberPsw: false, //
loginForm: {
loginName: 'admin',
password: '123456',
verifyCode: '',
loginName: "admin",
password: "123456",
verifyCode: ""
}, //
loginRules: {
loginName: [{ required: true, message: '用户名必填', trigger: 'blur' }],
password: [
{ required: true, message: '用户密码必填', trigger: 'blur' },
],
loginName: [{ required: true, message: "用户名必填", trigger: "blur" }],
password: [{ required: true, message: "用户密码必填", trigger: "blur" }]
}, //
passwordType: 'password', //
passwordType: "password", //
capsTooltip: false, //
loading: false, // loding
redirect: undefined, //
otherQuery: {}, //
needCaptcha: false,
}
needCaptcha: false
};
},
watch: {
$route: {
// from
handler: function (route) {
const query = route.query
handler: function(route) {
const query = route.query;
if (query) {
this.redirect = query.redirect
this.otherQuery = this.getOtherQuery(query)
this.redirect = query.redirect;
this.otherQuery = this.getOtherQuery(query);
}
},
immediate: true,
},
immediate: true
}
},
mounted () {
mounted() {
//
if (this.loginForm.loginName === '') {
this.$refs.loginName.focus()
} else if (this.loginForm.password === '') {
this.$refs.password.focus()
if (this.loginForm.loginName === "") {
this.$refs.loginName.focus();
} else if (this.loginForm.password === "") {
this.$refs.password.focus();
}
},
methods: {
//
getPsw () {
const cookVal = cookies.get(`u_${this.loginForm.loginName}`)
this.loginForm.password = cookVal && Decrypt(cookVal)
getPsw() {
const cookVal = cookies.get(`u_${this.loginForm.loginName}`);
this.loginForm.password = cookVal && Decrypt(cookVal);
},
// top
setTop (val) {
this.activeTop = val
setTop(val) {
this.activeTop = val;
},
//
checkCapslock (e) {
const { key } = e
this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
checkCapslock(e) {
const { key } = e;
this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
},
//
showPwd () {
if (this.passwordType === 'password') {
this.passwordType = ''
showPwd() {
if (this.passwordType === "password") {
this.passwordType = "";
} else {
this.passwordType = 'password'
this.passwordType = "password";
}
this.$nextTick(() => {
this.$refs.password.focus()
})
this.$refs.password.focus();
});
},
//
useVerify () {
this.$refs.loginForm.validate((valid) => {
useVerify() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.$refs.verify.show()
this.$refs.verify.show();
} else {
return false
return false;
}
})
});
},
//
verifylogin (params) {
this.loginForm.verifyCode = params.captchaVerification
verifylogin(params) {
this.loginForm.verifyCode = params.captchaVerification;
if (this.loginForm.verifyCode) {
this.loginApi()
this.loginApi();
}
},
//
handleLogin () {
this.$refs.loginForm.validate((valid) => {
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.loading = true;
//
if (this.needCaptcha) {
this.useVerify()
return
this.useVerify();
return;
}
this.loginApi()
this.loginApi();
} else {
return false
return false;
}
})
});
},
async loginApi () {
async loginApi() {
let obj = {
loginName: this.loginForm.loginName,
password: transPsw(this.loginForm.password),
verifyCode: '',
}
const { code, data } = await login(obj)
console.log(data)
this.loading = false
if (code != '200') return
setToken(data.token)
setAccessUser(data)
verifyCode: ""
};
const { code, data } = await login(obj);
this.loading = false;
if (code != "200") return;
setToken(data.token);
setAccessUser(data);
// cookie,15
this.rememberPsw &&
cookies.set(
`u_${this.loginForm.loginName}`,
Encrypt(this.loginForm.password),
{ expires: 15 }
)
);
if (data && data.captcha) {
this.needCaptcha = true
this.needCaptcha = true;
} else {
this.needCaptcha = false
this.needCaptcha = false;
this.$router.push({
path: this.redirect || '/index',
query: this.otherQuery,
})
path: this.redirect || "/index",
query: this.otherQuery
});
}
},
getOtherQuery (query) {
getOtherQuery(query) {
return Object.keys(query).reduce((acc, cur) => {
if (cur !== 'redirect') {
acc[cur] = query[cur]
if (cur !== "redirect") {
acc[cur] = query[cur];
}
return acc
}, {})
},
},
}
return acc;
}, {});
}
}
};
</script>
<style lang="scss">

Loading…
Cancel
Save