1 Star 0 Fork 0

danlansky/go-library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
jd_sdk.go 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
zhangminghua 提交于 2024-08-20 14:32 . feat:基础工具包
package jdsdk
import (
"encoding/json"
"errors"
"gitee.com/danlansky/go-library/httpclient"
"log"
"net/url"
"time"
)
type App struct {
SiteID string `json:"site_id"` //网站id
SiteName string `json:"site_name"` //网站名称,可为空
AppKey string `json:"app_key"` //网站对应appkey
SecretKey string `json:"secret_key"` //网站对应secretkey
AccessToken string `json:"access_token"` //授权Key
}
type JdUnionErrResp struct {
ErrorResponse ErrorResponse `json:"error_response"`
}
type ErrorResponse struct {
Code string `json:"code"`
Msg string `json:"zh_desc"`
}
const RouterURL = "https://api.jd.com/routerjson"
func (app *App) Request(method string, param interface{}) ([]byte, error) {
// 系统参数
params := map[string]string{}
params["method"] = method
params["app_key"] = app.AppKey
params["format"] = "json"
params["v"] = "1.0"
params["sign_method"] = "md5"
params["timestamp"] = time.Now().Format("2006-01-02 15:04:05")
// 业务参数
paramJsonStr, _ := json.Marshal(param)
params["360buy_param_json"] = string(paramJsonStr)
params["sign"] = GetSign(app.SecretKey, params)
// 请求
client := httpclient.New(httpclient.URLSetting{
Timeout: 2 * time.Second,
RetryCount: 0,
})
resp, err := client.PostForm(RouterURL, params)
if err != nil {
return nil, err
}
jdErr := JdUnionErrResp{}
if err := json.Unmarshal(resp.Body(), &jdErr); err != nil {
return nil, err
}
log.Printf("Responce:%v %v", jdErr, err)
if jdErr.ErrorResponse.Code != "" {
return nil, errors.New(jdErr.ErrorResponse.Msg)
}
return resp.Body(), nil
}
func (app *App) Values(params map[string]interface{}) url.Values {
vals := url.Values{}
for key, val := range params {
vals.Add(key, GetString(val))
}
return vals
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/danlansky/go-library.git
git@gitee.com:danlansky/go-library.git
danlansky
go-library
go-library
v1.0.0

搜索帮助