Files
baseapi/platformapi/mtwmapi/callback.go
2018-11-28 20:06:16 +08:00

63 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mtwmapi
import (
"net/http"
"net/url"
"strings"
"git.rosy.net.cn/baseapi/utils"
)
const (
MsgTypeWaybillStatus = "waybillStatus"
MsgTypeNewOrder = "newOrder"
MsgTypeOrderAccepted = "orderAccepted"
MsgTypeOrderFinished = "orderFinished"
MsgTypeOrderFinancial = "orderFinancial"
MsgTypeUserUrgeOrder = "userUrgeOrder"
MsgTypePrivateNumberDowngrade = "numberDowngrade"
MsgTypeOrderModified = "orderModified"
)
type CallbackResponse struct {
Data string `json:"data"`
}
// !!!特别注意Data中的数据
type CallbackMsg struct {
Data url.Values
Cmd string
}
var (
SuccessResponse = &CallbackResponse{Data: "ok"}
SignatureIsNotOk = &CallbackResponse{Data: "sign failed"}
)
func Err2CallbackResponse(err error, data string) *CallbackResponse {
if err == nil {
return SuccessResponse
}
if data == "" {
data = err.Error()
}
return &CallbackResponse{
Data: data,
}
}
func (a *API) GetCallbackMsg(request *http.Request) (msg *CallbackMsg, callbackResponse *CallbackResponse) {
data := utils.URLValues2Map(request.PostForm)
fullURL := strings.TrimRight(request.URL.String(), "/")
sign := a.signParams(fullURL+"?", data)
if sign != data[signKey] {
return nil, SignatureIsNotOk
}
cmd := strings.Trim(request.URL.EscapedPath(), "/")
msg = &CallbackMsg{
Cmd: cmd,
Data: request.PostForm,
}
return msg, nil
}