- refactor weixinapi
This commit is contained in:
104
platformapi/weixinapi/sns.go
Normal file
104
platformapi/weixinapi/sns.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package weixinapi
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
func mapData2SNSToken(result map[string]interface{}) *SNSTokenInfo {
|
||||
return &SNSTokenInfo{
|
||||
AccessToken: utils.Interface2String(result["access_token"]),
|
||||
ExpiresIn: int(utils.MustInterface2Int64(result["expires_in"])),
|
||||
RefreshToken: utils.Interface2String(result["refresh_token"]),
|
||||
OpenID: utils.Interface2String(result["openid"]),
|
||||
Scope: utils.Interface2String(result["scope"]),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) SNSRetrieveToken(code string) (tokenInfo *SNSTokenInfo, err error) {
|
||||
result, err := a.AccessAPI("sns/oauth2/access_token", utils.Params2Map("grant_type", "authorization_code", "code", code), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapData2SNSToken(result), nil
|
||||
}
|
||||
|
||||
func (a *API) SNSRefreshToken(refreshToken string) (tokenInfo *SNSTokenInfo, err error) {
|
||||
result, err := a.AccessAPI("sns/oauth2/refresh_token", utils.Params2Map("grant_type", "refresh_token", "refresh_token", refreshToken), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mapData2SNSToken(result), nil
|
||||
}
|
||||
|
||||
func (a *API) SNSGetUserInfo(accessToken, openid string) (*SNSUserInfo, error) {
|
||||
result, err := a.AccessAPI("sns/userinfo", map[string]interface{}{
|
||||
"access_token": accessToken,
|
||||
"openid": openid,
|
||||
}, "")
|
||||
if err == nil {
|
||||
retVal := &SNSUserInfo{
|
||||
OpenID: utils.Interface2String(result["openid"]),
|
||||
NickName: utils.Interface2String(result["nickname"]),
|
||||
Sex: int(utils.MustInterface2Int64(result["sex"])),
|
||||
Province: utils.Interface2String(result["province"]),
|
||||
City: utils.Interface2String(result["city"]),
|
||||
Country: utils.Interface2String(result["country"]),
|
||||
HeadImgURL: utils.Interface2String(result["headimgurl"]),
|
||||
Privilege: result["privilege"],
|
||||
UnionID: utils.Interface2String(result["unionid"]),
|
||||
}
|
||||
return retVal, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (a *API) SNSIsOpenIDValid(accessToken, openid string) (bool, error) {
|
||||
_, err := a.AccessAPI("sns/auth", map[string]interface{}{
|
||||
"access_token": accessToken,
|
||||
"openid": openid,
|
||||
}, "")
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (a *API) SNSCode2Session(jsCode string) (sessionInfo *SessionInfo, err error) {
|
||||
result, err := a.AccessAPI("sns/jscode2session", map[string]interface{}{
|
||||
"js_code": jsCode,
|
||||
"grant_type": "authorization_code",
|
||||
}, "")
|
||||
if err == nil {
|
||||
return &SessionInfo{
|
||||
OpenID: utils.Interface2String(result["openid"]),
|
||||
SessionKey: utils.Interface2String(result["session_key"]),
|
||||
UnionID: utils.Interface2String(result["unionid"]),
|
||||
}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (a *API) SNSDecodeMiniProgramData(encryptedData, sessionKey, iv string) (decryptedData []byte, err error) {
|
||||
decodedDataList, err := utils.Base64DecodeMultiString(encryptedData, sessionKey, iv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, err := aes.NewCipher(decodedDataList[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfbdec := cipher.NewCBCDecrypter(c, decodedDataList[2][:c.BlockSize()])
|
||||
decryptedData = make([]byte, len(decodedDataList[0]))
|
||||
cfbdec.CryptBlocks(decryptedData, decodedDataList[0])
|
||||
decryptedData = PKCS7UnPadding(decryptedData)
|
||||
return decryptedData, nil
|
||||
}
|
||||
|
||||
func PKCS7UnPadding(origData []byte) []byte {
|
||||
length := len(origData)
|
||||
unpadding := int(origData[length-1])
|
||||
return origData[:(length - unpadding)]
|
||||
}
|
||||
Reference in New Issue
Block a user