This commit is contained in:
richboo111
2023-01-04 18:32:59 +08:00
parent 871de0016e
commit 73a597fddb
13 changed files with 523 additions and 17 deletions

View File

@@ -0,0 +1,205 @@
package uuptapi
import (
"git.rosy.net.cn/baseapi/platformapi"
"net/http"
"sync"
)
const (
//正式环境
TokenURL = "https://openapi.uupt.com/v2_0/"
//沙箱环境
//TokenURL = "http://openapi.test.uupt.com/v2_0/"
RequestPost = "POST"
)
const (
//运单状态
StateConfirmSuccess = 1 //下单成功
StateRMGrabsOrder = 3 //跑男抢单
StateArrivedStore = 4 //已到达
StatePickUp = 5 //已取件
StateArrivedDestination = 6 //到达目的地
StateReceiverGetGoods = 10 //取件人已收货
StateOrderCancel = -1 //订单取消
//订单小类
SendTypeHelpMeDeliver = 0 //帮我送
SendTypeHelpMeBuy = 1 //帮我买
//预约类型
SubscribeTypeRealTime = 0 //实时订单
SubscribeTypeReservation = 1 //预约取件时间
//加急配送 默认不开启
UrgentOrderNormal = 0 //正常配送
UrgentOrderOpen = 1 //开启加急配送
//推送方式
PushTypeOpen = 0 //开放订单
PushTypeTest = 1 //测试订单
//特殊处理类型
SpecialTypeYes = 1 //需要
SpecialTypeNo = 0 //不需要
//取件是否需要打电话
CallMeWithTakeYes = 1 //需要
CallMeWithTakeNo = 0 //不需要
//支付方式
PayTypeBalance = 0 //账户余额支付 默认
PayTypeEnterprise = 1 //企业支付
//订单来源标识
OrderResourceMT = 1 //美团
OrderResourceELM = 2 //饿了么
OrderResourceOther = 3 //其他
)
var StateUuWaybill = map[int]string{
StateConfirmSuccess: "下单成功",
StateRMGrabsOrder: "跑男抢单",
StateArrivedStore: "已到达",
StatePickUp: "已取件",
StateArrivedDestination: "到达目的地",
StateReceiverGetGoods: "取件人已收货",
StateOrderCancel: "订单取消",
}
var SendType = map[int]string{
SendTypeHelpMeDeliver: "帮我送",
SendTypeHelpMeBuy: "帮我买",
}
type API struct {
openid string `json:"openid"` //用户openid,详情见 获取openid接口
appid string `json:"appid"` //appid
appKey string `json:"appkey"` //app密钥
sign string `json:"sign"` //加密签名
noncestr string `json:"nonce_str"` //随机字符串不长于32位
timestamp string `json:"timestamp"` //时间戳以秒计算时间即unix-timestamp
locker sync.RWMutex
client *http.Client
config *platformapi.APIConfig
}
type BaseReqInfo struct {
Sign string `json:"sign"` //加密签名
NonceStr string `json:"nonce_str"` //随机字符串不长于32位
Timestamp string `json:"timestamp"` //时间戳以秒计算时间即unix-timestamp
OpenID string `json:"openid"` //用户openid,详情见 获取openid接口
AppID string `json:"appid"` //appid
}
type BaseRespInfo struct {
Sign string `json:"sign"` //加密签名
NonceStr string `json:"nonce_str"` //随机字符串不长于32位
AppID string `json:"appid"` //第三方用户唯一凭证
ReturnMsg string `json:"return_msg"` //返回信息,如非空,为错误原因,如签名失败、参数格式校验错误
ReturnCode string `json:"return_code"` //状态ok/fail表示成功
}
//计算订单价格请求体
type GetOrderPriceReq struct {
//必填
BaseReqInfo *BaseReqInfo
OriginID string `json:"origin_id"` //第三方对接平台订单id
FromAddress string `json:"from_address"` //发货地址
ToAddress string `json:"to_address"` //收货人地址
CityName string `json:"city_name"` //订单所在城市名 称(如郑州市就填”郑州市“,必须带上“市”)
SendType string `json:"send_type"` //订单小类 0帮我送(默认) 1帮我买
ToLat string `json:"to_lat"` //收货地址坐标纬度(坐标系为百度地图坐标系)
ToLng string `json:"to_lng"` //收货地址坐标经度(坐标系为百度地图坐标系)
FromLat string `json:"from_lat"` //发货地址坐标纬度(坐标系为百度地图坐标系)
FromLng string `json:"from_lng"` //发货地址坐标经度(坐标系为百度地图坐标系)
//非必填
FromUserNote string `json:"from_usernote"` //发货详细地址具体门牌号或门店名称
ToUserNote string `json:"to_usernote"` //收货人详细地址具体门牌号
CountyName string `json:"county_name"` //订单所在县级地名称(如金水区就填“金水区”)
SubscribeTime string `json:"subscribe_time"` //预约时间2015-06-18 12:00:00没有可以传空字符串
SubscribeType string `json:"subscribe_type"` //预约类型 0实时订单 1预约取件时间
GoodsType string `json:"goods_type"` //物品类型:(美食、鲜花、蛋糕、手机、钥匙、文件、其他)仅支持指定类型不包含按其他发布
CouponID string `json:"coupon_id"` //优惠券ID(如果传入-1就不用优惠券否则系统自动匹配)
CouponType string `json:"coupon_type"` //优惠券类型 默认空 个人优惠券 1=企业优惠券(必须企业帐号才可以使用)
GoodsWeight int `json:"goods_weight"` //物品重量单位KG按城市标准进行超重计费
ShopID float64 `json:"shop_id"` //门店编号(门店列表可查看门店编号)
GoodsPrice int `json:"goods_price"` //商品保价价格1-10000按照一定费率收取保价费用
UrgentOrder string `json:"urgent_order"` //加急配送默认不开启0=正常配送 1=开启加急配送)
}
//计算订单价格返回体
type GetOrderPriceResp struct {
BaseRespInfo *BaseRespInfo
OriginID string `json:"origin_id"` //第三方对接平台订单id
PriceToken string `json:"price_token"` //金额令牌,提交订单前必须先计算价格
TotalMoney string `json:"total_money"` //订单总金额(优惠前)
NeedPayMoney string `json:"need_paymoney"` //实际需要支付金额
TotalPriceOff string `json:"total_priceoff"` //总优惠金额
Distance string `json:"distance"` //配送距离(单位:米)
CouponAmount string `json:"coupon_amount"` //优惠券金额
AddFee string `json:"addfee"` //加价金额
GoodsInsuranceMoney string `json:"goods_insurancemoney"` //商品保价金额
ExpiresIn string `json:"expires_in"` //price_token的过期时间单位
}
//发布订单请求体
type AddOrderReq struct {
PriceToken string `json:"price_token"` //金额令牌计算订单价格接口返回的price_token
OrderPrice string `json:"order_price"` //订单总金额优惠前计算订单价格接口返回的total_money
BalancePayMoney string `json:"balance_paymoney"` //实际需要支付金额计算订单价格接口返回的need_paymoney
Receiver string `json:"receiver"` //收件人名称
ReceiverPhone string `json:"receiver_phone"` //收件人电话 手机号码; 虚拟号码格式手机号_分机号码例如13700000000_1111
Note string `json:"note"` //订单备注 最长140个汉字
CallbackUrl string `json:"callback_url"` //订单提交成功后及状态变化的回调地址
PushType string `json:"push_type"` //推送方式0 开放订单2测试订单默认传0即可
SpecialType string `json:"special_type"` //特殊处理类型,是否需要保温箱 1需要 0不需要
CallMeWithTake string `json:"callme_withtake"` //取件是否给我打电话 1需要 0不需要
PubUserMobile string `json:"pubusermobile"` //发件人电话,(如果为空则是用户注册的手机号)
PayType string `json:"pay_type"` //支付方式1=企业支付 0账户余额支付企业余额不足自动转账户余额支付
OrderResource string `json:"orderresource"` //订单来源标示,示例1=美团 2=饿了么 3=其他)
ShortOrderNum string `json:"shortordernum"` //订单平台短单号0-10000该单号会展示给骑手突出展示“美团 #1”
ProductDetail string `json:"product_detail"` //商品详细信息json格式字符串建议请求接口encodeUrl处理例如[{"product_name":"幸福热奶茶(常规)","product_num":"2"},{"product_name":"香酥双鸡堡餐(常规)","product_num":"1"}]
ShopExpectTime string `json:"shop_expecttime"` //商家期望送达时间时间戳,以秒计算时间(仅用于展示给配送员)
ReceiverRealPhone string `json:"receiver_realphone"` //收件人真实脱敏电话(外卖订单虚拟号时,投递餐柜使用)格式137****0000或手机尾号0000
}
//发布订单返回体
type AddOrderResp struct {
BaseRespInfo *BaseRespInfo
OrderCode string `json:"ordercode"` //订单号
OriginID string `json:"origin_id"` //第三方对接平台订单id
}
//取消订单
type CancelOrderReq struct {
BaseRespInfo *BaseRespInfo
OrderCode string `json:"ordercode"` //订单号
OriginID string `json:"origin_id"` //第三方对接平台订单id
}
//获取订单详情返回体
type GetOrderDetailResp struct {
BaseRespInfo *BaseRespInfo
OriginID string `json:"origin_id"` //第三方对接平台id
OrderCode string `json:"order_code"` //uu跑腿订单编号
FromAddress string `json:"from_address"` //发货地址详细地址具体门牌号或门店名称
FromLat string `json:"from_lat"` //发货地址坐标纬度(坐标系为百度地图坐标系)
FromLng string `json:"from_lng"` //发货地址坐标经度(坐标系为百度地图坐标系)
ToAddress string `json:"to_address"` //收货人地址详细地址具体门牌号
ToLat string `json:"to_lat"` //收货地址坐标纬度(坐标系为百度地图坐标系)
ToLng string `json:"to_lng"` //收货地址坐标经度(坐标系为百度地图坐标系)
Distance string `json:"distance"` //订单距离
OrderPrice string `json:"order_price"` //订单总金额(优惠前),包含小费
Receiver string `json:"receiver"` //收件人
ReceiverPhone string `json:"receiver_phone"` //收件人电话
Note string `json:"note"` //订单备注
PriceOff string `json:"price_off"` //总优惠金额
State string `json:"state"` //当前状态1下单成功 3跑男抢单 4已到达 5已取件 6到达目的地 10收件人已收货 -1订单取消
AddTime string `json:"add_time"` //发单时间(格式2015-07-01 15:23:56)
FinishTime string `json:"finish_time"` //收货时间(格式2015-07-01 15:23:56)
StartLevel string `json:"start_level"` //评价星级
CommentNote string `json:"comment_note"` //评价内容
DriverName string `json:"driver_name"` //跑男姓名(跑男接单后)
DriverJobNum string `json:"driver_jobnum"` //跑男工号(跑男接单后)
DriverMobile string `json:"driver_mobile"` //跑男电话(跑男接单后)
SendType string `json:"send_type"` //订单小类 0帮我送 1帮我买
ExpectedarriveTime string `json:"expectedarrive_time"` //跑男接单后预计到达时间(格式2015-07-01 15:23:56)取货前为到达取货地,取货后为到达收货地
DriverLastloc string `json:"driver_lastloc"` //跑男的坐标,百度地图坐标系经纬度(格式:113.71776,34.767501
}
//运单提交后的回调
type CallbackResp struct {
}

