Feat: Wallpaper API

随机壁纸 API
This commit is contained in:
2022-04-11 00:56:10 +08:00
parent c2999ea5e7
commit e381eb9598
8 changed files with 102 additions and 23 deletions
+7 -6
View File
@@ -1,12 +1,12 @@
import { Api, Get, Query, useContext } from '@midwayjs/hooks';
import fetch from 'isomorphic-unfetch';
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
import fetch from "isomorphic-unfetch";
import { client } from '../utils/redis';
import { client } from "../utils/redis";
export default Api(
Get(),
Query<{ info: undefined }>(),
async (ddd) => {
Query<{ info?: string }>(),
async () => {
const ctx = useContext();
// 增加使用数量
@@ -51,11 +51,12 @@ export default Api(
// 没有 info,跳转图片地址
if (!("info" in ctx.query)) {
ctx.status = 302;
ctx.set('location', image.url);
ctx.set("location", image.url);
}
return {
code: 1,
msg: "Success",
data: image,
cached: cache.length ? true : false
};
+18 -15
View File
@@ -2,19 +2,22 @@ import { Api, Get } from '@midwayjs/hooks';
import { client } from '../utils/redis';
export default Api(Get(), async (repo: string) => {
const neteaseStat = await client.get("api-next:stat:netease");
const bingStat = await client.get("api-next:stat:bing");
export default Api(
Get(),
async () => {
const neteaseStat = await client.get("api-next:stat:netease");
const bingStat = await client.get("api-next:stat:bing");
return {
code: 1,
data: {
wallpaper: -1,
netease: Number(neteaseStat),
music: -1,
avatar: -1,
bili: -1,
bing: Number(bingStat),
}
};
});
return {
code: 1,
data: {
wallpaper: -1,
netease: Number(neteaseStat),
music: -1,
avatar: -1,
bili: -1,
bing: Number(bingStat),
}
};
}
);
+27
View File
@@ -0,0 +1,27 @@
import { Api, Get, Query, useContext } from "@midwayjs/hooks";
import { source } from "../data/wallpaper";
import { random } from "lodash";
import { client } from "../utils/redis";
export default Api(
Get(),
Query<{ source: string }>(),
async () => {
const ctx = useContext();
const sourceResult = source[ctx.query.source || "jsd"];
// 增加使用数量
await client.incr("api-next:stat:wallpaper");
const number = random(sourceResult.start, sourceResult.end);
ctx.status = 302;
ctx.set("location", `${sourceResult.url}${number}.jpg`);
return {
code: 1,
msg: "Success"
}
}
);
+23
View File
@@ -0,0 +1,23 @@
import { IWallpaperSource } from "../types/wallpaper";
// 随机动漫壁纸静态源
export const source: Record<string, IWallpaperSource> = {
gt: {
name: "Gitee",
url: "https://dreamer-paul.gitee.io/anime-wallpaper/",
start: 1,
end: 104
},
gh: {
name: "GitHub",
url: "https://dreamer-paul.github.io/Anime-Wallpaper/",
start: 1,
end: 104
},
jsd: {
name: "JSDelivr",
url: "https://fastly.jsdelivr.net/gh/Dreamer-Paul/Anime-Wallpaper/",
start: 1,
end: 104
}
}
+6
View File
@@ -0,0 +1,6 @@
export interface IWallpaperSource {
name: string
url: string
start: number
end: number
}