Browse Source

docs: 修改接口请求方式

chenlei 2 years ago
parent
commit
76201060d8

+ 1 - 1
packages/backend-cli/template/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@dage/backend-template",
-  "version": "1.0.0",
+  "version": "1.0.1",
   "private": true,
   "dependencies": {
     "@ant-design/icons": "^5.1.4",

+ 8 - 3
packages/backend-cli/template/src/configure.ts

@@ -2,6 +2,7 @@ import { compose, initial } from "@dage/service";
 import { getTokenInfo, removeTokenInfo } from "@dage/pc-components";
 import { message } from "antd";
 import { NoticeType } from "antd/es/message/interface";
+import { ResponseStatusCode } from "./types";
 
 const showMessage = (msg: string, type: NoticeType = "error") => {
   message.open({
@@ -26,9 +27,13 @@ initial({
     async (request, next) => {
       const response = await next();
 
-      if ([5001, 5002].includes(response.code)) {
-        const msg = "登录失效!";
-        showMessage(msg);
+      if (
+        [
+          ResponseStatusCode.TOKEN_INVALID,
+          ResponseStatusCode.TOKEN_INVALID2,
+        ].includes(response.code)
+      ) {
+        showMessage("登录失效!");
         removeTokenInfo();
         globalThis.location.href = "#/login";
       }

+ 24 - 1
packages/docs/.umirc.ts

@@ -14,6 +14,7 @@ export default defineConfig({
   },
   alias: {
     "@dage/utils": join(__dirname, "../utils/dist/"),
+    "@dage/service": join(__dirname, "../service/dist/"),
     "@dage/pc-components": join(__dirname, "../pc-components/dist/"),
   },
   navs: [
@@ -26,6 +27,10 @@ export default defineConfig({
       path: "/utils",
     },
     {
+      title: "service",
+      path: "/service",
+    },
+    {
       title: "更新日志",
       path: "/log",
     },
@@ -54,7 +59,13 @@ export default defineConfig({
       },
       {
         title: "工具",
-        children: ["/components/utils/services", "/components/utils/storage"],
+        children: [
+          /**
+           * @deprecated 将控制权交给应用,使用 @dage/service
+           */
+          // "/components/utils/services",
+          "/components/utils/storage",
+        ],
       },
     ],
     "/utils": [
@@ -66,6 +77,10 @@ export default defineConfig({
         title: "日期格式化",
         path: "/utils/date",
       },
+      {
+        title: "string 方法",
+        path: "/utils/string",
+      },
     ],
     "/log": [
       {
@@ -76,6 +91,14 @@ export default defineConfig({
         title: "backend-cli 更新日志",
         path: "/log/BACKEND-CLI_CHANGELOG",
       },
+      {
+        title: "utils 更新日志",
+        path: "/log/UTILS_CHANGELOG",
+      },
+      {
+        title: "service 更新日志",
+        path: "/log/SERVICE_CHANGELOG",
+      },
     ],
   },
   // more config: https://d.umijs.org/config

+ 2 - 0
packages/docs/docs/components/FileCheckbox/index.md

@@ -5,6 +5,8 @@
 ```tsx
 import React, { useCallback, useState } from "react";
 import { DageFileCheckbox, DageFileResponseType } from "@dage/pc-components";
+// 接口初始化
+import "../../configure";
 
 export default () => {
   const [value, setValue] = useState("");

+ 2 - 0
packages/docs/docs/components/Map/index.md

@@ -5,6 +5,8 @@
 ```tsx
 import React, { useCallback, useState } from "react";
 import { DageMap, DageMapLngLatChangeEvent } from "@dage/pc-components";
+// 接口初始化
+import "../../configure";
 
 export default () => {
   const [position, setPosition] = useState([121.473581, 31.230536]);

+ 5 - 3
packages/docs/docs/components/Upload/index.md

@@ -8,6 +8,8 @@
 import React, { useCallback, useState } from "react";
 import { message } from "antd";
 import { DageUpload, DageFileResponseType } from "@dage/pc-components";
+// 接口初始化
+import "../../configure";
 
 export default () => {
   const [value, setValue] = useState<DageFileResponseType[]>([]);
@@ -31,7 +33,7 @@ export default () => {
       value={value}
       maxCount={2}
       maxSize={20}
-      action="https://sit-shgybwg.4dage.com/api/cms/goods/upload"
+      action="/api/cms/goods/upload"
       tips="支持png、jpg和jpeg格式;最大20M,最多2张"
       onChange={handleChange}
     />
@@ -62,7 +64,7 @@ export default () => {
       maxCount={2}
       maxSize={20}
       dType={DageUploadType.MODEL}
-      action="https://sit-shgybwg.4dage.com/api/cms/goods/upload"
+      action="/api/cms/goods/upload"
       onChange={handleChange}
     />
   );
@@ -92,7 +94,7 @@ export default () => {
       maxCount={2}
       maxSize={20}
       dType={DageUploadType.AUDIO}
-      action="https://sit-shgybwg.4dage.com/api/cms/goods/upload"
+      action="/api/cms/goods/upload"
       onChange={handleChange}
     />
   );

+ 8 - 9
packages/docs/docs/components/login.md

@@ -5,18 +5,17 @@
 ```tsx
 import React, { useState } from "react";
 import { Button, Input, message } from "antd";
-import { Base64 } from '@dage/utils'
-import { encodeStr, service, setTokenInfo } from "@dage/pc-components";
+import { Base64 } from "@dage/utils";
+import { encodeStr, setTokenInfo } from "@dage/pc-components";
+import { request } from "@dage/service";
+import "../configure";
 
 export default () => {
   const login = async () => {
-    const { data } = await service.post(
-      "https://sit-shgybwg.4dage.com/api/admin/login",
-      {
-        userName: "admin",
-        passWord: encodeStr(Base64.encode("123456")),
-      }
-    );
+    const data = await request("/api/admin/login", {
+      userName: "admin",
+      passWord: encodeStr(Base64.encode("123456")),
+    });
     setTokenInfo(data);
     message.success("登陆成功");
   };

+ 40 - 0
packages/docs/docs/configure.ts

@@ -0,0 +1,40 @@
+// @ts-ignore
+import { compose, initial } from "@dage/service";
+// @ts-ignore
+import { getTokenInfo, removeTokenInfo } from "@dage/pc-components";
+import { message } from "antd";
+import { NoticeType } from "antd/es/message/interface";
+
+const showMessage = (msg: string, type: NoticeType = "error") => {
+  message.open({
+    type,
+    content: msg,
+    duration: 4,
+  });
+};
+
+initial({
+  fetch: window.fetch.bind(window),
+  baseURL: "https://sit-shgybwg.4dage.com",
+  interceptor: compose(
+    (request, next) => {
+      const { token } = getTokenInfo();
+      if (token) {
+        request.headers["token"] = token;
+      }
+      return next();
+    },
+    // 登陆失效
+    async (request, next) => {
+      const response = await next();
+
+      if ([5001, 5002].includes(response.code)) {
+        const msg = "登录失效!";
+        showMessage(msg);
+        removeTokenInfo();
+      }
+
+      return response;
+    }
+  ),
+});

+ 4 - 2
packages/docs/docs/index.md

@@ -6,12 +6,14 @@ hero:
     - text: 开始使用
       link: /components
 features:
+  - title: "@dage/backend-cli"
+    desc: 基于 `react` 的后台脚手架
   - title: "@dage/pc-components"
     desc: 基于 `antd` 的 PC 端[组件库](#/components)
+  - title: "@dage/service"
+    desc: 接口请求[工具](#/service)
   - title: "@dage/utils"
     desc: 工具[类库](#/utils)
-  - title: "@dage/backend-cli"
-    desc: 基于 `react` 的后台脚手架
 ---
 
 ## 如何使用

+ 12 - 0
packages/docs/docs/log/BACKEND-CLI_CHANGELOG.md

@@ -1,5 +1,17 @@
 # @dage/backend-cli
 
+## 1.2.1
+
+### Patch Changes
+
+- 修复安装 @dage/service 报错
+
+## 1.2.0
+
+### Minor Changes
+
+- 修改接口请求方式
+
 ## 1.1.0
 
 ### Minor Changes

+ 18 - 0
packages/docs/docs/log/PC-COMPONENTS_CHANGELOG.md

@@ -1,5 +1,23 @@
 # @dage/pc-components
 
+## 1.2.1
+
+### Patch Changes
+
+- 修复 babel-import-config 丢失
+
+## 1.2.0
+
+### Minor Changes
+
+- 删除 pc-components 内置 service,将控制权交给应用
+
+### Patch Changes
+
+- Updated dependencies
+  - @dage/utils@1.0.2
+  - @dage/service@1.0.1
+
 ## 1.1.0
 
 ### Minor Changes

+ 8 - 0
packages/docs/docs/log/SERVICE_CHANGELOG.md

@@ -0,0 +1,8 @@
+# @dage/service
+
+## 1.0.1
+
+### Patch Changes
+
+- Updated dependencies
+  - @dage/utils@1.0.2

+ 9 - 0
packages/docs/docs/log/UTILS_CHANGELOG.md

@@ -0,0 +1,9 @@
+# @dage/utils
+
+## 1.0.2
+
+### Patch Changes
+
+- 新增 addTrailingSlash 方法
+- 新增 removeHeadingString 方法
+- 新增 removeHeadingSlash 方法

+ 1 - 0
packages/docs/docs/service/helper.md

@@ -0,0 +1 @@
+## 接口请求工具

+ 18 - 0
packages/docs/docs/utils/date.md

@@ -4,6 +4,14 @@
 
 ### parseDate 日期解析
 
+```typescript
+parseDate(3600);
+// => 1970-01-01T00:00:03.600Z
+
+parseDate("2012/12/10 12:12:12");
+// => 2012-02-10T04:12:12.000Z
+```
+
 | Name       | Description                | Type                           | Default      |
 | ---------- | -------------------------- | ------------------------------ | ------------ |
 | dateString | 要转换成日期的数字和字符串 | `string \| number \| Date`     | `(required)` |
@@ -11,6 +19,16 @@
 
 ### formatDate 格式化日期
 
+```typescript
+const day = new Date("2012/12/12 12:12:12");
+
+const target = formatDate(day);
+// => target = "2012-02-10"
+
+const target2 = formatDate(day, "YYYY-MM-DD HH:mm:ss");
+// => target2 = "2012-02-10 12:12:12"
+```
+
 | Name        | Description                                                                 | Type                       | Default                     |
 | ----------- | --------------------------------------------------------------------------- | -------------------------- | --------------------------- |
 | date        | 日期                                                                        | `string \| number \| Date` | `(required)`                |

+ 35 - 0
packages/docs/docs/utils/string.md

@@ -0,0 +1,35 @@
+## 方法
+
+### addTrailingSlash 字符串`末尾`添加`/`
+
+```typescript
+addTrailingSlash("http://localhost:8000");
+// => http://localhost:8000/
+```
+
+| Name | Description | Type      | Default      |
+| ---- | ----------- | --------- | ------------ |
+| path | `--`        | `string ` | `(required)` |
+
+### removeHeadingString 删除特定`开头`的`字符串`
+
+```typescript
+removeHeadingString("/api/example", "/api");
+// => /example
+```
+
+| Name    | Description | Type      | Default      |
+| ------- | ----------- | --------- | ------------ |
+| path    | `--`        | `string ` | `(required)` |
+| heading | `--`        | `string ` | `(required)` |
+
+### removeHeadingSlash 删除字符串`开头`的`/`
+
+```typescript
+removeHeadingSlash("/api/example");
+// => api/example
+```
+
+| Name | Description | Type      | Default      |
+| ---- | ----------- | --------- | ------------ |
+| path | `--`        | `string ` | `(required)` |

+ 6 - 0
packages/pc-components/CHANGELOG.md

@@ -1,5 +1,11 @@
 # @dage/pc-components
 
+## 1.2.1
+
+### Patch Changes
+
+- 修复 babel-import-config 丢失
+
 ## 1.2.0
 
 ### Minor Changes

+ 3 - 2
packages/pc-components/package.json

@@ -1,12 +1,13 @@
 {
   "name": "@dage/pc-components",
-  "version": "1.2.0",
+  "version": "1.2.1",
   "description": "PC 端组件库",
   "module": "dist/index.js",
   "main": "dist/index.js",
   "typings": "dist/index.d.ts",
   "files": [
-    "dist"
+    "dist",
+    "babel-import-config.js"
   ],
   "scripts": {
     "start": "tsc --build tsconfig.build.json --watch",