thinkjs ajax跨域,前端nodeJS:thinkJS 跨域处理

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);

}

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值