| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  | import http from '@ohos.net.http'; | 
					
						
							|  |  |  | import convertxml from '@ohos.convertxml'; | 
					
						
							|  |  |  | import { BusinessError } from '@ohos.base'; | 
					
						
							|  |  |  | import promptAction from '@ohos.promptAction'; | 
					
						
							|  |  |  | import { RequestTag } from '../config'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface RequestOption { | 
					
						
							|  |  |  |   url: string | 
					
						
							|  |  |  |   data?: object | string | 
					
						
							|  |  |  |   xml: boolean | 
					
						
							|  |  |  |   method: http.RequestMethod | 
					
						
							|  |  |  |   host?: string | 
					
						
							|  |  |  |   isNewCenter?: boolean | 
					
						
							|  |  |  |   timeout?: number | 
					
						
							| 
									
										
										
										
											2025-04-22 14:35:34 +08:00
										 |  |  |   accept?: boolean | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function xmlToJSON(target: string): object { | 
					
						
							| 
									
										
										
										
											2025-04-15 14:24:27 +08:00
										 |  |  |   console.log("进入xml解析") | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |   let conv = new convertxml.ConvertXML() | 
					
						
							|  |  |  |   const result: object = conv.convertToJSObject(target, { | 
					
						
							|  |  |  |     trim: false, | 
					
						
							|  |  |  |     declarationKey: "_declaration", | 
					
						
							|  |  |  |     instructionKey: "_instruction", | 
					
						
							|  |  |  |     attributesKey: "_attributes", | 
					
						
							|  |  |  |     textKey: "_text", | 
					
						
							|  |  |  |     cdataKey: "_cdata", | 
					
						
							|  |  |  |     doctypeKey: "_doctype", | 
					
						
							|  |  |  |     commentKey: "_comment", | 
					
						
							|  |  |  |     parentKey: "_parent", | 
					
						
							|  |  |  |     typeKey: "_type", | 
					
						
							|  |  |  |     nameKey: "_name", | 
					
						
							|  |  |  |     elementsKey: "_elements", | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2025-04-15 14:24:27 +08:00
										 |  |  |   console.log("xml解析完成", JSON.stringify(result)) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |   return transfer(result['_elements']) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function setObj(target: Record<string, object>, key: string, value: object) { | 
					
						
							|  |  |  |   target[key] = value | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function transfer(target: Array<object>, name?: string): object { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |   const result: Record<string, ESObject> = {}; // 使用 Record 类型以便动态添加属性 | 
					
						
							|  |  |  |   target.forEach((el: ESObject) => { | 
					
						
							|  |  |  |     const _elements: Array<object> = el['_elements']; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |     if (el['_type'] === "element") { | 
					
						
							|  |  |  |       if (_elements === undefined) { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |       const jsonObj: ESObject = result[el['_name']]; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |       const handleCommonArray = (obj: object) => { | 
					
						
							|  |  |  |         if (Array.isArray(jsonObj)) { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |           jsonObj.push(obj); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |           result[el['_name']] = [jsonObj, obj]; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |       if (_elements && _elements.length && _elements[0]['_type'] === 'text') { | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         if (jsonObj) { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |           handleCommonArray(_elements[0]['_text']); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |           result[el['_name']] = _elements[0]['_text']; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         if (jsonObj) { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |           handleCommonArray(transfer(el['_elements'], el['_name'])); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |           result[el['_name']] = transfer(el['_elements'], el['_name']); | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else if (el['_attributes'] && name) { | 
					
						
							|  |  |  |       result[name] = { | 
					
						
							|  |  |  |         value: el['_text'], | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |         attributes: el['_attributes'], // 修复属性名 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |   }); | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface CenterCodeResult { | 
					
						
							|  |  |  |   code: string | 
					
						
							|  |  |  |   message?: string | 
					
						
							|  |  |  |   keystr?: string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function dealCenterCode(message: string, isNewCenter: boolean = false): CenterCodeResult | undefined { | 
					
						
							|  |  |  |   if (isNewCenter) { | 
					
						
							|  |  |  |     const msg: object = JSON.parse(message); | 
					
						
							|  |  |  |     const result: object = msg?.['data'][0]?.result; | 
					
						
							|  |  |  |     if (result) { | 
					
						
							|  |  |  |       if (result['code'] != '1') { | 
					
						
							|  |  |  |         const rMessage = decodeURIComponent(message as string) | 
					
						
							|  |  |  |         promptAction.showToast({ | 
					
						
							|  |  |  |           message: rMessage, | 
					
						
							|  |  |  |           duration: 3000 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         let returnResult: CenterCodeResult = { | 
					
						
							|  |  |  |           code: result['code'] as string, | 
					
						
							|  |  |  |           message: result['message'] as string, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return returnResult | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         let returnResult: CenterCodeResult = { | 
					
						
							|  |  |  |           code: result['code'] as string, | 
					
						
							|  |  |  |           keystr: result['retval'] as string, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return returnResult | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return undefined | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type RequestResult = Object | object | string | CenterCodeResult | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default function Request<T extends RequestResult>(options: RequestOption): Promise<T> { | 
					
						
							|  |  |  |   return new Promise((resolve, reject) => { | 
					
						
							| 
									
										
										
										
											2025-04-14 10:59:37 +08:00
										 |  |  |     const base: string = AppStorage.get<string>("host") || "http://127.0.0.1" | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |     const instance = http.createHttp() | 
					
						
							|  |  |  |     const baseURL = options.host || base | 
					
						
							| 
									
										
										
										
											2025-04-14 10:59:37 +08:00
										 |  |  |     console.log(RequestTag, "基础url:", baseURL) | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |     console.log(RequestTag, "参数", JSON.stringify(options.data)) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |     instance.request(baseURL + options.url, { | 
					
						
							|  |  |  |       method: options.method, | 
					
						
							|  |  |  |       header: { | 
					
						
							| 
									
										
										
										
											2025-04-22 14:35:34 +08:00
										 |  |  |         "Content-Type": options.xml ? "text/xml" : 'application/json', | 
					
						
							|  |  |  |         "Accept": options.accept ? "application/json" : "*/*", | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |       }, | 
					
						
							|  |  |  |       extraData: options.xml ? options.data : JSON.stringify(options.data), | 
					
						
							|  |  |  |       readTimeout: options.timeout || 30 * 1000 | 
					
						
							|  |  |  |     }).then(async data => { | 
					
						
							| 
									
										
										
										
											2025-04-14 14:35:18 +08:00
										 |  |  |       console.log(RequestTag, options.url, "返回的数据", JSON.stringify(data)) | 
					
						
							| 
									
										
										
										
											2025-04-22 14:35:34 +08:00
										 |  |  |       // let result = options.xml ? xmlToJSON(data.result as string) : data.result | 
					
						
							|  |  |  |       let result = options.xml ? (options.accept ? data.result : xmlToJSON(data.result as string)) : data.result; | 
					
						
							| 
									
										
										
										
											2025-04-15 14:24:27 +08:00
										 |  |  |       console.log("结束xml解析") | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |       let resObj: object = new Object() | 
					
						
							|  |  |  |       if (typeof result === 'string') { | 
					
						
							|  |  |  |         result = JSON.parse(result) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (result['Envelope'] !== undefined) { | 
					
						
							|  |  |  |         const msgXml: string = result['Envelope']['Body']['writeObjectOutResponse']['return']; | 
					
						
							|  |  |  |         resolve(dealCenterCode(msgXml, options.isNewCenter) as T) | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-04-22 14:35:34 +08:00
										 |  |  |       if (!options.xml || (options.xml && options.accept)) { | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |         if (result['head']['resultCode'] === '0') { | 
					
						
							|  |  |  |           resolve(result as T) | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           const resultMessage: string = result?.['body']?.['resultMessage'] || result?.['head']?.['resultMessage'] | 
					
						
							|  |  |  |           promptAction.showToast({ | 
					
						
							|  |  |  |             message: decodeURIComponent(resultMessage), | 
					
						
							|  |  |  |             duration: 3000 | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |           reject(false) | 
					
						
							|  |  |  |           return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Object.values(result).forEach((item: object) => { | 
					
						
							|  |  |  |         resObj = item['head'] | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       if (resObj['resultCode'] === '0') { | 
					
						
							|  |  |  |         resolve(result as T) | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         promptAction.showToast({ | 
					
						
							|  |  |  |           message: decodeURIComponent(resObj['resultMessage']), | 
					
						
							|  |  |  |           duration: 3000 | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         reject(result) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }).catch((err: BusinessError) => { | 
					
						
							| 
									
										
										
										
											2025-04-14 18:13:34 +08:00
										 |  |  |       console.error(RequestTag, "出错的url:", baseURL, options.url, "错误信息:", JSON.stringify(err)) | 
					
						
							| 
									
										
										
										
											2025-04-10 10:28:07 +08:00
										 |  |  |       if (!err || !(err?.message)) { | 
					
						
							|  |  |  |         reject({ | 
					
						
							|  |  |  |           code: -1 | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       promptAction.showToast({ | 
					
						
							|  |  |  |         message: err?.message, | 
					
						
							|  |  |  |         duration: 5000 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       reject({ | 
					
						
							|  |  |  |         code: err.code | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |       .finally(() => { | 
					
						
							|  |  |  |         instance.destroy() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } |