248 lines
10 KiB
Go
248 lines
10 KiB
Go
package fnpsapi
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"git.rosy.net.cn/baseapi"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"io/ioutil"
|
||
"net/http"
|
||
)
|
||
|
||
const (
|
||
OrderStatus = "orderStatusNotify" // 订单状态回调
|
||
AbnormalStatus = "abnormalReportNotify" // 异常报备回调
|
||
CookingFinishStatus = "cookingFinishNotify" // 商户出餐回调
|
||
ChainstoreStatus = "chainstoreStatusNotify" // 门店状态变更回调
|
||
ChainstoreServiceStatus = "chainstoreServiceStatusNotify" // 门店采购服务变更回调
|
||
NoServiceStatus = "noServiceNotify" // 城市屏蔽区域调整回调通知
|
||
)
|
||
|
||
var (
|
||
SuccessResponse = &CallbackResponse{Code: 200}
|
||
SignatureIsNotOk = &CallbackResponse{Code: -1}
|
||
)
|
||
|
||
type CallbackResponse struct {
|
||
Code int `json:"code"`
|
||
}
|
||
|
||
func Err2CallbackResponse(err error, data string) *CallbackResponse {
|
||
if err == nil {
|
||
return SuccessResponse
|
||
}
|
||
return &CallbackResponse{
|
||
Code: -1,
|
||
}
|
||
}
|
||
|
||
type ShortStatus struct {
|
||
AppId string `json:"app_id"`
|
||
Signature string `json:"signature"`
|
||
Timestamp string `json:"timestamp"`
|
||
BusinessData string `json:"business_data"`
|
||
}
|
||
|
||
// 门店状态回调
|
||
type ChainstoreStatusNotify struct {
|
||
CallbackBusinessType string `json:"callback_business_type"`
|
||
Param *ChainstoreParam `json:"param"`
|
||
}
|
||
|
||
// 门店状态回调paramter
|
||
type ChainstoreParam struct {
|
||
MerchantId string `json:"merchant_id"` // 商户id
|
||
ChainStoreId string `json:"chain_store_id"` // 蜂鸟门店id
|
||
OutShopCode string `json:"out_shop_code"` // 外部门店编码
|
||
Status string `json:"status"` // 门店认证状态
|
||
ModifyStatus string `json:"modify_status"` // 门店修改状态
|
||
Remark string `json:"remark"` // 门店认证、修改等驳回时返回原因
|
||
}
|
||
|
||
// 订单状态
|
||
type OrderStatusNottify struct {
|
||
CallbackBusinessType string `json:"callback_business_type"`
|
||
Param map[string]interface{} `json:"param"`
|
||
}
|
||
|
||
// 订单状态回调paramter
|
||
type OrderCallbackParam struct {
|
||
OrderId int64 `json:"order_id"` // 订单号
|
||
AppId string `json:"app_id"` // 应用id
|
||
PartnerOrderCode string `json:"partner_order_code"` // 外部订单号
|
||
OrderStatus int `json:"order_status"` // 订单状态 订单生成0,运单生成成功1,20:骑手接单,80:骑手到 店,2:配送中,3:已完成,4:已取消,5:配送异常
|
||
CarrierDriverId int64 `json:"carrier_driver_id"` // 骑手id
|
||
CarrierLat string `json:"carrier_lat"` // 坐标高德(不要使用)
|
||
CarrierLng string `json:"carrier_lng"` // 坐标高德(不要使用)
|
||
CarrierDriverName string `json:"carrier_driver_name"` // 骑手姓名
|
||
CarrierDriverPhone string `json:"carrier_driver_phone"` // 骑手电话
|
||
Description string `json:"description"` // 描述
|
||
ErrorCode string `json:"error_code"` // 异常code
|
||
ErrorScene string `json:"error_scene"` // 异常描述
|
||
DetailDescription string `json:"detail_description"` // 详情描述
|
||
PushTime int64 `json:"push_time"` // 状态推送时间 (毫秒)
|
||
Transfer int `json:"transfer"` // 转单标识 转单标识 1 是转单 0非转单
|
||
}
|
||
|
||
// 出餐回调
|
||
type CookingFinishNotify struct {
|
||
OrderId int64 `json:"order_id"` // 订单号
|
||
AppId string `json:"app_id"` // 应用id
|
||
PartnerOrderCode string `json:"partner_order_code"` // 外部订单号
|
||
ChainStoreId string `json:"chain_store_id"` // 蜂鸟门店id
|
||
MerchantId string `json:"merchant_id"` // 商户id
|
||
CookingFinishTime int64 `json:"cooking_finish_time"` // 状态推送时间 (毫秒)
|
||
}
|
||
|
||
// 异常状态回掉
|
||
type AbnormalStatusNotify struct {
|
||
CallbackBusinessType string `json:"callback_business_type"`
|
||
Param *AbnormalReportNotify `json:"param"`
|
||
}
|
||
|
||
// 异常状态回调
|
||
type AbnormalReportNotify struct {
|
||
OrderId int64 `json:"order_id"` // 订单号
|
||
AppId string `json:"app_id"` // 应用id
|
||
PartnerOrderCode string `json:"partner_order_code"` // 外部订单号
|
||
CarrierDriverId int64 `json:"carrier_driver_id"` // 骑手id
|
||
Longitude string `json:"longitude"` // 纬度
|
||
Latitude string `json:"latitude"` // 门店纬度
|
||
AbnormalCode string `json:"abnormal_code"` // 异常报备code
|
||
AbnormalDesc string `json:"abnormal_desc"` // 异常报备描述
|
||
AbnormalReportTime int64 `json:"abnormal_report_time"` // 异常报备时间 毫秒
|
||
|
||
}
|
||
|
||
// 获取门店状态回调消息
|
||
func (a *API) GetChainstoreStatusNotify(request *http.Request) (shopStatusMsg *ChainstoreStatusNotify, callbackResponse *CallbackResponse) {
|
||
data, err := ioutil.ReadAll(request.Body)
|
||
if err != nil {
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
|
||
storeNotify := make(map[string]interface{}, 4)
|
||
if err := json.Unmarshal(data, &storeNotify); err != nil {
|
||
baseapi.SugarLogger.Debugf("FN GetChainstoreStatusNotify failed with err:%v", err)
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
fmt.Println("回调数据=============================", storeNotify["business_data"])
|
||
|
||
fnNotify := &ChainstoreStatusNotify{}
|
||
if err := json.Unmarshal([]byte(utils.Interface2String(storeNotify["business_data"])), fnNotify); err != nil {
|
||
baseapi.SugarLogger.Debugf("FN callback string to GetChainstoreStatusNotify failed with err:%v", err)
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
|
||
return fnNotify, SuccessResponse
|
||
}
|
||
|
||
// 获取订单状态回调消息
|
||
func (a *API) GetChainOrderStatusNotify(request *http.Request) (shopStatusMsg *OrderStatusNottify, callbackResponse *CallbackResponse) {
|
||
//data, err := ioutil.ReadAll(request.Body)
|
||
//if err != nil {
|
||
// baseapi.SugarLogger.Debugf("FN GetChainOrderStatusNotify failed with No result msg err:%v", err)
|
||
// callbackResponse = &CallbackResponse{Code: -1}
|
||
// return nil, callbackResponse
|
||
//}
|
||
resultData := &ShortStatus{
|
||
AppId: request.FormValue("app_id"),
|
||
Signature: request.FormValue("signature"),
|
||
Timestamp: request.FormValue("timestamp"),
|
||
BusinessData: request.FormValue("business_data"),
|
||
}
|
||
|
||
fmt.Println("string==================", resultData.BusinessData)
|
||
//if err := json.Unmarshal([]byte(resultData.BusinessData), &storeNotify); err != nil {
|
||
// baseapi.SugarLogger.Debugf("FN GetShopStatusCallbackMsg failed with err:%v", err)
|
||
// callbackResponse = &CallbackResponse{Code: -1}
|
||
// return nil, callbackResponse
|
||
//}
|
||
//
|
||
//fmt.Println("======================", storeNotify)
|
||
fnNotify := &OrderStatusNottify{}
|
||
if err := json.Unmarshal([]byte(resultData.BusinessData), fnNotify); err != nil {
|
||
baseapi.SugarLogger.Debugf("FN callback string to ChainstoreStatusNotify failed with err:%v", err)
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
fmt.Println("======================business_data", fnNotify.Param)
|
||
fmt.Println("======================business_data", fnNotify.Param["orderId"])
|
||
fmt.Println("======================business_data", fnNotify.Param["partnerOrderCode"])
|
||
fmt.Println("======================business_data", fnNotify.Param)
|
||
fmt.Println("======================business_data", fnNotify.CallbackBusinessType)
|
||
|
||
return fnNotify, SuccessResponse
|
||
}
|
||
|
||
// 异常配送
|
||
func (a *API) GetChainAbnormaltatusNotify(request *http.Request) (shopStatusMsg *AbnormalStatusNotify, callbackResponse *CallbackResponse) {
|
||
data, err := ioutil.ReadAll(request.Body)
|
||
if err != nil {
|
||
baseapi.SugarLogger.Debugf("FN GetChainOrderStatusNotify failed with No result msg err:%v", err)
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
|
||
storeNotify := make(map[string]interface{}, 4)
|
||
if err := json.Unmarshal(data, &storeNotify); err != nil {
|
||
baseapi.SugarLogger.Debugf("FN GetShopStatusCallbackMsg failed with err:%v", err)
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
fmt.Println("======================", storeNotify)
|
||
fnNotify := &AbnormalStatusNotify{}
|
||
if err := json.Unmarshal([]byte(utils.Interface2String(storeNotify["business_data"])), fnNotify); err != nil {
|
||
baseapi.SugarLogger.Debugf("FN callback string to ChainstoreStatusNotify failed with err:%v", err)
|
||
callbackResponse = &CallbackResponse{Code: -1}
|
||
return nil, callbackResponse
|
||
}
|
||
|
||
return fnNotify, SuccessResponse
|
||
}
|
||
|
||
// 蜂鸟返回值解析
|
||
//func FnCallbackAnalysis(notify *ChainstoreStatusNotify) (result map[string]interface{}, err error) {
|
||
// switch notify.CallbackBusinessType {
|
||
// case OrderStatus: // 订单回调
|
||
// orderRes := &OrderCallbackParam{}
|
||
// if err := json.Unmarshal([]byte(notify.Param), orderRes); err != nil {
|
||
// return nil, err
|
||
// }
|
||
// notifyObj := utils.Struct2FlatMap(orderRes)
|
||
// notifyObj["orderStatusNotify"] = ChainstoreStatus
|
||
// return notifyObj, nil
|
||
// case AbnormalStatus: // 异常报备回调
|
||
// return nil, nil
|
||
// case CookingFinishStatus: // 商户出餐回调
|
||
// //cokking := &CookingFinishNotify{}
|
||
// //if err := json.Unmarshal([]byte(notify.Param), cokking); err != nil {
|
||
// // return nil, err
|
||
// //}
|
||
// //notifyObj := utils.Struct2FlatMap(cokking)
|
||
// //notifyObj["cookingFinishNotify"] = ChainstoreStatus
|
||
// //return notifyObj, nil
|
||
// return nil, nil
|
||
// case ChainstoreStatus: // 门店状态变更回调
|
||
// storeRes := &ChainstoreParam{}
|
||
// if err := json.Unmarshal([]byte(notify.Param), storeRes); err != nil {
|
||
// return nil, err
|
||
// }
|
||
// notifyObj := utils.Struct2FlatMap(storeRes)
|
||
// notifyObj["notifyUel"] = ChainstoreStatus
|
||
// return notifyObj, nil
|
||
// case ChainstoreServiceStatus: // 门店采购服务变更回调
|
||
// return nil, nil
|
||
// case NoServiceStatus: // 城市屏蔽区域调整回调通知
|
||
// return nil, nil
|
||
// default:
|
||
// return nil, errors.New("回调函数,回调路径错误")
|
||
// }
|
||
//
|
||
// globals.SugarLogger.Warnf("Fn callback url func err:=[%s],dont's exits", "notify.CallbackBusinessType")
|
||
// return nil, errors.New("回调函数,回调路径错误")
|
||
//}
|