Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
f committed Aug 15, 2021
2 parents 740effc + 862bcad commit 7313605
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 60 deletions.
57 changes: 57 additions & 0 deletions callback_resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sso_sdk

// 各类事件回调传入的参数

// TrSendBodyResp 收银台收款
type TrSendBodyResp struct {
Event string `json:"event,omitempty"` // cash_receive
Name string `json:"name,omitempty"`
OrderNo string `json:"order_no,omitempty"`
ToUser string `json:"to_user,omitempty"`
PayUser string `json:"pay_user,omitempty"`
Remark string `json:"remark,omitempty"`
Price uint64 `json:"price,omitempty"`
DetailUrl string `json:"detail_url,omitempty"`
SignBase
}

// ChangeInfoSendBodyResp 变更app里的用户基本信息
type ChangeInfoSendBodyResp struct {
Event string `json:"event,omitempty"` //app_change_info
AvatarUrl string `json:"avatar_url,omitempty"`
NickName string `json:"nick_name,omitempty"`
ToUser string `json:"to_user,omitempty"`
SignBase
}

// ChangePhoneSendBodyResp 变更用户手机号
type ChangePhoneSendBodyResp struct {
Event string `json:"event,omitempty"` // user_change_phone
PublicKey string `json:"public_key,omitempty"`
ToUser string `json:"to_user,omitempty"`
Phone string `json:"phone,omitempty"`
SignBase
}

type ProductInfo struct {
Uid string `json:"uid" comment:"唯一ID"`
Count uint64 `json:"count" comment:"对应数量"`
Name string `json:"name" comment:"商品名"`
Price uint64 `json:"price" comment:"价格"`
Desc string `json:"desc" comment:"描述"`
ImgUrl string `json:"img_url" comment:"主图"`
PreviewUrl []string `json:"preview_url" comment:"预览图"` // 其他图片
}

// ProductPaySendBodyResp 预下单商品支付完成
type ProductPaySendBodyResp struct {
Event string `json:"event,omitempty"` // product_pay
ToUser string `json:"to_user,omitempty"`
Extra string `json:"extra,omitempty"`
Substance string `json:"substance,omitempty"`
Remark string `json:"remark,omitempty"`
OrderNo string `json:"order_no,omitempty"`
PreOrderId string `json:"pre_order_id,omitempty"`
Product ProductInfo `json:"product"`
SignBase
}
43 changes: 14 additions & 29 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,40 @@ type SignBase struct {
T string `json:"t" form:"t" url:"t"` // unix时间戳字符串
}

func (c *SignBase) GenSign() {
sign, st, t := Sdk.Sign()
c.Sign = sign
c.RandomStr = st
c.T = t
}

// ProductPayBase 商品支付基础
type ProductPayBase struct {
ProductUid string `json:"product_uid" form:"product_uid"` // 商品uid
ProductName string `json:"product_name" form:"product_name" ` // 商品名 必填
ProductUrl string `json:"product_url" form:"product_url" ` // 商品url
ProductPrice uint64 `json:"product_price" form:"product_price" ` // 商品价格 必填
Remark string `json:"remark" form:"remark" ` // 备注
UserUid string `json:"user_uid" form:"user_uid" ` // 用户标识符
}

// ProductReceipt 商品收款
type ProductReceipt struct {
Uid string `json:"uid"`
Uid string `json:"uid"` // 用户uid
ProductPayBase
SignBase
}

// PreOrder 预下单
// PreOrder 预下单 除了name 和price之外都可以不传
type PreOrder struct {
Uid string `json:"uid"` // 对应商品UID 可不传
Count uint64 `json:"count"` // 对应商品数量 可不传
Substance string `json:"substance"` // 传什么吐什么
Name string `json:"name"`
Price uint64 `json:"price"` // 不允许有免费的出现
Desc string `json:"desc"`
ImgUrl string `json:"img_url"`
PreviewUrl []string `json:"preview_url"`
Extra string `json:"extra"`
Extra string `json:"extra"` // 传什么吐什么
ExpireTime time.Time `json:"expire_time"`
SignBase
}
Expand All @@ -50,30 +59,6 @@ type UserInfo struct {
Id string `json:"id"`
UniqueId string `json:"unique_id,omitempty"`
TelPhone string `json:"tel_phone"` // 手机号
Balance string `json:"balance"` // 余额
Balance uint64 `json:"balance"` // 余额
PromoteUserId string `json:"promote_user_id"` // 推荐人
}

