前端nodeJS:thinkJS 跨域处理

async __before(){
            this.header("Access-Control-Allow-Origin",this.header("origin")||"*");
            this.header("Access-Control-Allow-Headers", "x-requested-with,content-type");
            this.header("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
            this.header('Access-Control-Allow-Credentials',true);
            let method=this.method.toLowerCase();
            //  处理预检请求,给body设置一个值,否则报错404;
            if(method==="options"){
                this.ctx.body=200;
                return false;
            }
        }
复制代码
const BaseRest = require('../rest.js');

module.exports = class extends BaseRest {
    async __before(){
        this.header("Access-Control-Allow-Origin",this.header("origin")||"*");
        this.header("Access-Control-Allow-Headers", "x-requested-with,content-type");
        this.header("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        this.header('Access-Control-Allow-Credentials',true);
        let method=this.method.toLowerCase();
        //  处理预检请求,给body设置一个值,否则报错404;
        if(method==="options"){
            this.ctx.body=200;
            return false;
        }
    }
    async loginAction() {
        let {email, password,type} = this.post();
        const salt = 'geekQiaQia';
        password = think.md5(salt + password);
        const login_ip = this.ctx.ip;
        let dateTime = new Date();
        let login_time = think.datetime(dateTime);

        try{
            let user=await this.model("user").where({email: email}).find();

            if(user.password&&user.password===password){
                // await this.model("log").add({
                //     flag:1,email,login_time,password:password,login_ip
                // });
               let logStatus= await this.model("log").where({email}).find();
               console.log("email is exist=",logStatus.email);
               if(logStatus.email){
                   // 如果登录成功,则update登录日志;
                   await this.model("log").where({email}).update({
                       flag:1,login_time,password:password,login_ip
                   });
               }else{
                   // 如果首次登录,则添加首次登录记录;
                   await this.model("log").add({
                       flag:1,email,login_time,password:password,login_ip
                   });
               }
                let resJsonObj={
                    status:"true",
                    code:"0000",
                    desc:"操作成功",
                    type,
                    currentAuthority:"user",
                };
              return this.success(resJsonObj);
            }else{
                return this.fail("用户名或者密码错误");
            }
        }catch (e) {
            think.logger.error(new Error(e));
            let resJsonObj={
                status:"false",
                code:"0110",
                desc:"系统内部错误"
            };
            return this.fail(resJsonObj, e);
        }

    }

};
复制代码
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章