12月28, 2017

thinkJs-登录

session 存在于服务器 cookie 存在浏览器

thinkJs 登录理解

前端 validate验证

  1. 判断用户名是否存在数据库

  2. 用户名 密码 不为空

  3. 输入验证码

  4. 表单提交

$(form).ajaxSubmit({
                    dataType:"json",
                    success:function( jsondata ){
                        if( jsondata.code == '0' ){
                            window.location.href = '/index/index'
                        }else{
                            alert(code.msg);
                        }
                    }
                });

后端 session cookie

  1. 查询用户名 存在
 // 登录用户名字信息验证
    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;
        }
    }
  1. 获取验证码 判断用户数据是否存在数据库中
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: '验证码错误'
            }
        } 
    }
  1. 设置session cookie 有效期

本文链接:http://www.hijs.cc/post/thinkjs_login.html

-- EOF --

Comments