diff --git a/package.json b/package.json index 3c3669f..5007fa4 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "hooks-react-starter", + "name": "paul-api-next", "version": "1.0.0", "private": true, "scripts": { @@ -14,12 +14,14 @@ "@midwayjs/rpc": "^3.0.0", "ahooks": "^3.3.0", "isomorphic-unfetch": "^3.1.0", + "lodash": "^4.17.21", "react": "^17.0.2", "react-dom": "^17.0.2", "redis": "^4.0.6" }, "devDependencies": { "@midwayjs/mock": "^3.3.0", + "@types/lodash": "^4.14.181", "@types/react": "^17.0.44", "@types/react-dom": "^17.0.15", "@vitejs/plugin-react": "^1.3.0", diff --git a/src/api/bing.ts b/src/api/bing.ts index b8d06f0..2f84811 100644 --- a/src/api/bing.ts +++ b/src/api/bing.ts @@ -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 }; diff --git a/src/api/stat.ts b/src/api/stat.ts index 0a6314d..42831f4 100644 --- a/src/api/stat.ts +++ b/src/api/stat.ts @@ -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), + } + }; + } +); diff --git a/src/api/wallpaper.ts b/src/api/wallpaper.ts new file mode 100644 index 0000000..d060792 --- /dev/null +++ b/src/api/wallpaper.ts @@ -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" + } + } +); diff --git a/src/data/wallpaper.ts b/src/data/wallpaper.ts new file mode 100644 index 0000000..08d2faa --- /dev/null +++ b/src/data/wallpaper.ts @@ -0,0 +1,23 @@ +import { IWallpaperSource } from "../types/wallpaper"; + +// 随机动漫壁纸静态源 +export const source: Record = { + 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 + } +} diff --git a/src/types/wallpaper.ts b/src/types/wallpaper.ts new file mode 100644 index 0000000..b7cfb3d --- /dev/null +++ b/src/types/wallpaper.ts @@ -0,0 +1,6 @@ +export interface IWallpaperSource { + name: string + url: string + start: number + end: number +} diff --git a/tsconfig.json b/tsconfig.json index 2c20326..a8ab575 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ "allowJs": false, "esModuleInterop": true, "allowSyntheticDefaultImports": true, - "strict": false, + "strict": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "module": "ESNext", diff --git a/yarn.lock b/yarn.lock index be7153d..42d950f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -202,6 +202,13 @@ "@babel/plugin-syntax-jsx" "^7.16.7" "@babel/types" "^7.17.0" +"@babel/runtime@^7.7.6": + version "7.17.9" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.9.tgz#d19fbf802d01a8cb6cf053a64e472d42c434ba72" + integrity sha512-lSiBBvodq29uShpWGNbgFdKYNiFDo5/HIYsaCEY9ff4sb10x9jizo2+pRrSyF4jKZCXqgzuqBOQKbUm90gQwJg== + dependencies: + regenerator-runtime "^0.13.4" + "@babel/template@^7.16.7": version "7.16.7" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.16.7.tgz#8d126c8701fde4d66b264b3eba3d96f07666d155" @@ -868,6 +875,11 @@ "@types/koa-compose" "*" "@types/node" "*" +"@types/lodash@^4.14.181": + version "4.14.181" + resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.181.tgz#d1d3740c379fda17ab175165ba04e2d03389385d" + integrity sha512-n3tyKthHJbkiWhDZs3DkhkCzt2MexYHXlX0td5iMplyfwketaOeKboEVBqzceH7juqvEg3q5oUoBFxSLu7zFag== + "@types/mime@^1": version "1.3.2" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a" @@ -2487,6 +2499,11 @@ reflect-metadata@^0.1.13: resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08" integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg== +regenerator-runtime@^0.13.4: + version "0.13.9" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52" + integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA== + resize-observer-polyfill@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz#0e9020dd3d21024458d4ebd27e23e40269810464"