diff --git a/build/cool/virtual.ts b/build/cool/virtual.ts index 7d059b1..b149f59 100644 --- a/build/cool/virtual.ts +++ b/build/cool/virtual.ts @@ -33,6 +33,12 @@ export function virtual(): Plugin { // 代码保存时触发 eps 刷新 if (!file.includes("build/cool/dist")) { buildEps(server); + + // 通知客户端刷新 + server.ws.send({ + type: "custom", + event: "eps-update" + }); } }, resolveId(id) { diff --git a/src/cool/bootstrap/eps.ts b/src/cool/bootstrap/eps.ts index ea01ae7..6e99157 100644 --- a/src/cool/bootstrap/eps.ts +++ b/src/cool/bootstrap/eps.ts @@ -43,7 +43,7 @@ export function createEps(modules: Module[]) { list.push({ api, module: s.namespace.split("/")[0], - name: s.constructor.name, + name: s.constructor.name + "Entity", prefix: `/admin/${s.namespace}` }); } else { diff --git a/src/cool/service/index.ts b/src/cool/service/index.ts index 77a8a6b..60ce26e 100644 --- a/src/cool/service/index.ts +++ b/src/cool/service/index.ts @@ -3,11 +3,13 @@ import { hmr } from "../hook"; import { eps } from "virtual:eps"; import { merge } from "lodash-es"; +// service 数据集合 export const service: Eps.Service = hmr.getData("service", { request: new BaseService().request }); -function main() { +// 同步 service 数据 +function update() { function deep(d: any) { if (d.namespace) { const a = new BaseService(d.namespace); @@ -46,8 +48,17 @@ function main() { // 缓存 hmr.setData("service", service); + + // tips + console.log("[eps] update"); } -main(); +update(); + +if (import.meta.hot) { + import.meta.hot.on("eps-update", () => { + update(); + }); +} export * from "./base"; diff --git a/src/modules/demo/service/user/follow.ts b/src/modules/demo/service/user/follow.ts new file mode 100644 index 0000000..64bf0b6 --- /dev/null +++ b/src/modules/demo/service/user/follow.ts @@ -0,0 +1,6 @@ +import { BaseService, Service } from "/@/cool"; + +@Service("demo/user/follow") +class DemoUserFollow extends BaseService {} + +export default DemoUserFollow; diff --git a/src/modules/demo/service/user/info.ts b/src/modules/demo/service/user/info.ts new file mode 100644 index 0000000..d47ff1a --- /dev/null +++ b/src/modules/demo/service/user/info.ts @@ -0,0 +1,33 @@ +import axios from "axios"; +import { BaseService, Service } from "/@/cool"; +import dayjs from "dayjs"; + +@Service("demo/user/info") +class DemoUserInfo extends BaseService { + // 测试方法,使用 request 请求数据 + t1() { + return this.request({ + url: "/t1" // 测试地址,实际项目中请更换为真实接口地址 + }); + } + + // 自定义请求,通过 axios 返回数据 + t2() { + return axios({ + url: "https://" + }); + } + + // 自定义请求,通过 Promise 返回数据 + t3() { + return new Promise((resolve, reject) => { + setTimeout(() => { + resolve({ + date: dayjs().format("YYYY-MM-DD HH:mm:ss") + }); + }, 1500); + }); + } +} + +export default DemoUserInfo;