Files
bd-training-exam/app/service/User.js

183 lines
7.5 KiB
JavaScript

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
const fs = require('fs');
const path = require('path');
const fileName = "User";
class UserService extends Service {
// 信息完整性,合法性校验
async checkInfo(uid) {
const { ctx } = this;
try {
let attributes = ['user_name', 'identity_card', 'real_check', 'status'],
where = { uid },
dbOptions = { attributes, where };
const result = await ctx.model.Examinee.findOne(dbOptions);
if (result == null || result.dataValues.status == 2 || result.dataValues.real_check == 2) return false;
let type = 'OK';
const { user_name, identity_card, real_check } = result.dataValues;
if (real_check == 0) {
type = 'CNTEV';
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);
}
}
return type;
} catch (error) {
const blockName = fileName + "--" + "checkInfo";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户照片是否存在判断
async checkPhoto(uid) {
const { ctx } = this;
try {
let attributes = ['identity_card'],
where = { uid },
dbOptions = { attributes, where };
const result = await ctx.model.Examinee.findOne(dbOptions);
if (result == null) return false;
const filePath = path.join(__dirname, `../public/userPhoto/${ result.dataValues.identity_card }.jpg`);
// const filePath = `/disk/a/userPhoto/${ result.dataValues.identity_card }.jpg`;
const fileExists = fs.existsSync(filePath);
let exist = false;
if (fileExists) {
// 文件存在
exist = true;
} else {
// 文件不存在
let attributes = ['identity_card'],
where = { user_uid: uid },
order = [['created_at', 'DESC']],
limit = 1,
offset = 1,
dbOptions = { attributes, where, order, limit, offset };
const oldIdentityCard = await ctx.model.Audit.findOne(dbOptions);
if(!oldIdentityCard) {
exist = false;
} else {
const oldFilePath = path.join(__dirname, `../public/userPhoto/${ oldIdentityCard.dataValues.identity_card }.jpg`);
// const oldFilePath = `/disk/a/userPhoto/${ oldIdentityCard.dataValues.identity_card }.jpg`;
const fileExists = fs.existsSync(oldFilePath);
if (fileExists) {
// 文件存在
fs.renameSync(oldFilePath, filePath);
// 文件改名
exist = true;
} else {
// 文件不存在
exist = false;
}
}
}
// 本机服务器地址
// 'http://127.0.0.1:8019'
// 'http://192.168.31.28:8019'
// 公司服务器地址
// 'http://123.57.205.46:8019'
// 'http://certlogin.hwasmart.com//bdTrainingExam'
return { exist, photoUrl: exist ? 'http://127.0.0.1:8019' + `/public/userPhoto/${ result.dataValues.identity_card }.jpg?time=${ new Date().getTime() }` : '#' };
} catch (error) {
const blockName = fileName + "--" + "checkPhoto";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 身份证二要素核验
async CNTEV(user_name, identity_card) {
const { ctx } = this;
try {
let attributes = ['real_check'],
where = { user_name, identity_card },
dbOptions = { attributes, where };
const result = await ctx.model.Examinee.findOne(dbOptions);
if (result == null) return false;
return result.dataValues.real_check;
} catch (error) {
const blockName = fileName + "--" + "CNTEV";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 身份证二要素核验状态更新
async updateCNTEV(user_name, identity_card, real_check) {
const { ctx } = this;
try {
const where = { user_name, identity_card };
await ctx.model.Examinee.update({ real_check }, { where });
await ctx.model.Audit.update({ status: real_check }, { where });
} catch (error) {
const blockName = fileName + "--" + "CNTEV";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户上传照片接口
async uploadPhoto(uid, files) {
const { ctx } = this;
try {
let attributes = ['identity_card'],
where = { uid },
dbOptions = { attributes, where };
const result = await ctx.model.Examinee.findOne(dbOptions);
if (result == null) return false;
// 重命名用户上传的文件并保存到证件照文件夹中
files[0].filename = result.dataValues.identity_card + '.jpg';
const { filename, filepath } = files[0];
const uploadDir = path.join(__dirname, '../public/userPhoto'); // 设置文件保存目录
// const uploadDir = '/disk/a/userPhoto'; // 设置服务器文件保存目录
const newPath = path.join(uploadDir, filename); // 构造新文件路径
// 确保上传目录存在
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir, { recursive: true });
}
// 使用 copyFileSync 复制文件,而不是 renameSync
fs.copyFileSync(filepath, newPath);
// 删除临时文件
fs.unlinkSync(filepath);
return true;
} catch (error) {
const blockName = fileName + "--" + "uploadPhoto";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
// 用户找回密码接口
async resetPassword(updateForm, phone) {
const { ctx } = this;
try {
const result = await ctx.model.Examinee.update(updateForm, { where: { phone } });
return result[0] == 0 ? false : result;
} catch (error) {
const blockName = fileName + "--" + "resetPassword";
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
return 'error';
}
}
}
module.exports = UserService;