From d317978997b610306b65c3579784f5ec6f14dc44 Mon Sep 17 00:00:00 2001 From: gaosha <414542046@qq.com> Date: Wed, 3 Jan 2024 20:34:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90"=E5=8F=91=E9=80=81=E8=AF=B7=E6=B1=82"=20=20?= =?UTF-8?q?=E6=97=A0=E5=93=8D=E5=BA=94=E9=97=AE=E9=A2=98=20packages\plugin?= =?UTF-8?q?s\datasource\src\DataSourceRemotePanel=202=E3=80=81=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E8=A1=A8=E6=A0=BC=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E5=8A=A0=E8=BD=BD=E9=97=AE=E9=A2=98=20packag?= =?UTF-8?q?es\canvas\src\components\render\RenderMain.js=203=E3=80=81?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E6=97=B6=E8=A1=A8=E6=A0=BC=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=BA=90=E4=B8=8D=E8=83=BD=E5=8A=A0=E8=BD=BD=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20packages\design-core\src\preview\src\preview\srcFiles\dataSo?= =?UTF-8?q?urceMap.js=204=E3=80=81=E6=B7=BB=E5=8A=A0getRequest=20=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=96=B9=E6=B3=95=20packages\plugins\datasource\index?= =?UTF-8?q?.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/render/RenderMain.js | 30 ++-- .../src/preview/srcFiles/dataSourceMap.js | 131 +++++++++++++++--- packages/plugins/datasource/index.js | 4 +- .../datasource/src/DataSourceField.vue | 2 +- .../datasource/src/DataSourceRemoteForm.vue | 11 +- .../datasource/src/DataSourceRemotePanel.vue | 23 ++- 6 files changed, 167 insertions(+), 34 deletions(-) diff --git a/packages/canvas/src/components/render/RenderMain.js b/packages/canvas/src/components/render/RenderMain.js index 532821b7..45e358eb 100644 --- a/packages/canvas/src/components/render/RenderMain.js +++ b/packages/canvas/src/components/render/RenderMain.js @@ -18,7 +18,10 @@ import { generateFunction } from '@opentiny/tiny-engine-controller/utils' import renderer, { parseData, setConfigure, setController, globalNotify, isStateAccessor } from './render' import { getNode as getNodeById, clearNodes, getRoot, setContext, getContext, setCondition, context } from './context' import CanvasEmpty from './CanvasEmpty.vue' - +//添加类型转换方法 +import { obj2String, string2Obj } from '@opentiny/tiny-engine-controller/adapter' +//添加请求数据源方法(同发送请求) +import { getRequest } from '@opentiny/tiny-engine-plugin-datasource' //tiny-engine\packages\plugins\datasource\src\js\datasource.js const { BROADCAST_CHANNEL } = constants const reset = (obj) => { @@ -194,22 +197,29 @@ const getDataSourceMap = () => { } const setDataSourceMap = (list) => { + dataSourceMap.value = list.reduce((dMap, config) => { + + /* const dataSource = { config: config.data } - - const result = { + const result = { code: '', msg: 'success', data: {} - } - result.data = - dataSource.config.type === 'array' - ? { items: dataSource?.config?.data, total: dataSource?.config?.data?.length } - : dataSource?.config?.data - + } + result.data = dataSource.config.type === 'array' ? { items: dataSource?.config?.data , total: dataSource?.config?.data?.length } : dataSource?.config?.data + dataSource.load = () => Promise.resolve(result) dMap[config.name] = dataSource - + */ + + + if (config.data.options.params) { + config.data.options.params = string2Obj(config.data.options.params || "{}") + } + const dataSource = getRequest(config.data) + dataSource.load() + dMap[config.name] = dataSource return dMap }, {}) } diff --git a/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js b/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js index ae37f2e2..a75d1e96 100644 --- a/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js +++ b/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js @@ -11,25 +11,122 @@ */ import dataSources from './dataSource.js' +//import { getRequest } from './fetch.js' //@opentiny/tiny-engine-plugin-datasource' //tiny-engine\packages\plugins\datasource\src\js\datasource.js +import axios from 'axios' +//添加类型转换方法 -const dataSourceMap = {} +export const generateFunction = (body, context) => { + const Func = Function + try { + return new Func(`return ${body}`).call(context).bind(context) + } catch (error) { + // do nothing + } + return undefined +} + +const load = (http, options, dataSource, shouldFetch) => (params, customUrl) => { + if (!shouldFetch()) { + return undefined + } + dataSource.status = 'loading' + const { method, uri: url, params: defaultParams, timeout, headers } = options + const config = { method, url, headers, timeout } + + const data = params || defaultParams + + config.url = customUrl || config.url + + if (method.toLowerCase() === 'get') { + config.params = data + } else { + config.data = data + } + + return http.request(config) +} + +const createFn = (fnContent) => { + return (...args) => { + const fn = generateFunction(fnContent, this) + return fn.apply(this, args) + } +} + +export const getRequest = (config) => { + const http = axios.create() + const dataSource = { config } + const shouldFetch = createFn(config.shouldFetch.value) + const willFetch = createFn(config.willFetch.value) + + const dataHandler = (res) => { + const data = createFn(config.dataHandler.value)(res) + dataSource.status = 'loaded' + dataSource.data = data + return data + } + + const errorHandler = (error) => { + createFn(config.errorHandler.value)(error) + dataSource.status = 'error' + dataSource.error = error + } + + http.interceptors.request.use(willFetch, errorHandler) + + http.interceptors.response.use(dataHandler, errorHandler) + + dataSource.status = 'init' + dataSource.load = load(http, config.options, dataSource, shouldFetch) + + return dataSource +} + + +export const overrideOrMergeData = (isOverride, data, imported) => { + if (isOverride) { + return imported + } else { + return imported.concat(data || []) + } +} + +export const getDataAfterPage = (data, attrs) => { + const { currentPage, pageSize } = attrs + const offset = (currentPage - 1) * pageSize + return data.slice(offset, offset + pageSize) +} + + + +//添加请求数据源方法(同发送请求) +const dataSourceMap = {} Array.isArray(dataSources.list) && - dataSources.list.forEach((config) => { - const dataSource = { config: config.data } - - const result = { - code: '', - msg: 'success', - data: {} - } - result.data = - dataSource.config.type === 'array' - ? { items: dataSource?.config?.data, total: dataSource?.config?.data?.length } - : dataSource?.config?.data - dataSourceMap[config.name] = dataSource - - dataSource.load = () => Promise.resolve(result) - }) + dataSources.list.forEach((config) => { + console.log("axios:",axios); + /* + const dataSource = { config: config.data } + const result = { + code: '', + msg: 'success', + data: {} + } + result.data = dataSource.config.type === 'array' + ? { items: dataSource?.config?.data, total: dataSource?.config?.data?.length } + : dataSource?.config?.data + dataSource.load = () => Promise.resolve(result) + */ + + + + if (config.data.options.params) { + config.data.options.params = JSON.parse(config.data.options.params || "{}") + } + const dataSource = getRequest(config.data) + //dataSource.load() + dataSourceMap[config.name] = dataSource + + }) export default dataSourceMap diff --git a/packages/plugins/datasource/index.js b/packages/plugins/datasource/index.js index 22898068..706571dc 100644 --- a/packages/plugins/datasource/index.js +++ b/packages/plugins/datasource/index.js @@ -11,6 +11,8 @@ */ import { fetchDataSourceList, fetchDataSourceDetail } from './src/js/http' +import { getRequest } from './src/js/datasource' + import component from './src/Main.vue' export default { @@ -21,4 +23,4 @@ export default { component } -export { fetchDataSourceList, fetchDataSourceDetail } +export { fetchDataSourceList, fetchDataSourceDetail ,getRequest} diff --git a/packages/plugins/datasource/src/DataSourceField.vue b/packages/plugins/datasource/src/DataSourceField.vue index 9763b4aa..35ec6939 100644 --- a/packages/plugins/datasource/src/DataSourceField.vue +++ b/packages/plugins/datasource/src/DataSourceField.vue @@ -6,7 +6,7 @@ >
- {{ + {{ editable ? '修改远程配置' : '获取远程字段' }}
diff --git a/packages/plugins/datasource/src/DataSourceRemoteForm.vue b/packages/plugins/datasource/src/DataSourceRemoteForm.vue index e38cc112..fb7fa875 100644 --- a/packages/plugins/datasource/src/DataSourceRemoteForm.vue +++ b/packages/plugins/datasource/src/DataSourceRemoteForm.vue @@ -16,7 +16,7 @@ - + + + + + + + + + diff --git a/packages/plugins/datasource/src/DataSourceRemotePanel.vue b/packages/plugins/datasource/src/DataSourceRemotePanel.vue index e726a233..f169896d 100644 --- a/packages/plugins/datasource/src/DataSourceRemotePanel.vue +++ b/packages/plugins/datasource/src/DataSourceRemotePanel.vue @@ -6,6 +6,11 @@
+ + + 发送请求 + + @@ -167,10 +172,10 @@ export default { const options = { ...state.remoteData.options } if (options.params) { - options.params = string2Obj(options.params) + options.params = string2Obj(options.params || "{}") } - - const request = getRequest({ options, ...dataSourceRemoteAdapteRef.value.getEditorValue() }) + + const request = getRequest({ options, ...dataSourceRemoteAdapteRef.value.getEditorValue() }) /** * 按照数据源请求面板的提示,dataSourceMap函数的相应结果的结构应该会是: @@ -201,6 +206,7 @@ export default { -- Gitee From 0c309355f06ad01d42ca431d3fa762d3aa2ecb32 Mon Sep 17 00:00:00 2001 From: gaosha <414542046@qq.com> Date: Thu, 4 Jan 2024 09:59:29 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=A2=84=E8=A7=88=E6=97=B6=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=B7=B2=E6=98=AFjson=20=E4=B8=8D=E7=94=A8=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assets/plugin-icon-materials.svg | 17 +---------------- packages/design-core/vite.config.js | 5 ++++- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/packages/design-core/assets/plugin-icon-materials.svg b/packages/design-core/assets/plugin-icon-materials.svg index 31240724..2ed82c26 100644 --- a/packages/design-core/assets/plugin-icon-materials.svg +++ b/packages/design-core/assets/plugin-icon-materials.svg @@ -1,16 +1 @@ - - - - - - - - - + \ No newline at end of file diff --git a/packages/design-core/vite.config.js b/packages/design-core/vite.config.js index e149e0f2..71490575 100644 --- a/packages/design-core/vite.config.js +++ b/packages/design-core/vite.config.js @@ -12,7 +12,9 @@ import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' import { importmapPlugin } from './scripts/externalDeps' import visualizer from 'rollup-plugin-visualizer' -const origin = 'http://localhost:9090/' +//const origin = 'http://localhost:9090/' + +const origin = 'http://localhost:7011/' const config = { base: './', @@ -24,6 +26,7 @@ const config = { server: { // 这里保证本地启动服务是localhost,支持js多线程和谷歌浏览器读写本地文件api port: 8080, + open: '/?type=app&id=918&tenant=1', proxy: { '/app-center/v1/api': { -- Gitee From 31a2152b17fb3338be4eb2082a1de2e945cdcfad Mon Sep 17 00:00:00 2001 From: gaosha <414542046@qq.com> Date: Thu, 4 Jan 2024 10:09:17 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/preview/srcFiles/dataSourceMap.js | 153 +++++++++--------- 1 file changed, 72 insertions(+), 81 deletions(-) diff --git a/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js b/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js index a75d1e96..d08f338d 100644 --- a/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js +++ b/packages/design-core/src/preview/src/preview/srcFiles/dataSourceMap.js @@ -1,111 +1,107 @@ /** -* Copyright (c) 2023 - present TinyEngine Authors. -* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. -* -* Use of this source code is governed by an MIT-style license. -* -* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, -* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR -* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. -* -*/ + * Copyright (c) 2023 - present TinyEngine Authors. + * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. + * + * Use of this source code is governed by an MIT-style license. + * + * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, + * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR + * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. + * + */ import dataSources from './dataSource.js' //import { getRequest } from './fetch.js' //@opentiny/tiny-engine-plugin-datasource' //tiny-engine\packages\plugins\datasource\src\js\datasource.js -import axios from 'axios' +import axios from 'axios' //添加类型转换方法 export const generateFunction = (body, context) => { - const Func = Function - try { - return new Func(`return ${body}`).call(context).bind(context) - } catch (error) { - // do nothing - } - return undefined + const Func = Function + try { + return new Func(`return ${body}`).call(context).bind(context) + } catch (error) { + // do nothing + } + return undefined } const load = (http, options, dataSource, shouldFetch) => (params, customUrl) => { - if (!shouldFetch()) { - return undefined - } - dataSource.status = 'loading' - const { method, uri: url, params: defaultParams, timeout, headers } = options - const config = { method, url, headers, timeout } + if (!shouldFetch()) { + return undefined + } + dataSource.status = 'loading' + const { method, uri: url, params: defaultParams, timeout, headers } = options + const config = { method, url, headers, timeout } - const data = params || defaultParams + const data = params || defaultParams - config.url = customUrl || config.url + config.url = customUrl || config.url - if (method.toLowerCase() === 'get') { - config.params = data - } else { - config.data = data - } + if (method.toLowerCase() === 'get') { + config.params = data + } else { + config.data = data + } - return http.request(config) + return http.request(config) } const createFn = (fnContent) => { - return (...args) => { - const fn = generateFunction(fnContent, this) - return fn.apply(this, args) - } + return (...args) => { + const fn = generateFunction(fnContent, this) + return fn.apply(this, args) + } } export const getRequest = (config) => { - const http = axios.create() - const dataSource = { config } - const shouldFetch = createFn(config.shouldFetch.value) - const willFetch = createFn(config.willFetch.value) + const http = axios.create() + const dataSource = { config } + const shouldFetch = createFn(config.shouldFetch.value) + const willFetch = createFn(config.willFetch.value) - const dataHandler = (res) => { - const data = createFn(config.dataHandler.value)(res) - dataSource.status = 'loaded' - dataSource.data = data - return data - } + const dataHandler = (res) => { + const data = createFn(config.dataHandler.value)(res) + dataSource.status = 'loaded' + dataSource.data = data + return data + } - const errorHandler = (error) => { - createFn(config.errorHandler.value)(error) - dataSource.status = 'error' - dataSource.error = error - } + const errorHandler = (error) => { + createFn(config.errorHandler.value)(error) + dataSource.status = 'error' + dataSource.error = error + } - http.interceptors.request.use(willFetch, errorHandler) + http.interceptors.request.use(willFetch, errorHandler) - http.interceptors.response.use(dataHandler, errorHandler) + http.interceptors.response.use(dataHandler, errorHandler) - dataSource.status = 'init' - dataSource.load = load(http, config.options, dataSource, shouldFetch) + dataSource.status = 'init' + dataSource.load = load(http, config.options, dataSource, shouldFetch) - return dataSource + return dataSource } - export const overrideOrMergeData = (isOverride, data, imported) => { - if (isOverride) { - return imported - } else { - return imported.concat(data || []) - } + if (isOverride) { + return imported + } else { + return imported.concat(data || []) + } } export const getDataAfterPage = (data, attrs) => { - const { currentPage, pageSize } = attrs - const offset = (currentPage - 1) * pageSize - return data.slice(offset, offset + pageSize) + const { currentPage, pageSize } = attrs + const offset = (currentPage - 1) * pageSize + return data.slice(offset, offset + pageSize) } - - //添加请求数据源方法(同发送请求) -const dataSourceMap = {} +const dataSourceMap = {} Array.isArray(dataSources.list) && - dataSources.list.forEach((config) => { - console.log("axios:",axios); - /* + dataSources.list.forEach((config) => { + /* const dataSource = { config: config.data } const result = { code: '', @@ -116,17 +112,12 @@ Array.isArray(dataSources.list) && ? { items: dataSource?.config?.data, total: dataSource?.config?.data?.length } : dataSource?.config?.data dataSource.load = () => Promise.resolve(result) - */ - - - + if (config.data.options.params) { config.data.options.params = JSON.parse(config.data.options.params || "{}") - } - const dataSource = getRequest(config.data) - //dataSource.load() - dataSourceMap[config.name] = dataSource - - }) + } */ + const dataSource = getRequest(config.data) + dataSourceMap[config.name] = dataSource + }) export default dataSourceMap -- Gitee