This commit is contained in:
邹宗楠
2023-03-30 18:08:18 +08:00
parent de5cb4ca5e
commit 980fb17b1d
6 changed files with 104 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package kuaishou_mini
import (
"errors"
"fmt"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
@@ -13,18 +14,19 @@ const (
// KuaiShouBashUrl 基础域名
KuaiShouBashUrl = "https://open.kuaishou.com" // 域名
// 获取授权信息
KuaiShouAuthLogin = KuaiShouBashUrl + "/oauth2/mp/code2session" // 授权登录
KuaiShouAuthLogin = KuaiShouBashUrl + "/oauth2/mp/code2session" // 授权登录
KuaiShouGetToken = KuaiShouBashUrl + "/oauth2/access_token" // 获取授权token
KuaiShouPreCreateOrder = KuaiShouBashUrl + "/openapi/mp/developer/epay/create_order" // 预下单接口
)
type API struct {
appSecret string // 应用唯一标识对应的密钥
appId string // 应用唯一标识
client *http.Client
config *platformapi.APIConfig
locker sync.RWMutex
msgToken string // accessToken
expiresIn int64 // 过期时间
appSecret string // 应用唯一标识对应的密钥
appId string // 应用唯一标识
client *http.Client
config *platformapi.APIConfig
locker sync.RWMutex
accessToken string // accessToken
expiresIn int64 // 过期时间
}
func (a *API) GetAppID() string {
@@ -49,9 +51,32 @@ func New(appSecret, appId string, config ...*platformapi.APIConfig) *API {
}
}
// GetToken 获取快手token
func (a *API) GetToken() error {
if a.appId == "" || a.appSecret == "" {
return errors.New("快手appId为空或appSecret为空")
}
result, err := a.AccessAPI2(KuaiShouGetToken, map[string]interface{}{"app_id": a.appId, "app_secret": a.appSecret, "grant_type": "client_credentials"})
if err != nil {
return err
}
var data *GetAutoTokenRes
if err := utils.Map2StructByJson(result, &data, false); err != nil {
return err
}
if data.Result != 1 {
return errors.New(utils.Int64ToStr(data.Result))
}
a.expiresIn = data.ExpiresIn
a.accessToken = data.AccessToken
return nil
}
// AccessAPI2 发送请求
func (a *API) AccessAPI2(url string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(utils.Map2URLValues(params).Encode()))

View File

@@ -15,3 +15,29 @@ type ResultInfo struct {
Error string `json:"error"` //错误类型
ErrorMsg string `json:"error_msg"` // 错误消息
}
// GetAutoTokenRes 快手授权返回值
type GetAutoTokenRes struct {
Result int64 `json:"result"`
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
TokenType string `json:"token_type"`
}
// PreCreateOrderRes 快手预发单
type PreCreateOrderRes struct {
OutOrderNo string `json:"out_order_no"` // 商户系统内部订单号只能是数字、大小写字母_-*且在同一个商户号下唯一
OpenId string `json:"open_id"` // 快手用户在当前小程序的open_id
TotalAmount int64 `json:"total_amount"` // 用户支付金额,单位为[分]。
Subject string `json:"subject"` // 商品描述。注1汉字=2字符。
Detail string `json:"detail"` // 商品详情。注1汉字=2字符。
TypeDetail int `json:"type"` // 商品类型,不同商品类目的编号见
ExpireTime int `json:"expire_time"` // 订单过期时间,单位秒
Sign string `json:"sign"` // 签名
Attach string `json:"attach"` // 开发者自定义字段,回调原样回传.
NotifyUrl string `json:"notify_url"` // 回调地址
GoodsId string `json:"goods_id"` // 下单商品id需与商品对接 (opens new window)时的product_id一致长度限制256个英文字符1个汉字=2个英文字符
GoodsDetailUrl string `json:"goods_detail_url"` // 订单详情页跳转path。
MultiCopiesGoodsInfo string `json:"multi_copies_goods_info"` // 单商品购买多份场景 "[{"copies":2}]"
CancelOrder int64 `json:"cancel_order"` // 该字段表示创建订单的同时是否覆盖之前已存在的订单。
}

View File

@@ -0,0 +1,32 @@
package kuaishou_mini
import (
"fmt"
)
// PreCreateOrder 预下单生成支付信息
//func (a *API) PreCreateOrder(param *PreCreateOrderRes) error {
// if a.appId == "" || a.appSecret == "" {
// return error
// }
//
// result, err := a.AccessAPI2(a.FullUrl(KuaiShouPreCreateOrder), utils.Struct2MapByJson(param))
// if err != nil {
// return "", "", err
// }
//
// auth := GetLoginAuth{}
// if err := utils.Map2StructByJson(result, &auth, false); err != nil {
// return "", "", err
// }
//
// if auth.Error != "" {
// return "", "", errors.New(auth.ErrorMsg)
// }
//
// return auth.SessionKey, auth.OpenId, nil
//}
func (a *API) FullUrl(bashUrl string) string {
return fmt.Sprintf(bashUrl+"?app_id=%s&access_token=%s", a.appId, a.accessToken)
}

View File

@@ -37,6 +37,7 @@ func TestDecryptUserMsg(t *testing.T) {
globals.SugarLogger.Debugf("data := %s", data)
globals.SugarLogger.Debugf("err := %v", err)
}
func DecryptUserMsg(sessionKey, iv, msg string) (string, error) {
decodeMsg, err := base64.StdEncoding.DecodeString(msg)
if err != nil {
@@ -62,3 +63,7 @@ func DecryptUserMsg(sessionKey, iv, msg string) (string, error) {
//return result.PhoneNumber, nil
return string(userInfo), nil
}
func TestCreateToken(t *testing.T) {
api.GetToken()
}