Merge branch 'feat/rax-miniapp' of http://gitlab.alibaba-inc.com/ali-lowcode/ali-lowcode-engine into feat/rax-miniapp

This commit is contained in:
wanying.jwy 2020-08-24 16:04:43 +08:00
commit 473a06dba9
5 changed files with 111 additions and 92 deletions

View File

@ -50,7 +50,8 @@
"rax-view": "^1.0.0", "rax-view": "^1.0.0",
"react-is": "^16.10.1", "react-is": "^16.10.1",
"serialize-javascript": "^1.7.0", "serialize-javascript": "^1.7.0",
"whatwg-fetch": "^3.0.0" "whatwg-fetch": "^3.0.0",
"@ali/ui-table": "^1.0.1-beta.6"
}, },
"devDependencies": { "devDependencies": {
"@alib/build-scripts": "^0.1.0", "@alib/build-scripts": "^0.1.0",

View File

@ -1,11 +1,12 @@
/* eslint-disable object-curly-newline */
import { transformArrayToMap, isJSFunction, transformStringToFunction, clone, comboSkeletonConfig } from './index'; 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 = { const DS_STATUS = {
INIT: 'init', INIT: 'init',
LOADING: 'loading', LOADING: 'loading',
LOADED: 'loaded', LOADED: 'loaded',
ERROR: 'error' ERROR: 'error',
}; };
export default class DataHelper { export default class DataHelper {
@ -34,20 +35,18 @@ export default class DataHelper {
this.ajaxList = (config && config.list) || []; this.ajaxList = (config && config.list) || [];
const ajaxMap = transformArrayToMap(this.ajaxList, 'id'); const ajaxMap = transformArrayToMap(this.ajaxList, 'id');
// 删除已经移除的接口 // 删除已经移除的接口
Object.keys(this.ajaxMap).forEach(key => { Object.keys(this.ajaxMap).forEach((key) => {
if (!ajaxMap[key]) { if (!ajaxMap[key]) {
delete this.dataSourceMap[key]; delete this.dataSourceMap[key];
} }
}); });
this.ajaxMap = ajaxMap; this.ajaxMap = ajaxMap;
// 添加未加入到dataSourceMap中的接口 // 添加未加入到dataSourceMap中的接口
this.ajaxList.forEach(item => { this.ajaxList.forEach((item) => {
if (!this.dataSourceMap[item.id]) { if (!this.dataSourceMap[item.id]) {
this.dataSourceMap[item.id] = { this.dataSourceMap[item.id] = {
status: DS_STATUS.INIT, status: DS_STATUS.INIT,
load: (...args) => { load: (...args) => this.getDataSource(item.id, ...args),
return this.getDataSource(item.id, ...args);
}
}; };
} }
}); });
@ -56,33 +55,31 @@ export default class DataHelper {
generateDataSourceMap() { generateDataSourceMap() {
const res = {}; const res = {};
this.ajaxList.forEach(item => { this.ajaxList.forEach((item) => {
res[item.id] = { res[item.id] = {
status: DS_STATUS.INIT, status: DS_STATUS.INIT,
load: (...args) => { load: (...args) => this.getDataSource(item.id, ...args),
return this.getDataSource(item.id, ...args);
}
}; };
}); });
return res; return res;
} }
updateDataSourceMap(id, data, error) { 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].data = data;
this.dataSourceMap[id].status = error ? DS_STATUS.ERROR : DS_STATUS.LOADED; this.dataSourceMap[id].status = error ? DS_STATUS.ERROR : DS_STATUS.LOADED;
} }
getInitData() { getInitData() {
const initSyncData = this.parser(this.ajaxList).filter(item => { const initSyncData = this.parser(this.ajaxList).filter((item) => {
if (item.isInit) { if (item.isInit) {
this.dataSourceMap[item.id].status = DS_STATUS.LOADING; this.dataSourceMap[item.id].status = DS_STATUS.LOADING;
return true; return true;
} }
return false; return false;
}); });
return this.asyncDataHandler(initSyncData).then(res => { return this.asyncDataHandler(initSyncData).then((res) => {
let dataHandler = this.config.dataHandler; let { dataHandler } = this.config;
if (isJSFunction(dataHandler)) { if (isJSFunction(dataHandler)) {
dataHandler = transformStringToFunction(dataHandler.value); dataHandler = transformStringToFunction(dataHandler.value);
} }
@ -91,7 +88,6 @@ export default class DataHelper {
return dataHandler.call(this.host, res); return dataHandler.call(this.host, res);
} catch (e) { } catch (e) {
console.error('请求数据处理函数运行出错', e); console.error('请求数据处理函数运行出错', e);
return;
} }
}); });
} }
@ -119,15 +115,15 @@ export default class DataHelper {
? params || options.params ? params || options.params
: { : {
...options.params, ...options.params,
...params ...params,
}, },
headers: { headers: {
...options.headers, ...options.headers,
...headers ...headers,
}, },
...otherProps ...otherProps,
} },
} },
]) ])
.then(res => { .then(res => {
try { try {
@ -180,79 +176,81 @@ export default class DataHelper {
method: 'POST', method: 'POST',
params: { params: {
data: JSON.stringify(doserReq), data: JSON.stringify(doserReq),
_tb_token_ _tb_token_,
} },
} },
}); });
} }
if (allReq.length === 0) resolve({}); if (allReq.length === 0) resolve({});
const res = {}; const res = {};
Promise.all( Promise.all(
allReq.map(item => { allReq.map(
return new Promise(resolve => { (item) =>
const { type, id, dataHandler, options } = item; new Promise((resolve) => {
const doFetch = (type, options) => { const { type, id, dataHandler, options } = item;
this.fetchOne(type, options) const fetchHandler = async (data, error) => {
.then(async data => { if (type === 'doServer') {
if (afterRequest) { if (!Array.isArray(data)) {
this.appHelper.utils.afterRequest(item, data, undefined, async (data, error) => { data = [data];
await fetchHandler(data, error);
});
} else {
await fetchHandler(data, undefined);
} }
}) doserList.forEach(async (id, idx) => {
.catch(async err => { const req = this.ajaxMap[id];
if (afterRequest) { if (req) {
// 必须要这么调用否则beforeRequest中的this会丢失 res[id] = await this.dataHandler(id, req.dataHandler, data && data[idx], error);
this.appHelper.utils.afterRequest(item, undefined, err, async (data, error) => { this.updateDataSourceMap(id, res[id], error);
await fetchHandler(data, error); }
}); });
} else { } else {
await fetchHandler(undefined, err); // debugger;
} res[id] = await this.dataHandler(id, dataHandler, data, error);
}); this.updateDataSourceMap(id, res[id], error);
};
const fetchHandler = async (data, error) => {
if (type === 'doServer') {
if (!Array.isArray(data)) {
data = [data];
} }
doserList.forEach(async (id, idx) => { resolve();
const req = this.ajaxMap[id]; };
if (req) { const doFetch = (type, req) => {
res[id] = await this.dataHandler(id, req.dataHandler, data && data[idx], error); this.fetchOne(type, req)
this.updateDataSourceMap(id, res[id], error); .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 { } else {
res[id] = await this.dataHandler(id, dataHandler, data, error); this.dataSourceMap[id].status = DS_STATUS.LOADING;
this.updateDataSourceMap(id, res[id], error);
} }
resolve(); // 请求切片
}; if (beforeRequest) {
// 必须要这么调用否则beforeRequest中的this会丢失
if (type === 'doServer') { this.appHelper.utils.beforeRequest(item, clone(options), () => doFetch(type, item));
doserList.forEach(item => { } else {
this.dataSourceMap[item].status = DS_STATUS.LOADING; doFetch(type, item);
}); }
} 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);
}
});
})
) )
.then(() => { .then(() => {
resolve(res); resolve(res);
}) })
.catch(e => { .catch((e) => {
reject(e); reject(e);
}); });
}); });
@ -266,12 +264,12 @@ export default class DataHelper {
try { try {
return await dataHandler.call(this.host, data, error); return await dataHandler.call(this.host, data, error);
} catch (e) { } catch (e) {
console.error('[' + id + ']单个请求数据处理函数运行出错', e); console.error(`[${id}]单个请求数据处理函数运行出错`, e);
return;
} }
} }
fetchOne(type, options) { fetchOne(type, req) {
const { options } = req;
let { uri, method = 'GET', headers, params, ...otherProps } = options; let { uri, method = 'GET', headers, params, ...otherProps } = options;
otherProps = otherProps || {}; otherProps = otherProps || {};
switch (type) { switch (type) {
@ -284,19 +282,16 @@ export default class DataHelper {
return bzb(uri, params, { return bzb(uri, params, {
method, method,
headers, headers,
...otherProps ...otherProps,
}); });
case 'legao': case 'legao':
// todo: return webTableProxy(req);
if (method === 'JSONP') {
return jsonp(otherProps.url, params, otherProps);
}
break;
default: default:
method = method.toUpperCase(); method = method.toUpperCase();
if (method === 'GET') { if (method === 'GET') {
return get(uri, params, headers, otherProps); return get(uri, params, headers, otherProps);
} else if (method === 'POST') { }
if (method === 'POST') {
return post(uri, params, headers, otherProps); return post(uri, params, headers, otherProps);
} }
return request(uri, method, params, headers, otherProps); return request(uri, method, params, headers, otherProps);

View File

@ -679,6 +679,7 @@ export function parseData(schema, self) {
} }
/* 全匹配{{开头,}}结尾的变量表达式或者对象类型JSExpression且均不支持省略this */ /* 全匹配{{开头,}}结尾的变量表达式或者对象类型JSExpression且均不支持省略this */
// todo:
export function parseExpression(str, self) { export function parseExpression(str, self) {
try { try {
const contextArr = ['"use strict";', 'var __self = arguments[0];']; const contextArr = ['"use strict";', 'var __self = arguments[0];'];
@ -697,6 +698,7 @@ export function parseExpression(str, self) {
if (inSameDomain() && window.parent.__newFunc) { if (inSameDomain() && window.parent.__newFunc) {
return window.parent.__newFunc(tarStr)(self); return window.parent.__newFunc(tarStr)(self);
} }
return new Function(tarStr)(self); return new Function(tarStr)(self);
} catch (err) { } catch (err) {
debug('parseExpression.error', err, str, self); debug('parseExpression.error', err, str, self);

View File

@ -4,6 +4,15 @@ import fetchJsonp from 'fetch-jsonp';
import bzbRequest from '@ali/bzb-request'; import bzbRequest from '@ali/bzb-request';
import { serialize, buildUrl, parseUrl } from '@ali/b3-one/lib/url'; 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 = {}) { export function get(dataAPI, params = {}, headers = {}, otherProps = {}) {
headers = { headers = {
Accept: 'application/json', Accept: 'application/json',
@ -169,3 +178,15 @@ export function bzb(apiCode, params, otherProps = {}) {
...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;
}

View File

@ -40,7 +40,7 @@
"serialize-javascript": "^1.7.0", "serialize-javascript": "^1.7.0",
"socket.io-client": "^2.2.0", "socket.io-client": "^2.2.0",
"whatwg-fetch": "^3.0.0", "whatwg-fetch": "^3.0.0",
"@ali/ui-table": "^1.0.1-beta.1" "@ali/ui-table": "^1.0.1-beta.6"
}, },
"devDependencies": { "devDependencies": {
"@alib/build-scripts": "^0.1.18", "@alib/build-scripts": "^0.1.18",