92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
package tysk
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"gitea.cdlsxd.cn/self-tools/l_request"
|
|
"gitea.cdlsxd.cn/self-tools/tysk/pkg"
|
|
"gitea.cdlsxd.cn/self-tools/tysk/tysk_constant"
|
|
"gitea.cdlsxd.cn/self-tools/tysk/tysk_entity"
|
|
"github.com/duke-git/lancet/v2/convertor"
|
|
)
|
|
|
|
func (g *Tysk) getRequestUrl() string {
|
|
switch g.Env {
|
|
case "dev":
|
|
return "http://192.168.6.81:6789"
|
|
default:
|
|
return ""
|
|
}
|
|
|
|
}
|
|
|
|
func (g *Tysk) handleRequest(Action tysk_constant.RequestCode, requestData map[string]interface{}, result interface{}) (err error) {
|
|
reqData := pkg.XmlRequest{
|
|
Action: Action,
|
|
UserName: g.UserName,
|
|
ExtraFields: requestData,
|
|
}
|
|
gbkXML, err := reqData.MarshalToXML()
|
|
if err != nil {
|
|
return
|
|
}
|
|
req := l_request.Request{
|
|
Method: "POST",
|
|
Url: g.getRequestUrl(),
|
|
Xml: gbkXML,
|
|
}
|
|
response, err := req.Send()
|
|
response.Content, err = convertor.GbkToUtf8(response.Content)
|
|
response.Text = string(response.Content)
|
|
if err != nil {
|
|
return
|
|
}
|
|
resByte, err := pkg.XMLToByte(response.Content)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var commonResponse tysk_entity.RespCommon
|
|
err = json.Unmarshal(resByte, &commonResponse)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if commonResponse.Status != string(tysk_constant.Success) {
|
|
return fmt.Errorf("请求失败,错误码:%s, 失败类型:%s,失败原因:%s", commonResponse.Status, commonResponse.StatusText, commonResponse.FailReason)
|
|
}
|
|
if result == nil {
|
|
return
|
|
}
|
|
return json.Unmarshal(resByte, result)
|
|
}
|
|
|
|
func (g *Tysk) handleReqStructToMap(reqStruct interface{}) (requestData map[string]interface{}) {
|
|
if !pkg.IsStruct(reqStruct) {
|
|
return nil
|
|
}
|
|
var reqMap map[string]interface{}
|
|
reqJsonByte, err := json.Marshal(reqStruct)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
err = json.Unmarshal(reqJsonByte, &reqMap)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
requestData = make(map[string]interface{})
|
|
for k, v := range reqMap {
|
|
if v == nil {
|
|
continue
|
|
}
|
|
switch v.(type) {
|
|
case []interface{}:
|
|
a, k0 := pkg.BuildNestedMap(k, v)
|
|
requestData[k0] = a[k0]
|
|
default:
|
|
requestData[k] = v
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
}
|