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
142 lines
2.9 KiB
Go
package zking
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/tidwall/gjson"
|
|
"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(¶m)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := api.post("/api/v1/insurance/public/suiXing/create", 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) SuixingRevoke(param CommonParam) (*Result, error) {
|
|
err := api.Sign(¶m)
|
|
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(¶m)
|
|
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)
|
|
data, err := json.Marshal(param)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dataRet := gjson.Get(string(data), "data")
|
|
fmt.Println(dataRet.String())
|
|
vals := url.Values{}
|
|
vals.Set("appid", global.Config.Zking.AppID)
|
|
vals.Set("requestid", param.RequestID)
|
|
vals.Set("timestamp", param.Timestamp)
|
|
vals.Set("version", param.Version)
|
|
vals.Set("data", utils.Md5(dataRet.String()))
|
|
vals.Set("appsecret", global.Config.Zking.AppSecret)
|
|
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(¶m)
|
|
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
|
|
}
|
|
|
|
fmt.Println(string(dataStr))
|
|
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
|
|
}
|