You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.0 KiB
Go

5 months ago
package zking
import (
"encoding/json"
"fmt"
"insure/e"
"insure/global"
"insure/utils"
"io"
"net/http"
"net/url"
"strings"
)
var InsureApi insureApi = insureApi{}
type insureApi struct {
}
func (api *insureApi) SuixingCreate(param CommonParam) (*Result, error) {
err := api.Sign(&param)
if err != nil {
return nil, err
}
res, err := api.post("/api/v1/insurance/public/suiXing/claim", param)
if err != nil {
return nil, err
}
result := Result{}
err = json.Unmarshal(res, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (api *insureApi) Sign(param *CommonParam) error {
//vals, err := utils.StructToURLValues(param)
vals := url.Values{}
vals.Set("appid", param.AppID)
vals.Set("requestid", param.RequestID)
vals.Set("timestamp", param.Timestamp)
vals.Set("version", param.Version)
vals.Set("data", utils.Md5(param.Data))
vals.Set("appsecret", "aaaaaa")
fmt.Println(vals.Encode())
param.Sign = utils.Sha1(vals.Encode())
return nil
}
func (api *insureApi) Verify(param CommonParam) bool {
postSign := param.Sign
err := api.Sign(&param)
if err != nil {
fmt.Println("sign error")
return false
}
if postSign != param.Sign {
return false
}
return true
}
func (api *insureApi) post(uri string, data interface{}) ([]byte, error) {
client := &http.Client{}
dataStr, err := json.Marshal(data)
if err != nil {
return nil, err
}
body := strings.NewReader(string(dataStr))
u := global.Config.Zking.BaseUrl + uri
fmt.Println("url: " + u)
request, err := http.NewRequest("POST", u, body)
request.Header.Set("content-type", "application/json")
if err != nil {
return nil, e.NewError("请求错误")
}
response, err := client.Do(request)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(response.Body)
bodyBytes, err := io.ReadAll(response.Body)
fmt.Println("res: ", string(bodyBytes))
if err != nil {
return nil, e.NewError("返回内容错误")
}
return bodyBytes, nil
}