Skip to content

Commit

Permalink
feat(request): add umi-request
Browse files Browse the repository at this point in the history
  • Loading branch information
promonkeyli committed Aug 5, 2024
1 parent f7beb06 commit 213b24f
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 38 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_BASE_URL=http://api.promonkeyli.top:8080
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EXT_PUBLIC_BASE_URL=http://127.0.0.1:8080
3 changes: 1 addition & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"noBannedTypes": "off"
},
"correctness": {
"useExhaustiveDependencies": "off",
"noUndeclaredVariables": "error"
"useExhaustiveDependencies": "off"
},
"style": {
"noNonNullAssertion": "off",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"next": "15.0.0-rc.0",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"umi-request": "^1.4.0",
"zustand": "^4.5.2"
},
"devDependencies": {
Expand Down
67 changes: 52 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion script/genOpenApi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { generateService } from "@umijs/openapi";

const BASE_URL = "http://api.promonkeyli.top:8080";

generateService({
schemaPath: "http://127.0.0.1:8001/swagger/doc.json",
requestLibPath: 'import request from "@/utils/http"',
schemaPath: `${BASE_URL}/swagger/doc.json`,
serversPath: "src",
});
32 changes: 20 additions & 12 deletions src/api/api0.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
// @ts-ignore
/* eslint-disable */
// @ts-ignore
import { request } from "umi";
import request from '@/utils/http';

/** 用户登录 POST /login */
export async function postLogin(body: API.User, options?: { [key: string]: any }) {
return request<API.User>('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: body,
...(options || {}),
});
}

/** 我的工具项新增 POST /tool/add */
export async function postAdd(
body: API.Tool,
options?: { [key: string]: any },
) {
return request<API.Tool>("/tool/add", {
method: "POST",
export async function postToolAdd(body: API.Tool, options?: { [key: string]: any }) {
return request<API.Tool>('/tool/add', {
method: 'POST',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json',
},
data: body,
...(options || {}),
});
}

/** 我的工具信息列表获取 GET /tool/list */
export async function getList(options?: { [key: string]: any }) {
return request<API.Tool>("/tool/list", {
method: "GET",
export async function getToolList(options?: { [key: string]: any }) {
return request<API.Tool>('/tool/list', {
method: 'GET',
...(options || {}),
});
}
5 changes: 5 additions & 0 deletions src/api/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ declare namespace API {
name?: string;
url?: string;
};

type User = {
password?: string;
username?: string;
};
}
23 changes: 15 additions & 8 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use client";
import { postLogin } from "@/api/api0";
import Footer from "@/components/footer";
import { SYSTEM_ROLE } from "@/constants";
import { useRouter } from "next/navigation";
Expand Down Expand Up @@ -26,11 +27,11 @@ export default function Page() {
</select>
{role === SYSTEM_ROLE.ADMIN && (
<input
type="text"
type="password"
className="input w-full"
placeholder="password"
value={pwd}
onBlur={handlePwdChange}
onChange={handlePwdChange}
/>
)}
<button type="button" className="btn w-full" onClick={handleSubmit}>
Expand All @@ -53,12 +54,18 @@ function useLogin() {
const handlePwdChange = (e: any) => {
setPwd(e.target.value);
};
const handleSubmit = () => {
if (role === SYSTEM_ROLE.ADMIN) {
router.push("/admin/index");
} else {
router.push("/");
}
const handleSubmit = async () => {
const user: API.User = {
username: role,
password: pwd,
};
const res = await postLogin(user);
console.log(res);
// if (role === SYSTEM_ROLE.ADMIN) {
// router.push("/admin/index");
// } else {
// router.push("/");
// }
};
return {
role,
Expand Down
9 changes: 9 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
"use client";
import IDock from "@/components/dock";
import IMenu from "@/components/menu";
import useUserStore from "@/stores/auth/user";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

export default function Home() {
const router = useRouter();
useEffect(() => {
if (!useUserStore.getState().token) {
router.replace("/login");
}
}, []);
return (
<div className="mac-bg">
<IMenu />
Expand Down
Loading

0 comments on commit 213b24f

Please sign in to comment.