ebu_ecology_dev1/javascript/common/Utils.js

150 lines
4.1 KiB
JavaScript

window.Utils = {
/**
* @author youhong.ai
* @desc 发起请求
*/
request: function (url, type = "GET", data, isAsync = true, success = () => {
}, error = () => {
}, complete = () => {
}, contentType = 'application/json', beforeSend = () => {
}) {
let options = {
url,
type,
dataType: "json",
contentType,
async: isAsync,
data,
beforeSend,
success,
error,
complete,
}
if (contentType == 'application/json') {
options.data = JSON.stringify(data)
}
return $.ajax(options)
},
/**
* @author youhong.ai
* @desc 发起请求
*/
api: function (requestOptions = {
url: "",
type: "GET",
data: "",
isAsync: true,
success: () => {
},
error: () => {
},
complete: () => {
},
contentType: 'application/json',
beforeSend: () => {
}
}) {
let options = Object.assign({
url: "",
type: "GET",
data: "",
isAsync: true,
success: () => {
},
error: () => {
},
complete: () => {
},
contentType: 'application/json',
beforeSend: () => {
}
}, requestOptions)
return $.ajax(options)
},
/**
* @author youhong.ai
* @desc 获取react组件实例
*/
findReact: function (dom, traverseUp = 0) {
const key = Object.keys(dom).find(key => {
return key.startsWith("__reactFiber$") // react 17+
|| key.startsWith("__reactInternalInstance$")
|| key.startsWith("__reactEventHandlers$"); // react <17
});
const domFiber = dom[key];
if (domFiber == null) return null;
// react <16
if (domFiber._currentElement) {
let compFiber = domFiber._currentElement._owner;
for (let i = 0; i < traverseUp; i++) {
compFiber = compFiber._currentElement._owner;
}
return compFiber._instance;
}
// react 16+
const GetCompFiber = fiber => {
let parentFiber = fiber.return;
while (typeof parentFiber.type == "string") {
parentFiber = parentFiber.return;
}
return parentFiber;
};
let compFiber = GetCompFiber(domFiber);
for (let i = 0; i < traverseUp; i++) {
compFiber = GetCompFiber(compFiber);
}
return compFiber.stateNode;
},
/**
* 转换字段名为字段ID
* @param fieldName 字段名称
* @returns {*|string}
*/
convertNameToIdUtil: function (fieldName) {
let fieldIds = [];
if (fieldName instanceof Array) {
fieldName.forEach(item => {
fieldIds.push(Utils.convertNameObjToId(item))
})
return fieldIds.join(',')
}
return Utils.convertNameObjToId(fieldName)
},
/**
* 将字段名称转为字段id
* @param fieldObj 字段名称对象 {string|object}
* @returns {*}
*/
convertNameObjToId: function (fieldObj = {fieldName: '', table: 'main'}) {
if (typeof fieldObj === 'object') {
return WfForm.convertFieldNameToId(fieldObj.fieldName, fieldObj.table)
}
return WfForm.convertFieldNameToId(fieldObj)
},
/**
* 根据字段名称查询字段值
* @param fieldName 字段名称
* @param rowIndex 明细行下表(明细获取才传递)
* @returns {*} 字段值
*/
getFiledValueByName: function (fieldName, rowIndex) {
return WfForm.getFieldValue(Utils.convertNameObjToId(fieldName) + (rowIndex ? '_' + rowIndex : ''))
},
/**
* 通过字段名称修改字段值
* @param fieldName 字段名称
* @param value 值
*/
changeFieldValueByName: function (fieldName, value) {
WfForm.changeFieldValue(Utils.convertNameObjToId(fieldName), {value})
}
}