copyright

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2023 License: MIT Imports: 18 Imported by: 0

README

安装
go get github.com/vtoday/copyright
客户端初始化
    import (
        "github.com/vtoday/copyright"
    )

    privateKey := "MIIEvAIBADANBgkq......jkl9aD/5k8I/Hag=="
    publicKey := "MIIBIjANBgkq......IDAQAB"

    opens := []copyright.OptionFunc{}
    client, err := copyright.New("1001", privateKey, publicKey, false, opens...)
    if err != nil {
        return
    }
请求 查询版权交易平台账号信息 接口
    var res map[string]interface{}
    if e := client.DoRequestAndVerify("user.info", map[string]interface{}{"phone": "xxxxxx"}, &res); e != nil {
        //todo error
    }
    fmt.Println(res)
回调通知验签和数据解密

    req *http.Request := ... //TODO 获取请求request
    request, err := copyright.ParseRequest(req)
    if err != nil {
        return
    }

    //校验请求
    if ok, e := client.VerifyRequestSign(request); !ok {
        return
    }

    //解密请求data
    data, e := client.DecryptRequestData(request)

    var param map[string]interface{}
    if err := json.Unmarshal([]byte(data), &param); err != nil {
        return
    }

    // do something ......

Documentation

Index

Constants

View Source
const (
	HeaderContentType = "Content-Type"

	MIMEApplicationJSON = "application/json"
	MIMEApplicationForm = "application/x-www-form-urlencoded"
	MIMEMultipartForm   = "multipart/form-data"
)

Variables

This section is empty.

Functions

func New

func New(appId, privateKey, publicKey string, isProd bool, opts ...OptionFunc) (client *Client, e *BizErr)

func Nonce

func Nonce() string

func URLValuesToJsonString

func URLValuesToJsonString(param url.Values) (s string, err error)

Types

type BizErr

type BizErr struct {
	Code    Code   `json:"code"`
	Message string `json:"message"`
	Err     error
}

func IsBizErr

func IsBizErr(err error) (*BizErr, bool)

func NewError

func NewError(code Code, message ...string) *BizErr

func (*BizErr) Error

func (r *BizErr) Error() string

func (*BizErr) GetMessage

func (r *BizErr) GetMessage() string

func (*BizErr) SetErr

func (r *BizErr) SetErr(err error) *BizErr

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) CheckRequestParams

func (c *Client) CheckRequestParams(request *Request) *BizErr

func (*Client) DecryptRequestData

func (c *Client) DecryptRequestData(request *Request) (ds string, e *BizErr)

func (*Client) DoRequest

func (c *Client) DoRequest(method string, params map[string]interface{}) (data []byte, e *BizErr)

func (*Client) DoRequestAndVerify

func (c *Client) DoRequestAndVerify(method string, params map[string]interface{}, result interface{}) *BizErr

func (*Client) FormatResponse

func (c *Client) FormatResponse(code, message, method string, data interface{}) (res *Response, e *BizErr)

func (*Client) SetApiDomain

func (c *Client) SetApiDomain(url string)

func (*Client) URLValues

func (c *Client) URLValues(method string, params map[string]interface{}) (value url.Values, e *BizErr)

func (*Client) VerifyRequestSign

func (c *Client) VerifyRequestSign(req *Request) (bool, *BizErr)

func (*Client) VerifyResponseSign

func (c *Client) VerifyResponseSign(res *Response) (bool, *BizErr)

func (*Client) VerifySign

func (c *Client) VerifySign(data url.Values) (ok bool, e *BizErr)

func (*Client) VerifySignWithDecryptResponse

func (c *Client) VerifySignWithDecryptResponse(data []byte, result interface{}) (e *BizErr)

type Code

type Code string
const (
	CSuccess            Code = "200"  // 成功
	CDataDecryptFailure Code = "3001" // 数据解密失败
	CSignFailure        Code = "3002" // 签名校验失败
	CParamInvalid       Code = "4000" // 参数无效
	CNotAppIdParam      Code = "4001" // 缺少参数app_id
	CNotSignParam       Code = "4002" // 缺少参数sign
	CNotNonceParam      Code = "4003" // 缺少参数nonce
	CNotDataParam       Code = "4004" // 缺少业务参数
	CNotConfig          Code = "4005" // 缺少配置参数
	CNotMethod          Code = "4006" // 缺少参数method
	CNotTimestamp       Code = "4007" // 缺少参数timestamp
	CNotTimeExpired     Code = "4008" // 请求已过期(timestamp超过10分钟)
	CCertError          Code = "4042" // 密钥或证书错误
	CMethodNotExist     Code = "4051" // method路由不存在

	CApiResBizError Code = "4100" // 接口返回业务错误数据

	CTodayException     Code = "5001" // 交易平台服务异常
	CPlatformException  Code = "5002" // 一级平台服务异常
	CDataDecodeFailure  Code = "5003" // 数据解析失败
	CApiRequestFailure  Code = "5004" // 接口请求失败
	CApiResponseFailure Code = "5005" // 接口返回数据异常
	CUnknown            Code = "5500" // 未知异常

	CUserNotFound     Code = "20101" //未找到用户
	CUserRealNameDiff Code = "20102" //用户实名信息不一致
)

func (Code) IsSuccess

func (c Code) IsSuccess() bool

func (*Code) UnmarshalJSON

func (c *Code) UnmarshalJSON(b []byte) error

type OptionFunc

type OptionFunc func(c *Client)

func WithApiDomain

func WithApiDomain(apiDomain string) OptionFunc

func WithHTTPClient

func WithHTTPClient(client *http.Client) OptionFunc

func WithLogger

func WithLogger(entry *logrus.Entry) OptionFunc

func WithTimeLocation

func WithTimeLocation(location *time.Location) OptionFunc

type Request

type Request struct {
	AppId     string `json:"app_id"`
	Method    string `json:"method"`
	Nonce     string `json:"nonce"`
	Timestamp string `json:"timestamp"`
	Sign      string `json:"sign"`
	Data      string `json:"data"`
}

func ParseRequest

func ParseRequest(req *http.Request) (r *Request, err error)

type Response

type Response struct {
	Code      Code   `json:"code"`
	Message   string `json:"message"`
	Method    string `json:"method"`
	Nonce     string `json:"nonce"`
	Timestamp string `json:"timestamp"`
	Sign      string `json:"sign"`
	Data      string `json:"data"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL