Files

55 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
const Controller = require('../core/base_controller');
const fileName = "Exam";
class ExamController extends Controller {
// 北斗应用技术培训证书项目报名系统登录
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const phone = this.decryptRequest(body.phone);
const password = this.decryptRequest(body.password);
const user = await ctx.service.exam.loginUser(phone);
if(user == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (user && user.dataValues.status !== 2 && user.dataValues.password == this.shaForm(password, this.decryptRequest(user.dataValues.salt))) {
const result =await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/user/authorize', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
state: 'xyz',
response_type: 'code',
username: user.dataValues.uid,
password: password,
client_id: "exam",
client_secret: "exam",
redirect_uri: 'http://127.0.0.1:' + ctx.app.server.address().port + '/getToken/'
},
});
const timestamp = new Date().getTime();
const data = {
data: this.encryptRequest(user.dataValues.uid + '?' + timestamp),
loginCode: result.headers.location.split('?')[1].split('&')[0].split('=')[1]
};
await ctx.service.exam.loginStatus(user.dataValues.uid, 1);
this.setResponse(ctx, 200, '登录成功', "deconstruction", data);
} else {
const data = user.dataValues.status == 2 ? { msg: '该账号已被封禁,请联系管理员申诉', code: 444 } : { msg: '登录失败', code: 404 };
this.setResponse(ctx, 404, '登录失败', "deconstruction", data);
}
} catch (error) {
const blockName = fileName + "--" + "login";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = ExamController;