daShangDao_kfzgw-info/erp/erp.go
2026-01-13 16:21:38 +08:00

106 lines
2.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
/*
#include <stdlib.h>
*/
import "C"
import (
"encoding/json"
"fmt"
"github.com/parnurzeal/gorequest"
"unsafe"
)
/*
* 接口转发
* param requestMethod[string] 请求方式 现在只支持GET\POST\PUT
* param erpUrl[string] erpURl地址
* param requestJson[string] 请求json字符串
* return 订单同步结构体字符串,错误信息
*/
func interfaceForward(requestMethod, erpUrl string, requestJson string) (string, error) {
var reqJson map[string]interface{}
err := json.Unmarshal([]byte(requestJson), &reqJson)
if err != nil {
return "", fmt.Errorf("requestJson JSON解析失败: %v", err)
}
request := gorequest.New()
switch requestMethod {
case "POST":
resp, body, errs := request.Post(erpUrl).
Type("multipart").
Send(reqJson).
End()
if errs != nil {
return "", fmt.Errorf("请求失败: %v", errs)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("HTTP状态码异常: %d, 响应: %s", resp.StatusCode, body)
}
return body, nil
case "GET":
var erpNewUrl string
erpUrl = erpUrl + "?"
count := 0
total := len(reqJson)
for k, v := range reqJson {
fmt.Println(k, v)
erpNewUrl = fmt.Sprintf("%s=%s", k, v)
erpUrl = erpUrl + erpNewUrl
count++
if count < total {
erpUrl = erpUrl + "&"
}
}
fmt.Println(erpNewUrl)
resp, body, errs := request.Get(erpUrl).
End()
if errs != nil {
return "", fmt.Errorf("请求失败: %v", errs)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("HTTP状态码异常: %d, 响应: %s", resp.StatusCode, body)
}
return body, nil
case "PUT":
resp, body, errs := request.Put(erpUrl).
Send(requestJson).
End()
if errs != nil {
return "", fmt.Errorf("请求失败: %v", errs)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("HTTP状态码异常: %d, 响应: %s", resp.StatusCode, body)
}
return body, nil
}
return "", fmt.Errorf("请求失败,请求方式: %s", requestMethod)
}
// InterfaceForward 接口转发
//
//export InterfaceForward
func InterfaceForward(requestMethod, erpUrl *C.char, requestJson *C.char) *C.char {
goRequestMethod := C.GoString(requestMethod)
goErpUrl := C.GoString(erpUrl)
goEequestJson := C.GoString(requestJson)
synchronization, err := interfaceForward(goRequestMethod, goErpUrl, goEequestJson)
if err != nil {
return C.CString(err.Error())
}
return C.CString(synchronization)
}
// FreeCString 释放C字符串内存
//
//export FreeCString
func FreeCString(str *C.char) {
C.free(unsafe.Pointer(str))
}
// 空main函数编译DLL时需要
func main() {
}