type UidGetUserResp struct {
User UserInfo `json:"user"`
Info BaseUserInfo `json:"info"`
}

// 商品支付
type ProductPay struct {
ProductUid string `json:"product_uid" form:"product_uid"` // 商品uid
ProductName string `json:"product_name" form:"product_name"` // 商品名
ProductUrl string `json:"product_url" form:"product_url"` // 商品url
ProductPrice uint64 `json:"product_price" form:"product_price"` // 商品价格
Remark string `json:"remark" form:"remark"`
}

// 上传key请求resp
type UploadKeyResp struct {
SecretID string
SecretKey string
SessionToken string
ExpiredTime uint64
Prefix string
Visit string
}
1 change: 0 additions & 1 deletion shortcut.go

This file was deleted.

45 changes: 29 additions & 16 deletions sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func (c *Sso) getTimeUnixStr() string {
func (c *Sso) Sign() (string, string, string) {
rs := c.randomStr(16)
us := c.getTimeUnixStr()
return c.s(rs, us), rs, us
return c.sign(rs, us), rs, us
}

// 加密方法
func (c *Sso) s(randomStr, timeUnix string) string {
func (c *Sso) sign(randomStr, timeUnix string) string {
h := md5.New()
h.Write([]byte(randomStr))
h.Write([]byte(c.SecretKey))
Expand All @@ -80,7 +80,7 @@ func (c *Sso) s(randomStr, timeUnix string) string {

// CheckSign 验证加密
func (c *Sso) CheckSign(sign, randomStr, timeUnix string) bool {
nowSign := c.s(randomStr, timeUnix)
nowSign := c.sign(randomStr, timeUnix)
return sign == nowSign
}

Expand All @@ -91,6 +91,7 @@ func (c *Sso) UrlGen(prefix string, p string) string {

// RunTr 发起交易 receipt 是否是商品收款
func (c *Sso) RunTr(data ProductReceipt, receipt bool) (ProductPayResp, error, int) {
data.GenSign()
var d ProductPayResp
var msg string
var url string
Expand All @@ -112,7 +113,7 @@ func (c *Sso) RunTr(data ProductReceipt, receipt bool) (ProductPayResp, error, i
if code == http.StatusUpgradeRequired {
return d, errors.New("余额不足"), code
}
return d, errors.New(fmt.Sprintf("%s响应错误 %d %s", msg, code, resp.String())), code
return d, errors.New(fmt.Sprintf("%s响应错误 %d %sign", msg, code, resp.String())), code
}
err = resp.ToJSON(&d)
if err != nil {
Expand All @@ -123,15 +124,16 @@ func (c *Sso) RunTr(data ProductReceipt, receipt bool) (ProductPayResp, error, i

// ProductPreOrder 预下单
func (c *Sso) ProductPreOrder(data PreOrder) (PreOrderResp, error) {
data.GenSign()
var d PreOrderResp
url := c.UrlGen(c.Prefix, "/pre_order")
resp, err := c.getReq().Post(url, c.getParam(), req.BodyJSON(data))
resp, err := c.getReq().Post(url, req.BodyJSON(data))
if err != nil {
return d, errors.Wrap(err, "预下单出错")
}
code := resp.Response().StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("预下单相应失败 %d %s", code, resp.String()))
return d, errors.New(fmt.Sprintf("预下单相应失败 %d %sign", code, resp.String()))
}
err = resp.ToJSON(&d)
if err != nil {
Expand All @@ -144,14 +146,16 @@ func (c *Sso) ProductPreOrder(data PreOrder) (PreOrderResp, error) {
func (c *Sso) UidGetUserInfo(uid string) (UidGetUserResp, error) {
var d UidGetUserResp
url := c.UrlGen(c.Prefix, "/get_user")
body := req.BodyJSON(map[string]interface{}{"uid": uid})
resp, err := c.getReq().Post(url, c.getParam(), body)
var p UidGetUserReq
p.Uid = uid
p.GenSign()
resp, err := c.getReq().Post(url, req.BodyJSON(p))
if err != nil {
return d, errors.Wrap(err, "获取用户信息请求出错")
}
code := resp.Response().StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("获取用户信息请求错误 %d %s", code, resp.String()))
return d, errors.New(fmt.Sprintf("获取用户信息请求错误 %d %sign", code, resp.String()))
}
err = resp.ToJSON(&d)
if err != nil {
Expand All @@ -170,7 +174,7 @@ func (c *Sso) GetUploadKey() (UploadKeyResp, error) {
}
code := resp.Response().StatusCode
if code != http.StatusOK {
return d, errors.New(fmt.Sprintf("获取上传凭据请求出错 %d %s", code, resp.String()))
return d, errors.New(fmt.Sprintf("获取上传凭据请求出错 %d %sign", code, resp.String()))
}
err = resp.ToJSON(&d)
if err != nil {
Expand All @@ -180,17 +184,22 @@ func (c *Sso) GetUploadKey() (UploadKeyResp, error) {
}

// PreOrderIdGetSuccessList 通过预下单ID获取成交列表
func (c *Sso) PreOrderIdGetSuccessList(preOrderId string, page, pageSize uint16) ([]BalanceChangeHistoryResp, error) {
var r = make([]BalanceChangeHistoryResp, 0)
func (c *Sso) PreOrderIdGetSuccessList(preOrderId string, page, pageSize uint64) (*BalanceChangeHistoryResp, error) {
var r = new(BalanceChangeHistoryResp)
url := c.UrlGen(c.Prefix, "/pre_order_id")
params := req.Param{"pre_order_id": preOrderId, "page": page, "page_size": pageSize}
sign, st, t := Sdk.Sign()
params["sign"] = sign
params["random_str"] = st
params["t"] = t

resp, err := c.getReq().Get(url, params)
if err != nil {
return nil, errors.Wrap(err, "获取成交列表失败")
}
code := resp.Response().StatusCode
if code != http.StatusOK {
return nil, errors.New(fmt.Sprintf("获取成交列表请求出错 %d %s", code, resp.String()))
return nil, errors.New(fmt.Sprintf("获取成交列表请求出错 %d %sign", code, resp.String()))
}
err = resp.ToJSON(&r)
if err != nil {
Expand All @@ -200,17 +209,21 @@ func (c *Sso) PreOrderIdGetSuccessList(preOrderId string, page, pageSize uint16)
}

// OrderIdGetInfo 通过orderId获取成交记录
func (c *Sso) OrderIdGetInfo(orderId string) (BalanceChangeHistoryResp, error) {
var r BalanceChangeHistoryResp
func (c *Sso) OrderIdGetInfo(orderId string) (GetOrderInfoResp, error) {
var r GetOrderInfoResp
url := c.UrlGen(c.Prefix, "/order_id")
params := req.Param{"order_id": orderId}
sign, st, t := Sdk.Sign()
params["sign"] = sign
params["random_str"] = st
params["t"] = t
resp, err := c.getReq().Get(url, params)
if err != nil {
return r, errors.Wrap(err, "获取成交列表失败")
}
code := resp.Response().StatusCode
if code != http.StatusOK {
return r, errors.New(fmt.Sprintf("获取成交列表请求出错 %d %s", code, resp.String()))
return r, errors.New(fmt.Sprintf("获取成交列表请求出错 %d %sign", code, resp.String()))
}
err = resp.ToJSON(&r)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions sso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,42 @@ func TestNew(t *testing.T) {
}
t.Log(resp.Prefix)

// 预下单
order, err := s.ProductPreOrder(PreOrder{
Name: "测试预下单",
Price: 1,
Desc: "描述",
Extra: "aaaa",
Substance: "测试sub",
Uid: "uuuiiiddd",
Count: 10,
})
if err != nil {
t.Error(err)
return
}
t.Log(order.PreOrderId)

// 获取用户信息
info, err := s.UidGetUserInfo("3ITM5gDN3MDMzA")
if err != nil {
t.Error(err)
return
}
t.Log(info.Info.NickName)

l, err := s.PreOrderIdGetSuccessList(order.PreOrderId, 1, 10)
if err != nil {
t.Error(err)
return
}
println(len(l.Data))

getInfo, err := s.OrderIdGetInfo("4213bcd65bf64312a27191f6ca46bacc")
if err != nil {
t.Error(err)
return
}
t.Log(getInfo.PreOrder.ProductInfo.Desc)

}
Loading

0 comments on commit 7313605

Please sign in to comment.