diff --git a/callback_resp.go b/callback_resp.go new file mode 100644 index 0000000..0fb376e --- /dev/null +++ b/callback_resp.go @@ -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 +} diff --git a/model.go b/model.go index ff4fa0a..781b700 100644 --- a/model.go +++ b/model.go @@ -9,6 +9,13 @@ 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 @@ -16,24 +23,26 @@ type ProductPayBase struct { 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 } @@ -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 -} diff --git a/shortcut.go b/shortcut.go deleted file mode 100644 index 78ed9fd..0000000 --- a/shortcut.go +++ /dev/null @@ -1 +0,0 @@ -package sso_sdk diff --git a/sso.go b/sso.go index 6d53489..b060ded 100644 --- a/sso.go +++ b/sso.go @@ -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)) @@ -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 } @@ -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 @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/sso_test.go b/sso_test.go index 92b2f57..344bbc6 100644 --- a/sso_test.go +++ b/sso_test.go @@ -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) + } diff --git a/validator.go b/validator.go index af71df9..5dc44ae 100644 --- a/validator.go +++ b/validator.go @@ -2,6 +2,12 @@ package sso_sdk import "time" +type RespBase struct { + Id string `json:"id,omitempty" url:"id" form:"id"` + UpdateAt time.Time `json:"update_at" url:"update_at" form:"update_at"` + CreateAt time.Time `json:"create_at" url:"create_at" form:"create_at"` +} + // ProductPayResp 商品收款返回 type ProductPayResp struct { OrderNo string `json:"order_no" form:"order_no"` @@ -13,19 +19,59 @@ type PreOrderResp struct { PreOrderId string `json:"pre_order_id" form:"pre_order_id"` } -// BalanceChangeHistoryResp 成交记录 type BalanceChangeHistoryResp struct { - Id string `json:"id,omitempty" url:"id" form:"id"` - UpdateAt time.Time `json:"update_at" url:"update_at" form:"update_at"` - CreateAt time.Time `json:"create_at" url:"create_at" form:"create_at"` - UserId string `json:"user_id,omitempty" url:"user_id" form:"user_id"` - PreOrderId string `json:"pre_order_id,omitempty" url:"pre_order_id" form:"pre_order_id"` - MapId string `json:"map_id,omitempty" url:"map_id" form:"map_id"` - Quantity uint64 `json:"quantity,omitempty" url:"quantity" form:"quantity"` // 支付金额 - ProductUid string `json:"product_uid,omitempty" url:"product_uid" form:"product_uid"` - ProductName string `json:"product_name,omitempty" url:"product_name" form:"product_name"` - ProductUrl string `json:"product_url,omitempty" url:"product_url" form:"product_url"` - Remark string `json:"remark,omitempty" url:"remark" form:"remark"` - OrderUid string `json:"order_uid,omitempty" url:"order_uid" form:"order_uid"` - PublicKey string `json:"public_key,omitempty" url:"public_key" form:"public_key"` + Page uint64 `json:"page"` + PageSize uint64 `json:"page_size"` + Data []BalanceChangeHistoryItem `json:"data"` + Total uint64 `json:"total"` // +} + +type PreOrderItem struct { + RespBase + AppId string `json:"app_id"` + ProductInfo ProductInfo `json:"product_info"` + Substance string `json:"substance"` + Extra string `json:"extra"` + ExpireTime time.Time `json:"expire_time"` +} + +type GetOrderInfoResp struct { + Pay BalanceChangeHistoryItem `json:"pay" form:"pay"` + PreOrder PreOrderItem `json:"pre_order"` +} + +// BalanceChangeHistoryItem 成交记录 +type BalanceChangeHistoryItem struct { + RespBase + UserId string `json:"user_id,omitempty" url:"user_id" form:"user_id"` + PreOrderId string `json:"pre_order_id,omitempty" url:"pre_order_id" form:"pre_order_id"` + MapId string `json:"map_id,omitempty" url:"map_id" form:"map_id"` + Quantity uint64 `json:"quantity,omitempty" url:"quantity" form:"quantity"` // 支付金额 + ProductUid string `json:"product_uid,omitempty" url:"product_uid" form:"product_uid"` + ProductName string `json:"product_name,omitempty" url:"product_name" form:"product_name"` + ProductUrl string `json:"product_url,omitempty" url:"product_url" form:"product_url"` + Remark string `json:"remark,omitempty" url:"remark" form:"remark"` + OrderUid string `json:"order_uid,omitempty" url:"order_uid" form:"order_uid"` + PublicKey string `json:"public_key,omitempty" url:"public_key" form:"public_key"` + Extra string `json:"extra" url:"extra" from:"extra"` +} + +type UidGetUserReq struct { + Uid string `json:"uid" form:"uid"` + SignBase +} + +type UidGetUserResp struct { + User UserInfo `json:"user"` + Info BaseUserInfo `json:"info"` +} + +// UploadKeyResp 上传key请求resp +type UploadKeyResp struct { + SecretID string + SecretKey string + SessionToken string + ExpiredTime uint64 + Prefix string + Visit string }