5、页面的修订
Login.vw代码修订
<template>
<div class="login box">
<img src="@/assets/img/Loginbg.jpg" alt="">
<div class="login">
<div class="login-title">
<img src="@/assets/img/Logotitle.png" alt="">
<p>帮助有志向的年轻人通过努力学习获得体面的工作和生活!</p>
</div>
<div class="login_box">
<div class="title">
<span :class="{active: a0}" @click="changeLogin(0)">密码登录</span>
<span :class="{active: a1}" @click="changeLogin(1)">短信登录</span>
</div>
<div class="inp" v-if="login_type==0">
<input v-model="username" type="text" placeholder="用户名 / 手机号码" class="user">
<input v-model="password" type="password" name="" class="pwd" placeholder="密码">
<div id="geetest1"></div>
<div class="rember">
<p>
<input id="checkbox" type="checkbox" class="no" v-model="remember"/>
<span>记住密码</span>
</p>
<p>忘记密码</p>
</div>
<button class="login_btn" @click="loginAction">登录</button>
<p class="go_login">没有账号 <router-link to="/register">立即注册</router-link></p>
</div>
<div class="inp" v-show="login_type==1">
<input v-model="mobile" type="text" placeholder="手机号码" class="user">
<div class="sms">
<input v-model="sms" type="text" placeholder="输入验证码" class="user">
<span class="sms_btn" @click="send_sms">{{sms_interval_tips}}</span>
</div>
<button class="login_btn" @click="loginMobile">登录</button>
<p class="go_login">没有账号 <router-link to="/register">立即注册</router-link></p>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'Login',
data() {
return {
a0: 1,
a1: 0,
login_type: 0,
username: "",
password: "",
remember: false,
mobile: "",
sms: "",
is_send: false, // 是否在60s内发送了短信
sms_interval_tips: "获取验证码",
}
},
methods: {
changeLogin(i) {
this.login_type = i;
if (i) {
this.a0 = 0;
this.a1 = 1;
} else {
this.a0 = 1;
this.a1 = 0;
}
},
loginAction() {
if (!this.username || !this.password) {
return
}
this.$axios({
url: this.$settings.Host + 'user/login/',
method: 'post',
data: {
'username': this.username,
'password': this.password
}
}).then((response) => {
// 判断用户是否要记住密码
window.console.log(">>>>", response.data);
if (this.remember) { // 记住密码
sessionStorage.clear();
localStorage.token = response.data.token;
localStorage.user_name = response.data.user.username;
localStorage.user_mobile = response.data.user.mobile;
} else { /// 没记住密码
localStorage.clear();
sessionStorage.token = response.data.token;
sessionStorage.user_id = response.data.user.username;
sessionStorage.user_name = response.data.user.mobile;
}
// 页面跳转
let _this = this;
this.$alert("欢迎回来!", "登录成功!", {
confirmButtonText: '确定',
callback() {
// 跳转页面
_this.$router.go(-1); // 返回上一页
// 进行制定的网站内部地址跳转
// this.$router.push("站内地址");
}
})
}).catch((error) => {
window.console.log('失败:', error);
// 页面跳转
let _this = this;
this.$alert("检查账号密码!", "登录失败!", {
confirmButtonText: '确定',
callback() {
_this.username = '';
_this.password = '';
}
});
})
},
send_sms() {
// 发送短信
if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
this.$message({
message: "对不起!手机号码格式有误!"
});
return false;
}
// 判断是否在60s内发送过短信
if (this.is_send) {
this.$message({
message: "对不起,不能频繁发送短信验证!"
});
return false;
}
// 请求发送短信
this.$axios({
url: this.$settings.Host + 'user/sms/',
method: 'get',
params: {
mobile: this.mobile
}
}).then(response => {
let msg = response.data.result
this.$message({
message: msg,
});
if (msg === '短信发送失败') return
// 修改短信的发送状态
this.is_send = true;
// 设置间隔时间60s
let sms_interval_time = 60;
// 设置短信发送间隔倒计时,.60s后把is_send改成false
let timer = setInterval(() => {
if (sms_interval_time <= 1) {
clearInterval(timer);
this.sms_interval_tips = "获取验证码";
this.is_send = false; // 重新回复点击发送功能的条件
} else {
sms_interval_time -= 1;
this.sms_interval_tips = `${sms_interval_time}秒后再次获取`;
}
}, 1000);
}).catch(error => {
this.$message({
message: error.response.data.result,
})
});
},
loginMobile() {
// 注册信息提交
if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
this.$message({
message: "对不起!手机号码格式有误!"
});
return false;
}
if (this.sms.length < 1) {
this.$message({
message: "短信验证码不能为空!"
});
return false;
}
this.$axios({
url: this.$settings.Host + 'user/login/mobile/',
method: 'post',
data: {
mobile: this.mobile,
sms: this.sms
}
}).then(response => {
let _this = this;
let status = response.data.status;
let msg = response.data.msg;
_this.$message({
message: msg,
onClose() {
if (status == 0) {
// 保存登录状态
sessionStorage.token = response.data.token;
sessionStorage.user_name = response.data.user.username;
// sessionStorage.user_mobile = response.data.user.mobile;
// 跳转到主页
_this.$router.push('/');
}
}
});
}).catch(error => {
this.$message({
message: error.response.data.result
});
})
},
},
};
</script>
<style scoped>
.box {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.box img {
width: 100%;
min-height: 100%;
}
.box .login {
position: absolute;
width: 500px;
height: 400px;
left: 0;
margin: auto;
right: 0;
bottom: 0;
top: -338px;
}
.login .login-title {
width: 100%;
text-align: center;
padding-top: 20px;
}
.login-title img {
width: 190px;
height: auto;
}
.login-title p {
font-family: PingFangSC-Regular;
font-size: 18px;
color: #fff;
letter-spacing: .29px;
padding-top: 10px;
padding-bottom: 50px;
}
.login_box {
width: 400px;
height: auto;
background: #fff;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
border-radius: 4px;
margin: 0 auto;
padding-bottom: 40px;
}
.login_box .title {
font-size: 20px;
color: #9b9b9b;
letter-spacing: .32px;
border-bottom: 1px solid #e6e6e6;
display: flex;
justify-content: space-around;
padding: 50px 60px 0 60px;
margin-bottom: 20px;
cursor: pointer;
}
.login_box .title span.active {
color: #4a4a4a;
border-bottom: 2px solid #84cc39;
}
.inp {
width: 350px;
margin: 0 auto;
}
.inp input {
outline: 0;
width: 100%;
height: 45px;
border-radius: 4px;
border: 1px solid #d9d9d9;
text-indent: 20px;
font-size: 14px;
background: #fff !important;
}
.inp input.user {
margin-bottom: 16px;
}
.inp .rember {
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
margin-top: 10px;
}
.inp .rember p:first-of-type {
font-size: 12px;
color: #4a4a4a;
letter-spacing: .19px;
margin-left: 22px;
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
/*position: relative;*/
}
.inp .rember p:nth-of-type(2) {
font-size: 14px;
color: #9b9b9b;
letter-spacing: .19px;
cursor: pointer;
}
.inp .rember input {
outline: 0;
width: 30px;
height: 45px;
border-radius: 4px;
border: 1px solid #d9d9d9;
text-indent: 20px;
font-size: 14px;
background: #fff !important;
}
.inp .rember p span {
display: inline-block;
font-size: 12px;
width: 100px;
/*position: absolute;*/
/*left: 20px;*/
}
#geetest {
margin-top: 20px;
}
.login_btn {
width: 100%;
height: 45px;
background: #84cc39;
border-radius: 5px;
font-size: 16px;
color: #fff;
letter-spacing: .26px;
margin-top: 30px;
}
.inp .go_login {
text-align: center;
font-size: 14px;
color: #9b9b9b;
letter-spacing: .26px;
padding-top: 20px;
}
.inp .go_login a {
color: #84cc39;
cursor: pointer;
}
#get_code {
border: 0;
width: 120px;
height: 30px;
background-color: antiquewhite;
outline: none;
}
#get_code:active {
color: white;
}
#checkbox {
width: 20px;
height: 20px;
}
.sms {
position: relative;
}
.sms .sms_btn {
position: absolute;
top: -12px;
right: 0;
bottom: 0;
margin: auto;
width: 130px;
text-align: center;
height: 24px;
color: #ff7000;
cursor: pointer;
border-left: 1px solid #999;
}
</style>
Header.vue代码修订
<template>
<div class="header-box">
<div class="header">
<div class="content">
<div class="logo full-left">
<router-link to="/"><img @click="jump('/')" src="@/assets/img/logo.svg" alt=""></router-link>
</div>
<ul class="nav full-left">
<li v-for="nav in nav_list" :key="nav.name">
<span @click="jump(nav.link)" :class="this_nav==nav.link?'this':''">{{nav.name}}</span>
</li>
</ul>
<!-- 改造的部分 -->
<div v-if="token" class="login-bar full-right">
<div class="shop-cart full-left">
<img src="@/assets/img/cart.svg" alt="">
<span><router-link to="/cart">购物车</router-link></span>
</div>
<div class="login-box login-box1 full-left">
<router-link to="">学习中心</router-link>
<el-menu width="200" id="member" class="el-menu-demo full-right" mode="horizontal">
<el-submenu index="2">
<template slot="title">
<router-link to=""><img src="@/assets/img/logo@2x.png" alt=""></router-link>
</template>
<el-menu-item index="2-1">我的账户</el-menu-item>
<el-menu-item index="2-2">我的订单</el-menu-item>
<el-menu-item index="2-3"><span @click="logout">退出登录</span></el-menu-item>
</el-submenu>
</el-menu>
</div>
</div>
<div v-if="!token" class="login-bar full-right">
<div class="shop-cart full-left">
<img src="@/assets/img/cart.svg" alt="">
<span><router-link to="/cart">购物车</router-link></span>
</div>
<div class="login-box login-box2 full-left">
<router-link to="/login"><span>登录</span></router-link>
|
<router-link to="/register"><span>注册</span></router-link>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Header",
data() {
return {
token: localStorage.token || sessionStorage.token,
this_nav: "",
nav_list: [],
}
},
created() {
this.this_nav = localStorage.this_nav;
let _this = this;
// 获取服务端的导航信息
this.$axios({
url: this.$settings.Host + "home/nav/header/"
}).then(function (response) {
_this.nav_list = response.data
}).catch(function (error) {
window.console.log(error)
});
},
methods: {
jump(location) {
localStorage.this_nav = location;
// vue-router除了提供router-link标签跳转页面以外,还提供了 js跳转的方式
this.$router.push(location);
},
// 改造的部分
logout() {
// 重置token
this.token = false;
// 移除用户登录信息
sessionStorage.clear();
localStorage.clear();
// 退出登录提示
let _this = this;
_this.$message({
message: '退出登录成功!',
type: 'success',
duration: '1500',
onClose() {
// 跳转到首页
_this.$router.push("/");
}
});
}
},
}
</script>
<style scoped>
.header-box {
height: 80px;
}
.header {
width: 100%;
height: 80px;
box-shadow: 0 0.5px 0.5px 0 #c9c9c9;
position: fixed;
top: 0;
left: 0;
right: 0;
margin: auto;
z-index: 99;
background: #fff;
}
.header .content {
max-width: 1200px;
width: 100%;
margin: 0 auto;
}
.header .content .logo {
height: 80px;
line-height: 80px;
margin-right: 50px;
cursor: pointer; /* 设置光标的形状为爪子 */
}
.header .content .logo img {
vertical-align: middle;
}
.header .nav li {
float: left;
height: 80px;
line-height: 80px;
margin-right: 30px;
font-size: 16px;
color: #4a4a4a;
cursor: pointer;
}
.header .nav li span {
padding-bottom: 16px;
padding-left: 5px;
padding-right: 5px;
}
.header .nav li span a {
display: inline-block;
}
.header .nav li .this {
color: #4a4a4a;
border-bottom: 4px solid #ffc210;
}
.header .nav li:hover span {
color: #000;
}
.header .login-bar {
height: 80px;
}
.header .login-bar .shop-cart {
margin-right: 20px;
border-radius: 17px;
background: #f7f7f7;
cursor: pointer;
font-size: 14px;
height: 28px;
width: 88px;
margin-top: 30px;
line-height: 32px;
text-align: center;
}
.header .login-bar .shop-cart:hover {
background: #f0f0f0;
}
.header .login-bar .shop-cart img {
width: 15px;
margin-right: 4px;
margin-left: 6px;
}
.header .login-bar .shop-cart span {
margin-right: 6px;
}
.header .login-bar .login-box {
margin-top: 33px;
}
.header .login-bar .login-box span {
color: #4a4a4a;
cursor: pointer;
}
.header .login-bar .login-box span:hover {
color: #000000;
}
/*改造的部分*/
#member {
border: none;
margin-top: -22px;
}
#member img {
width: 26px;
height: 26px;
border-radius: 50%;
}
</style>
程序猿,要对自己狠一点!