From 980fb17b1daa6d70faa45c06c32a8d9322d20cfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=AE=97=E6=A5=A0?= Date: Thu, 30 Mar 2023 18:08:18 +0800 Subject: [PATCH] 1 --- platformapi/alipayapi/utils.go | 5 ++- platformapi/jdshopapi/jxshopapi_test.go | 3 +- platformapi/kuaishou_mini/kuaishou_api.go | 45 ++++++++++++++++----- platformapi/kuaishou_mini/kuaishou_model.go | 26 ++++++++++++ platformapi/kuaishou_mini/kuaishou_pay.go | 32 +++++++++++++++ platformapi/kuaishou_mini/kuaishou_test.go | 5 +++ 6 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 platformapi/kuaishou_mini/kuaishou_pay.go diff --git a/platformapi/alipayapi/utils.go b/platformapi/alipayapi/utils.go index b632c7d6..2cf0a34b 100644 --- a/platformapi/alipayapi/utils.go +++ b/platformapi/alipayapi/utils.go @@ -2,6 +2,7 @@ package alipayapi import ( "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/globals" ) const ( @@ -56,7 +57,9 @@ func (a *API) SystemAuthToken(grantType, code, refreshToken string) (tokenInfo * //params["alipay_public_cert_sn"] = aliPayPublicCertSN //params["alipay_root_cert_sn"] = aliPayRootCertSN - retVal, err := a.AccessAPI("alipay.system.oauth.token", params, nil, false) + retVal, err := a.AccessAPI("alipay.system.oauth.token", nil, params, false) + globals.SugarLogger.Debugf("==========aliPay := %s", utils.Format4Output(retVal, false)) + globals.SugarLogger.Debugf("==========err := %s", utils.Format4Output(err, false)) if err == nil { err = utils.Map2StructByJson(retVal, &tokenInfo, false) } diff --git a/platformapi/jdshopapi/jxshopapi_test.go b/platformapi/jdshopapi/jxshopapi_test.go index d3319ca0..23e645cf 100644 --- a/platformapi/jdshopapi/jxshopapi_test.go +++ b/platformapi/jdshopapi/jxshopapi_test.go @@ -16,7 +16,8 @@ func init() { baseapi.Init(sugarLogger) //api = New("de8157b447584885910f429011e49cb93yjq", "E1D746D42474D5F1F1A10CECE75D99F6", "efa7e1d1a22640fa990e6cf164b28608") // api = New("9ad9cf5fc8b140b19a1dbbb700b47676kown", "D5E8352BE0786ED16F77B4548F62F09A", "71e1061ac2f246f6ac27efb900edba12") - api = New("f9c5ce9a5ce24218936924f7c4864cc9owe1", "E1D746D42474D5F1F1A10CECE75D99F6", "efa7e1d1a22640fa990e6cf164b28608") + //api = New("f9c5ce9a5ce24218936924f7c4864cc9owe1", "E1D746D42474D5F1F1A10CECE75D99F6", "efa7e1d1a22640fa990e6cf164b28608") + api = New("37d36b62c0d14bd4b872f948b335c95czinj", "E1D746D42474D5F1F1A10CECE75D99F6", "efa7e1d1a22640fa990e6cf164b28608") api.SetCookieWithStr(`__jdu=16496474755891719920568; unpl=JF8EALNnNSttDU0HBElWE0BAQ1oHWw4KGEcLOmRRUVhQHlUBHlIeEBF7XlVdXhRKEh9vZxRUWFNIVQ4YBysSEXteU11bD00VB2xXVgQFDQ8WUUtBSUt-SF1UXVsJSxMHbG4GZG1bS2QFGjIbFRVDWlFYWA5PEwpnZwNUVVBIVQEcCysiF3ttZFpaCUMTC19mNVVtGh8IDRwGGhMQBl1TW1UPThEGaWMBXVVYTVQNEwEaFhdCbVVuXg; areaId=22; ipLoc-djd=22-1930-0-0; PCSYCityID=CN_510000_510100_0; __jdv=122270672|baidu|-|organic|notset|1649658041982; pinId=0P4L2K_8z11ZUOOc8Pyh4w; pin=jxc4liulei; unick=jxc4liulei; _tp=SNh2AuY0LCWg91owlmuS1Q%3D%3D; _pst=jxc4liulei; ceshi3.com=000; _vender_=TNK3O6PALVQGG3P24NJQFXSBHYQNFQDBDAAAFNJIJAECQG2WSQGU5RJ6KSCMVVQQRHQZD6AECSQT3YKKYMRQJDFZHEEPXXHCR3STQEL55G2S4OB7Q6QUPCVBRUAZKDRVRF2MP5BK57JOU57NKALCQQWNR5XRJWJ4AGVNOAVSZUQ4QLRQZLY62QPOMBQHAQAGNLFRNQDWAO2N2HSD33FC2BJBV3S3QAJEAEJCDK4QQCZD6R72KBKNNWCITHQ32SVZKLN526Z2KM5T3DCVCND4YAK4AZMYIVI7ULYSDQD34K63YVC2TN5AJHZFWVYZEQGPZ65I6MTWTEDVOTLQ5D2AY2PBT4JHYXKEV6DFST3FWH5NOTDOJY3IASEKA7SG6LJSRN63HAW554GZVREIKMQHQAC5IVI445ONJ6GMFECU4SNRFRAYRKKYNISVYBKRTNHJCGFX2442SELHC4MUUVLVNBKEDYTDRCC4ZMMDMWSG75UUSWWXISJA; wlfstk_smdl=91h55jyt5sfm1901tdbgehyi67o5iaor; language=zh_CN; TrackID=1oqBrsNfaNsPFZVtK-124Hk1wwpg113ZwmS2Cajw8VwRsb0RCE3ph1kEhr7BLLOhyaQ196cgJsdscvrvGuBRoZm_dfJ1RuXfLocegNnMjRM8; thor=01BA10CC3862D8315A8F658F7DD2838C910211F45B05336172446EFDFC25E684EB4B9A81E2F1012525DBB65E171F7F5049FBFB076DA4FAC0AE7898C136389DC95DE18C259981364B775EAB7385F4CFFB2BEA505D94D2C1B8D5899C421825A32F0E60967FBF20170B0F1F31A27F795E2C40F4F9A141DD7260894C5EA879FE0810E99DDEFFC34ED45459429571A06DB76E; b-sec=3XU3DWGGC3WZSRRUDORQHBODFJXKGJRSNQI4WVMLXDV5NBOUPIJGYAR6Q5ZBP3KGR7GY3A37P62ZS; __jda=191429163.16496474755891719920568.1649647476.1649658035.1649658042.4; __jdc=191429163; 3AB9D23F7A4B3C9B=CKENC5MAUU744ZQDKVRY2L7UIJ23PJRDYJ6HF3ZMQHC5FFD46FMUQXRINFCOC4SKHL5MU3PPNPP4C4AX5ZYBXJ7LZ4; _base_=YKH2KDFHMOZBLCUV7NSRBWQUJPBI7JIMU5R3EFJ5UDHJ5LCU7R2NILKK5UJ6GLA2RGYT464UKXAI5KK7PNC5B5UHJ2HVQ4ENFP57OC2XJVD24IJG4VRA63GZVNQY5IXRTCNE6YVKRXISVJLYYILNIP6OVKS3GKYL2ZCNGXSSG4SOQWCP5WPWO6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4IC2ET3NVN6PBVCLI56OPZPR7RNFOAEYFZTP2BSCS5ZMNTWN4HIOX5NUKDT5EUE5QQ722X2U2H7DKPHIF7OHQ6IR4HMB5NA7ACWBHEXOENOEOZZIHVQJO7KJEXLBYIYCIDSN4MFK5XWTHEBPJHFWGJBRDK2VBA6IMVCSGASA4TPDBKXN5UZZAL2JZNRSJTR363NRPUBYOPSTMWNXIMF4UYZPUUZPLTJX56EXDHFSRQF3F4FZXK47M3A33GZHU6UCVEAOAOFE3CAHDO4DMIK7334CESHKG3LT2KSPPDT3GXU5UUQOJGGQTYURJRST3PF7U6LS5URX2WPD7XV3OBKJQ2C3NFHU35TKJPWKEJUIFFUHXPJBS6AEJ7JV4RL5QDPT7ZGGIHSED3ZCDW563MYWFUFFCNMWQEZXMPDHUOHRDBNSOWPCAI4ZR4B4NENJLWZUO3XKK4L3AUAJKICWYY6MNDA`) //api.SetCookieWithStr(`language=zh_CN; Domain=.jd.com; Expires=Sat, 16-Apr-2022 09:49:40 GMT; Path=/`) //api.SetCookieWithStr(`_base_=YKH2KDFHMOZBLCUV7NSRBWQUJPBI7JIMU5R3EFJ5UDHJ5LCU7R2NILKK5UJ6GLA2RGYT464UKXAI5KK7PNC5B5UHJ2HVQ4ENFP57OC2XJVD24IJG4VRA63GZVNQY5IXRTCNE6YVKRXISVJLYYILNIP6OVKS3GKYL2ZCNGXSSG4SOQWCP5WPWO6EFS7HEHMRWVKBRVHB33TFD4SMNBHRJCTDFHU6SZXCZD6RFXX2OORYGL5H2GYF2IIH2KKD4T72IR4F577G2E5II2OMMXYF2GDYNTS7WGAUXFEWRJ3CTKDBDWMHUKJQF4ZFOTNBBYBIZRXZYERXXIG6ASWNF62HTTIQSOQAEZEGZNSDAGQWOFW3BKDHOZ5FDR4MS74LDWUDI4FNIT374F4VDI; Domain=.jd.com; Expires=Sat, 15-Apr-2023 09:49:40 GMT; Path=/`) diff --git a/platformapi/kuaishou_mini/kuaishou_api.go b/platformapi/kuaishou_mini/kuaishou_api.go index 8231e08d..6d2d7946 100644 --- a/platformapi/kuaishou_mini/kuaishou_api.go +++ b/platformapi/kuaishou_mini/kuaishou_api.go @@ -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())) diff --git a/platformapi/kuaishou_mini/kuaishou_model.go b/platformapi/kuaishou_mini/kuaishou_model.go index 2d500d9e..30d25c1a 100644 --- a/platformapi/kuaishou_mini/kuaishou_model.go +++ b/platformapi/kuaishou_mini/kuaishou_model.go @@ -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"` // 该字段表示创建订单的同时是否覆盖之前已存在的订单。 +} diff --git a/platformapi/kuaishou_mini/kuaishou_pay.go b/platformapi/kuaishou_mini/kuaishou_pay.go new file mode 100644 index 00000000..7ee833a6 --- /dev/null +++ b/platformapi/kuaishou_mini/kuaishou_pay.go @@ -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) +} diff --git a/platformapi/kuaishou_mini/kuaishou_test.go b/platformapi/kuaishou_mini/kuaishou_test.go index d9a493e8..2461d0d3 100644 --- a/platformapi/kuaishou_mini/kuaishou_test.go +++ b/platformapi/kuaishou_mini/kuaishou_test.go @@ -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() +}