注册 登录
编程论坛 VFP论坛

VFP如何实现与其他ERP软件数据接口对接

asis 发布于 2023-12-20 12:51, 343 次点击
    用VFP开发的一个管理软件,需要与ERP软件实现数据接口对接,就是实现在VFP管理软件新增和修改一条存货记录会马上通过
数据接口对ERP软件的相关数据进行更新。ERP软件供应商提供了JAVA语言的存货档案增加和修改的示例,最后面是获取开放平台
token的JAVA代码。

请问以下JAVA代码能否转换为VFP代码?是否非常复杂?希望各位老师能指点一下,谢谢。


以下是ERP软件供应商提供了JAVA语言的存货档案增加和修改的示例:

package com.dz.tplus.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
public class Demo {

    /**
     * appKey 固定值
     */
    private static final String APP_KEY = "tvkBjvkP";

    /**
     * appSecret 固定值
     */
    private static final String APP_SECRET = "9C79DCA99C7B3DF37651453D75D7380C";

    /**
     * OpenApi服务URI 固定值
     */
    private static final String BASE_URI = "https://openapi.

    /**
     * TOKEN代理服务地址 固定值
     */
    private static final String TOKEN_PROXY_URL = "https://k847700d02.yicp.fun/tplus_token";

    /**
     * Spring的RestTemplate组件,用于HTTP调用
     */
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 存货档案增加示例
     */
    public void invAdd() throws Exception {

        // 获取开放平台token
        String openToken = getOpenToken();

        // 如果openToken没有获取到,则抛出异常
        if (StringUtils.isEmpty(openToken)) {
            throw new Exception("新增存货失败,未获取到OpenToken()。");
        }

        // 设置header参数
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); // 此行等同于headers.set("content-type", "application/json");
        headers.set("appKey", APP_KEY);
        headers.set("appSecret", APP_SECRET);
        headers.set("openToken", openToken);

        // 设置body参数,格式为JSON结构的字符串
        String body = STR."""
                {
                    "dto":{
                        "Code":"01002",
                        "Name":"中南海1mg",
                        "Shorthand":"ZNH1mg",
                        "Specification":"10mg",
                        "InventoryClass":{
                            "Code":"01",
                            "Name":"原材料"
                        },
                        "Unit": {
                            "Code":"01",
                            "Name":"条"
                        },
                        "IsPurchase":"true",
                        "IsSale": "true"
                    }
                }
                """;

        // HTTP调用开始(HTTP调用使用的Spring的RestTemplate组件库)
        HttpEntity<String> entity = new HttpEntity<String>(body, headers);
        String url = BASE_URI + "/tplus/api/v2/inventory/Create";
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
        // HTTP调用结束

        // 打印日志 HTTP调用返回的状态,200为正常,其他为异常
        log.info("StatusCode:" + responseEntity.getStatusCodeValue());
        // 打印日志 HTTP调用返回的内容
        log.info("ResBody:" + responseEntity.getBody());

        // 临时变量:HTTP调用返回的状态
        int statusCodeValue = responseEntity.getStatusCodeValue();

        // 判断HTTP调用返回的状态是否为200
        if (statusCodeValue != HttpStatus.OK.value()) {

            // 不是200,进入if分支
            // 将 HTTP调用返回的内容 转换为JSON对象
            JSONObject jsonBody = JSON.parseObject(responseEntity.getBody());
            // 抛出异常,异常信息为JSON对象里的"message"内容
            throw new Exception(jsonBody.getString("message"));
        }
    }

    /**
     * 存货档案修改示例(修改了存货名称,代码与新增基本一致,只有body参数和接口地址不同)
     */
    public void invModify() throws Exception {

        // 获取开放平台token
        String openToken = getOpenToken();

        // 如果openToken没有获取到,则抛出异常
        if (StringUtils.isEmpty(openToken)) {
            throw new Exception("修改存货失败,未获取到OpenToken()。");
        }

        // 设置header参数
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON); // 此行等同于headers.set("content-type", "application/json");
        headers.set("appKey", APP_KEY);
        headers.set("appSecret", APP_SECRET);
        headers.set("openToken", openToken);

        // 设置body参数,格式为JSON结构的字符串
        // 与新增不同的是,将名称由"中南海1mg"变更为"地中海1mg"
        String body = STR."""
                {
                    "dto":{
                        "Code":"01002",
                        "Name":"地中海1mg",
                        "Shorthand":"ZNH1mg",
                        "Specification":"10mg",
                        "InventoryClass":{
                            "Code":"01",
                            "Name":"原材料"
                        },
                        "Unit": {
                            "Code":"01",
                            "Name":"条"
                        },
                        "IsPurchase":"true",
                        "IsSale": "true"
                    }
                }
                """;

        // HTTP调用开始(HTTP调用使用的Spring的RestTemplate组件库)
        HttpEntity<String> entity = new HttpEntity<String>(body, headers);
        String url = BASE_URI + "/tplus/api/v2/inventory/Update";
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
        // HTTP调用结束

        // 打印日志 HTTP调用返回的状态,200为正常,其他为异常
        log.info("StatusCode:" + responseEntity.getStatusCodeValue());
        // 打印日志 HTTP调用返回的内容
        log.info("ResBody:" + responseEntity.getBody());

        // 临时变量:HTTP调用返回的状态
        int statusCodeValue = responseEntity.getStatusCodeValue();

        // 判断HTTP调用返回的状态是否为200
        if (statusCodeValue != HttpStatus.OK.value()) {

            // 不是200,进入if分支
            // 将 HTTP调用返回的内容 转换为JSON对象
            JSONObject jsonBody = JSON.parseObject(responseEntity.getBody());
            // 抛出异常,异常信息为JSON对象里的"message"内容
            throw new Exception(jsonBody.getString("message"));
        }
    }


    /**
     * 通过代理服务获取开放平台token
     * @return
     */
    private String getOpenToken() {

        // HTTP调用开始(HTTP调用使用的Spring的RestTemplate组件库)
        // 因为使用了代理服务获取token,所以此处调用没有传任何参数
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(TOKEN_PROXY_URL, String.class);
        // HTTP调用结束

        // 判断HTTP调用的返回值
        // 如果值为200(HttpStatus.OK.value),则从返回值中提取token的值,返回
        // 否则返回空值
        int statusCodeValue = responseEntity.getStatusCodeValue();
        if (statusCodeValue == HttpStatus.OK.value()) {
            JSONObject responseBody = JSON.parseObject(responseEntity.getBody());
            return responseBody.getString("token");
        } else {
            return "";
        }
    }
}
1 回复
#2
老去的流星雨2024-02-19 13:38
你的问题难点在于token的获取
json可以用text来拼接
TEXT TO cJson NOSHOW TEXTMERGE
拼接的内容
ENDTEXT
网络调用有XMLHTTP组件,在论坛找了段代码
loXmlHttp = createobject("Microsoft.XMLHTTP")
loXmlHttp.open("POST", 接口url, .F.)
loXmlHttp.setRequestHeader("Content-Type","application/json")
loXmlHttp.setRequestHeader("charset", "utf-8")
loXmlHttp.send(要发送的json)
PAPIresult = loXmlHttp.responseText
RELEASE loXmlHttp
1