10 前端登录注册(功能详解)(2)
作者:互联网
day10 前端登录注册(功能详解)
一、前端
安装:前端项目目录下的终端
cnpm install vue-cookies
配置:main.js
import cookies from 'vue-cookies'
Vue.prototype.$cookies = cookies;
登录功能(Login.vue)
<script>
export default {
name: "Login",
data() {
return {
username: '',
password: '',
mobile: '',
sms: '',
login_method: 'is_pwd',
sms_interval: '获取验证码',
is_send: false,
}
},
methods: {
send_sms() {
},
// 登录功能
login_password() {
// 判断用户名和密码是否正确
if (this.username && this.password) {
// 用户名密码正确发送请求
this.$axios.post(this.$settings.base_url + '/user/login/', {
username: this.username,
password: this.password
}).then(response => {
console.log(response)
if (response.data.code == 100) {
// 把用户信息保存都cookie中
// this.$cookies.set(key, value, 过期时间(秒))
this.$cookies.set('token', response.data.token, '7d')
this.$cookies.set('username', response.data.username, '7d')
//登录成功关闭窗口(子传父)
this.$emit('close')
// 给父组件Head传递一个事件,让它从cookie中取出token和username
this.$emit('loginsuccess')
} else {
this.$message({
message: '用户名或密码错误',
type: 'error'
})
}
})
} else {
this.$message({
message: '用户名或密码必填',
type: 'warning'
})
}
}
}
}
</script>
登录功能后显示登录注销(Head.vue)
<template>
<div class="header">
<div class="slogan">
<p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
</div>
<div class="nav">
<li class="ele">
<span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">轻课</span>
</li>
</ul>
<div class="right-part">
<!-- 3.当username没有值,不变,有值显示登录注销 -->
<div v-if="!username">
<span @click="put_login">登录</span>
<span class="line">|</span>
<span @click="put_register">注册</span>
</div>
<div v-else>
<span>{{username}}</span>
<span class="line">|</span>
<span>注销</span>
</div>
</div>
<Login v-if="is_login" @close="close_login" @go="put_register"/>
<Register v-if="is_register" @close="close_register" @go="put_login"/>
</div>
</div>
</template>
<script>
// 导入登录组件
import Login from "./Login";
import Register from "./Register";
export default {
name: "Header",
data() {
return {
url_path: sessionStorage.url_path || '/',
is_login: false,
is_register: false,
// 1.创建两个变量
token:'',
username:''
}
},
methods: {
goPage(url_path) {
},
created() {
sessionStorage.url_path = this.$route.path;
this.url_path = this.$route.path;
// 2.当页面一创建,就去cookie中取token和username
this.username=this.$cookies.get('username') // 取到就有值,取不到就为空
this.token=this.$cookies.get('token') // 取到就有值,取不到就为空
},
components: {
Login,
Register,
},
}
</script>
登录功能后刷新后显示登录注销
第一步:
<!--Login.vue-->
<script>
export default {
name: "Login",
methods: {
login_password() {
if (this.username && this.password) {
this.$axios.post(this.$settings.base_url + '/user/login/', {
username: this.username,
password: this.password
}).then(response => {
// 1.给父组件Head传递一个事件,让它从cookie中取出token和username
this.$emit('loginsuccess')
})
</script>
第二步:
<!--Head.vue-->
<template>
<div class="header">
<div class="slogan">
<p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
</div>
<div class="right-part">
<!-- 3.当username没有值,不变,有值显示登录注销 -->
<div v-if="!username">
<span @click="put_login">登录</span>
<span class="line">|</span>
<span @click="put_register">注册</span>
</div>
<div v-else>
<span>{{username}}</span>
<span class="line">|</span>
<span>注销</span>
</div>
</div>
<!-- 3.刷新页面loginsuccess,@close和Login中$emit一样的名字 -->
<Login v-if="is_login" @close="close_login" @go="put_register" @loginsuccess="login_success"/>
<Register v-if="is_register" @close="close_register" @go="put_login"/>
</div>
</div>
</template>
<script>
// 导入登录组件
import Login from "./Login";
import Register from "./Register";
export default {
name: "Header",
data() {
return {
}
},
methods: {
close_register() {
this.is_register = false;
},
login_success(){
// 2.刷新页面会变成用户名和注册,一刷新会触发点击事件获取username和token
this.username=this.$cookies.get('username')
this.token=this.$cookies.get('token')
}
},
}
</script>
注销功能
<template>
<div class="header">
<div class="slogan">
<p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
<div v-else>
<span>{{username}}</span>
<span class="line">|</span>
<!-- 3.绑定点击事件 -->
<span @click="logout">注销</span>
</div>
</div>
</template>
<script>
// 导入登录组件
import Login from "./Login";
import Register from "./Register";
export default {
name: "Header",
methods: {
login_success(){
},
logout(){
// 1.清除cookie
this.$cookies.remove('token')
this.$cookies.remove('username')
// 2.把两个变量清空
this.token=''
this.username=''
}
},
}
</script>
短信发送功能
<template>
<div class="login">
<div class="box">
<i class="el-icon-close" @click="close_login"></i>
<div class="content">
<div class="nav">
<span :class="{active: login_method === 'is_pwd'}"
@click="change_login_method('is_pwd')">密码登录</span>
<span :class="{active: login_method === 'is_sms'}"
@click="change_login_method('is_sms')">短信登录</span>
</div>
<el-form v-if="login_method === 'is_pwd'">
<el-input
placeholder="用户名/手机号/邮箱"
prefix-icon="el-icon-user"
v-model="username"
clearable>
</el-input>
<el-input
placeholder="密码"
prefix-icon="el-icon-key"
v-model="password"
clearable
show-password>
</el-input>
<el-button type="primary" @click="login_password">登录</el-button>
</el-form>
<el-form v-if="login_method === 'is_sms'">
<el-input
placeholder="手机号"
prefix-icon="el-icon-phone-outline"
v-model="mobile"
clearable
@blur="check_mobile">
</el-input>
<!--发送短信sms-->
<el-input
placeholder="验证码"
prefix-icon="el-icon-chat-line-round"
v-model="sms"
clearable>
<template slot="append">
<span class="sms" @click="send_sms">{{ sms_interval }}</span>
</template>
</el-input>
<el-button type="primary" @click="sms_login">登录</el-button>
</el-form>
<div class="foot">
<span @click="go_register">立即注册</span>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
username: '',
password: '',
mobile: '',
sms: '',
login_method: 'is_pwd',
sms_interval: '获取验证码',
is_send: false,
}
},
methods: {
// 发送验证码功能
send_sms() {
// 前端发送验证码功能
if (!this.is_send) return;
this.is_send = false;
let sms_interval_time = 60;
this.sms_interval = "发送中...";
let timer = setInterval(() => {
if (sms_interval_time <= 1) {
clearInterval(timer);
this.sms_interval = "获取验证码";
this.is_send = true; // 重新回复点击发送功能的条件
} else {
sms_interval_time -= 1;
this.sms_interval = `${sms_interval_time}秒后再发`;
}
}, 1000);
// 跟后端发送验证码接口对接
this.$axios.get(this.$settings.base_url + '/user/send_sms/?phone=' + this.mobile).then(res => {
if (res.data.code == 100) {
this.$message({
message: '发送成功',
type: 'success'
})
} else {
this.$message({
message: '发送失败,请稍后在试',
type: 'error'
})
}
})
},
}
}
</script>
短信登录功能
<template>
<div class="login">
<div class="box">
<i class="el-icon-close" @click="close_login"></i>
<div class="content">
<div class="nav">
<span :class="{active: login_method === 'is_pwd'}"
@click="change_login_method('is_pwd')">密码登录</span>
<span :class="{active: login_method === 'is_sms'}"
@click="change_login_method('is_sms')">短信登录</span>
</div>
<el-form v-if="login_method === 'is_pwd'">
<el-input
placeholder="用户名/手机号/邮箱"
prefix-icon="el-icon-user"
v-model="username"
clearable>
</el-input>
<el-input
placeholder="密码"
prefix-icon="el-icon-key"
v-model="password"
clearable
show-password>
</el-input>
<el-button type="primary" @click="login_password">登录</el-button>
</el-form>
<el-form v-if="login_method === 'is_sms'">
<el-input
placeholder="手机号"
prefix-icon="el-icon-phone-outline"
v-model="mobile"
clearable
@blur="check_mobile">
</el-input>
<el-input
placeholder="验证码"
prefix-icon="el-icon-chat-line-round"
v-model="sms"
clearable>
<template slot="append">
<span class="sms" @click="send_sms">{{ sms_interval }}</span>
</template>
</el-input>
<!--绑定点击事件登录-->
<el-button type="primary" @click="sms_login">登录</el-button>
</el-form>
<div class="foot">
<span @click="go_register">立即注册</span>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
username: '',
password: '',
mobile: '',
sms: '',
login_method: 'is_pwd',
sms_interval: '获取验证码',
is_send: false,
}
},
methods: {
close_login() {
this.$emit('close')
},
go_register() {
this.$emit('go')
},
change_login_method(method) {
this.login_method = method;
},
check_mobile() {
if (!this.mobile) return;
if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
this.$message({
message: '手机号有误',
type: 'warning',
duration: 1000,
onClose: () => {
this.mobile = '';
}
});
return false;
}
this.is_send = true;
},
// 发送验证码功能
send_sms() {
// 前端发送验证码功能
if (!this.is_send) return;
this.is_send = false;
let sms_interval_time = 60;
this.sms_interval = "发送中...";
let timer = setInterval(() => {
if (sms_interval_time <= 1) {
clearInterval(timer);
this.sms_interval = "获取验证码";
this.is_send = true; // 重新回复点击发送功能的条件
} else {
sms_interval_time -= 1;
this.sms_interval = `${sms_interval_time}秒后再发`;
}
}, 1000);
// 跟后端发送验证码接口对接
this.$axios.get(this.$settings.base_url + '/user/send_sms/?phone=' + this.mobile).then(res => {
if (res.data.code == 100) {
this.$message({
message: '发送成功',
type: 'success'
})
} else {
this.$message({
message: '发送失败,请稍后在试',
type: 'error'
})
}
})
},
// 短信登录功能
sms_login() {
// 如果都有值,向后端发送请求
if (this.mobile && this.sms) {
// 用户名密码正确发送请求
this.$axios.post(this.$settings.base_url + '/user/mobile_login/', {
mobile: this.mobile,
code: this.sms,
}).then(response => {
console.log(response)
if (response.data.code == 100) {
// 把用户信息保存都cookie中
// this.$cookies.set(key, value, 过期时间(秒))
this.$cookies.set('token', response.data.token, '7d')
this.$cookies.set('username', response.data.username, '7d')
//登录成功关闭窗口(子传父)
this.$emit('close')
// 给父组件Head传递一个事件,让它从cookie中取出token和username
this.$emit('loginsuccess')
} else {
this.$message({
message: '用户名或验证码错误错误',
type: 'error'
})
this.username = ''
this.password = ''
}
})
} else {
this.$message({
message: '手机号或验证码不能为空',
type: 'warning'
})
}
},
}
}
</script>
总结
# cookies修改页面登录状态
1.重点cookies操作
this.$cookies.set('key',value,过期时间(秒)) # 保存cookies信息
this.$cookies.get('key') # 获取cookies信息
this.cookies.remove('key') # 删除cookies信息
标签:username,10,cookies,登录,sms,token,详解,message 来源: https://www.cnblogs.com/mengqingqiang/p/15946394.html