vue+element-ui实现cookie登录
//效果

//login.vue <template>
<div>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm loginFrom">
<h1 style="font-size: 1.5rem;color: #fff;font-weight: bold;padding: 1rem 0;">登陆</h1>
<el-form-item>
<el-input placeholder="账号" v-model="ruleForm.userName"></el-input>
</el-form-item>
<el-form-item>
<el-input type="password" v-model="ruleForm.password" placeholder="密码"></el-input>
</el-form-item>
<div style="padding: 1rem 0 2rem 0;" class="clear">
<span class="lf" style="color:#0489cc;">帮助</span>
<div class="rt">
<el-checkbox v-model="checked" style="color:#a0a0a0;">一周内自动登录</el-checkbox>
<span @click="clearCookie" style="cursor: pointer;color: #f19149;font-size: 0.75rem;margin-left: 5px;">忘记密码?</span>
</div>
</div>
<el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%;">登陆</el-button>
</el-form>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm loginFrom">
<h1 style="font-size: 1.5rem;color: #fff;font-weight: bold;padding: 1rem 0;">登陆</h1>
<el-form-item>
<el-input placeholder="账号" v-model="ruleForm.userName"></el-input>
</el-form-item>
<el-form-item>
<el-input type="password" v-model="ruleForm.password" placeholder="密码"></el-input>
</el-form-item>
<div style="padding: 1rem 0 2rem 0;" class="clear">
<span class="lf" style="color:#0489cc;">帮助</span>
<div class="rt">
<el-checkbox v-model="checked" style="color:#a0a0a0;">一周内自动登录</el-checkbox>
<span @click="clearCookie" style="cursor: pointer;color: #f19149;font-size: 0.75rem;margin-left: 5px;">忘记密码?</span>
</div>
</div>
<el-button type="primary" @click="submitForm('ruleForm')" style="width: 100%;">登陆</el-button>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
ruleForm: {
userName: "", //用户名
password: "" //密码
}
};
},
methods: {
//点击登录调用方法
submitForm(formName) {
//保存的账号
var name = this.ruleForm.userName;
//保存的密码
var pass = this.ruleForm.password;
if (name == "" || name == null) {
alert("请输入正确的用户名");
return;
} else if (pass == "" || pass == null) {
alert("请输入正确的密码");
return;
}
//判断复选框是否被勾选 勾选则调用配置cookie方法
if ((this.checked = true)) {
//传入账号名,密码,和保存天数3个参数
this.setCookie(name, pass, 7);
}
//接口
var url = "myserver/user/login";
this.$http.post(url, this.ruleForm, { emulateJSON: true }).then(res => {
if (res.body == "fail") {
alert("用户名或密码错误,请重新输入");
this.ruleForm.userName = "";
this.ruleForm.password = "";
return;
} else {
alert("登陆成功");
this.$router.push("/index");
}
});
},
//设置cookie
setCookie(c_name, c_pwd, exdays) {
var exdate = new Date(); //获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
//字符串拼接cookie
window.document.cookie =
"userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
window.document.cookie =
"userPwd" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
},
//读取cookie
getCookie: function() {
if (document.cookie.length > 0) {
var arr = document.cookie.split("; "); //这里显示的格式需要切割一下自己可输出看下
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split("="); //再次切割
//判断查找相对应的值
if (arr2[0] == "userName") {
this.ruleForm.userName = arr2[1]; //保存到保存数据的地方
} else if (arr2[0] == "userPwd") {
this.ruleForm.password = arr2[1];
}
}
}
},
//清除cookie
clearCookie: function() {
this.setCookie("", "", -1); //修改2值都为空,天数为负1天就好了
}
},
//页面加载调用获取cookie值
mounted() {
this.getCookie();
}
};
</script>
<style></style>
vue+element-ui实现cookie登录的更多相关文章
- 基于vue(element ui) + ssm + shiro 的权限框架
zhcc 基于vue(element ui) + ssm + shiro 的权限框架 引言 心声 现在的Java世界,各种资源很丰富,不得不说,从分布式,服务化,orm,再到前端控制,权限等等玲琅满目 ...
- 分享一个自搭的框架,使用Spring boot+Vue+Element UI
废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...
- Vue + Element UI 实现权限管理系统
Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html
- vue + element ui 实现实现动态渲染表格
前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...
- vue + element ui 表格自定义表头,提供线上demo
前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...
- vue+element ui 的上传文件使用组件
前言:工作中用到 vue+element ui 的前端框架,使用到上传文件,则想着封装为组件,达到复用,可扩展.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9 ...
- vue+element ui 的表格列使用组件
前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...
- vue+element ui 的tab 动态增减,切换时提示用户是否切换
前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui 有一个 bug,这里记录一下如何实现.转载 ...
- 基于 vue+element ui 的cdn网站(多页面,都是各种demo)
前言:这个网站持续更新中...,有网上预览,github上也有源码,喜欢记得star哦,欢迎留言讨论. 网站地址:我的个人vue+element ui demo网站 github地址:yuleGH g ...
- vue + element ui 阻止表单输入框回车刷新页面
问题 在 vue+element ui 中只有一个输入框(el-input)的情况下,回车会提交表单. 解决方案 在 el-form 上加上 @submit.native.prevent 这个则会阻止 ...
随机推荐
- Thinkphp的import使用方法
Thinkphp的import使用方法主要有以下4种,在此记下以供查询.原文链接:http://www.jb51.net/article/51765.htm 感谢. 1.用法一 import( ...
- LeetCode: 455 Assign Cookies(easy)
题目: Assume you are an awesome parent and want to give your children some cookies. But, you should gi ...
- Codeforces Round #527 (Div. 3)C(多重集,STRING)
#include<bits/stdc++.h>using namespace std;const int maxn=1e6+7;pair<string,int>p[maxn]; ...
- 2017乌鲁木齐区域赛I(带权并查集)
#include<bits/stdc++.h>using namespace std;int f[200010];//代表元long long rl[200010];//记rl[i]为结点 ...
- Ckeditor 4 复制粘贴截图并转化base64格式保存至数据库
虽然Ckeditor 中自带的有上传图片和文件的功能,但是有时候我们并不需要把图片保存至服务器的文件夹中. 反而是截图复制粘贴,把图片转化为base64格式保存在数据库中即可满足要求. 1.首先下载安 ...
- thinkphp5文件上传问题
tp5中文件上传如果没有数据就会报错,所以要先做一个判断 //先接收文件数据 $isfile=$_FILES;//判断是否上传图片数据,如果没有上传数据二位数组中的name会为空,如下例:if($is ...
- javascript的模块发展
谨以此文记录了解js模块的过程 随着ES6的出现,js模块已经成为正式的标准了.曾经为了解决js模块问题而发展起来的民间秘籍,requireJs(AMD).SeaJs(CMD).Node(Common ...
- P1067 多项式输出(模拟水题)
题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_ix^iaixi称为ii次项,a_iai 称为ii次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: ...
- myeclipse-9.0安装svn客户端插件
SVN插件配置到MyEclipse中的步骤 听语音 | 浏览:20471 | 更新:2015-01-09 10:26 | 标签:myeclipse 1 2 3 4 5 6 7 分步阅读 MyEclip ...
- docker 资源限制
docker run 时使用-m指定可以使用的内存大小, 记录在cgroup配置文件中 cat /sys/fs/cgroup/memory/memory.limit_in_bytes jvm内存会超过 ...