diff --git a/packages/command-services/lib/controller.service.ts b/packages/command-services/lib/controller.service.ts index 866f2a64d42498767613a44615863cca42753cb6..49cd2b695fbd3be07ab440baa4d63b5233d4bea4 100644 --- a/packages/command-services/lib/controller.service.ts +++ b/packages/command-services/lib/controller.service.ts @@ -1,4 +1,4 @@ -import { Entity, EntityPath, HttpMethod, HttpMethods, HttpRequestConfig, Injector, ViewModel, ViewModelState } from '@farris/devkit-vue'; +import { Entity, EntityPath, HttpClient, HttpMethod, HttpMethods, HttpRequestConfig, Injector, ViewModel, ViewModelState } from '@farris/devkit-vue'; import { BefProxy, BefRepository, RequestInfoUtil } from '@farris/bef-vue'; import { RenderEngineService } from './render-engine.service'; import { TemplateService } from './template.service'; @@ -8,7 +8,7 @@ import { FormNotifyService } from './form-notify.service'; import { CommandService } from './command.service'; import { CancelDataService, CardDataService, ListDataService, LoadDataService, RemoveDataService, SaveDataService, UpdateDataService } from './data-services'; import { ContextService } from './devkit-services/index'; -import { ApiClientContext, ComponentContext, ControllerContext, DataServiceContext, EntityStoreContext, I18nContext, MethodContext, ServiceContext, UtilContext } from './types'; +import { ApiClientContext, ComponentContext, ControllerContext, DataServiceContext, EntityStoreContext, HttpClientContext, I18nContext, MethodContext, ServiceContext, UtilContext } from './types'; import { TranslateService } from './locale'; import { DialogService } from './dialog.service'; import { StateMachineService } from './state-machine.service'; @@ -34,6 +34,7 @@ export class ControllerService { protected removeDataService: RemoveDataService; protected saveDataService: SaveDataService; protected cancelDataService: CancelDataService; + protected httpClient: HttpClient; public context: ControllerContext; @@ -58,7 +59,7 @@ export class ControllerService { this.removeDataService = this.injector.get(RemoveDataService); this.saveDataService = this.injector.get(SaveDataService); this.cancelDataService = this.injector.get(CancelDataService); - + this.httpClient = this.injector.get(HttpClient); this.context = this.buildContext(); } @@ -83,6 +84,7 @@ export class ControllerService { } private buildContext(): ControllerContext { return { + httpClient: this.buildHttpClientContext(), apiClient: this.buildApiClientContext(), util: this.buildUtilContext(), i18n: this.buildI18nContext(), @@ -94,6 +96,7 @@ export class ControllerService { method: this.buildMethodContext(), repository: this.repository, dataService: this.buildDataServiceContext(), + module: this.viewModel.getModule() }; } private buildUtilContext(): UtilContext { @@ -124,6 +127,18 @@ export class ControllerService { openLookup: this.dialogService.openLookup.bind(this.dialogService) }; } + private buildHttpClientContext(): HttpClientContext { + return { + get: this.httpClient.get.bind(this.httpClient), + post: this.httpClient.post.bind(this.httpClient), + put: this.httpClient.put.bind(this.httpClient), + patch: this.httpClient.patch.bind(this.httpClient), + delete: this.httpClient.delete.bind(this.httpClient), + request: (url: string, method: HttpMethod, requestConfig?: HttpRequestConfig) => { + return this.httpClient.request(method, url, requestConfig); + }, + }; + } /** * 构造接口客户端上下文 * @returns diff --git a/packages/command-services/lib/types.ts b/packages/command-services/lib/types.ts index e53779fbde0ae1436c6038257e2d72982443bda4..7e375108c0fcf4d25741d627c8a5ed4457ac9a34 100644 --- a/packages/command-services/lib/types.ts +++ b/packages/command-services/lib/types.ts @@ -1,5 +1,5 @@ import { BefRepository, RequestInfo } from '@farris/bef-vue'; -import { Entity, EntityList, EntityPath, EntityState, EntityStore, HttpMethod, HttpRequestConfig, UIState, UIStore } from '@farris/devkit-vue'; +import { Entity, EntityList, EntityPath, EntityState, EntityStore, HttpMethod, HttpRequestConfig, Module, UIState, UIStore, ViewModel, ViewModelState } from '@farris/devkit-vue'; import { LookupConfig, ModalConfig } from './dialog.service'; import { StateMachineService } from './state-machine.service'; @@ -132,8 +132,7 @@ export interface BuildFrameworkTabIdOptions { funcId?: string; tabId?: string; } - -export interface ApiClientContext { +export interface HttpClientContext { /** * 使用GET方法调用后端接口 * @param url api地址 @@ -187,6 +186,8 @@ export interface ApiClientContext { */ request(url: string, method: HttpMethod, requestConfig?: HttpRequestConfig): Promise; } +export interface ApiClientContext extends HttpClientContext { +} export interface I18nContext { /** * 多语翻译 @@ -376,16 +377,25 @@ export interface DataServiceContext { cancel(showConfirm?: boolean): Promise; } export interface ControllerContext { + /** + * http请求库 + * @description 该请求库用于向非表单接口发送请求,发送请求时不会携带表单变更集 + */ + httpClient: HttpClientContext; /** * api客户端 + * @description 用于调用表单后端接口,请求时会自动携带变更,不需要开发者手动构造变更集 + * @summary 请不要使用apiClient请求非表单接口,会导致变更集丢失,请求非表单资源请使用httpClient */ apiClient: ApiClientContext; /** * 多语上下文 + * @description 用于获取当前语言环境,翻译多语key为适配当前语言环境的文本 */ i18n: I18nContext; /** * 工具上下文 + * @description 工具方法集合,如展示或关闭加载动画、展示提示消息等 */ util: UtilContext; /** @@ -394,6 +404,7 @@ export interface ControllerContext { entityStore: EntityStoreContext | null; /** * 界面状态仓库上下文 + * @description 常用实体操作方法 */ uiStore: UIStore | null; /** @@ -402,6 +413,7 @@ export interface ControllerContext { component: ComponentContext; /** * 服务上下文 + * @description 获取组件实例、更新组件属性等 */ service: ServiceContext; /** @@ -410,6 +422,7 @@ export interface ControllerContext { eventParams?: any; /** * 方法上下文 + * @description 用于命令转调等 */ method: MethodContext; /** @@ -422,4 +435,14 @@ export interface ControllerContext { * @description 数据服务是对repository和entityStore的封装,即调用后端接口并更新本地数据仓库 */ dataService: DataServiceContext; + /** + * 视图模型上下文 + * @description 可以快速切换视图模型,获取entityStore、uiStore等对象 + */ + viewModel?: ViewModel; + /** + * 表单上下文 + * @description 模块,代表表单,可以使用模块快速切换视图模型、变量仓库 + */ + module: Module; } diff --git a/packages/devkit/lib/http/http-client.ts b/packages/devkit/lib/http/http-client.ts index b5f40ed3b7e2b56988d1e64f961f14f796c453ba..2bbb310e6bff36f2a9e6e4acdfb19c80f1d38c95 100644 --- a/packages/devkit/lib/http/http-client.ts +++ b/packages/devkit/lib/http/http-client.ts @@ -24,14 +24,14 @@ class HttpClient { /** * 发送GET请求 */ - public get(url: string, requestConfig: HttpRequestConfig): Promise { + public get(url: string, requestConfig?: HttpRequestConfig): Promise { return this.request('GET', url, requestConfig); } /** * 发送POST请求 */ - public post(url: string, body: any, requestConfig: HttpRequestConfig): Promise { + public post(url: string, body: any, requestConfig?: HttpRequestConfig): Promise { requestConfig = HttpUtil.appendBodyToRequestConfig(body, requestConfig); return this.request('POST', url, requestConfig); } @@ -39,7 +39,7 @@ class HttpClient { /** * 发送PUT请求 */ - public put(url: string, body: any, requestConfig: HttpRequestConfig): Promise { + public put(url: string, body: any, requestConfig?: HttpRequestConfig): Promise { requestConfig = HttpUtil.appendBodyToRequestConfig(body, requestConfig); return this.request('PUT', url, requestConfig); } @@ -47,7 +47,7 @@ class HttpClient { /** * 发送PATCH请求 */ - public patch(url: string, body: any, requestConfig: HttpRequestConfig): Promise { + public patch(url: string, body: any, requestConfig?: HttpRequestConfig): Promise { requestConfig = HttpUtil.appendBodyToRequestConfig(body, requestConfig); return this.request('PATCH', url, requestConfig); } @@ -62,7 +62,7 @@ class HttpClient { /** * 发送请求 */ - public request(method: HttpMethod, url: string, requestConfig: HttpRequestConfig): Promise { + public request(method: HttpMethod, url: string, requestConfig?: HttpRequestConfig): Promise { const axiosRequestConfig = HttpUtil.buildAxiosRequestConfig(method, url, requestConfig); const responsePromise = this.axiosInstance.request(axiosRequestConfig).then((axiosResponse: AxiosResponse) => { const httpResponse = HttpUtil.buildHttpResponse(axiosResponse); diff --git a/packages/devkit/lib/http/http-util.ts b/packages/devkit/lib/http/http-util.ts index a6d958edbedd7b1caa2b201765997bcf62da0c86..7e0998ac05705e4884684799efdfd65f78d48f5b 100644 --- a/packages/devkit/lib/http/http-util.ts +++ b/packages/devkit/lib/http/http-util.ts @@ -26,7 +26,7 @@ class HttpUtil { /** * 构造AxiosReqeustConfig */ - public static buildAxiosRequestConfig(method: HttpMethod, url: string, requestConfig: HttpRequestConfig): AxiosRequestConfig { + public static buildAxiosRequestConfig(method: HttpMethod, url: string, requestConfig?: HttpRequestConfig): AxiosRequestConfig { requestConfig = requestConfig || {}; const axiosRequestConfig: AxiosRequestConfig = { diff --git a/packages/devkit/lib/module/module.ts b/packages/devkit/lib/module/module.ts index af50d065e365848b69e88b47ee12e1b1166aa740..77b1d7d5e0966e7eedc48e0c0a98975be605a492 100644 --- a/packages/devkit/lib/module/module.ts +++ b/packages/devkit/lib/module/module.ts @@ -187,7 +187,13 @@ class Module implements IDisposable { public getEntityStores(): EntityStore>[] { return Array.from(this.entityStores.values()); } - + /** + * 获取根组件对应的变量仓库 + * @returns + */ + public getRootUIStore() { + return this.getRootViewModel().uiStore; + } /** * 获取UI仓库 */ diff --git a/packages/devkit/lib/store/entity-store/entity-state-updater.ts b/packages/devkit/lib/store/entity-store/entity-state-updater.ts index 5c0e037706269107737a5c65eda542bdae635525..4e10a2d0113d914bcb7207fbd58ba335142b9c85 100644 --- a/packages/devkit/lib/store/entity-store/entity-state-updater.ts +++ b/packages/devkit/lib/store/entity-store/entity-state-updater.ts @@ -138,8 +138,11 @@ class EntityStateUpdater { /** * 根据属性Path,更新属性值 + * @param path 属性路径 + * @param newValue 新值 + * @param persistent 是否持久化变更,默认true */ - public setValueByPath(path: EntityPath, newValue: any): void { + public setValueByPath(path: EntityPath, newValue: any, persistent = true): void { const parentPath = path.getParentEntityPath(); const entity = this.entityQuery.getEntityByPath(parentPath) as any; @@ -166,7 +169,7 @@ class EntityStateUpdater { oldValue, newValue }; - this.entityStore.triggerChange(change); + this.entityStore.triggerChange(change, persistent); } /** diff --git a/packages/devkit/lib/store/entity-store/entity-store.ts b/packages/devkit/lib/store/entity-store/entity-store.ts index b0413f5fa568df1eb62e7d287c7a2187dda7b048..f8bf8c0318ee1de8ae290e3904bd95ca0a29c38e 100644 --- a/packages/devkit/lib/store/entity-store/entity-store.ts +++ b/packages/devkit/lib/store/entity-store/entity-store.ts @@ -234,8 +234,11 @@ class EntityStore> extends Store { /** * 根据path更新字段值 + * @param path 字段路径 + * @param value 字段值 + * @param persistent 是否持久化变更,默认true。持久化变更会将变更记录到变更集中,并提交给后端进行持久化,不持久化变更只更新前端界面 */ - public setValueByPath(path: string | EntityPath, value: any): void { + public setValueByPath(path: string | EntityPath, value: any, persistent = true): void { const entityPath = this.createPath(path); this.entityUpdater.setValueByPath(entityPath, value); } @@ -325,10 +328,14 @@ class EntityStore> extends Store { /** * 触发变更 + * @param change 变更信息 + * @param recordChange 是否记录变更历史,默认true */ - public triggerChange(change: any) { + public triggerChange(change: any, recordChange: boolean = true) { this.refreshState(); - this.entityChangeHistory.addChange(change); + if (recordChange) { + this.entityChangeHistory.addChange(change); + } super.triggerChange(change as any); } diff --git a/packages/renderer/src/composition/index.ts b/packages/renderer/src/composition/index.ts index db0d73fda9b11998f9f2aea3057ad3939e2a4778..c3c626f118011296867ab76b172e8a384b4873da 100644 --- a/packages/renderer/src/composition/index.ts +++ b/packages/renderer/src/composition/index.ts @@ -23,3 +23,4 @@ export * from './use-custom-component-renders'; export * from './use-translate'; export * from './use-custom-css'; export * from './use-rich-editor-provide'; +export * from './use-resource'; diff --git a/packages/renderer/src/composition/use-resource.ts b/packages/renderer/src/composition/use-resource.ts new file mode 100644 index 0000000000000000000000000000000000000000..4cb2828330a2484ef570a86db932eca42be06b2b --- /dev/null +++ b/packages/renderer/src/composition/use-resource.ts @@ -0,0 +1,28 @@ +import { Ref } from "vue"; + +export function useResource(metadataRef: Ref) { + const { resources } = metadataRef.value.form.content.module; + if(!resources || resources.length<1){ + return; + } + function loadScriptFile(jsFilePath: string) { + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = jsFilePath; + document.head.appendChild(script); + } + function loadStyleFile(cssFilePath: string) { + const link = document.createElement('link'); + link.rel = 'stylesheet'; + link.type = 'text/css'; + link.href = cssFilePath; + document.head.appendChild(link); + } + resources.forEach((item: any) => { + if (item.type === 'js' && item.path) { + loadScriptFile(item.path); + } else if (item.type === 'css' && item.path) { + loadStyleFile(item.path); + } + }); +}