Skip to content

Commit ab623bf

Browse files
committed
添加代理 __PROXY_LIST__
1 parent 98b7a92 commit ab623bf

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

src/core/service/decorator.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isObject } from "../utils";
22

3+
console.log(__PROXY_LIST__);
4+
35
export function Permission(value: string) {
46
return function (target: any, key: any, descriptor: any) {
57
if (!target.permission) {
@@ -26,13 +28,18 @@ export function Service(value: any) {
2628
// 复杂项
2729
if (isObject(value)) {
2830
const { proxy, namespace, url, mock } = value;
31+
const item = __PROXY_LIST__[proxy];
32+
33+
if (proxy && !item) {
34+
console.error(`${proxy} 指向的地址不存在!`);
35+
}
2936

3037
target.prototype.namespace = namespace;
3138
target.prototype.mock = mock;
3239

3340
if (proxy) {
3441
target.prototype.proxy = proxy;
35-
target.prototype.url = url;
42+
target.prototype.url = url || item ? item.target : null;
3643
}
3744
}
3845
};

src/shims-vue.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ declare module "mockjs" {
6262
const Mock: any;
6363
export default Mock;
6464
}
65+
66+
declare const __PROXY_LIST__: any[];

vite.config.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path";
2-
import type { UserConfig } from "vite";
2+
import { UserConfig } from "vite";
33
import vue from "@vitejs/plugin-vue";
44
import vueJsx from "@vitejs/plugin-vue-jsx";
55
import viteCompression from "vite-plugin-compression";
@@ -13,6 +13,21 @@ function resolve(dir: string) {
1313
// https://vitejs.dev/config/
1414

1515
export default (): UserConfig => {
16+
// 请求代理地址
17+
const proxy = {
18+
"/dev": {
19+
target: "http://127.0.0.1:8001",
20+
changeOrigin: true,
21+
rewrite: (path: string) => path.replace(/^\/dev/, "")
22+
},
23+
24+
"/pro": {
25+
target: "https://show.cool-admin.com",
26+
changeOrigin: true,
27+
rewrite: (path: string) => path.replace(/^\/pro/, "/api")
28+
}
29+
};
30+
1631
return {
1732
base: "/",
1833
plugins: [vue(), viteCompression(), Components(), vueJsx(), svgBuilder("./src/icons/svg/")],
@@ -32,23 +47,14 @@ export default (): UserConfig => {
3247
},
3348
server: {
3449
port: 9000,
50+
proxy,
3551
hmr: {
3652
overlay: true
37-
},
38-
proxy: {
39-
"/dev": {
40-
target: "http://127.0.0.1:8001",
41-
changeOrigin: true,
42-
rewrite: (path) => path.replace(/^\/dev/, "")
43-
},
44-
45-
"/pro": {
46-
target: "https://show.cool-admin.com",
47-
changeOrigin: true,
48-
rewrite: (path) => path.replace(/^\/pro/, "/api")
49-
}
5053
}
5154
},
55+
define: {
56+
__PROXY_LIST__: JSON.stringify(proxy)
57+
},
5258
build: {
5359
sourcemap: false,
5460
polyfillDynamicImport: false // 必须为false

0 commit comments

Comments
 (0)