From 1b4482ef34211f808369f59139d5d9754b8aaf3a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=8D=9A=E6=96=87?= <1179111926@qq.com>
Date: Wed, 10 Aug 2022 11:49:01 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=B0=81=E8=A3=85=E5=AE=8C?=
=?UTF-8?q?=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/pages/index.tsx | 17 ++++++++--
src/utils/fetch.ts | 80 +++++++++++++++++++++++++++++++++++++++++++++
src/utils/url.ts | 7 ++++
3 files changed, 102 insertions(+), 2 deletions(-)
create mode 100755 src/utils/fetch.ts
create mode 100755 src/utils/url.ts
diff --git a/src/pages/index.tsx b/src/pages/index.tsx
index 3a0c0c8..2e9ddef 100644
--- a/src/pages/index.tsx
+++ b/src/pages/index.tsx
@@ -1,10 +1,23 @@
import styles from './index.less';
+import { message } from 'antd';
+import Fetch from '@/utils/fetch';
+import { useEffect } from 'react';
export default function IndexPage() {
- console.log(22);
+ useEffect(() => {
+ getData();
+ }, []);
+ const getData = async () => {
+ const res = await Fetch('/openi/resource/clickOpenResource', {
+ method: 'post',
+ });
+ console.log(res);
+ };
return (
-
Page index
+ message.info(2222)} className={styles.title}>
+ Page index
+
);
}
diff --git a/src/utils/fetch.ts b/src/utils/fetch.ts
new file mode 100755
index 0000000..b6791e0
--- /dev/null
+++ b/src/utils/fetch.ts
@@ -0,0 +1,80 @@
+/**
+ * request 网络请求工具
+ * 更详细的 api 文档: https://github.com/umijs/umi-request
+ */
+import { extend } from 'umi-request';
+import { notification } from 'antd';
+import publicUrl from '@/utils/url';
+
+// import { history } from 'umi';
+
+const codeMessage: any = {
+ 200: '服务器成功返回请求的数据。',
+ 201: '新建或修改数据成功。',
+ 202: '一个请求已经进入后台排队(异步任务)。',
+ 204: '删除数据成功。',
+ 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
+ 401: '用户没有权限(令牌、用户名、密码错误)。',
+ 403: '用户得到授权,但是访问是被禁止的。',
+ 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
+ 406: '请求的格式不可得。',
+ 410: '请求的资源被永久删除,且不会再得到的。',
+ 422: '当创建一个对象时,发生一个验证错误。',
+ 500: '服务器发生错误,请检查服务器。',
+ 502: '网关错误。',
+ 503: '服务不可用,服务器暂时过载或维护。',
+ 504: '网关超时。',
+};
+/**
+ * 异常处理程序
+ */
+
+const errorHandler = (error: any) => {
+ const { response } = error;
+ console.log(error, 'error');
+ if (response && response.status) {
+ const errorText = codeMessage[response.status] || response.statusText;
+ const { status, url } = response;
+ notification.error({
+ message: `请求错误 ${status}: ${url}`,
+ description: errorText,
+ });
+ } else if (!response) {
+ notification.error({
+ description: '您的网络发生异常,无法连接服务器',
+ message: '网络异常',
+ });
+ }
+
+ throw error;
+};
+
+/**
+ * 配置request请求时的默认参数
+ */
+const request = extend({
+ errorHandler, // 默认错误处理
+ prefix: publicUrl, //地址
+ crossOrigin: true, // 开启CORS跨域
+ method: 'get', //默认get
+ credentials: 'include', // 默认请求是否带上cookie
+});
+
+// 中间件,对请求前添加 userId token 的基础参数
+request.interceptors.request.use((url, options) => {
+ const newOptions = { ...options };
+ if (!(newOptions.data instanceof FormData)) {
+ newOptions.data = {
+ ...newOptions.data,
+ // token: 'adsadsafcdscd',
+ };
+ } else {
+ // newOptions.data.append('token', 'adsadsafcdscd');
+ }
+ return {
+ url: `${url}`,
+ options: { ...newOptions },
+ };
+});
+
+export default request;
diff --git a/src/utils/url.ts b/src/utils/url.ts
new file mode 100755
index 0000000..0e36543
--- /dev/null
+++ b/src/utils/url.ts
@@ -0,0 +1,7 @@
+const env = {
+ dev: 'https://testxgd.educoder.net',
+ build: 'https://testxgd.educoder.net',
+};
+console.log('当前环境:', process.env.NODE_ENV);
+const url = process.env.NODE_ENV === '"development"' ? env.dev : env.build;
+export default url;