2024-01-05 11:11:15 +08:00
|
|
|
import http from '@ohos.net.http';
|
|
|
|
|
import convertxml from '@ohos.convertxml';
|
|
|
|
|
import prompt from '@ohos.prompt'
|
2024-07-05 09:34:03 +08:00
|
|
|
import { sendGreen } from '../../pages/judgeSDK/utils/judgeCommon';
|
2024-05-09 13:42:56 +08:00
|
|
|
import {GlobalConfig} from '../../config/index'
|
2024-08-14 14:46:33 +08:00
|
|
|
import FileUtil from '../../common/utils/File';
|
|
|
|
|
import { dateFormat } from '../utils/tools'
|
|
|
|
|
|
2024-01-05 11:11:15 +08:00
|
|
|
const config = {
|
2024-05-09 13:42:56 +08:00
|
|
|
host:GlobalConfig.host,
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let httpRequest = http.createHttp();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author: Renjun Su
|
|
|
|
|
* @date: 2023/2/20
|
|
|
|
|
* @desc 接口请求封装
|
|
|
|
|
* @param {method}
|
|
|
|
|
* @param {xml} xml请求的数据 xml是字符串
|
|
|
|
|
* @param {data} post请求的数据
|
|
|
|
|
* @param {params} get请求的数据
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export default async function request(req: any) {
|
2024-07-31 13:47:40 +08:00
|
|
|
const {url,params = {},data = {},xml,method = 'get',host,isNewCenter = false} = req;
|
2024-01-05 11:11:15 +08:00
|
|
|
const options = {
|
|
|
|
|
method: http.RequestMethod[method.toUpperCase()],
|
|
|
|
|
header: {
|
|
|
|
|
'Content-Type': xml ? 'text/xml' : 'application/json'
|
|
|
|
|
},
|
2024-12-29 21:43:12 +08:00
|
|
|
extraData: xml ? data : JSON.stringify(data),
|
|
|
|
|
connectTimeout:120000,
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
//params转url拼接参数
|
|
|
|
|
let paramsStr = Reflect.ownKeys(params).reduce((p: string, n: string) => (`${p}${n}=${params[n]}&`), '?') || '';
|
|
|
|
|
paramsStr = paramsStr.toString();
|
|
|
|
|
paramsStr = paramsStr.substring(0, paramsStr.length-1)
|
|
|
|
|
try {
|
2024-07-16 17:23:42 +08:00
|
|
|
let baseUrl=host?host:globalThis.host
|
2024-01-31 14:35:16 +08:00
|
|
|
// let baseUrl=host?config.csptHost:config.host'
|
2024-12-30 13:20:49 +08:00
|
|
|
console.log('optionsoptions',JSON.stringify(data).length)
|
2024-08-15 13:42:36 +08:00
|
|
|
console.log('响应头地址' + baseUrl,url)
|
2024-01-05 11:11:15 +08:00
|
|
|
const {result,responseCode} = await httpRequest.request(`${baseUrl}${url}${paramsStr}`, options);
|
2024-08-14 14:46:33 +08:00
|
|
|
// fileUtil.editFile(`${folderPath}/request.txt`, JSON.stringify(arrList))
|
|
|
|
|
// writeLog({
|
|
|
|
|
// time:dateFormat(new Date()),
|
|
|
|
|
// url,
|
|
|
|
|
// result:JSON.stringify(result),
|
|
|
|
|
// extraData: JSON.stringify(data),
|
|
|
|
|
// state:'end'
|
|
|
|
|
// })
|
2024-08-05 09:47:44 +08:00
|
|
|
console.log('响应头地址' + JSON.stringify(result))
|
2024-07-09 11:11:31 +08:00
|
|
|
let res:any = xml ? xmlToJson(result,url) : result;
|
2024-01-05 11:11:15 +08:00
|
|
|
console.log('响应头地址' + JSON.stringify(res))
|
2024-08-14 14:46:33 +08:00
|
|
|
|
2024-01-05 11:11:15 +08:00
|
|
|
let resObj = null;
|
|
|
|
|
if(typeof res === "string"){
|
|
|
|
|
res=JSON.parse(res)
|
|
|
|
|
}
|
|
|
|
|
//处理中心服务code
|
|
|
|
|
if(res.Envelope){
|
|
|
|
|
const msgXml = res.Envelope.Body.writeObjectOutResponse.return;
|
2024-11-18 15:16:42 +08:00
|
|
|
//Envelope.Body.writeObjectOutResponse.return
|
2024-07-31 13:47:40 +08:00
|
|
|
const dd = handleCenterCode(msgXml,isNewCenter);
|
2024-06-27 20:53:36 +08:00
|
|
|
return dd
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!xml){
|
|
|
|
|
if(res.head.resultCode === '0'){
|
|
|
|
|
return res
|
|
|
|
|
}else{
|
|
|
|
|
const resultMessage = res?.body?.resultMessage || res?.head?.resultMessage
|
|
|
|
|
prompt.showToast({
|
|
|
|
|
message: decodeURIComponent(resultMessage),
|
|
|
|
|
duration: 3000
|
|
|
|
|
});
|
2024-08-21 11:57:03 +08:00
|
|
|
return false
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for( let i in res ) {
|
|
|
|
|
resObj = res[i].head
|
|
|
|
|
}
|
2024-08-21 11:57:03 +08:00
|
|
|
console.info('jiangsong:res in request' + url + JSON.stringify(resObj))
|
2024-01-05 11:11:15 +08:00
|
|
|
if(resObj.resultCode === '0') {
|
|
|
|
|
return res
|
|
|
|
|
}else {
|
|
|
|
|
prompt.showToast({
|
|
|
|
|
message: decodeURIComponent(resObj.resultMessage),
|
|
|
|
|
duration: 3000
|
|
|
|
|
});
|
2024-08-19 09:32:41 +08:00
|
|
|
return res
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2024-10-28 13:47:03 +08:00
|
|
|
console.info('surenjunerror',JSON.stringify(e))
|
2024-07-03 16:21:56 +08:00
|
|
|
if(!e||!(e?.message)){
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
const code = e?.code;
|
2024-01-31 14:35:16 +08:00
|
|
|
prompt.showToast({
|
|
|
|
|
message: e?.message,
|
|
|
|
|
duration: 5000
|
|
|
|
|
});
|
|
|
|
|
switch (code){
|
|
|
|
|
//断网
|
|
|
|
|
case 2300007: return 2300007
|
|
|
|
|
default:
|
|
|
|
|
}
|
2024-01-05 11:11:15 +08:00
|
|
|
return false
|
|
|
|
|
console.info('test-error' + url + ' error:resp: '+ JSON.stringify(e))
|
|
|
|
|
httpRequest.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-14 14:46:33 +08:00
|
|
|
async function writeLog(param){
|
|
|
|
|
const fileUtil = new FileUtil(globalThis.context)
|
|
|
|
|
const folderPath = await fileUtil.initFolder(`/errorMsg/`);
|
|
|
|
|
fileUtil.editFile(`${folderPath}/request.txt`, JSON.stringify(param)+`\n`)
|
2024-01-05 11:11:15 +08:00
|
|
|
|
2024-08-14 14:46:33 +08:00
|
|
|
}
|
2024-01-05 11:11:15 +08:00
|
|
|
//xml格式转JSON
|
2024-07-09 11:11:31 +08:00
|
|
|
function xmlToJson(result,url) {
|
|
|
|
|
console.log("xmlToJson begin",url);
|
2024-01-05 11:11:15 +08:00
|
|
|
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}
|
|
|
|
|
|
|
|
|
|
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);
|
2024-07-09 11:11:31 +08:00
|
|
|
console.log("xmlToJson end",url);
|
2024-01-05 11:11:15 +08:00
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//处理中心服务code
|
2024-07-31 13:47:40 +08:00
|
|
|
function handleCenterCode(msgXml,isNewCenter){
|
|
|
|
|
//新监管
|
|
|
|
|
if(isNewCenter){
|
|
|
|
|
const msg = JSON.parse(msgXml);
|
|
|
|
|
const result = msg?.data[0]?.result;
|
|
|
|
|
if(result){
|
2024-09-03 19:28:07 +08:00
|
|
|
const {code,message,retval} = result
|
2024-07-31 13:47:40 +08:00
|
|
|
if(code != '1'){
|
2024-08-12 10:19:31 +08:00
|
|
|
const rMessage = decodeURIComponent(message as string)
|
2024-08-26 15:09:13 +08:00
|
|
|
// globalThis.title=rMessage
|
|
|
|
|
// globalThis.type='1'
|
|
|
|
|
// globalThis.errorDialog.open()
|
2024-07-31 13:47:40 +08:00
|
|
|
prompt.showToast({
|
2024-08-12 10:19:31 +08:00
|
|
|
message:rMessage,
|
|
|
|
|
duration: 3000
|
2024-07-31 13:47:40 +08:00
|
|
|
});
|
2024-08-12 10:19:31 +08:00
|
|
|
return {code,message}
|
2024-07-31 13:47:40 +08:00
|
|
|
}else{
|
2024-09-03 19:28:07 +08:00
|
|
|
return { code ,keystr:retval}
|
2024-07-31 13:47:40 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 11:11:15 +08:00
|
|
|
//正则匹配code message字段
|
2024-08-28 16:39:18 +08:00
|
|
|
const [code,message,keystr] = [/<code>(.*)<\/code>/i,/<message>(.*)<\/message>/i,/<keystr>(.*)<\/keystr>/i].map(pattern=>{
|
2024-01-05 11:11:15 +08:00
|
|
|
const patternArr = pattern.exec(msgXml);
|
2024-09-03 19:28:07 +08:00
|
|
|
return patternArr && patternArr[1]
|
2024-01-05 11:11:15 +08:00
|
|
|
});
|
2024-08-28 16:39:18 +08:00
|
|
|
|
|
|
|
|
if(code != '1'){
|
2024-01-05 11:11:15 +08:00
|
|
|
prompt.showToast({
|
2024-07-31 13:47:40 +08:00
|
|
|
message: decodeURIComponent(message as string),
|
2024-01-05 11:11:15 +08:00
|
|
|
duration: 3000
|
|
|
|
|
});
|
2024-08-26 15:09:13 +08:00
|
|
|
// globalThis.type='1'
|
|
|
|
|
// globalThis.title=decodeURIComponent(message as string)
|
|
|
|
|
// globalThis.errorDialog.open()
|
2024-08-12 10:19:31 +08:00
|
|
|
return {code,message:decodeURIComponent(message)}
|
2024-01-05 11:11:15 +08:00
|
|
|
}else{
|
2024-08-28 16:39:18 +08:00
|
|
|
return { code ,keystr, message }
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//JSON转xml格式
|
|
|
|
|
const deeml = (elements,_name?)=>{
|
|
|
|
|
const json = {}
|
|
|
|
|
|
|
|
|
|
elements.map(ele =>{
|
|
|
|
|
const _elements = ele._elements
|
|
|
|
|
if(ele._type === 'element'){
|
|
|
|
|
if(_elements==undefined){
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const thisJson = json[ele._name]
|
|
|
|
|
if(_elements && _elements.length === 1 && _elements[0]._type === 'text'){
|
|
|
|
|
// 如果值存在了
|
|
|
|
|
if(thisJson){
|
|
|
|
|
handleCommonArr(_elements[0]._text)
|
|
|
|
|
}else{
|
|
|
|
|
json[ele._name] = _elements[0]._text
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
if(thisJson){
|
|
|
|
|
handleCommonArr( deeml(ele._elements,ele._name) )
|
|
|
|
|
}else{
|
|
|
|
|
json[ele._name] = deeml(ele._elements,ele._name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//通用处理重复标签
|
|
|
|
|
function handleCommonArr(obj){
|
|
|
|
|
if(thisJson){
|
|
|
|
|
if(Array.isArray(thisJson)){
|
|
|
|
|
json[ele._name].push(obj)
|
|
|
|
|
}else{
|
|
|
|
|
json[ele._name] = [json[ele._name],obj]
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
json[ele._name] = obj
|
|
|
|
|
}
|
|
|
|
|
}0
|
|
|
|
|
}else{
|
|
|
|
|
//标签上有属性
|
|
|
|
|
if(ele._attributes){
|
|
|
|
|
json[_name] = {
|
|
|
|
|
value: ele._text,
|
|
|
|
|
attributes:ele.__attributes
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
return json
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function isEmpty(param) {
|
|
|
|
|
return!Object.keys(param).length;
|
|
|
|
|
}
|