2025-01-14 15:34:13 +08:00
|
|
|
import http from '@ohos.net.http';
|
|
|
|
|
import convertxml from '@ohos.convertxml';
|
2025-01-20 08:50:40 +08:00
|
|
|
import prompt from '@ohos.prompt';
|
|
|
|
|
import { tcpUtil } from './TcpRequest';
|
|
|
|
|
import Prompt from '@system.prompt';
|
|
|
|
|
import tempRequest from './tempRequest';
|
2025-03-18 17:27:06 +08:00
|
|
|
import connection from '@ohos.net.connection';
|
2025-03-04 21:34:12 +08:00
|
|
|
|
2025-03-05 19:27:15 +08:00
|
|
|
export function tcpRequest<T>(req: any): Promise<T> {
|
2025-01-20 08:50:40 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let {url,params = {},data = {},xml,method = 'get',host,isNewCenter = false, timeout} = req;
|
2025-01-14 19:43:14 +08:00
|
|
|
try {
|
|
|
|
|
const options = {
|
|
|
|
|
method: http.RequestMethod[method.toUpperCase()],
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': xml ? 'text/xml' : 'application/json'
|
|
|
|
|
},
|
|
|
|
|
extraData: xml ? data : JSON.stringify(data),
|
|
|
|
|
}
|
|
|
|
|
let paramsStr = Reflect.ownKeys(params).reduce((p: string, n: string) => (`${p}${n}=${params[n]}&`), '?') || '';
|
|
|
|
|
paramsStr = paramsStr.toString();
|
|
|
|
|
paramsStr = paramsStr.substring(0, paramsStr.length - 1)
|
|
|
|
|
let baseUrl = host ? host : globalThis.host
|
|
|
|
|
console.log('响应头地址1' + baseUrl, url, options.extraData.length)
|
2025-01-20 08:50:40 +08:00
|
|
|
|
2025-03-04 21:34:12 +08:00
|
|
|
// globalThis.TcpClient.request(`${baseUrl}${url}${paramsStr}`, {
|
2025-01-20 08:50:40 +08:00
|
|
|
tcpUtil.request(`${baseUrl}${url}${paramsStr}`, {
|
|
|
|
|
method: options.method,
|
|
|
|
|
data: xml ? data : JSON.stringify(data),
|
|
|
|
|
type: xml ? 1 : 0,
|
|
|
|
|
contentType: xml ? 'text/xml' : 'application/json',
|
2025-03-04 21:34:12 +08:00
|
|
|
timeout: timeout || 15 * 1000
|
2025-01-20 08:50:40 +08:00
|
|
|
}, (data) => {
|
2025-03-04 21:34:12 +08:00
|
|
|
const result = JSON.parse(JSON.stringify(data))
|
|
|
|
|
console.log('响应头地址' + JSON.stringify(result))
|
|
|
|
|
let res: any = xml ? xmlToJson(result, url) : result;
|
|
|
|
|
console.log('响应头地址' + JSON.stringify(res))
|
2025-01-14 19:43:14 +08:00
|
|
|
|
2025-03-04 21:34:12 +08:00
|
|
|
let resObj = null;
|
|
|
|
|
if (typeof res === "string") {
|
|
|
|
|
res = JSON.parse(res)
|
|
|
|
|
}
|
|
|
|
|
//处理中心服务code
|
|
|
|
|
if (res.Envelope) {
|
|
|
|
|
const msgXml = res.Envelope.Body.writeObjectOutResponse.return;
|
|
|
|
|
const dd = handleCenterCode(msgXml, isNewCenter);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
resolve(dd)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-01-14 15:34:13 +08:00
|
|
|
|
2025-03-04 21:34:12 +08:00
|
|
|
if (!xml) {
|
|
|
|
|
if (res.head.resultCode === '0') {
|
2025-01-20 08:50:40 +08:00
|
|
|
resolve(res)
|
2025-01-14 19:43:14 +08:00
|
|
|
return
|
|
|
|
|
} else {
|
2025-03-04 21:34:12 +08:00
|
|
|
const resultMessage = res?.body?.resultMessage || res?.head?.resultMessage
|
2025-01-20 08:50:40 +08:00
|
|
|
Prompt.showToast({
|
2025-03-04 21:34:12 +08:00
|
|
|
message: decodeURIComponent(resultMessage),
|
2025-01-14 19:43:14 +08:00
|
|
|
duration: 3000
|
|
|
|
|
});
|
2025-03-04 21:34:12 +08:00
|
|
|
reject(false)
|
2025-01-14 19:43:14 +08:00
|
|
|
}
|
2025-01-20 08:50:40 +08:00
|
|
|
}
|
2025-03-04 21:34:12 +08:00
|
|
|
for (let i in res) {
|
|
|
|
|
resObj = res[i].head
|
|
|
|
|
}
|
|
|
|
|
console.info('jiangsong:res in request' + url + JSON.stringify(resObj))
|
|
|
|
|
if (resObj.resultCode === '0') {
|
|
|
|
|
resolve(res)
|
|
|
|
|
return
|
|
|
|
|
} else {
|
|
|
|
|
Prompt.showToast({
|
|
|
|
|
message: decodeURIComponent(resObj.resultMessage),
|
|
|
|
|
duration: 3000
|
|
|
|
|
});
|
|
|
|
|
reject(res)
|
2025-01-20 08:50:40 +08:00
|
|
|
}
|
2025-03-04 21:34:12 +08:00
|
|
|
}, (error) => {
|
|
|
|
|
console.info('test-error0' + url + ' error:resp: ' + JSON.stringify(error.message))
|
2025-01-20 08:50:40 +08:00
|
|
|
Prompt.showToast({
|
|
|
|
|
message: error?.message,
|
2025-01-14 19:43:14 +08:00
|
|
|
duration: 5000
|
2025-01-14 15:34:13 +08:00
|
|
|
});
|
2025-03-04 21:34:12 +08:00
|
|
|
reject(error)
|
2025-01-14 19:43:14 +08:00
|
|
|
})
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.info('test-error' + url + ' error:resp: ' + JSON.stringify(e))
|
2025-01-14 15:13:50 +08:00
|
|
|
}
|
2025-01-14 19:43:14 +08:00
|
|
|
})
|
2025-03-04 21:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 19:05:07 +08:00
|
|
|
function getNetWorkStatus() {
|
|
|
|
|
connection.getDefaultNet().then(handle => {
|
|
|
|
|
console.log("[http request] network handle", JSON.stringify(handle))
|
|
|
|
|
connection.getNetCapabilities(handle).then(res => {
|
|
|
|
|
console.log("[http request] network result", JSON.stringify(res))
|
|
|
|
|
})
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.log("[http request] network error", JSON.stringify(err))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 21:34:12 +08:00
|
|
|
export default async function request<T>(req: any): Promise<T> {
|
2025-03-05 16:22:24 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
tempRequest(req).then((response) => {
|
|
|
|
|
console.log("[http request] only http request success")
|
|
|
|
|
resolve(response as T)
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.log("[http request] only http request error")
|
|
|
|
|
reject(err)
|
2025-03-18 19:05:07 +08:00
|
|
|
}).finally(() => {
|
|
|
|
|
getNetWorkStatus()
|
2025-03-04 21:34:12 +08:00
|
|
|
})
|
2025-03-05 16:22:24 +08:00
|
|
|
})
|
2025-01-14 15:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//xml格式转JSON
|
2025-01-14 19:43:14 +08:00
|
|
|
function xmlToJson(result, url) {
|
|
|
|
|
console.log("xmlToJson begin", url);
|
|
|
|
|
let xmlOptions = {
|
|
|
|
|
trim: false,
|
|
|
|
|
declarationKey: "_declaration",
|
|
|
|
|
instructionKey: "_instruction",
|
|
|
|
|
attributesKey: "_attributes",
|
|
|
|
|
textKey: "_text",
|
|
|
|
|
cdataKey: "_cdata",
|
|
|
|
|
doctypeKey: "_doctype",
|
|
|
|
|
commentKey: "_comment",
|
|
|
|
|
parentKey: "_parent",
|
|
|
|
|
typeKey: "_type",
|
|
|
|
|
nameKey: "_name",
|
|
|
|
|
elementsKey: "_elements",
|
|
|
|
|
"skipPreprocess": true
|
|
|
|
|
}
|
2025-01-14 15:34:13 +08:00
|
|
|
|
|
|
|
|
let strXml = result.toString();
|
|
|
|
|
let conv = new convertxml.ConvertXML();
|
|
|
|
|
|
|
|
|
|
console.log("xmlToJson result.length = " + result.length);
|
|
|
|
|
console.log("xmlToJson result content = " + result);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
let {_elements:xmlArr} = conv.convertToJSObject(strXml, xmlOptions);
|
|
|
|
|
console.log("xmlToJson deeml begin");
|
|
|
|
|
let res = deeml(xmlArr);
|
2025-01-14 19:43:14 +08:00
|
|
|
console.log("xmlToJson end", url);
|
2025-01-14 15:34:13 +08:00
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//处理中心服务code
|
2025-01-14 19:43:14 +08:00
|
|
|
function handleCenterCode(msgXml, isNewCenter) {
|
2025-01-14 15:34:13 +08:00
|
|
|
//新监管
|
2025-01-14 19:43:14 +08:00
|
|
|
if (isNewCenter) {
|
2025-01-14 15:34:13 +08:00
|
|
|
const msg = JSON.parse(msgXml);
|
|
|
|
|
const result = msg?.data[0]?.result;
|
2025-01-14 19:43:14 +08:00
|
|
|
if (result) {
|
2025-01-14 15:34:13 +08:00
|
|
|
const {code,message,retval} = result
|
2025-01-14 19:43:14 +08:00
|
|
|
if (code != '1') {
|
2025-01-14 15:34:13 +08:00
|
|
|
const rMessage = decodeURIComponent(message as string)
|
|
|
|
|
prompt.showToast({
|
2025-01-14 19:43:14 +08:00
|
|
|
message: rMessage,
|
2025-01-14 15:34:13 +08:00
|
|
|
duration: 3000
|
|
|
|
|
});
|
2025-01-14 19:43:14 +08:00
|
|
|
return { code, message }
|
|
|
|
|
} else {
|
|
|
|
|
return { code, keystr: retval }
|
2025-01-14 15:13:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 15:34:13 +08:00
|
|
|
//正则匹配code message字段
|
2025-01-14 19:43:14 +08:00
|
|
|
const [code, message, keystr] = [/<code>(.*)<\/code>/i, /<message>(.*)<\/message>/i, /<keystr>(.*)<\/keystr>/i].map(pattern => {
|
|
|
|
|
const patternArr = pattern.exec(msgXml);
|
2025-01-14 15:34:13 +08:00
|
|
|
return patternArr && patternArr[1]
|
|
|
|
|
});
|
2025-01-14 15:13:50 +08:00
|
|
|
|
2025-01-14 19:43:14 +08:00
|
|
|
if (code != '1') {
|
2025-01-14 15:34:13 +08:00
|
|
|
prompt.showToast({
|
|
|
|
|
message: decodeURIComponent(message as string),
|
|
|
|
|
duration: 3000
|
|
|
|
|
});
|
2025-01-14 19:43:14 +08:00
|
|
|
return { code, message: decodeURIComponent(message) }
|
|
|
|
|
} else {
|
|
|
|
|
return { code, keystr, message }
|
2025-01-14 15:34:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//JSON转xml格式
|
2025-01-14 19:43:14 +08:00
|
|
|
const deeml = (elements, _name?) => {
|
2025-01-14 15:34:13 +08:00
|
|
|
const json = {}
|
|
|
|
|
|
2025-01-14 19:43:14 +08:00
|
|
|
elements.map(ele => {
|
2025-01-14 15:34:13 +08:00
|
|
|
const _elements = ele._elements
|
2025-01-14 19:43:14 +08:00
|
|
|
if (ele._type === 'element') {
|
|
|
|
|
if (_elements == undefined) {
|
2025-01-14 15:13:50 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-01-14 15:34:13 +08:00
|
|
|
const thisJson = json[ele._name]
|
2025-01-14 19:43:14 +08:00
|
|
|
if (_elements && _elements.length === 1 && _elements[0]._type === 'text') {
|
2025-01-14 15:34:13 +08:00
|
|
|
// 如果值存在了
|
2025-01-14 19:43:14 +08:00
|
|
|
if (thisJson) {
|
2025-01-14 15:34:13 +08:00
|
|
|
handleCommonArr(_elements[0]._text)
|
2025-01-14 19:43:14 +08:00
|
|
|
} else {
|
2025-01-14 15:34:13 +08:00
|
|
|
json[ele._name] = _elements[0]._text
|
2025-01-14 15:13:50 +08:00
|
|
|
}
|
2025-01-14 19:43:14 +08:00
|
|
|
} else {
|
|
|
|
|
if (thisJson) {
|
|
|
|
|
handleCommonArr(deeml(ele._elements, ele._name))
|
|
|
|
|
} else {
|
|
|
|
|
json[ele._name] = deeml(ele._elements, ele._name)
|
2025-01-07 15:50:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-14 15:13:50 +08:00
|
|
|
|
2025-01-14 15:34:13 +08:00
|
|
|
//通用处理重复标签
|
2025-01-14 19:43:14 +08:00
|
|
|
function handleCommonArr(obj) {
|
|
|
|
|
if (thisJson) {
|
|
|
|
|
if (Array.isArray(thisJson)) {
|
2025-01-14 15:34:13 +08:00
|
|
|
json[ele._name].push(obj)
|
2025-01-14 19:43:14 +08:00
|
|
|
} else {
|
|
|
|
|
json[ele._name] = [json[ele._name], obj]
|
2025-01-14 15:34:13 +08:00
|
|
|
}
|
2025-01-14 19:43:14 +08:00
|
|
|
} else {
|
2025-01-14 15:34:13 +08:00
|
|
|
json[ele._name] = obj
|
2025-01-07 15:50:48 +08:00
|
|
|
}
|
2025-01-14 19:43:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
} else {
|
2025-01-14 15:34:13 +08:00
|
|
|
//标签上有属性
|
2025-01-14 19:43:14 +08:00
|
|
|
if (ele._attributes) {
|
2025-01-14 15:34:13 +08:00
|
|
|
json[_name] = {
|
|
|
|
|
value: ele._text,
|
2025-01-14 19:43:14 +08:00
|
|
|
attributes: ele.__attributes
|
2025-01-14 15:13:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-07 15:50:48 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-14 15:34:13 +08:00
|
|
|
});
|
|
|
|
|
return json
|
|
|
|
|
};
|
2024-01-05 11:11:15 +08:00
|
|
|
|
2025-01-20 08:50:40 +08:00
|
|
|
|