//效果

//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登录的更多相关文章

  1. 基于vue(element ui) + ssm + shiro 的权限框架

    zhcc 基于vue(element ui) + ssm + shiro 的权限框架 引言 心声 现在的Java世界,各种资源很丰富,不得不说,从分布式,服务化,orm,再到前端控制,权限等等玲琅满目 ...

  2. 分享一个自搭的框架,使用Spring boot+Vue+Element UI

    废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...

  3. Vue + Element UI 实现权限管理系统

    Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html

  4. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

  5. vue + element ui 表格自定义表头,提供线上demo

    前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...

  6. vue+element ui 的上传文件使用组件

    前言:工作中用到 vue+element ui 的前端框架,使用到上传文件,则想着封装为组件,达到复用,可扩展.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9 ...

  7. vue+element ui 的表格列使用组件

    前言:工作中用到 vue+element ui 的前端框架,有这个场景:很多表格的列有许多一样的,所以考虑将列封装为组件.转载请注明出处:https://www.cnblogs.com/yuxiaol ...

  8. vue+element ui 的tab 动态增减,切换时提示用户是否切换

    前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui  有一个 bug,这里记录一下如何实现.转载 ...

  9. 基于 vue+element ui 的cdn网站(多页面,都是各种demo)

    前言:这个网站持续更新中...,有网上预览,github上也有源码,喜欢记得star哦,欢迎留言讨论. 网站地址:我的个人vue+element ui demo网站 github地址:yuleGH g ...

  10. vue + element ui 阻止表单输入框回车刷新页面

    问题 在 vue+element ui 中只有一个输入框(el-input)的情况下,回车会提交表单. 解决方案 在 el-form 上加上 @submit.native.prevent 这个则会阻止 ...

随机推荐

  1. 我的笔记文档版本控制系统-MediaWiki-目录悬浮+隐藏

    13年11份把北京的工作辞了,出去从北到南找同学玩了二十多天,因为各种原因,回家(宁夏)找工作,想找一个Linux相关的工作,但涉及Linux的都是运维.支持一类,最后因为各种原因找了个做java的本 ...

  2. linux学习第一周小结

    这几天学习linux课程,安装环境,遇到不会的查询资料,在这个过程中发现了很多有意思的网页,看到了一些不一样的内容,现在对linux的学习兴趣增强了许多.学习解决问题也是很有意思的事情,解决问题的过程 ...

  3. 给WPF的MessageBox启用视觉样式

    WPF的MessageBox为什么会这样 我的一个同学跟我说:“WPF不是新一代技术吗?怎么连MessageBox都没WinForm 的好看?” 上图是Windows Forms 的MesssageB ...

  4. EIP权限工作流平台总结-3后端框架

    1.预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com/A ...

  5. 在Android中使用FlatBuffers(下篇)

    本文来自网易云社区. FlatBuffers编码数组 编码数组的过程如下: 先执行 startVector(),这个方法会记录数组的长度,处理元素的对齐,准备足够的空间,并设置nested,用于指示记 ...

  6. 存储引擎:MySQL系列之七

    一.MyISAM存储引擎 缺点: 不支持事务 最小粒度锁:表级 读写相互阻塞,写入不能读,读时不能写 不支持MVCC(支持多版本并发控制机制) 不支持聚簇索引 不支持数据缓存 不支持外键 崩溃恢复性较 ...

  7. 5、python数据类型之元组(tuple)

    元组 元组和列表最大的区别是元组中的元素固定,元组不能修改,所以不能对元组进行增.删.改 1.创建 tu = (11,22,33) tu = tuple(11,22,33) tu = tuple([] ...

  8. spring /* 和 /** 的 区别。

    例如 /** 拦截 /index/1   和  /index /* 代表 /index/1 而  /index 则不会被拦截

  9. Draw a Mess (并查集)

    It's graduated season, every students should leave something on the wall, so....they draw a lot of g ...

  10. BigDecimal默认用四舍五入方式

    import java.math.BigDecimal; target.setWeight(source.getWeight().setScale(3, BigDecimal.ROUND_HALF_U ...