- GetUserInfoByCode
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
package dingdingapi
|
package dingdingapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/platformapi"
|
"git.rosy.net.cn/baseapi/platformapi"
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
@@ -74,7 +78,7 @@ func (a *API) GetToken() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) RetrieveToken() (token string, err error) {
|
func (a *API) RetrieveToken() (token string, err error) {
|
||||||
result, err := a.AccessAPI(getTokenAction, nil, "")
|
result, err := a.AccessAPI(getTokenAction, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -83,7 +87,13 @@ func (a *API) RetrieveToken() (token string, err error) {
|
|||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) AccessAPI(action string, params map[string]interface{}, body string) (retVal map[string]interface{}, err error) {
|
func (a *API) signParams(timestamp int64) string {
|
||||||
|
mac := hmac.New(sha256.New, []byte(a.GetSecret()))
|
||||||
|
mac.Write([]byte(utils.Int64ToStr(timestamp)))
|
||||||
|
return string(mac.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) AccessAPI(action string, params map[string]interface{}, bodyMap map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||||
params2 := make(map[string]interface{})
|
params2 := make(map[string]interface{})
|
||||||
for k, v := range params {
|
for k, v := range params {
|
||||||
params2[k] = v
|
params2[k] = v
|
||||||
@@ -100,6 +110,11 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
|
|||||||
panic("token is empty!")
|
panic("token is empty!")
|
||||||
}
|
}
|
||||||
params2["access_token"] = accessToken
|
params2["access_token"] = accessToken
|
||||||
|
} else {
|
||||||
|
params2["appkey"] = a.GetAppID()
|
||||||
|
timestamp := time.Now().Unix()
|
||||||
|
params2["timestamp"] = timestamp
|
||||||
|
params2["signature"] = a.signParams(timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
fullURL := utils.GenerateGetURL(prodURL, action, params2)
|
fullURL := utils.GenerateGetURL(prodURL, action, params2)
|
||||||
@@ -108,10 +123,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
|
|||||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||||
func() *http.Request {
|
func() *http.Request {
|
||||||
var request *http.Request
|
var request *http.Request
|
||||||
if body == "" {
|
if bodyMap == nil {
|
||||||
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
|
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
|
||||||
} else {
|
} else {
|
||||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
|
request, _ = http.NewRequest(http.MethodPost, fullURL, bytes.NewReader(utils.MustMarshal(bodyMap)))
|
||||||
}
|
}
|
||||||
return request
|
return request
|
||||||
},
|
},
|
||||||
|
|||||||
23
platformapi/dingdingapi/qrcode.go
Normal file
23
platformapi/dingdingapi/qrcode.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package dingdingapi
|
||||||
|
|
||||||
|
import "git.rosy.net.cn/baseapi/utils"
|
||||||
|
|
||||||
|
type QRCodeUserInfo struct {
|
||||||
|
Nickname string `json:"nick"`
|
||||||
|
OpenID string `json:"openid"`
|
||||||
|
UnionID string `json:"unionid"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) GetUserInfoByCode(code string) (userInfo *QRCodeUserInfo, err error) {
|
||||||
|
result, err := a.AccessAPI("sns/getuserinfo_bycode", nil, map[string]interface{}{
|
||||||
|
"tmp_auth_code": code,
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
return &QRCodeUserInfo{
|
||||||
|
Nickname: utils.Interface2String(result["nick"]),
|
||||||
|
OpenID: utils.Interface2String(result["openid"]),
|
||||||
|
UnionID: utils.Interface2String(result["unionid"]),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ type UserID struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) GetUserID(code string) (userID *UserID, err error) {
|
func (a *API) GetUserID(code string) (userID *UserID, err error) {
|
||||||
result, err := a.AccessAPI("user/getuserinfo", utils.Params2Map("code", code), "")
|
result, err := a.AccessAPI("user/getuserinfo", utils.Params2Map("code", code), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,7 @@ func (a *API) GetUserID(code string) (userID *UserID, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) GetUserDetail(userID string) (userDetail map[string]interface{}, err error) {
|
func (a *API) GetUserDetail(userID string) (userDetail map[string]interface{}, err error) {
|
||||||
result, err := a.AccessAPI("user/get", utils.Params2Map("userid", userID), "")
|
result, err := a.AccessAPI("user/get", utils.Params2Map("userid", userID), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user