Feat: Wallpaper API
随机壁纸 API
This commit is contained in:
+7
-6
@@ -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
@@ -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),
|
||||
}
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export interface IWallpaperSource {
|
||||
name: string
|
||||
url: string
|
||||
start: number
|
||||
end: number
|
||||
}
|
||||
Reference in New Issue
Block a user