307 lines
7.5 KiB
JavaScript
307 lines
7.5 KiB
JavaScript
import * as http from "./utils/axios.js";
|
||
|
||
const BASE_URL = location.origin;
|
||
|
||
class Query {
|
||
static bodyParams = true; //设置请求体里需不需要传body
|
||
constructor(params) {
|
||
if (!params) {
|
||
params = {};
|
||
}
|
||
this.filters = params.filters || [];
|
||
this.fields = params.fields || null;
|
||
this.currPage = params.currPage >= 1 ? params.currPage : 1;
|
||
this.pageSize = params.pageSize >= 0 ? params.pageSize : 0;
|
||
this.order = params.order || null;
|
||
this.httpConfig = null; //针对需要传不同请求头的情况
|
||
this._type = "cellsys"; //默认查询结果为cellsys
|
||
}
|
||
|
||
setHttpConfig(httpConfig) {
|
||
this.httpConfig = httpConfig;
|
||
}
|
||
|
||
setType(type) {
|
||
if (type) {
|
||
this._type = type;
|
||
}
|
||
}
|
||
|
||
getType() {
|
||
return this._type;
|
||
}
|
||
|
||
addFilter(fieldName, operator, value, type) {
|
||
let fieldObj = {
|
||
field: fieldName,
|
||
operator: operator,
|
||
value: value,
|
||
type: type,
|
||
};
|
||
this.filters.push(fieldObj);
|
||
}
|
||
|
||
setFields(fields) {
|
||
//数组
|
||
this.fields = fields;
|
||
}
|
||
|
||
setCurrPage(currPage) {
|
||
this.currPage = currPage;
|
||
}
|
||
|
||
setPageSize(pageSize) {
|
||
this.pageSize = pageSize;
|
||
}
|
||
|
||
setOrder(orderObj) {
|
||
this.order = orderObj;
|
||
}
|
||
|
||
getQuery() {
|
||
let param = this._getParam();
|
||
let content = null;
|
||
let cellsysContent = param;
|
||
if (Query.bodyParams) {
|
||
cellsysContent = { body: param };
|
||
}
|
||
switch (this._type.toLocaleLowerCase()) {
|
||
default:
|
||
case "cellsys":
|
||
content = cellsysContent;
|
||
break;
|
||
case "mongodb":
|
||
content = param;
|
||
break;
|
||
}
|
||
return content;
|
||
}
|
||
|
||
_getParam() {
|
||
let params = {};
|
||
// let params = {'body': null}
|
||
if (this.filters.length > 0) {
|
||
params["filter"] = this.filters;
|
||
}
|
||
if (this.fields) {
|
||
params["fields"] = this.fields;
|
||
}
|
||
if (this.currPage || this.currPage === 0) {
|
||
params["currPage"] = this.currPage;
|
||
} else {
|
||
params["currPage"] = 1;
|
||
}
|
||
if (this.pageSize || this.pageSize === 0) {
|
||
params["pageSize"] = this.pageSize;
|
||
} else {
|
||
params["pageSize"] = 0;
|
||
}
|
||
if (this.order) {
|
||
params["order"] = this.order;
|
||
}
|
||
return params;
|
||
}
|
||
}
|
||
|
||
class QueryTask {
|
||
static hostName = "";
|
||
constructor(url, getPageInfo) {
|
||
//过滤java接口
|
||
if (url.indexOf("cellsys/api") !== -1) {
|
||
this.url = url;
|
||
} else if (url.indexOf("https") !== -1) {
|
||
//过滤应用模板部分调用的完整接口地址
|
||
this.url = url;
|
||
} else {
|
||
//接口版本改为V2版本
|
||
this.url = "/v2" + url;
|
||
}
|
||
this.url = QueryTask.hostName + this.url;
|
||
this.getPageInfo = getPageInfo; //是否返回分页信息
|
||
}
|
||
|
||
execute(query) {
|
||
let params = null;
|
||
if (query) {
|
||
params = query.getQuery();
|
||
} else {
|
||
query = new Query();
|
||
}
|
||
// this.getPageInfo = query.pageSize !== 0;
|
||
|
||
let httpConfig = query.httpConfig;
|
||
return this._queryByCellsys(params, httpConfig);
|
||
}
|
||
|
||
_queryByCellsys(param, httpConfig) {
|
||
return new Promise((resolve, reject) => {
|
||
if (param) {
|
||
http
|
||
.post(this.url, param, httpConfig)
|
||
.then((res) => {
|
||
if (res) {
|
||
let result;
|
||
if (this.url.indexOf("v2/rpc") === -1) {
|
||
result = res[0];
|
||
} else {
|
||
//兼容v2版本的api,返回的数据不是数组而是对象
|
||
result = res;
|
||
}
|
||
if (this.getPageInfo) {
|
||
resolve({
|
||
currPage: result.currPage,
|
||
pageSize: result.pageSize,
|
||
totalCount: result.totalCount,
|
||
totalPage: result.totalPage,
|
||
data: result.data ? result.data : [],
|
||
});
|
||
} else {
|
||
resolve(result.data ? result.data : []);
|
||
}
|
||
} else {
|
||
console.log("返回格式有误", res);
|
||
}
|
||
})
|
||
.catch((e) => {
|
||
reject(e);
|
||
});
|
||
} else {
|
||
http
|
||
.post(this.url)
|
||
.then((res) => {
|
||
resolve(res || null);
|
||
})
|
||
.catch((e) => {
|
||
reject(e);
|
||
});
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
class EditTask {
|
||
constructor(url) {
|
||
//过滤java接口
|
||
if (url.indexOf("cellsys/api") !== -1) {
|
||
this.url = url;
|
||
} else if (url.indexOf("https") !== -1) {
|
||
//过滤应用模板部分调用的完整接口地址
|
||
this.url = url;
|
||
} else {
|
||
//接口版本改为V2版本
|
||
this.url = "/v2" + url;
|
||
}
|
||
this.params = { body: {} };
|
||
this.httpConfig = null; //针对需要传不同请求头的情况
|
||
}
|
||
|
||
addParam(param_name, param_value) {
|
||
if (param_name) {
|
||
this.params["body"][param_name] = param_value;
|
||
}
|
||
}
|
||
|
||
setHttpConfig(httpConfig) {
|
||
this.httpConfig = httpConfig;
|
||
}
|
||
|
||
execute() {
|
||
return new Promise((resolve, reject) => {
|
||
let params = this.params,
|
||
body = params["body"];
|
||
if (!(Object.keys(body).length > 0)) {
|
||
body = null;
|
||
}
|
||
http
|
||
.post(this.url, params, this.httpConfig)
|
||
.then((res) => {
|
||
// if (res && res.length > 0 && res[0].code === 200) {
|
||
// resolve({ data: res[0].data, message: res[0].message });
|
||
// } else {
|
||
// console.log('返回格式有误', res);
|
||
// }
|
||
if (res) {
|
||
let result;
|
||
if (this.url.indexOf("v2/rpc") === -1) {
|
||
result = res[0];
|
||
} else {
|
||
//兼容v2版本的api,返回的数据不是数组而是对象
|
||
result = res;
|
||
}
|
||
if (result.code === 200) {
|
||
resolve({ data: result.data, message: result.message });
|
||
}
|
||
} else {
|
||
console.log("返回格式有误", res);
|
||
}
|
||
})
|
||
.catch((err) => {
|
||
console.log(err);
|
||
reject(err);
|
||
});
|
||
});
|
||
}
|
||
}
|
||
|
||
class SubmitTask {
|
||
constructor(url) {
|
||
//过滤java接口
|
||
|
||
if (url.indexOf("cellsys/api") !== -1) {
|
||
this.url = url;
|
||
} else if (url.indexOf("https") !== -1) {
|
||
//过滤应用模板部分调用的完整接口地址
|
||
this.url = url;
|
||
} else {
|
||
//接口版本改为V2版本
|
||
this.url = "/v2" + url;
|
||
}
|
||
this.params = { body: {} };
|
||
}
|
||
|
||
addParam(param_name, param_value) {
|
||
if (param_name) {
|
||
this.params["body"][param_name] = param_value;
|
||
}
|
||
}
|
||
|
||
execute() {
|
||
return new Promise((resolve, reject) => {
|
||
let params = this.params,
|
||
body = params["body"];
|
||
if (!(Object.keys(body).length > 0)) {
|
||
body = null;
|
||
}
|
||
http
|
||
.post(this.url, params)
|
||
.then((res) => {
|
||
// if (res.length > 0 && res[0].code === 200) {
|
||
// resolve({ data: res[0].data, message: res[0].message });
|
||
// } else {
|
||
// console.log('返回格式有误', res);
|
||
// }
|
||
if (res) {
|
||
let result;
|
||
if (this.url.indexOf("v2/rpc") === -1) {
|
||
result = res[0];
|
||
} else {
|
||
//兼容v2版本的api,返回的数据不是数组而是对象
|
||
result = res;
|
||
}
|
||
if (result.code === 200) {
|
||
resolve({ data: result.data, message: result.message });
|
||
}
|
||
} else {
|
||
console.log("返回格式有误", res);
|
||
}
|
||
})
|
||
.catch((err) => {
|
||
console.log(err);
|
||
reject(err);
|
||
});
|
||
});
|
||
}
|
||
}
|
||
|
||
export { Query, QueryTask, EditTask, SubmitTask, BASE_URL };
|