Thinkjs cross-domain processing:

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(); // Set body to a value, otherwise error 404; if(method==="options"){ this.ctx.body=200; return false; }}Copy the code

Code examples:

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(); // Set body to a value, otherwise error 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){ let logStatus= await this.model("log").where({email}).find(); console.log("email is exist=",logStatus.email); If (logstatus.email){// If the login succeeds, update the login log; await this.model("log").where({email}).update({ flag:1,login_time,password:password,login_ip }); }else{// If the first login, add the first login record; await this.model("log").add({ flag:1,email,login_time,password:password,login_ip }); } let resJsonObj={status:"true", code:"0000", desc:" successful ", type, currentAuthority:"user",}; return this.success(resJsonObj); }else{return this.fail(" username or password error "); } }catch (e) { think.logger.error(new Error(e)); Let resJsonObj={status:"false", code:"0110", desc:" system internal error "}; return this.fail(resJsonObj, e); }}};Copy the code