Files

181 lines
7.1 KiB
JavaScript

const Controller = require('../core/base_controller');
const fileName = "User";
const fs = require('fs');
const path = require('path');
class UserController extends Controller {
// 信息完整性,合法性校验
async checkInfo() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const result = await ctx.service.user.checkInfo(uid);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result == 'OK') {
this.setResponse(ctx, 200, '信息校验完成', "none");
} else if (result == 'CNTEV') {
this.setResponse(ctx, 303, '信息校验中', "none");
} else {
this.setResponse(ctx, 404, '信息校验失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "checkInfo";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 用户照片是否存在判断
async checkPhoto() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const result = await ctx.service.user.checkPhoto(uid);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result.exist) {
ctx.set('Cache-Control', 'no-cache, no-store, must-revalidate');
ctx.set("Pragma", "no-cache");
ctx.set("Expires", "0");
this.setResponse(ctx, 200, '照片校验成功', "deconstruction", result);
} else {
this.setResponse(ctx, 404, '照片校验失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "checkPhoto";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 身份证二要素核验
async CNTEV() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const { user_name, identity_card } = ctx.request.body;
const result = await ctx.service.user.CNTEV(user_name, identity_card);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result == "1") {
this.setResponse(ctx, 200, '身份证二要素核验成功', "none");
} else if (result == "0") {
const check = await ctx.app.redis.get('db3').get(identity_card);
if (check == null) {
ctx.curl('http://127.0.0.1:' + 8011 + '/CNTEV', {
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
method: 'POST',
timeout: 3000,
data: {
user_name,
identity_card,
},
});
} else {
await ctx.service.user.updateCNTEV(user_name, identity_card, JSON.parse(check).real_check);
await ctx.app.redis.get('db3').del(identity_card);
}
this.setResponse(ctx, 303, '正在核验中', "none");
} else {
this.setResponse(ctx, 404, '身份证二要素核验失败,请修改正确后等待管理员审核', "none");
}
} catch (error) {
const blockName = fileName + "--" + "CNTEV";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 用户上传照片
async uploadPhoto() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const { files } = ctx.request;
const result = await ctx.service.user.uploadPhoto(uid, files);
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 + "--" + "uploadPhoto";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
} finally {
// 需要删除临时文件
await ctx.cleanupRequestFiles();
}
}
// 用户找回密码
async resetPassword() {
const { ctx } = this;
try {
const body = ctx.request.body;
// redis 存放短信验证码,过期时间为 10 分钟
const phone = this.decryptRequest(body.phone);
const redis = ctx.app.redis;
const codeResult = await redis.get('db3').get(phone);
if(!codeResult || JSON.parse(codeResult).status != '1') {
this.setResponse(ctx, 333, '短信验证码错误', "none");
return;
}
const salt = this.generateRandomString();
const updateForm = {
salt: this.encryptRequest(salt),
password: this.shaForm(this.decryptRequest(body.password), salt)
}
const result = await ctx.service.user.resetPassword(updateForm, phone);
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 + "--" + "resetPassword";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = UserController;