diff --git a/packages/rax-render/package.json b/packages/rax-render/package.json index 72fedcab0..5f6e11d3a 100644 --- a/packages/rax-render/package.json +++ b/packages/rax-render/package.json @@ -50,7 +50,8 @@ "rax-view": "^1.0.0", "react-is": "^16.10.1", "serialize-javascript": "^1.7.0", - "whatwg-fetch": "^3.0.0" + "whatwg-fetch": "^3.0.0", + "@ali/ui-table": "^1.0.1-beta.6" }, "devDependencies": { "@alib/build-scripts": "^0.1.0", diff --git a/packages/rax-render/src/utils/dataHelper.js b/packages/rax-render/src/utils/dataHelper.js index 3439ae78d..0bc9d1462 100644 --- a/packages/rax-render/src/utils/dataHelper.js +++ b/packages/rax-render/src/utils/dataHelper.js @@ -1,11 +1,12 @@ +/* eslint-disable object-curly-newline */ import { transformArrayToMap, isJSFunction, transformStringToFunction, clone, comboSkeletonConfig } from './index'; -import { jsonp, mtop, request, get, post, bzb } from './request'; +import { jsonp, mtop, request, get, post, bzb, webTableProxy } from './request'; const DS_STATUS = { INIT: 'init', LOADING: 'loading', LOADED: 'loaded', - ERROR: 'error' + ERROR: 'error', }; export default class DataHelper { @@ -34,20 +35,18 @@ export default class DataHelper { this.ajaxList = (config && config.list) || []; const ajaxMap = transformArrayToMap(this.ajaxList, 'id'); // 删除已经移除的接口 - Object.keys(this.ajaxMap).forEach(key => { + Object.keys(this.ajaxMap).forEach((key) => { if (!ajaxMap[key]) { delete this.dataSourceMap[key]; } }); this.ajaxMap = ajaxMap; // 添加未加入到dataSourceMap中的接口 - this.ajaxList.forEach(item => { + this.ajaxList.forEach((item) => { if (!this.dataSourceMap[item.id]) { this.dataSourceMap[item.id] = { status: DS_STATUS.INIT, - load: (...args) => { - return this.getDataSource(item.id, ...args); - } + load: (...args) => this.getDataSource(item.id, ...args), }; } }); @@ -56,33 +55,31 @@ export default class DataHelper { generateDataSourceMap() { const res = {}; - this.ajaxList.forEach(item => { + this.ajaxList.forEach((item) => { res[item.id] = { status: DS_STATUS.INIT, - load: (...args) => { - return this.getDataSource(item.id, ...args); - } + load: (...args) => this.getDataSource(item.id, ...args), }; }); return res; } updateDataSourceMap(id, data, error) { - this.dataSourceMap[id].error = error ? error : undefined; + this.dataSourceMap[id].error = error || undefined; this.dataSourceMap[id].data = data; this.dataSourceMap[id].status = error ? DS_STATUS.ERROR : DS_STATUS.LOADED; } getInitData() { - const initSyncData = this.parser(this.ajaxList).filter(item => { + const initSyncData = this.parser(this.ajaxList).filter((item) => { if (item.isInit) { this.dataSourceMap[item.id].status = DS_STATUS.LOADING; return true; } return false; }); - return this.asyncDataHandler(initSyncData).then(res => { - let dataHandler = this.config.dataHandler; + return this.asyncDataHandler(initSyncData).then((res) => { + let { dataHandler } = this.config; if (isJSFunction(dataHandler)) { dataHandler = transformStringToFunction(dataHandler.value); } @@ -91,7 +88,6 @@ export default class DataHelper { return dataHandler.call(this.host, res); } catch (e) { console.error('请求数据处理函数运行出错', e); - return; } }); } @@ -119,15 +115,15 @@ export default class DataHelper { ? params || options.params : { ...options.params, - ...params + ...params, }, headers: { ...options.headers, - ...headers + ...headers, }, - ...otherProps - } - } + ...otherProps, + }, + }, ]) .then(res => { try { @@ -180,79 +176,81 @@ export default class DataHelper { method: 'POST', params: { data: JSON.stringify(doserReq), - _tb_token_ - } - } + _tb_token_, + }, + }, }); } if (allReq.length === 0) resolve({}); const res = {}; Promise.all( - allReq.map(item => { - return new Promise(resolve => { - const { type, id, dataHandler, options } = item; - const doFetch = (type, options) => { - this.fetchOne(type, options) - .then(async data => { - if (afterRequest) { - this.appHelper.utils.afterRequest(item, data, undefined, async (data, error) => { - await fetchHandler(data, error); - }); - } else { - await fetchHandler(data, undefined); + allReq.map( + (item) => + new Promise((resolve) => { + const { type, id, dataHandler, options } = item; + const fetchHandler = async (data, error) => { + if (type === 'doServer') { + if (!Array.isArray(data)) { + data = [data]; } - }) - .catch(async err => { - if (afterRequest) { - // 必须要这么调用,否则beforeRequest中的this会丢失 - this.appHelper.utils.afterRequest(item, undefined, err, async (data, error) => { - await fetchHandler(data, error); - }); - } else { - await fetchHandler(undefined, err); - } - }); - }; - const fetchHandler = async (data, error) => { - if (type === 'doServer') { - if (!Array.isArray(data)) { - data = [data]; + doserList.forEach(async (id, idx) => { + const req = this.ajaxMap[id]; + if (req) { + res[id] = await this.dataHandler(id, req.dataHandler, data && data[idx], error); + this.updateDataSourceMap(id, res[id], error); + } + }); + } else { + // debugger; + res[id] = await this.dataHandler(id, dataHandler, data, error); + this.updateDataSourceMap(id, res[id], error); } - doserList.forEach(async (id, idx) => { - const req = this.ajaxMap[id]; - if (req) { - res[id] = await this.dataHandler(id, req.dataHandler, data && data[idx], error); - this.updateDataSourceMap(id, res[id], error); - } + resolve(); + }; + const doFetch = (type, req) => { + this.fetchOne(type, req) + .then(async (data) => { + console.log(data); + if (afterRequest) { + this.appHelper.utils.afterRequest(item, data, undefined, async (data, error) => { + await fetchHandler(data, error); + }); + } else { + await fetchHandler(data, undefined); + } + }) + .catch(async (err) => { + if (afterRequest) { + // 必须要这么调用,否则beforeRequest中的this会丢失 + this.appHelper.utils.afterRequest(item, undefined, err, async (data, error) => { + await fetchHandler(data, error); + }); + } else { + await fetchHandler(undefined, err); + } + }); + }; + if (type === 'doServer') { + doserList.forEach((item) => { + this.dataSourceMap[item].status = DS_STATUS.LOADING; }); } else { - res[id] = await this.dataHandler(id, dataHandler, data, error); - this.updateDataSourceMap(id, res[id], error); + this.dataSourceMap[id].status = DS_STATUS.LOADING; } - resolve(); - }; - - if (type === 'doServer') { - doserList.forEach(item => { - this.dataSourceMap[item].status = DS_STATUS.LOADING; - }); - } else { - this.dataSourceMap[id].status = DS_STATUS.LOADING; - } - // 请求切片 - if (beforeRequest) { - // 必须要这么调用,否则beforeRequest中的this会丢失 - this.appHelper.utils.beforeRequest(item, clone(options), options => doFetch(type, options)); - } else { - doFetch(type, options); - } - }); - }) + // 请求切片 + if (beforeRequest) { + // 必须要这么调用,否则beforeRequest中的this会丢失 + this.appHelper.utils.beforeRequest(item, clone(options), () => doFetch(type, item)); + } else { + doFetch(type, item); + } + }), + ), ) .then(() => { resolve(res); }) - .catch(e => { + .catch((e) => { reject(e); }); }); @@ -266,12 +264,12 @@ export default class DataHelper { try { return await dataHandler.call(this.host, data, error); } catch (e) { - console.error('[' + id + ']单个请求数据处理函数运行出错', e); - return; + console.error(`[${id}]单个请求数据处理函数运行出错`, e); } } - fetchOne(type, options) { + fetchOne(type, req) { + const { options } = req; let { uri, method = 'GET', headers, params, ...otherProps } = options; otherProps = otherProps || {}; switch (type) { @@ -284,19 +282,16 @@ export default class DataHelper { return bzb(uri, params, { method, headers, - ...otherProps + ...otherProps, }); case 'legao': - // todo: - if (method === 'JSONP') { - return jsonp(otherProps.url, params, otherProps); - } - break; + return webTableProxy(req); default: method = method.toUpperCase(); if (method === 'GET') { return get(uri, params, headers, otherProps); - } else if (method === 'POST') { + } + if (method === 'POST') { return post(uri, params, headers, otherProps); } return request(uri, method, params, headers, otherProps); diff --git a/packages/rax-render/src/utils/index.js b/packages/rax-render/src/utils/index.js index 9100f9619..cc73f899c 100644 --- a/packages/rax-render/src/utils/index.js +++ b/packages/rax-render/src/utils/index.js @@ -679,6 +679,7 @@ export function parseData(schema, self) { } /* 全匹配{{开头,}}结尾的变量表达式,或者对象类型JSExpression,且均不支持省略this */ +// todo: export function parseExpression(str, self) { try { const contextArr = ['"use strict";', 'var __self = arguments[0];']; @@ -697,6 +698,7 @@ export function parseExpression(str, self) { if (inSameDomain() && window.parent.__newFunc) { return window.parent.__newFunc(tarStr)(self); } + return new Function(tarStr)(self); } catch (err) { debug('parseExpression.error', err, str, self); diff --git a/packages/rax-render/src/utils/request.js b/packages/rax-render/src/utils/request.js index f03bd4203..811c54c58 100644 --- a/packages/rax-render/src/utils/request.js +++ b/packages/rax-render/src/utils/request.js @@ -4,6 +4,15 @@ import fetchJsonp from 'fetch-jsonp'; import bzbRequest from '@ali/bzb-request'; import { serialize, buildUrl, parseUrl } from '@ali/b3-one/lib/url'; +import { Table, schema } from '@ali/ui-table'; + +const webTable = new Table('yida'); +webTable.init(schema); +window._webTable = webTable; +// webTable.on('ready', () => { +// window._webTable = webTable; +// }); + export function get(dataAPI, params = {}, headers = {}, otherProps = {}) { headers = { Accept: 'application/json', @@ -169,3 +178,15 @@ export function bzb(apiCode, params, otherProps = {}) { ...otherProps }); } + +export async function webTableProxy(req) { + const { _webTable } = window; + if (!_webTable) { + const data = await { success: false, content: 'web table 尚未加载' }; + return data; + } + // const { name } = req; + // const data = await _webTable.fetch({ id: name }); + const data = await _webTable.find({ id: 'one' }); + return data; +} diff --git a/packages/react-renderer/package.json b/packages/react-renderer/package.json index 086eb1e69..ce53a7d4a 100644 --- a/packages/react-renderer/package.json +++ b/packages/react-renderer/package.json @@ -40,7 +40,7 @@ "serialize-javascript": "^1.7.0", "socket.io-client": "^2.2.0", "whatwg-fetch": "^3.0.0", - "@ali/ui-table": "^1.0.1-beta.1" + "@ali/ui-table": "^1.0.1-beta.6" }, "devDependencies": { "@alib/build-scripts": "^0.1.18",