北斗启航科技教育平台登录系统

This commit is contained in:
2026-03-27 17:50:18 +08:00
commit bdc25653a4
215 changed files with 5299 additions and 0 deletions

109
app/controller/BSide.js Normal file
View File

@@ -0,0 +1,109 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "BSide";
const checkUid = async (thisPlace) => {
const ctx = thisPlace.ctx;
const token = ctx.request.header.authorization;
const redis = ctx.app.redis;
let rdmcode = await redis.get('db0').get(token);
if (!rdmcode) {
return false;
}
return rdmcode;
};
class BSideController extends Controller {
// B端登录
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const name = this.decryptRequest(body.name);
const password = this.decryptRequest(body.password);
const user = await ctx.service.bSide.loginUser(name);
if(user == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
const client = await ctx.service.verify.loginClient(body.client_id);
if(client == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
// await ctx.service.bSide.subscribe(user.dataValues, client.dataValues);
if (user && client && 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: client.dataValues.client_id,
client_secret: client.dataValues.client_secret,
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),
redirect: client.dataValues.client_url + '?' + result.headers.location.split('?')[1].split('&')[0].split('=')[1]
};
await ctx.service.bSide.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");
}
}
//B端管理员修改用户基本信息
async editUserManager() {
const ctx = this.ctx;
try {
let rdmcode = await checkUid(this);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const body = ctx.request.body;
const Manager = await ctx.service.bSide.uidfinder(uid);
if (Manager == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (Manager && Manager.dataValues.role == '1') {
const userData = await ctx.service.bSide.editUserManager(body.editMsg, body.userMsg);
if (userData == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (userData[0] !== 0) {
this.setResponse(ctx, 200, '修改用户基本信息成功', "none");
} else {
this.setResponse(ctx, 404, '修改用户基本信息失败', "none");
}
} else {
this.setResponse(ctx, 404, '修改用户基本信息失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "editUserManager";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = BSideController;

View File

@@ -0,0 +1,50 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "CNTEVService";
class CNTEVServiceController extends Controller {
// 身份证二要素核验
async CNTEV() {
const { ctx } = this;
try {
const { user_name, identity_card } = ctx.request.body;
const result = await ctx.service.cNTEVService.CNTEV(user_name, identity_card);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result.status == "1") {
await ctx.service.cNTEVService.realMark(user_name, identity_card, true);
this.setResponse(ctx, 200, result.msg, "none");
} else if (result.status == "0" || result.status == "2" || result.status == "400") {
await ctx.service.cNTEVService.realMark(user_name, identity_card, false);
this.setResponse(ctx, 200, result.msg, "none");
} else if (result.status == "600") {
setTimeout(async () => {
await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/CNTEV', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
identity_card,
user_name
},
});
}, 3000);
this.setResponse(ctx, 200, '正在核验中', "none");
} else {
await ctx.service.cNTEVService.realMark(user_name, identity_card, false);
this.setResponse(ctx, 404, '核验失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "CNTEV";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = CNTEVServiceController;

170
app/controller/CSide.js Normal file
View File

@@ -0,0 +1,170 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "CSide";
class CSideController extends Controller {
// 比较用户名
async diffName() {
try {
const ctx = this.ctx;
const body = ctx.request.body;
const result = await ctx.service.cSide.loginUser(body.name);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 404, '该用户名已存在', "none");
} else {
this.setResponse(ctx, 200, '验证通过', "none");
}
} catch (error) {
const blockName = fileName + "--" + "diffName";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 比较电话号码
async diffPhone() {
try {
const ctx = this.ctx;
const body = ctx.request.body;
const result = await ctx.service.cSide.diffPhone(body.phone);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 404, '该手机号已注册', "none");
} else {
this.setResponse(ctx, 200, '验证通过', "none");
}
} catch (error) {
const blockName = fileName + "--" + "diffPhone";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 比较邮箱
async diffEmail() {
try {
const ctx = this.ctx;
const body = ctx.request.body;
const result = await ctx.service.cSide.diffEmail(body.email);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 404, '该邮箱已经注册', "none");
} else {
this.setResponse(ctx, 200, '验证通过', "none");
}
} catch (error) {
const blockName = fileName + "--" + "diffEmail";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// C端用户注册
async registered() {
const { ctx } = this;
try {
const salt = this.generateRandomString();
const body = ctx.request.body;
const form = {
uid: this.generateNumberString('C'),
salt: this.encryptRequest(salt),
status: 0,
role: 0,
avatar: 'default',
account_name: this.decryptRequest(body.account_name),
password: this.shaForm(this.decryptRequest(body.password), salt),
user_name: body.user_name == "" ? "" : this.decryptRequest(body.user_name),
phone: this.decryptRequest(body.phone),
gender: body.gender == "" ? null : this.decryptRequest(body.gender),
age: body.age == "" ? null : this.decryptRequest(body.age),
email: body.email == "" ? "" : this.decryptRequest(body.email),
address: body.address == "" ? "" : this.decryptRequest(body.address),
post_code: body.post_code == "" ? "" : this.decryptRequest(body.post_code),
created_at: new Date(),
updated_at: new Date(),
login_at: new Date(),
login_status: 0,
};
const result = await ctx.service.cSide.registered(form);
if(result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, result.code, result.message, "none");
} else {
this.setResponse(ctx, 404, '注册失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "registered";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 通用服务登录
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const name = this.decryptRequest(body.name);
const password = this.decryptRequest(body.password);
const user = await ctx.service.cSide.loginUser(name);
if(user == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
const client = await ctx.service.verify.loginClient(body.client_id);
if(client == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
// await ctx.service.cSide.subscribe(user.dataValues, client.dataValues);
if (user && client && 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: client.dataValues.client_id,
client_secret: client.dataValues.client_secret,
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),
redirect: client.dataValues.client_url + '?' + result.headers.location.split('?')[1].split('&')[0].split('=')[1]
};
await ctx.service.cSide.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 = CSideController;

60
app/controller/Contest.js Normal file
View File

@@ -0,0 +1,60 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "Contest";
class ContestController extends Controller {
// 竞赛用户登录
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const name = this.decryptRequest(body.name);
const password = this.decryptRequest(body.password);
const user = await ctx.service.contest.loginUser(name);
if(user == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
const client = await ctx.service.verify.loginClient(body.client_id);
if(client == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (user && client && 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: client.dataValues.client_id,
client_secret: client.dataValues.client_secret,
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),
redirect: client.dataValues.client_url + '?' + result.headers.location.split('?')[1].split('&')[0].split('=')[1]
};
await ctx.service.contest.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 = ContestController;

55
app/controller/Exam.js Normal file
View File

@@ -0,0 +1,55 @@
'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;

View File

@@ -0,0 +1,55 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "ExamManager";
class ExamManagerController extends Controller {
// 北斗应用技术培训证书项目报名系统管理系统登录
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const name = this.decryptRequest(body.name);
const password = this.decryptRequest(body.password);
const user = await ctx.service.examManager.loginUser(name);
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-manager",
client_secret: "exam-manager",
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.examManager.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 = ExamManagerController;

View File

@@ -0,0 +1,47 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "Id2MVService";
class Id2MVServiceController extends Controller {
// 身份二要素核验
async checkIdCard() {
const { ctx } = this;
try {
// this.decryptRequest(body.idCard)
// this.decryptRequest(body.name)
const { idCard, name } = ctx.request.body;
const result = await ctx.service.id2MVService.checkIdCard(idCard, name);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result.status == "1" || result.status == "2" || result.status == "3") {
this.setResponse(ctx, 200, result.msg, "none");
} else if (result.status == "600") {
setTimeout(async () => {
await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/checkIdCard', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
idCard,
name
},
});
}, 3000);
this.setResponse(ctx, 200, '正在核验中', "none");
} else {
this.setResponse(ctx, 404, '核验失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "checkIdCard";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = Id2MVServiceController;

View File

@@ -0,0 +1,55 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "Insiders";
class InsidersController extends Controller {
// 管理系统登录
async login() {
const { ctx } = this;
try {
const body = ctx.request.body;
const name = this.decryptRequest(body.name);
const password = this.decryptRequest(body.password);
const user = await ctx.service.insiders.loginUser(name);
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: "insiders-manager",
client_secret: "insiders-manager",
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.insiders.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 = InsidersController;

View File

@@ -0,0 +1,68 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "SMSService";
class SMSServiceController extends Controller {
// 向手机发送短信验证码
async sendSMS() {
const { ctx } = this;
try {
const { phone } = ctx.request.body;
const result = await ctx.service.sMSService.sendSMS(this.decryptRequest(phone));
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result.status == "200") {
this.setResponse(ctx, 200, '验证码已发送', "none");
} else if (result.status == "600") {
setTimeout(async () => {
await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/sendSMS', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 6000,
data: {
phone
},
});
}, 3000);
this.setResponse(ctx, 200, '正在发送验证码', "none");
} else {
this.setResponse(ctx, 404, '请重试', "none");
}
} catch (error) {
const blockName = fileName + "--" + "sendSMS";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 校验短信验证码
async checkCode() {
const { ctx } = this;
try {
const body = ctx.request.body;
const phone = this.decryptRequest(body.phone);
const code = this.decryptRequest(body.code);
const result = await ctx.service.sMSService.checkCode(phone, code);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '验证通过', "none");
} else {
this.setResponse(ctx, 404, '验证失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "checkCode";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = SMSServiceController;

396
app/controller/verify.js Normal file
View File

@@ -0,0 +1,396 @@
'use strict';
const Controller = require('../core/base_controller');
const fileName = "verify";
const redisType = {
"B": "db0",
"C": "db1",
"M": "db2",
"T": "db5",
"E": "db6",
"A": "db7"
};
const modelType = {
"B": "bSide",
"C": "cSide",
"M": "insiders",
"T": "contest",
"E": "exam",
"A": "examManager"
};
const checkUid = async (thisPlace) => {
const ctx = thisPlace.ctx;
const uid = ctx.request.body.uid;
const token = ctx.request.header.authorization;
const redis = ctx.app.redis;
let rdmcode;
if (!uid) {
let allRedis = [];
Object.entries(redisType).forEach(([key, value]) => {
allRedis.push(redis.get(value).get(token));
});
const [buser, cuser, muser, tuser, euser, auser] = await Promise.all(allRedis);
rdmcode = buser || cuser || muser || tuser || euser || auser;
} else {
let decryptUid = thisPlace.decryptRequest(uid).split('?')[0];
const lastCode = decryptUid[decryptUid.length - 1];
rdmcode = await redis.get(redisType[lastCode]).get(token);
}
if (!rdmcode) {
return false;
}
return rdmcode;
};
class VerifyController extends Controller {
// 确定登录页面
async toLogin() {
const ctx = this.ctx;
try {
// 请求发出的源网址,适用于不同端用户登录不同域名网站
const origin_url = ctx.request.header.origin;
// 请求发出的具体网址,适用于不同端用户登录同一域名的网站
// const { origin_url } = ctx.request.body;
const result = await ctx.service.verify.toLogin(origin_url);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
const data = { redirect: result.dataValues.login_url + '?client_id=' + result.dataValues.temporary_id };
this.setResponse(ctx, 200, '跳转到登录页', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '未找到登录页', "none");
}
} catch (error) {
const blockName = fileName + "--" + "toLogin";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 确定登录状态
async loginStatus() {
const ctx = this.ctx;
try {
const body = ctx.request.body;
const uid = this.decryptRequest(body.data).split('?')[0];
const lastCode = uid[uid.length - 1];
const status = await ctx.app.redis.get(redisType[lastCode]).get(uid);
const client = await ctx.service.verify.loginClient(body.client_id);
if (client == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (status !== null && client.dataValues.login_url == ctx.request.header.referer.split('?')[0] && client.dataValues.client_id == JSON.parse(status).clientId) {
const timestamp = new Date().getTime();
const serverType = ctx.service[modelType[lastCode]];
await serverType.loginStatus(uid, 1);
const data = {
data: this.encryptRequest(uid + '?' + timestamp),
access_token: JSON.parse(status).accessToken,
refresh_token: JSON.parse(status).refreshToken,
redirect: client.dataValues.client_url + '?' + body.data,
};
this.setResponse(ctx, 200, '已登录', "deconstruction", data);
} else {
const serverType = ctx.service[modelType[lastCode]];
await serverType.loginStatus(uid, 0);
this.setResponse(ctx, 404, '未登录', "none");
}
} catch (error) {
const blockName = fileName + "--" + "loginStatus";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 退出登录
async unLogin() {
const ctx = this.ctx;
try {
let rdmcode = await checkUid(this);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const lastCode = uid[uid.length - 1];
const serverType = ctx.service[modelType[lastCode]];
if(modelType == 'B' || modelType == 'C' || modelType == 'M') await serverType.loginStatus(uid, 0);
const redisDbType = redisType[lastCode];
await ctx.app.redis.get(redisDbType).del(ctx.request.body.access_token);
await ctx.app.redis.get(redisDbType).del(ctx.request.body.refresh_token);
await ctx.app.redis.get(redisDbType).del(uid);
this.setResponse(ctx, 200, 'unLogin', "none");
} catch (error) {
const blockName = fileName + "--" + "unLogin";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取 token
async getToken() {
const ctx = this.ctx;
try {
const body = ctx.request.body;
let code = body.code, type = this.decryptRequest(body.type), codeObj, typeMark = type, accessToken, refreshToken;
if (type === 'code') {
codeObj = await ctx.app.redis.get('db3').get(code);
if (!codeObj) {
typeMark = 'passLogin';
code = this.decryptRequest(code).split('?')[0];
const redisDbType = redisType[code[code.length - 1]];
codeObj = await ctx.app.redis.get(redisDbType).get(code);
if (!codeObj) return codeObj;
accessToken = JSON.parse(codeObj).accessToken;
refreshToken = JSON.parse(codeObj).refreshToken;
codeObj = await ctx.app.redis.get(redisDbType).get(refreshToken);
}
} else {
type = type.split('?')[0];
const redisDbType = redisType[type[type.length - 1]];
codeObj = await ctx.app.redis.get(redisDbType).get(code);
}
if (!codeObj) {
this.setResponse(ctx, 402, '请重新登录', "none");
} else {
const client_id = JSON.parse(codeObj).clientId;
const client = await ctx.service.verify.getToken(client_id);
const timestamp = new Date().getTime();
const origin_url = ctx.request.header.origin;
if (client == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
} else if (client && typeMark == "passLogin") {
const data = {
data: this.encryptRequest(JSON.parse(codeObj).uid + '?' + timestamp),
access_token: accessToken,
refresh_token: refreshToken,
redirect: origin_url
};
this.setResponse(ctx, 200, '验证通过', "deconstruction", data);
} else if (client && typeMark !== "passLogin") {
const redisCode = { code, type };
const result = await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/user/token', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
grant_type: 'authorization_code',
code: JSON.stringify(redisCode),
client_id: client.dataValues.client_id,
client_secret: client.dataValues.client_secret,
redirect_uri: 'http://127.0.0.1:' + ctx.app.server.address().port + '/getToken/',
},
});
if (result.status == 200) {
const data = {
data: this.encryptRequest(result.data.access_token.uid + '?' + timestamp),
access_token: result.data.access_token.token,
refresh_token: result.data.refresh_token,
redirect: client.dataValues.client_id == "insiders-manager" ? origin_url : client.dataValues.client_url
}
this.setResponse(ctx, 200, '验证通过', "deconstruction", data);
} else {
this.setResponse(ctx, 402, '请重新登录', "none");
}
} else {
this.setResponse(ctx, 402, '请重新登录', "none");
}
}
} catch (error) {
const blockName = fileName + "--" + "getToken";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 验证token
async getMsg() {
const { ctx } = this;
try {
// 客户端实现
// 访问 /user/token 端口验证 accessToken,获取用户信息
let accessToken = {
access_token: ctx.request.header.authorization,
uid: this.decryptRequest(ctx.request.body.data).split('?')[0]
};
const result = await ctx.curl('http://127.0.0.1:' + ctx.app.server.address().port + '/user/authenticate', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
status: 'success',
access_token: JSON.stringify(accessToken)
},
});
if (result.status == 200) {
let uid = result.data.data.token.user.uid;
let lastCode = uid[uid.length - 1];
const userdata = await ctx.service[modelType[lastCode]].uidfinder(uid);
if (userdata == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
const timestamp = new Date().getTime();
const encryptData = this.encryptRequest(userdata.uid + '?' + timestamp);
if (userdata) {
const data = { data: encryptData };
this.setResponse(ctx, 200, '验证通过', "deconstruction", data);
} else {
this.setResponse(ctx, 401, 'token错误或过期', "none");
}
} else {
this.setResponse(ctx, 401, 'token错误或过期', "none");
}
} catch (error) {
const blockName = fileName + "--" + "getMsg";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
async show() {
const ctx = this.ctx;
try {
const data = { data: ctx.state.oauth };
this.setResponse(ctx, 200, '返回信息', "deconstruction", data);
} catch (error) {
const blockName = fileName + "--" + "show";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 获取用户基本信息
async getUser() {
const ctx = this.ctx;
try {
let rdmcode = await checkUid(this);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const lastCode = uid[uid.length - 1];
const userData = await ctx.service[modelType[lastCode]].getUser(uid);
if (userData == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (userData) {
const data = { userData };
this.setResponse(ctx, 200, '获取用户基本信息成功', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '获取用户基本信息失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "getUser";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 修改用户基本信息
async editUser() {
const ctx = this.ctx;
try {
let rdmcode = await checkUid(this);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const body = ctx.request.body;
const lastCode = uid[uid.length - 1];
const userData = await ctx.service[modelType[lastCode]].editUser(body.userMsg, uid);
if (userData == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (userData[0] != 0) {
this.setResponse(ctx, 200, '修改用户基本信息成功', "none");
} else {
this.setResponse(ctx, 404, '修改用户基本信息失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "editUser";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 修改密码
async changePassword() {
const ctx = this.ctx;
try {
let rdmcode = await checkUid(this);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const lastCode = uid[uid.length - 1];
const body = ctx.request.body;
const oldPassword = this.decryptRequest(body.passwordForm.oldPassword);
const newPassword = this.decryptRequest(body.passwordForm.newPassword);
const changeList = await ctx.service[modelType[lastCode]].changePassword(uid, oldPassword, newPassword);
if (changeList == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (changeList && changeList[0] !== 0) {
this.setResponse(ctx, 200, '修改密码成功', "none");
} else {
this.setResponse(ctx, 404, '修改密码失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "changePassword";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 重置密码
async resetPassword() {
const ctx = this.ctx;
try {
let rdmcode = await checkUid(this);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const lastCode = uid[uid.length - 1];
const body = ctx.request.body;
const model = this.decryptRequest(body.model);
const defaultPassword = this.decryptRequest(body.defaultPassword);
const password = this.decryptRequest(body.password);
const requestMsg = body.userMsg;
const resetType = body.resetType;
const resetList = await ctx.service[modelType[lastCode]].resetPassword(uid, model, defaultPassword, password, requestMsg, resetType);
if (resetList == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (resetList) {
this.setResponse(ctx, 200, '重置密码成功', "none");
} else {
this.setResponse(ctx, 404, '重置密码失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "resetPassword";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = VerifyController;