2024-10-15 18:53:09 +08:00

98 lines
2.0 KiB
TypeScript

import { UserApp } from '@/module/get-user-app.ts';
import { app } from '../../app.ts';
import { redis } from '@/module/redis/redis.ts';
import { CustomError } from '@abearxiong/router';
import fs from 'fs';
import { useFileStore } from '@abearxiong/use-file-store';
const fileStore = useFileStore('upload');
app
.route({
path: 'app',
key: 'list',
})
.define(async (ctx) => {
const keys = await redis.keys('user:app:*');
// const keys = await redis.keys('user:app:exist:*');
// const data = await redis.mget(...keys);
ctx.body = {
// data: data,
keys,
};
})
.addTo(app);
app
.route({
path: 'app',
key: 'delete',
})
.define(async (ctx) => {
const { user, app } = ctx.query;
try {
const userApp = new UserApp({ user, app });
await userApp.clearCacheData();
} catch (error) {
console.error(error);
throw new CustomError('删除失败');
}
ctx.body = 'successfully';
})
.addTo(app);
app
.route({
path: 'app',
key: 'deleteAll',
})
.define(async (ctx) => {
const keys = await redis.keys('user:app:*');
for (const key of keys) {
await redis.set(key, '', 'EX', 1);
}
ctx.body = {
keys,
};
})
.addTo(app);
app
.route({
path: 'app',
key: 'clear',
})
.define(async (ctx) => {
const keys = await redis.keys('user:app:*');
await redis.del(...keys);
fs.rmSync(fileStore, { recursive: true });
ctx.body = {
keys,
};
})
.addTo(app);
app
.route({
path: 'app',
key: 'get',
})
.define(async (ctx) => {
const { user, app } = ctx.query.data || {};
if (!user || !app) {
if (!user) {
throw new CustomError('user is required');
}
if (!app) {
throw new CustomError('app is required');
}
}
const userApp = new UserApp({ user, app });
const cache = await userApp.getCache();
if (!cache) {
throw new CustomError('Not Found App');
}
ctx.body = cache;
})
.addTo(app);