| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import os from 'os';
- import fs from 'fs';
- import path from 'path';
- // 一些常用的给工具函数
- // 1:------------ 获取本机器192xxxx地址
- export const getLocalIP = () => {
- const interfaces = os.networkInterfaces();
- for (let interfaceName in interfaces) {
- const interfaceList = interfaces[interfaceName];
- if (interfaceList) {
- for (let i = 0; i < interfaceList.length; i++) {
- const alias = interfaceList[i];
- if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
- return alias.address;
- }
- }
- }
- }
- return '0.0.0.0';
- };
- import svgCaptcha from 'svg-captcha';
- import dayjs from 'dayjs';
- // 2:------------ 生成验证码
- export const generateCaptcha = () => {
- const res = svgCaptcha.create({
- size: 4, // 验证码长度
- noise: 2, // 干扰线条数
- color: true, // 验证码字体颜色
- background: '#f0f0f0', // 背景颜色
- ignoreChars: '0oO1ilI', // 排除容易混淆的字符
- width: 120,
- height: 40,
- });
- return res;
- };
- //3:------------ 获取请求者ip 用来确定验证码是否正确
- export const ipLocResFu = (req: any) => {
- // 获取IP,并处理可能的IPv6格式
- let rawIp = req.socket.remoteAddress;
- // 简单的处理,如果包含`::ffff:`则去除,否则直接使用
- const clientIp = rawIp && rawIp.replace(/^.*:/, '');
- // console.log('客户端IP:', clientIp);
- return clientIp;
- };
- //4:------------ 追加内容写入文件
- export const AddTxtFileFu = (nowPath: any, fileName: string, content: string) => {
- const filePath = path.join(nowPath, fileName);
- // 在要追加的内容末尾加上换行符 `\n`
- const dataToAppend = dayjs().format('YYYY-MM-DD HH:mm:ss') + '------' + content + '\n';
- fs.appendFile(filePath, dataToAppend, 'utf8', (err) => {
- if (err) {
- return;
- }
- });
- };
|