package main /* #include */ 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() { }