session 存在于服务器 cookie 存在浏览器
thinkJs 登录理解
前端 validate验证
判断用户名是否存在数据库
用户名 密码 不为空
输入验证码
表单提交
$(form).ajaxSubmit({
dataType:"json",
success:function( jsondata ){
if( jsondata.code == '0' ){
window.location.href = '/index/index'
}else{
alert(code.msg);
}
}
});
后端 session cookie
- 查询用户名 存在
// 登录用户名字信息验证
async checkNameLogAction() {
let name = this.ctx.post().name;
let data = await this.model('users').where({name: name}).select();
if (think.isEmpty(data)) { // 不存在
// 内容为空时的处理
this.body = false;
} else { // 存在
this.body = true;
}
}
- 获取验证码 判断用户数据是否存在数据库中
async userloginAction(){
// 获取存储的验证码
let code = await this.session('code');
if (parseInt(this.ctx.post().code) === code) {
let name = this.ctx.post().name || '';
let password = this.ctx.post().password || '';
let res = await this.model('users').where({name: name, password: password}).select();
// 查不到数据 内容为空时的处理
if (think.isEmpty(res)) {
this.body = {
code: -1,
msg: '用户名或密码错误'
}
} else {
await this.session('name', name);
this.body = {
code: 0,
msg: '登陆成功'
}
}
} else {
this.body = {
code: -1,
msg: '验证码错误'
}
}
}
- 设置session cookie 有效期
Comments