View File

@@ -0,0 +1,25 @@
package uuptapi
import (
"fmt"
"testing"
)
var (
api = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e")
)
func TestSignParam(t *testing.T) {
testStr := map[string]interface{}{
"origin_id": "T00001",
"from_address": "郑州国际会展中心",
"from_usernote": "1层",
"to_address": "商务外环路17号",
"to_usernote": "1楼101",
}
ans := api.signParam(testStr)
fmt.Print(ans)
}
func TestRandStr(t *testing.T) {
fmt.Print(randStr())
}

View File

@@ -0,0 +1,103 @@
package uuptapi
import (
"crypto/md5"
"encoding/json"
"fmt"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
"math/rand"
"net/http"
"sort"
"strings"
"time"
)
const (
ReturnSuccess = "ok"
ReturnFail = "fail"
signKey = "sign"
secretKey = "secret"
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
)
func (a *API) MakeUURequestHead() map[string]interface{} {
requestParam := make(map[string]interface{}, 5)
requestParam["sign"] = a.sign
requestParam["timestamp"] = a.timestamp
requestParam["nonce_str"] = a.noncestr
requestParam["openid"] = a.openid
requestParam["appid"] = a.appid
return requestParam
}
func New(appID, appKey, openID string) *API {
if appID == "" {
return nil
}
return &API{
appid: appID,
appKey: appKey,
openid: openID,
}
}
func (a *API) signParam(params map[string]interface{}) (sign string) {
keyValues := make([]string, 0)
for k, v := range params {
if k != signKey {
if temp := fmt.Sprint(v); temp != "" {
keyValues = append(keyValues, k+"="+temp)
}
}
}
sort.Sort(sort.StringSlice(keyValues)) //字典升序
sign = strings.Join(keyValues, "&")
sign += "&key=" + a.appKey //末尾拼接密钥
sign = strings.ToUpper(sign) //大写
return fmt.Sprintf("%X", md5.Sum([]byte(sign)))
}
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
bizParams["timestamp"] = utils.Int64ToStr(time.Now().Unix() * 1000)
bizParams["sign"] = a.signParam(bizParams)
//序列化
data, err := json.Marshal(bizParams)
if err != nil {
return nil, err
}
//完整请求url
fullPath := utils.GenerateGetURL(baseUrl, actionApi, nil)
//发送请求
sendUrl := func() *http.Request {
var request *http.Request
if method == RequestPost {
request, _ = http.NewRequest(http.MethodPost, fullPath, strings.NewReader(string(data)))
} else {
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
}
return request
}
//数据解析
dataMarshal := func(response *http.Response, bodyStr string, jsonResp map[string]interface{}) (errMsg string, err error) {
if jsonResp == nil {
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
}
if jsonResp["return_code"] != ReturnSuccess {
errMsg = platformapi.ErrLevelGeneralFail
err = utils.NewErrorCode(jsonResp["return_msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResp["return_code"])))
}
retVal = jsonResp
return errMsg, err
}
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
return retVal, err
}
//以下为辅助函数
func randStr() string {
b := make([]byte, 16)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

View File

@@ -0,0 +1,14 @@
package uuptapi
import "encoding/json"
//计算订单价格
func (a *API) GetOrderPrice(req *GetOrderPriceReq) (needPayMoney, priceToken string, err error) {
preOrder := a.MakeUURequestHead()
bytes, err := json.Marshal(req)
if err != nil {
return "", "", err
}
preOrder["param"] = string(bytes)
return "", "", nil
}