tjx
2025-04-10 aa845e0ae6620b80bc04ce9a27e7140b9d1fe680
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.gs.xiaomi;
 
import com.alibaba.fastjson.JSON;
import com.gs.xiaomi.config.DataAcquisitionConfiguration;
import com.gs.xiaomi.dto.BizDocument;
import com.gs.xiaomi.dto.BizDocumentResult;
import com.gs.xiaomi.dto.ZfmWsApiRequest;
import com.gs.xiaomi.service.SoapApiService;
import com.gs.xiaomi.util.SoapXmlBuilder;
import okhttp3.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.StringReader;
 
@SpringBootTest
class XiaomiApplicationTests {
 
    @Autowired
    private SoapApiService service;
 
    @Test
    void contextLoads() throws Exception {
        ZfmWsApiRequest req = new ZfmWsApiRequest();
        req.setIvCode(DataAcquisitionConfiguration.IV_CODEXM104);
        req.setIvEvent("");
        req.setIvFlag("");
        BizDocument doc = new BizDocument();
        doc.setBizDate("2025-04-01");
        doc.setLifnr(DataAcquisitionConfiguration.LIFNR);
        req.setIvInfo(doc);
        req.setIvPass(DataAcquisitionConfiguration.getIvPass(DataAcquisitionConfiguration.IV_CODEXM104));
        req.setIvUser(DataAcquisitionConfiguration.IV_USER);
        String soapXml = SoapXmlBuilder.build(req);
        System.out.println(soapXml);
    }
 
    @Test
    void testService() throws Exception {
        ZfmWsApiRequest req = new ZfmWsApiRequest();
        req.setIvCode(DataAcquisitionConfiguration.IV_CODEXM104);
        req.setIvEvent("");
        req.setIvFlag("");
        BizDocument doc = new BizDocument();
        doc.setBizDate("2025-04-01");
        doc.setLifnr(DataAcquisitionConfiguration.LIFNR);
        req.setIvInfo(doc);
        req.setIvPass(DataAcquisitionConfiguration.getIvPass(DataAcquisitionConfiguration.IV_CODEXM104));
        req.setIvUser(DataAcquisitionConfiguration.IV_USER);
 
        BizDocumentResult bizDocumentResult = service.callAndParse(DataAcquisitionConfiguration.XM104_URL, req);
 
        System.out.println(JSON.toJSONString(bizDocumentResult));
    }
 
    @Test
    void sta() throws IOException, JAXBException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/xml");
        RequestBody body = RequestBody.create(mediaType, "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\r\n    <soapenv:Header/>\r\n    <soapenv:Body>\r\n        <urn:ZFM_WS_API>\r\n            <IV_CODE>VDATA.XM104</IV_CODE>\r\n            <IV_EVENT></IV_EVENT>\r\n            <IV_FLAG></IV_FLAG>\r\n            <IV_INFO><![CDATA[<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<document>\r\n    <BIZ_DATE>2025-04-01</BIZ_DATE>\r\n    <I_LIFNR>177301</I_LIFNR>\r\n</document>]]></IV_INFO>\r\n            <IV_PASS>D25932DF5772D0A2DF9620A2A0AB121A</IV_PASS>\r\n            <IV_USER>V177301</IV_USER>\r\n        </urn:ZFM_WS_API>\r\n    </soapenv:Body>\r\n</soapenv:Envelope>");
        Request request = new Request.Builder()
                .url("https://mipoq.p.mi.com/XISOAPAdapter/MessageServlet?p=ic/9e8a118fb0b7379aa11c2a5c2860982a&senderParty=&senderService=ZJ_XIAOBU_QAS&receiverParty=&receiverService=&interface=SI_ZFM_WS_API_OUT&interfaceNamespace=http://xiaomi.com/srm/b2b")
                .method("POST", body)
//                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Content-Type", "application/xml")
                .addHeader("Authorization", "Basic UkZDWkpYSUFPQlU6cHBCOVIhSGU=")
                .addHeader("Accept", "*/*")
//                .addHeader("Host", "mipoq.p.mi.com")
                .addHeader("Connection", "keep-alive")
//                .addHeader("Cookie", "saplb_*=(J2EE4281420)4281450; MYSAPSSO2=AjExMDAgABJwb3J0YWw6UkZDWkpYSUFPQlWIAAdkZWZhdWx0AQALUkZDWkpYSUFPQlUCAAMwMDADAANQT1EEAAwyMDI1MDQwOTAwMTcFAAQAAAAICgALUkZDWkpYSUFPQlX%2FAQUwggEBBgkqhkiG9w0BBwKggfMwgfACAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHATGB0DCBzQIBATAiMB0xDDAKBgNVBAMTA1BPUTENMAsGA1UECxMESjJFRQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMjUwNDA5MDAxNzU2WjAjBgkqhkiG9w0BCQQxFgQUvvJ1HSnOLckboGYaMR9Wiaem6z4wCQYHKoZIzjgEAwQvMC0CFQDKLPrpUC19VM2gcslWaHAnh7dfxQIUNs2dTxwGcKdNU%2FdAQPs4f08yaw4%3D; JSESSIONID=_6bPldDq9mFJPXEd11bc-GvrWMAVlgFqVEEA_SAPF-GrpRbcuqxMK_gShBYGdmdp; JSESSIONMARKID=nUs7PQMlYui9UqFCm4Z-ZRqUiI8YQ_hLJPJWpUQQA")
                .build();
        Response response = client.newCall(request).execute();
 
