Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Dec 29, 2024
1 parent 13973ab commit df7f5b4
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions utils/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";

const pwdHash = new PasswordHash();
const prisma = new PrismaClient();
export { prisma };
const s3config = {
endpoint: await configManager.getConfig("s3.endpoint"),
region: await configManager.getConfig("s3.region"),
Expand All @@ -22,7 +21,7 @@ logger.debug(s3config);

const s3 = new S3Client(s3config);

export const S3update = async (name, file) => {
async function S3update(name, file) {
try {
const fileContent = fs.readFileSync(file);
const command = new PutObjectCommand({
Expand All @@ -39,41 +38,44 @@ export const S3update = async (name, file) => {
} catch (err) {
logger.error("S3 update Error:", err);
}
};
}

function md5(data) {
return crypto.createHash("md5").update(data).digest("base64");
}

export const md5 = (data) =>
crypto.createHash("md5").update(data).digest("base64");
function hash(data) {
return pwdHash.hashPassword(data);
}

export const hash = (data) => pwdHash.hashPassword(data);
export const checkhash = (pwd, storeHash) =>
pwdHash.checkPassword(pwd, storeHash);
function checkhash(pwd, storeHash) {
return pwdHash.checkPassword(pwd, storeHash);
}

export const userpwTest = (pw) =>
/^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+){6,16}$/.test(pw);
function userpwTest(pw) {
return /^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+){6,16}$/.test(pw);
}

export const emailTest = (email) =>
/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(email);
function emailTest(email) {
return /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(email);
}

export const phoneTest = (phone) => /^1[3456789]\d{9}$/.test(phone);
function phoneTest(phone) {
return /^1[3456789]\d{9}$/.test(phone);
}

export const msg_fail = { status: "fail", msg: "请再试一次19" };
const msg_fail = { status: "fail", msg: "请再试一次19" };

export const randomPassword = (len = 12) => {
function randomPassword(len = 12) {
const chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
const maxPos = chars.length;
const password = Array.from({ length: len - 4 }, () =>
chars.charAt(Math.floor(Math.random() * maxPos))
).join("");
return `${password}@Aa1`;
};

export const jwtToken = (data) => {
const token = jwt.sign(data, "test");
logger.debug(token);
return token;
};
}

export const generateJwt = async (json) => {
async function generateJwt(json) {
try {
const secret = await configManager.getConfig("security.jwttoken");
logger.debug(secret);
Expand All @@ -85,9 +87,9 @@ export const generateJwt = async (json) => {
logger.error("Error generating JWT:", error);
throw error;
}
};
}

export const isJSON = (str) => {
function isJSON(str) {
if (typeof str !== "string") return false;
try {
const obj = JSON.parse(str);
Expand All @@ -96,4 +98,20 @@ export const isJSON = (str) => {
logger.error("error:", str, e);
return false;
}
}

export {
prisma,
S3update,
md5,
hash,
checkhash,
userpwTest,
emailTest,
phoneTest,
msg_fail,
randomPassword,
generateJwt,
isJSON,
};

0 comments on commit df7f5b4

Please sign in to comment.