liufumingren bd2363bf00 chore: 初始化仓库(backend / frontend / extension 三块)
- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权;
  已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF),
  开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。
  单测 91 个通过(不需要本机数据库)。
- frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 /
  首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。
- extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。
- 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、
  .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
2026-10-17 08:50:32 +08:00

90 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Service } = require('egg');
/**
* 邮件发送(注册验证码 / 重置密码链接)
*
* 两种模式:
* - **开发模式**(`config.email.devMode`,即没配 SMTP_HOST 时):不发真邮件,
* 把验证码/重置链接写进服务端日志,并在返回值里回传,让页面能提示出来(仅本地/测试用)。
* - **SMTP 模式**:配好 SMTP_HOST / SMTP_PORT / SMTP_USER / SMTP_PASSWORD(+ SMTP_FROM)后自动启用,
* 用 nodemailer 真发信(依赖懒加载,开发模式不需要它)。
*/
class MailService extends Service {
/**
* 发送验证码邮件
* @param {string} email 收件邮箱
* @param {string} code 6 位验证码
* @param {object} options { ttl 有效期(秒) }
* @return {Promise<{devMode:boolean, code?:string, failed?:boolean, messageId?:string}>}
*/
async sendVerifyCode(email, code, { ttl } = {}) {
const cfg = this.app.config.email;
const site = this.app.config.siteName;
const minutes = Math.max(Math.round((ttl || cfg.codeTtl) / 60), 1);
const text = `你的 ${site} 注册验证码是 ${code},${minutes} 分钟内有效。\n`
+ '如果不是你本人操作,忽略本邮件即可。';
if (cfg.devMode) {
this.ctx.logger.info('[mail][dev] 未配置 SMTP,验证码不发真邮件 → %s : %s', email, code);
return { devMode: true, code };
}
const res = await this.deliver({ to: email, subject: `${site} 注册验证码`, text });
return Object.assign({ devMode: false }, res);
}
/**
* 发送重置密码链接邮件
* @param {string} email 收件邮箱
* @param {string} url 重置地址(绝对地址,带 token)
* @param {object} options { ttl 链接有效期(秒) }
* @return {Promise<{devMode:boolean, url?:string, failed?:boolean, messageId?:string}>}
*/
async sendResetLink(email, url, { ttl } = {}) {
const cfg = this.app.config.email;
const site = this.app.config.siteName;
const minutes = Math.max(Math.round((ttl || cfg.resetTtl) / 60), 1);
const text = `我们收到了重置 ${site} 密码的请求,请打开下面的链接设置新密码(${minutes} 分钟内有效):\n`
+ `${url}\n`
+ '如果不是你本人操作,忽略本邮件即可,你的密码不会被修改。';
if (cfg.devMode) {
this.ctx.logger.info('[mail][dev] 未配置 SMTP,重置链接不发真邮件 → %s : %s', email, url);
return { devMode: true, url };
}
const res = await this.deliver({ to: email, subject: `${site} 重置密码`, text });
return Object.assign({ devMode: false }, res);
}
/**
* 真正发信(SMTP 模式)
* @param {object} mail { to, subject, text }
* @return {Promise<{messageId?:string, failed?:boolean, message?:string}>}
*/
async deliver({ to, subject, text }) {
const cfg = this.app.config.email;
try {
// 懒加载:开发模式(默认)下不依赖这个包
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: cfg.smtp.host,
port: cfg.smtp.port,
secure: cfg.smtp.secure,
auth: cfg.smtp.user ? { user: cfg.smtp.user, pass: cfg.smtp.password } : undefined,
});
const info = await transporter.sendMail({ from: cfg.from, to, subject, text });
this.ctx.logger.info('[mail] 邮件已发送 %s (%s)', to, info.messageId || '');
return { messageId: info.messageId };
} catch (err) {
this.ctx.logger.error('[mail] 邮件发送失败 %s : %s', to, err.message);
return { failed: true, message: err.message };
}
}
}
module.exports = MailService;