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.

142 lines
2.9 KiB
Go

5 months ago
package zking
import (
"encoding/json"
"fmt"
5 months ago
"github.com/tidwall/gjson"
5 months ago
"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
}
5 months ago
res, err := api.post("/api/v1/insurance/public/suiXing/create", param)
5 months ago
if err != nil {
return nil, err
}
result := Result{}
err = json.Unmarshal(res, &result)
if err != nil {
return nil, err
}
return &result, nil
}
4 months ago
func (api *insureApi) SuixingRevoke(param CommonParam) (*Result, error) {
err := api.Sign(&param)
if err != nil {
return nil, err
}
res, err := api.post("/api/v1/insurance/public/suiXing/surrend", 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) SuixingClaim(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
}
5 months ago
func (api *insureApi) Sign(param *CommonParam) error {
//vals, err := utils.StructToURLValues(param)
5 months ago
data, err := json.Marshal(param)
if err != nil {
return err
}
dataRet := gjson.Get(string(data), "data")
fmt.Println(dataRet.String())
5 months ago
vals := url.Values{}
5 months ago
vals.Set("appid", global.Config.Zking.AppID)
5 months ago
vals.Set("requestid", param.RequestID)
vals.Set("timestamp", param.Timestamp)
vals.Set("version", param.Version)
5 months ago
vals.Set("data", utils.Md5(dataRet.String()))
vals.Set("appsecret", global.Config.Zking.AppSecret)
5 months ago
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
}
5 months ago
fmt.Println(string(dataStr))
5 months ago
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
}