diff --git a/src/core/service/decorator.ts b/src/core/service/decorator.ts index 39bda02..592b0aa 100644 --- a/src/core/service/decorator.ts +++ b/src/core/service/decorator.ts @@ -1,5 +1,7 @@ import { isObject } from "../utils"; +console.log(__PROXY_LIST__); + export function Permission(value: string) { return function (target: any, key: any, descriptor: any) { if (!target.permission) { @@ -26,13 +28,18 @@ export function Service(value: any) { // 复杂项 if (isObject(value)) { const { proxy, namespace, url, mock } = value; + const item = __PROXY_LIST__[proxy]; + + if (proxy && !item) { + console.error(`${proxy} 指向的地址不存在!`); + } target.prototype.namespace = namespace; target.prototype.mock = mock; if (proxy) { target.prototype.proxy = proxy; - target.prototype.url = url; + target.prototype.url = url || item ? item.target : null; } } }; diff --git a/src/shims-vue.d.ts b/src/shims-vue.d.ts index 4699ee7..ab65e79 100644 --- a/src/shims-vue.d.ts +++ b/src/shims-vue.d.ts @@ -62,3 +62,5 @@ declare module "mockjs" { const Mock: any; export default Mock; } + +declare const __PROXY_LIST__: any[]; diff --git a/vite.config.ts b/vite.config.ts index f66aa2b..6c9299e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,5 +1,5 @@ import path from "path"; -import type { UserConfig } from "vite"; +import { UserConfig } from "vite"; import vue from "@vitejs/plugin-vue"; import vueJsx from "@vitejs/plugin-vue-jsx"; import viteCompression from "vite-plugin-compression"; @@ -13,6 +13,21 @@ function resolve(dir: string) { // https://vitejs.dev/config/ export default (): UserConfig => { + // 请求代理地址 + const proxy = { + "/dev": { + target: "http://127.0.0.1:8001", + changeOrigin: true, + rewrite: (path: string) => path.replace(/^\/dev/, "") + }, + + "/pro": { + target: "https://show.cool-admin.com", + changeOrigin: true, + rewrite: (path: string) => path.replace(/^\/pro/, "/api") + } + }; + return { base: "/", plugins: [vue(), viteCompression(), Components(), vueJsx(), svgBuilder("./src/icons/svg/")], @@ -32,23 +47,14 @@ export default (): UserConfig => { }, server: { port: 9000, + proxy, hmr: { overlay: true - }, - proxy: { - "/dev": { - target: "http://127.0.0.1:8001", - changeOrigin: true, - rewrite: (path) => path.replace(/^\/dev/, "") - }, - - "/pro": { - target: "https://show.cool-admin.com", - changeOrigin: true, - rewrite: (path) => path.replace(/^\/pro/, "/api") - } } }, + define: { + __PROXY_LIST__: JSON.stringify(proxy) + }, build: { sourcemap: false, polyfillDynamicImport: false // 必须为false