183 lines
5.1 KiB
Plaintext
Raw Normal View History

import http from '@ohos.net.http';
2025-02-11 09:09:34 +08:00
import convertxml from '@ohos.convertxml';
import { BusinessError } from '@ohos.base';
import promptAction from '@ohos.promptAction';
import { RequestTag } from '../config';
2025-02-11 14:14:41 +08:00
interface RequestOption {
url: string
2025-02-12 09:02:36 +08:00
data?: object | string
2025-02-11 14:14:41 +08:00
xml: boolean
method: http.RequestMethod
host?: string
isNewCenter?: boolean
2025-02-12 09:02:36 +08:00
timeout?: number
2025-02-11 09:09:34 +08:00
}
2025-02-11 14:14:41 +08:00
const base: string = ""
2025-02-11 09:09:34 +08:00
2025-02-11 14:14:41 +08:00
function xmlToJSON(target: string): object {
let conv = new convertxml.ConvertXML()
const result: object = conv.convertToJSObject(target, {
2025-02-11 09:09:34 +08:00
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-02-11 14:14:41 +08:00
})
return transfer(result['_elements'])
}
2025-02-11 09:09:34 +08:00
2025-02-11 14:14:41 +08:00
function transfer(target: Array<object>, name?: string): object {
const result: object = new Object()
2025-02-11 09:09:34 +08:00
2025-02-11 14:14:41 +08:00
target.forEach((el: object) => {
const _elements: Array<object> = el['_elements']
if (el['_type'] === "element") {
if (_elements === undefined) {
return
}
const jsonObj: object = result[el['_name']]
const handleCommonArray = (obj: object) => {
if (Array.isArray(jsonObj)) {
result[el['_name']].push(obj)
} else {
result[el['_name']] = [result[el['_name']], obj]
}
}
if (_elements && _elements.length === 1 && _elements[0]['_type'] === 'text') {
if (jsonObj) {
handleCommonArray(_elements[0]['_text'])
} else {
jsonObj[el['_name']] = _elements[0]["_text"]
}
} else {
if (jsonObj) {
handleCommonArray(transfer(el['_elements'], el['_name']))
} else {
jsonObj[el['_name']] = transfer(el['_elements'], el['_name'])
}
}
} else if (el['_attributes']) {
result[name] = {
value: el['_text'],
attributes: el['__attributes']
}
}
})
return result
}
interface CenterCodeResult {
code: string
message?: string
keystr?: string
2025-02-11 09:09:34 +08:00
}
2025-02-11 14:14:41 +08:00
function dealCenterCode(message: string, isNewCenter: boolean): CenterCodeResult {
2025-02-11 09:09:34 +08:00
if (isNewCenter) {
2025-02-11 14:14:41 +08:00
const msg: object = JSON.parse(message);
const result: object = msg?.['data'][0]?.result;
2025-02-11 09:09:34 +08:00
if (result) {
2025-02-11 14:14:41 +08:00
if (result['code'] != '1') {
2025-02-11 09:09:34 +08:00
const rMessage = decodeURIComponent(message as string)
2025-02-11 14:14:41 +08:00
promptAction.showToast({
2025-02-11 09:09:34 +08:00
message: rMessage,
duration: 3000
});
2025-02-11 14:14:41 +08:00
let returnResult: CenterCodeResult = {
code: result['code'] as string,
message: result['message'] as string,
}
return returnResult
2025-02-11 09:09:34 +08:00
} else {
2025-02-11 14:14:41 +08:00
let returnResult: CenterCodeResult = {
code: result['code'] as string,
keystr: result['retval'] as string,
}
return returnResult
2025-02-11 09:09:34 +08:00
}
}
}
}
2025-02-12 09:16:01 +08:00
export default function Request<T = object>(options: RequestOption): Promise<T> {
2025-02-11 14:14:41 +08:00
return new Promise((resolve, reject) => {
const instance = http.createHttp()
const baseURL = options.host || base
instance.request(baseURL + options.url, {
method: options.method,
header: {
"Content-Type": options.xml ? "text/xml" : 'application/json'
},
2025-02-12 09:02:36 +08:00
extraData: options.xml ? options.data : JSON.stringify(options.data),
readTimeout: options.timeout || 30 * 1000
2025-02-11 14:14:41 +08:00
}).then(async data => {
let result = options.xml ? xmlToJSON(data.result as string) : data.result
let resObj: object
if (typeof result === 'string') {
result = JSON.parse(result)
}
if (result['Envelope']) {
const msgXml: string = result['Envelope']['Body']['writeObjectOutResponse']['return'];
resolve(dealCenterCode(msgXml, options.isNewCenter) as T)
2025-02-11 09:09:34 +08:00
return
}
2025-02-11 14:14:41 +08:00
if (!options.xml) {
if (result['head']['resultCode'] === '0') {
resolve(result as T)
return
2025-02-11 09:09:34 +08:00
} else {
2025-02-11 14:14:41 +08:00
const resultMessage: string = result?.['body']?.['resultMessage'] || result?.['head']?.['resultMessage']
promptAction.showToast({
message: decodeURIComponent(resultMessage),
duration: 3000
});
reject(false)
return
2025-02-11 09:09:34 +08:00
}
}
2025-02-11 14:14:41 +08:00
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)
2025-02-11 09:09:34 +08:00
}
2025-02-11 14:14:41 +08:00
}).catch((err: BusinessError) => {
console.error(RequestTag, "出错的url:", options.url, "错误信息:", JSON.stringify(err))
2025-02-11 14:14:41 +08:00
if (!err || !(err?.message)) {
reject({
code: -1
})
2025-02-11 09:09:34 +08:00
}
2025-02-11 14:14:41 +08:00
promptAction.showToast({
message: err?.message,
duration: 5000
});
reject({
code: err.code
})
})
.finally(() => {
instance.destroy()
})
})
}