class CusUtils { /** * @author youhong.ai * @desc 发起请求 */ request(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(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(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(fieldName) { let fieldIds = []; if (fieldName instanceof Array) { fieldName.forEach(item => { fieldIds.push(Utils.convertNameObjToId(item)) }) return fieldIds.join(',') } return Utils.convertNameObjToId(fieldName) } getQueryString(name) { let reg = new RegExp("(^|&|\/?)" + name + "=([^&]*)(&|$)", "i"); let searchStr = window.location.href if (searchStr.startsWith('&')) { searchStr = searchStr.substr(1) } let search = searchStr.match(reg) if (search != null) { return unescape(search[2]); } return null; } /** * 将字段名称转为字段id * @param fieldObj 字段名称对象 {string|object} * @returns {*} */ convertNameObjToId(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(fieldName, rowIndex) { return WfForm.getFieldValue(Utils.convertNameObjToId(fieldName) + (rowIndex ? '_' + rowIndex : '')) } /** * 通过字段名称修改字段值 * @param fieldName 字段名称 * @param value 值 */ changeFieldValueByName(fieldName, value) { WfForm.changeFieldValue(Utils.convertNameObjToId(fieldName), {value}) } } window.Utils = new CusUtils()