@yoooloo42/bean 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +0 -2
- package/package.json +1 -1
- package/src/libs/Email.js +0 -128
- package/src/libs/FileSaver.js +0 -49
- package/src/libs/index.js +0 -8
package/index.js
CHANGED
package/package.json
CHANGED
package/src/libs/Email.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import nodemailer from 'nodemailer';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Node.js 项目中发送电子邮件的函数
|
|
5
|
-
* @param {string} to - 收件人邮箱地址
|
|
6
|
-
* @param {string} subject - 邮件主题
|
|
7
|
-
* @param {string} htmlContent - 邮件的 HTML 内容(正文)
|
|
8
|
-
* @param {string} [textContent] - 邮件的纯文本内容(可选,作为 HTML 无法显示的备用)
|
|
9
|
-
*/
|
|
10
|
-
async function sendEmail(to, subject, htmlContent, textContent = '') {
|
|
11
|
-
// ⚠️ 1. 配置 Transporter (SMTP 服务器设置)
|
|
12
|
-
//
|
|
13
|
-
// 如果使用 Gmail:
|
|
14
|
-
// - 您需要启用“两步验证”并生成一个“应用专用密码”(App Password),而不是使用您的主账户密码。
|
|
15
|
-
// - 否则,Google 可能会阻止登录。
|
|
16
|
-
const transporter = nodemailer.createTransport({
|
|
17
|
-
// host: 'smtp.exmail.qq.com', // 替换为你的 SMTP 服务器地址 (例如: 'smtp.gmail.com', 'smtp-mail.outlook.com')
|
|
18
|
-
// port: 465, // 常用端口: 465 (安全连接) 或 587 (TLS)
|
|
19
|
-
// secure: true, // true 为 465 端口, false 为其它端口
|
|
20
|
-
service: '126',
|
|
21
|
-
auth: {
|
|
22
|
-
// user: 'your_email@example.com', // 替换为你的发件箱地址
|
|
23
|
-
user: 'lyxdrwhy000@126.com',
|
|
24
|
-
// pass: 'your_app_password' // 替换为你的邮箱密码或“应用专用密码”
|
|
25
|
-
pass: 'JSQNIEMQCCPCVSJW'
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// 2. 构造邮件内容对象
|
|
30
|
-
const mailOptions = {
|
|
31
|
-
// from: '"Your Name" <your_email@example.com>', // 发件人信息 (会显示在收件箱中)
|
|
32
|
-
from: `"Stellarium - 企业应用集成平台" <lyxdrwhy000@126.com>`,
|
|
33
|
-
to: to, // 收件人地址
|
|
34
|
-
subject: subject, // 邮件主题
|
|
35
|
-
html: htmlContent, // 邮件 HTML 正文
|
|
36
|
-
text: textContent // 邮件纯文本正文 (可选)
|
|
37
|
-
// 附件可以在这里添加:
|
|
38
|
-
/* attachments: [
|
|
39
|
-
{
|
|
40
|
-
filename: 'report.pdf',
|
|
41
|
-
path: '/path/to/your/report.pdf'
|
|
42
|
-
}
|
|
43
|
-
] */
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
// 3. 发送邮件
|
|
48
|
-
let info = await transporter.sendMail(mailOptions);
|
|
49
|
-
|
|
50
|
-
console.log('邮件发送成功:');
|
|
51
|
-
console.log(' 收件人:', to);
|
|
52
|
-
console.log(' 主题:', subject);
|
|
53
|
-
console.log(' Message ID:', info.messageId);
|
|
54
|
-
|
|
55
|
-
return { success: true, messageId: info.messageId };
|
|
56
|
-
|
|
57
|
-
} catch (error) {
|
|
58
|
-
console.error('邮件发送失败:', error);
|
|
59
|
-
return { success: false, error: error.message };
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 生成指定长度的随机数字验证码
|
|
65
|
-
* @param {number} length - 验证码长度 (默认 6 位)
|
|
66
|
-
* @returns {string} - 随机数字字符串
|
|
67
|
-
*/
|
|
68
|
-
function vercode(length = 6) {
|
|
69
|
-
let code = '';
|
|
70
|
-
// 确保第一位不是 0,除非长度只有一位
|
|
71
|
-
code += Math.floor(Math.random() * 9) + 1;
|
|
72
|
-
|
|
73
|
-
for (let i = 1; i < length; i++) {
|
|
74
|
-
code += Math.floor(Math.random() * 10);
|
|
75
|
-
}
|
|
76
|
-
return code;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 发送包含验证码的邮件
|
|
81
|
-
* * @param {string} recipientEmail - 收件人邮箱地址
|
|
82
|
-
* @param {number} [codeLength=6] - 验证码长度
|
|
83
|
-
* @param {number} [expirationMinutes=5] - 验证码有效时间(分钟)
|
|
84
|
-
* @returns {Promise<{success: boolean, code?: string, error?: string}>} - 包含发送结果和生成的验证码
|
|
85
|
-
*/
|
|
86
|
-
async function sendEmailVercode(recipientEmail, codeLength = 6, expirationMinutes = 5) {
|
|
87
|
-
|
|
88
|
-
// 1. 生成验证码
|
|
89
|
-
const verificationCode = vercode(codeLength);
|
|
90
|
-
|
|
91
|
-
// 2. 构造邮件内容
|
|
92
|
-
const subject = `您的验证码:${verificationCode},请注意妥善保管,切勿泄露`;
|
|
93
|
-
|
|
94
|
-
const htmlContent = `
|
|
95
|
-
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: auto; border: 1px solid #ddd; padding: 20px;">
|
|
96
|
-
<h2 style="color: #333;">验证码通知</h2>
|
|
97
|
-
<p>您正在进行Email验证码操作,请在页面中输入以下验证码:</p>
|
|
98
|
-
<div style="background-color: #f5f5f5; padding: 15px; text-align: center; border-radius: 5px; margin: 20px 0;">
|
|
99
|
-
<span style="font-size: 30px; font-weight: bold; color: #007bff;">${verificationCode}</span>
|
|
100
|
-
</div>
|
|
101
|
-
<p style="color: #e44d26;">请注意:该验证码将在 ${expirationMinutes} 分钟内失效。</p>
|
|
102
|
-
<p style="font-size: 12px; color: #999;">如果不是您本人操作,请忽略此邮件。</p>
|
|
103
|
-
</div>
|
|
104
|
-
`;
|
|
105
|
-
|
|
106
|
-
// 3. 调用核心发送函数
|
|
107
|
-
const sendResult = await sendEmail(recipientEmail, subject, htmlContent);
|
|
108
|
-
|
|
109
|
-
// 4. 返回结果和验证码
|
|
110
|
-
if (sendResult.success) {
|
|
111
|
-
return {
|
|
112
|
-
success: true,
|
|
113
|
-
vercode: verificationCode
|
|
114
|
-
};
|
|
115
|
-
} else {
|
|
116
|
-
return {
|
|
117
|
-
success: false,
|
|
118
|
-
error: sendResult.error || '邮件发送失败'
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const bean = {
|
|
124
|
-
sendEmail,
|
|
125
|
-
vercode,
|
|
126
|
-
sendEmailVercode
|
|
127
|
-
}
|
|
128
|
-
export default bean
|
package/src/libs/FileSaver.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import * as XLSX from 'xlsx';
|
|
2
|
-
import FileSaver from 'file-saver';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 将 JSON 数组数据导出为 Excel 文件
|
|
6
|
-
* @param {Array<Object>} json - 要导出的数据数组 (el-table 的 :data 绑定的数据)
|
|
7
|
-
* @param {Array<string>} header - 表格的表头(中文名)
|
|
8
|
-
* @param {Array<string>} keys - 对应表头的数据字段名(英文键名)
|
|
9
|
-
* @param {string} filename - 导出的文件名
|
|
10
|
-
*/
|
|
11
|
-
function jsonToExcel({
|
|
12
|
-
json,
|
|
13
|
-
header,
|
|
14
|
-
keys,
|
|
15
|
-
filename = 'excel-file',
|
|
16
|
-
}) {
|
|
17
|
-
// 1. 转换数据格式
|
|
18
|
-
const data = json.map(item => keys.map(key => item[key]));
|
|
19
|
-
|
|
20
|
-
// 2. 将表头和数据组合
|
|
21
|
-
const aoa = [header, ...data];
|
|
22
|
-
|
|
23
|
-
// 3. 创建工作簿和工作表
|
|
24
|
-
const ws = XLSX.utils.aoa_to_sheet(aoa);
|
|
25
|
-
const wb = XLSX.utils.book_new();
|
|
26
|
-
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
|
|
27
|
-
|
|
28
|
-
// 4. 生成 Excel 文件
|
|
29
|
-
const wbout = XLSX.write(wb, {
|
|
30
|
-
bookType: 'xlsx',
|
|
31
|
-
bookSST: true,
|
|
32
|
-
type: 'array'
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// 5. 保存文件
|
|
36
|
-
try {
|
|
37
|
-
FileSaver.saveAs(
|
|
38
|
-
new Blob([wbout], { type: 'application/octet-stream' }),
|
|
39
|
-
`${filename}.xlsx`
|
|
40
|
-
);
|
|
41
|
-
} catch (e) {
|
|
42
|
-
if (typeof console !== 'undefined') console.log(e, wbout);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const bean = {
|
|
47
|
-
jsonToExcel
|
|
48
|
-
}
|
|
49
|
-
export default bean
|