        if (response.isSuccessful() && response.body() != null) {
            String bodyStr = response.body().string();
 
            String evCode = extractEvCode(bodyStr);
            if (!"Y".equalsIgnoreCase(evCode)) {
                throw new RuntimeException("SOAP business failed, EV_CODE: " + evCode);
            }
 
            String decodedXml = decodeXmlEntities(bodyStr);
 
            String innerXml = extractCdata(decodedXml);
            if (innerXml == null) throw new RuntimeException("No CDATA <document> found in response");
 
//            JAXBContext context = JAXBContext.newInstance(BizDocumentResult.class);
//            Unmarshaller unmarshaller = context.createUnmarshaller();
            //BizDocumentResult unmarshal = (BizDocumentResult) unmarshaller.unmarshal(new StringReader(decodedXml));
            System.out.println(JSON.toJSONString(innerXml));
        } else {
            throw new RuntimeException("SOAP request failed: " + response.code() + " - " + response.message());
        }
    }
 
 
    @Test
    void contextLoads2() throws Exception {
        String bodyStr = "<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>" +
                "  <SOAP:Header/>" +
                "  <SOAP:Body xmlns:urn='urn:sap-com:document:sap:rfc:functions'>" +
                "    <n0:ZFM_WS_APIResponse xmlns:n0='urn:sap-com:document:sap:rfc:functions' xmlns:prx='urn:sap.com:proxy:FRD:/1SAI/TAS82FD47019F21003C052D:752'>" +
                "      <EV_CODE>Y</EV_CODE>" +
                "      <EV_EVENT/>" +
                "      <EV_FLAG/>" +
                "      <EV_RESULT>&#60;?xml version=\"1.0\" encoding=\"utf-8\"?&#62;&#60;document&#62;&#60;et_headers type=\"h\"&#62;&#60;et_header idx=\"1\"&#62;&#60;zzasn&#62;9311031471&#60;/zzasn&#62;&#60;zzasn_type&#62;BL&#60;/zzasn_type&#62;&#60;imclr&#62;&#60;/imclr&#62;&#60;lifnr&#62;0000177301&#60;/lifnr&#62;&#60;vendor_name&#62;浙江小步出行有限公司&#60;/vendor_name&#62;&#60;zzship_addr&#62;测试&#60;/zzship_addr&#62;&#60;zzship_name&#62;11&#60;/zzship_name&#62;&#60;zzship_phon&#62;1111&#60;/zzship_phon&#62;&#60;zzship_date&#62;2025-04-01&#60;/zzship_date&#62;&#60;zzwarehouse_id&#62;0000017560&#60;/zzwarehouse_id&#62;&#60;zzwarehouse&#62;中小件-北京中心仓&#60;/zzwarehouse&#62;&#60;zzreceive_addr&#62;北京市大兴区经济开发区科创十街十五号(亦庄小米产业园)T1楼1层&#60;/zzreceive_addr&#62;&#60;zzcarrier&#62;&#60;/zzcarrier&#62;&#60;zzdriver_name&#62;&#60;/zzdriver_name&#62;&#60;zzdriver_phon&#62;&#60;/zzdriver_phon&#62;&#60;zzarrive_time&#62;20250402&#60;/zzarrive_time&#62;&#60;zzarrive_time1&#62;00:00:00&#60;/zzarrive_time1&#62;&#60;zzcarrier_no&#62;&#60;/zzcarrier_no&#62;&#60;road_transport_no&#62;&#60;/road_transport_no&#62;&#60;zztrans_model&#62;&#60;/zztrans_model&#62;&#60;zzdriver_license&#62;&#60;/zzdriver_license&#62;&#60;zzlicense_numb&#62;&#60;/zzlicense_numb&#62;&#60;zzseal_no&#62;&#60;/zzseal_no&#62;&#60;zzcanceled&#62;&#60;/zzcanceled&#62;&#60;ship_email&#62;11@163.com&#60;/ship_email&#62;&#60;lifnr_dg&#62;&#60;/lifnr_dg&#62;&#60;short_name&#62;&#60;/short_name&#62;&#60;reserve_date&#62;&#60;/reserve_date&#62;&#60;reserve_time&#62;&#60;/reserve_time&#62;&#60;bukrs&#62;1110&#60;/bukrs&#62;&#60;asn_desc&#62;&#60;/asn_desc&#62;&#60;price_stat&#62;&#60;/price_stat&#62;&#60;final_contact&#62;&#60;/final_contact&#62;&#60;final_phone&#62;&#60;/final_phone&#62;&#60;final_addr&#62;&#60;/final_addr&#62;&#60;final_company&#62;&#60;/final_company&#62;&#60;status&#62;S002&#60;/status&#62;&#60;status_sn&#62;&#60;/status_sn&#62;&#60;purchase_mode&#62;&#60;/purchase_mode&#62;&#60;hub_lifnr&#62;&#60;/hub_lifnr&#62;&#60;items type=\"h\"&#62;&#60;item idx=\"1\"&#62;&#60;zzasn&#62;9311031471&#60;/zzasn&#62;&#60;zzitem_id&#62;0000000010&#60;/zzitem_id&#62;&#60;zzpo_no&#62;4500173648&#60;/zzpo_no&#62;&#60;zzpo_item_id&#62;0000000010&#60;/zzpo_item_id&#62;&#60;zzreqnr&#62;&#60;/zzreqnr&#62;&#60;zzreqln&#62;&#60;/zzreqln&#62;&#60;zzproduct_id&#62;950102000010&#60;/zzproduct_id&#62;&#60;zzvproduct_id&#62;&#60;/zzvproduct_id&#62;&#60;zzsku&#62;BHR4008CN&#60;/zzsku&#62;&#60;zzquantity&#62;5.000&#60;/zzquantity&#62;&#60;zzunit&#62;ST&#60;/zzunit&#62;&#60;brand&#62;&#60;/brand&#62;&#60;place&#62;&#60;/place&#62;&#60;brand_type&#62;&#60;/brand_type&#62;&#60;is_identification&#62;&#60;/is_identification&#62;&#60;zzopen_qty&#62;5.000&#60;/zzopen_qty&#62;&#60;changed_at&#62;20250401071304&#60;/changed_at&#62;&#60;created_at&#62;20250401071304&#60;/created_at&#62;&#60;app_models&#62;11-AD&#60;/app_models&#62;&#60;ext_doc_no&#62;8897300&#60;/ext_doc_no&#62;&#60;ext_doc_item&#62;2884654&#60;/ext_doc_item&#62;&#60;address_detail&#62;北京市大兴区经济开发区科创十街十五号(亦庄小米产业园)T1楼1层&#60;/address_detail&#62;&#60;batch_id&#62;&#60;/batch_id&#62;&#60;batch_row&#62;&#60;/batch_row&#62;&#60;xmsto_no&#62;&#60;/xmsto_no&#62;&#60;xmsto_item_no&#62;&#60;/xmsto_item_no&#62;&#60;modelnumber&#62;&#60;/modelnumber&#62;&#60;shipment_id&#62;&#60;/shipment_id&#62;&#60;shipmentid_num&#62;0&#60;/shipmentid_num&#62;&#60;maktx&#62;米家智能侧吸油烟机&#60;/maktx&#62;&#60;bednr&#62;&#60;/bednr&#62;&#60;ware_type_from&#62;&#60;/ware_type_from&#62;&#60;ware_type_to&#62;&#60;/ware_type_to&#62;&#60;act_gr_qty&#62;0.000&#60;/act_gr_qty&#62;&#60;pn_matnr_l&#62;&#60;/pn_matnr_l&#62;&#60;pn_matnr_name&#62;&#60;/pn_matnr_name&#62;&#60;/item&#62;&#60;/items&#62;&#60;box_items type=\"h\"&#62;&#60;/box_items&#62;&#60;box_sums type=\"h\"&#62;&#60;/box_sums&#62;&#60;purchase_mode2&#62;&#60;/purchase_mode2&#62;&#60;receiver&#62;杨丹丹,周晨光&#60;/receiver&#62;&#60;receive_phone&#62;18811578183,13002157815&#60;/receive_phone&#62;&#60;receive_addr&#62;北京市大兴区经济开发区科创十街十五号(亦庄小米产业园)T1楼1层&#60;/receive_addr&#62;&#60;receive_warehouse_id&#62;0000017560&#60;/receive_warehouse_id&#62;&#60;receive_warehouse_short&#62;中小件-北京中心仓&#60;/receive_warehouse_short&#62;&#60;product_type&#62;GM&#60;/product_type&#62;&#60;ekorg&#62;2120&#60;/ekorg&#62;&#60;ekotx&#62;&#60;/ekotx&#62;&#60;stock_org_from&#62;&#60;/stock_org_from&#62;&#60;stock_org_from_desc&#62;&#60;/stock_org_from_desc&#62;&#60;stock_org_to&#62;MI_IOT&#60;/stock_org_to&#62;&#60;stock_org_to_desc&#62;生态链库存组织(IOT)&#60;/stock_org_to_desc&#62;&#60;migo_type&#62;&#60;/migo_type&#62;&#60;zrproject&#62;A1&#60;/zrproject&#62;&#60;logistics_no&#62;&#60;/logistics_no&#62;&#60;logistics_company&#62;&#60;/logistics_company&#62;&#60;last4_phone_number&#62;&#60;/last4_phone_number&#62;&#60;logistics_company_name&#62;&#60;/logistics_company_name&#62;&#60;business_model&#62;&#60;/business_model&#62;&#60;/et_header&#62;&#60;/et_headers&#62;&#60;ev_message&#62;&#60;/ev_message&#62;&#60;ev_msgty&#62;S&#60;/ev_msgty&#62;&#60;/document&#62;</EV_RESULT>" +
                "    </n0:ZFM_WS_APIResponse>" +
                "  </SOAP:Body>" +
                "</SOAP:Envelope>";
 
        String evCode = extractEvCode(bodyStr);
        if (!"Y".equalsIgnoreCase(evCode)) {
            throw new RuntimeException("SOAP business failed, EV_CODE: " + evCode);
        }
 
        String decodedXml = decodeXmlEntities(bodyStr);
 
        String innerXml = extractCdata(decodedXml);
        if (innerXml == null) throw new RuntimeException("No CDATA <document> found in response");
 
 
        JAXBContext context = JAXBContext.newInstance(BizDocumentResult.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        BizDocumentResult unmarshal = (BizDocumentResult) unmarshaller.unmarshal(new StringReader(innerXml));
        System.out.println(JSON.toJSONString(unmarshal));
    }
 
    private String extractEvCode(String xml) {
        int start = xml.indexOf("<EV_CODE>");
        int end = xml.indexOf("</EV_CODE>", start);
        if (start == -1 || end == -1) return null;
        return xml.substring(start + 9, end).trim();
    }
 
    private String decodeXmlEntities(String xml) {
        return xml.replace("&#60;", "<")
                .replace("&#62;", ">")
                .replace("&lt;", "<")
                .replace("&gt;", ">")
                .replace("&amp;", "&");
    }
 
    private String extractCdata(String xml) {
        int cdataStart = xml.indexOf("<EV_RESULT>");
        int cdataEnd = xml.indexOf("</EV_RESULT>", cdataStart);
        if (cdataStart == -1 || cdataEnd == -1) return null;
        return xml.substring(cdataStart + 11, cdataEnd).trim();
    }
 
}