Commit 0e77d571 by Zllgogo

no message

parent 6e1fa95b
......@@ -7,6 +7,7 @@
"@alicloud/sms-sdk": "^1.1.6",
"@types/node": "^10.12.18",
"compression": "^1.7.4",
"crypto": "^1.0.1",
"express": "^4.17.1",
"express-async-handler": "^1.1.4",
"express-history-api-fallback": "^2.2.1",
......
......@@ -14,6 +14,7 @@ import { eccFormParam } from "../util/verificationParam";
import { ERRORENUM } from "../config/enum/errorEnum";
import { getMySqlMs, getToken, randomId } from "../tools/system";
import moment = require("moment");
import { sendVerificationCode } from "./mail";
//==登录登出
......@@ -37,7 +38,6 @@ export async function enterpriseLogin(phone:string, pwd:string) {
};
await operationalData(OPERATIONALDATATYPE.修改, TABLENAME.企业用户表, updateUserInfo, {uId:enterpriseInfo.uId});
let userInfo = {
userId:enterpriseInfo.uId,
userName:enterpriseInfo.userName,
......@@ -48,39 +48,74 @@ export async function enterpriseLogin(phone:string, pwd:string) {
return {dataInfo:userInfo};
}
/**
* 小程序登录改为邮箱认证--todo
* @param email 邮箱
* @param code 验证码
* @returns
*/
// export async function enterpriseLogin(email:string, code:string) {
// // let dbInfo = await selectData(OPERATIONALDATATYPE.查询单个, TABLENAME.企业基础信息表, {uscc}, ["eId"]);
export async function enterpriseEmailLogin(email: string, code: string) {
// let filesList = ["userName", "eId", "code", "uId", "email"];
// let enterpriseInfo = await selectData(OPERATIONALDATATYPE.查询单个, TABLENAME.企业用户表, {email}, filesList);
let filesList = ["userName", "eId", "code", "uId", "email", "codeMs"];
let enterpriseInfo = await selectData(OPERATIONALDATATYPE.查询单个, TABLENAME.企业用户表, { email }, filesList);
// if (!enterpriseInfo || !enterpriseInfo.eId) {
// throw new BizError(ERRORENUM.账号或密码错误);
// }
// if (enterpriseInfo.code != code) {
// throw new BizError(ERRORENUM.账号或密码错误);
// }
if (!enterpriseInfo || !enterpriseInfo.eId) {
throw new BizError(ERRORENUM.账号或密码错误);
}
// let updateUserInfo = {
// token : getToken(enterpriseInfo.uId),
// tokenMs : getMySqlMs()
// };
// await operationalData(OPERATIONALDATATYPE.修改, TABLENAME.企业用户表, updateUserInfo, {uId:enterpriseInfo.uId});
// 检查验证码是否存在
if (!enterpriseInfo.code || !enterpriseInfo.codeMs) {
throw new BizError(ERRORENUM.验证码不存在);
}
// let userInfo = {
// userId:enterpriseInfo.uId,
// userName:enterpriseInfo.userName,
// phemailone:enterpriseInfo.email,
// token:updateUserInfo.token,
// };
// 获取当前时间戳(毫秒)
const currentMs = Date.parse(getMySqlMs());
console.log("currentMs", currentMs);
// return {dataInfo:userInfo};
// }
// 检查验证码时效(假设5分钟过期)
if (currentMs - enterpriseInfo.codeMs > 5 * 60 * 1000) {
throw new BizError(ERRORENUM.验证码过期);
}
// 验证码校验
if (enterpriseInfo.code !== code) {
throw new BizError(ERRORENUM.code无效);
}
let updateUserInfo = {
token: getToken(enterpriseInfo.uId),
tokenMs: getMySqlMs()
};
await operationalData(OPERATIONALDATATYPE.修改, TABLENAME.企业用户表, updateUserInfo, { uId: enterpriseInfo.uId });
let userInfo = {
userId: enterpriseInfo.uId,
userName: enterpriseInfo.userName,
email: enterpriseInfo.email,
token: updateUserInfo.token,
};
return { dataInfo: userInfo };
}
/**
* 请求发送验证码到指定邮箱
* @param email 用户邮箱
* @returns
*/
export async function requestVerificationCode(email: string) {
try {
const code = await sendVerificationCode(email);
console.log(`验证码已发送到邮箱: ${email}, ${code}`);
// 可以将验证码存储在会话中或前端缓存中,以便后续验证
} catch (error) {
console.error('发送验证码失败:', error);
throw new BizError(ERRORENUM.验证码发送失败);
}
return { result: "验证码已发送到邮箱" };
}
export async function enterpriseLogout(uId) {
let filesList = ["userName", "eId", "pwd", "uId"];
......@@ -91,7 +126,6 @@ export async function enterpriseLogout(uId) {
};
await operationalData(OPERATIONALDATATYPE.修改, TABLENAME.企业用户表, updateUserInfo, {uId:enterpriseInfo.uId});
return {isSuccess:true};
}
......@@ -126,7 +160,6 @@ export async function enterpriseByPhone(phone:string) {
//===首页
export async function homePage(eId:string) {
let filesList = ["enterpriseName", "eId"];
let enterpriseInfo = await selectData(OPERATIONALDATATYPE.查询单个, TABLENAME.企业基础信息表, {eId}, filesList);
......
import { OPERATIONALDATATYPE, TABLENAME } from "../config/enum/dbEnum";
import { operationalData } from "../data/operationalData";
import { getMySqlMs } from "../tools/system";
const nodemailer = require('nodemailer');
// 创建一个 SMTP 客户端配置(动态生成)
function createTransporter(user: string, pass: string) {
return nodemailer.createTransport({
host: 'smtp.qq.com', // 邮箱服务器地址
port: 465, // 端口号
secure: true, // 是否使用安全传输协议
auth: {
user, // 发件人邮箱地址
pass // 发件人邮箱授权码
}
});
}
// 发送邮件的函数(支持动态发件人配置)
async function sendEmail(to: string,subject: string,text: string,user: string,pass: string): Promise<void> {
const transporter = createTransporter(user, pass);
let mailOptions = {
from: user, // 动态设置发件人地址
to: to, // 收件人地址
subject: subject, // 邮件主题
text: text // 邮件正文
};
try {
await transporter.sendMail(mailOptions);
console.log('邮件发送成功');
} catch (error) {
console.error('邮件发送失败:', error);
throw error; // 抛出错误以便调用方处理
}
}
/**
* 根据收件人邮箱地址选择合适的发件人邮箱
* @param email 收件人邮箱地址
* @returns 发件人邮箱地址和授权码
*/
function getSenderInfo(email: string): { user: string; pass: string } {
// 根据收件人邮箱地址选择合适的发件人邮箱
// 这里可以根据业务需求进行扩展,例如根据域名选择不同的发件人
if (email.endsWith('@example.com')) {
return { user: 'example_sender@example.com', pass: 'example_password' };
} else if (email.endsWith('@anotherdomain.com')) {
return { user: 'another_sender@anotherdomain.com', pass: 'another_password' };
} else {
return { user: '1685675085@qq.com', pass: 'hppnsfvnzzhlbdfh' }; // 默认发件人邮箱
// return { user: 'wdc18234720432@163.com', pass: 'GYhamUicpFavmnGY' };
}
}
/**
* 发送验证码到指定邮箱
* @param email 用户邮箱
*/
function randomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
export async function sendVerificationCode(email: string): Promise<{ success: boolean; message: string; code?: string }> {
try {
// 生成6位数字验证码
const code = String(randomInt(100000, 999999));
// 获取发件人信息
const { user, pass } = getSenderInfo(email);
// 构造邮件内容
const mailOptions = {
to: email,
subject: "羽翼小程序的专属验证码",
text: `您的验证码为:${code},5分钟内有效,请妥善保管。`
};
// 发送邮件(支持动态发件人配置)
await sendEmail(mailOptions.to, mailOptions.subject, mailOptions.text, user, pass);
// 更新数据库,存储验证码和发送时间
let updateResult = await operationalData(OPERATIONALDATATYPE.修改,TABLENAME.企业用户表,{ code, codeMs: getMySqlMs() },{ email });
if (!updateResult) {
return { success: false, message: '数据库更新失败' };
}
return { success: true, message: '验证码发送成功', code };
} catch (error) {
console.error('验证码发送失败:', error);
return { success: false, message: '验证码发送失败' };
}
}
/**
*备份的发送验证码到指定邮箱
*/
// import { OPERATIONALDATATYPE, TABLENAME } from "../config/enum/dbEnum";
// import { operationalData } from "../data/operationalData";
// import { getMySqlMs } from "../tools/system";
// const nodemailer = require('nodemailer');
// // 创建一个 SMTP 客户端配置
// let transporter = nodemailer.createTransport({
// host: 'smtp.qq.com', // 邮箱服务器地址
// port: 465, // 端口号
// secure: true, // 是否使用安全传输协议
// auth: {
// user: '1685675085@qq.com', // 发件人邮箱地址
// pass: 'hppnsfvnzzhlbdfh' // 发件人邮箱授权码
// }
// })
// // 发送邮件的函数
// async function sendEmail(to: string, subject: string, text: string) {
// let mailOptions = {
// from: '1685675085@qq.com', // 发件人地址
// to: to, // 收件人地址
// subject: subject, // 邮件主题
// text: text // 邮件正文
// };
// try {
// await transporter.sendMail(mailOptions);
// console.log('邮件发送成功');
// return text;
// }
// catch (error) {
// console.error('邮件发送失败:', error);
// return error;
// }
// }
// /**
// * 发送验证码到指定邮箱
// * @param email 用户邮箱
// */
// function randomInt(min: number, max: number): number {
// return Math.floor(Math.random() * (max - min + 1)) + min;
// }
// export async function sendVerificationCode(email: string): Promise<{}> {
// // 生成6位数字验证码
// const code = String(randomInt(100000, 999999));
// // 构造邮件内容
// const mailOptions = {
// to: email,
// subject: "羽翼小程序的专属验证码",
// text: `您的验证码为:${code},5分钟内有效,请妥善保管。`
// };
// // 发送邮件
// await sendEmail(mailOptions.to, mailOptions.subject, mailOptions.text);
// // 更新数据库,存储验证码和发送时间
// await operationalData(OPERATIONALDATATYPE.修改, TABLENAME.企业用户表, { code, codeMs: getMySqlMs() }, { email });
// return code;
// }
......@@ -27,6 +27,9 @@ export enum ERRORENUM {
数据不存在,
不可重复提交,
存在重复的年份数据,
验证码过期,
验证码发送失败,
验证码不存在,
}
export enum ERRORCODEENUM {
......
......@@ -9,6 +9,9 @@ import { checkUser } from '../middleware/user';
export function setRouter(httpServer) {
//登录登出
httpServer.post('/xcx/enterprise/login', asyncHandler(login));
httpServer.post('/xcx/enterprise/email/login', asyncHandler(emailLogin));// 邮箱验证码登录
httpServer.post('/xcx/enterprise/request/verification/code', asyncHandler(requestVerificationCode));// 发送验证码
/**登出 */
httpServer.post('/xcx/enterprise/logout', checkUser, asyncHandler(logout));
/**首页 */
......@@ -139,6 +142,29 @@ async function login(req, res) {
res.success(result);
}
/**
* 邮箱验证码登录
* @param req
* @param res
*/
async function emailLogin(req, res) {
let {email, code } = req.body
let result = await enterpriseInfoBiz.enterpriseEmailLogin(email, code );
res.success(result);
}
/**
* 发送验证码
* @param req
* @param res
*/
async function requestVerificationCode(req, res) {
let {email } = req.body
let result = await enterpriseInfoBiz.requestVerificationCode(email );
res.success(result);
}
/**
*
......
......@@ -44,7 +44,7 @@ function getEnumItf(enumCof) {
dataList.push({key, value:enumCof[key]});
}
}
console.log(dataList);
// console.log(dataList);
res.success({dataList});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment