feat: 用户套餐,用户支付功能

This commit is contained in:
xiaojunnuo
2024-12-22 14:00:46 +08:00
parent d70e2b66a3
commit a019956698
69 changed files with 2071 additions and 738 deletions
+5
View File
@@ -8,6 +8,7 @@ export * from './util.hash.js';
export * from './util.merge.js';
export * from './util.cache.js';
export * from './util.string.js';
export * from './util.lock.js';
import { stringUtils } from './util.string.js';
import sleep from './util.sleep.js';
import { http, download } from './util.request.js';
@@ -24,6 +25,8 @@ import { domainUtils } from './util.domain.js';
import { optionsUtils } from './util.options.js';
import { nanoid } from 'nanoid';
import * as id from './util.id.js';
import { locker } from './util.lock.js';
import { mitter } from './util.mitter.js';
export const utils = {
sleep,
http,
@@ -41,4 +44,6 @@ export const utils = {
domain: domainUtils,
options: optionsUtils,
string: stringUtils,
locker,
mitter,
};
+4 -1
View File
@@ -3,7 +3,10 @@ import crypto from 'crypto';
function md5(data: string) {
return crypto.createHash('md5').update(data).digest('hex');
}
function sha256(data: string) {
return crypto.createHash('sha256').update(data).digest('hex');
}
export const hashUtils = {
md5,
sha256,
};
@@ -0,0 +1,51 @@
import { logger, utils } from './index.js';
export class Locker {
locked: Record<string, any> = {};
async execute(lockStr: string, callback: any) {
await this.lock(lockStr);
const timeoutId = setTimeout(() => {
logger.warn('Lock timeout,自动解锁', lockStr);
this.unlock(lockStr);
}, 20000);
try {
return await callback();
} finally {
clearTimeout(timeoutId);
this.unlock(lockStr);
}
}
async lock(str: string) {
const isLocked = this.isLocked(str);
if (isLocked) {
let count = 0;
while (true) {
await utils.sleep(100);
if (!this.isLocked(str)) {
break;
}
count++;
if (count > 20) {
throw new Error('Lock timeout');
}
}
}
this.locked[str] = true;
}
unlock(str: string) {
const isLocked = this.locked[str];
if (isLocked != null) {
return;
}
delete this.locked[str];
}
isLocked(str: string) {
return this.locked[str] ?? false;
}
}
export const locker = new Locker();
@@ -0,0 +1,2 @@
import mitt from 'mitt';
export const mitter = mitt();