|
@@ -1,7 +1,6 @@
|
|
|
package wechat
|
|
|
|
|
|
import (
|
|
|
- "context"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
@@ -16,7 +15,6 @@ import (
|
|
|
"hongze/hongze_yb/global"
|
|
|
"hongze/hongze_yb/models/response/pc"
|
|
|
"hongze/hongze_yb/models/tables/wx_token"
|
|
|
- "hongze/hongze_yb/utils"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -73,56 +71,51 @@ func init() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func GetWxChat(wxAppId, wxAppSecret string) (officialAccount *officialaccount.OfficialAccount) {
|
|
|
+func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
|
|
|
wc := wechat.NewWechat()
|
|
|
memory := cache.NewMemory()
|
|
|
conf := &config.Config{
|
|
|
- AppID: wxAppId,
|
|
|
- AppSecret: wxAppSecret,
|
|
|
+ AppID: WxAppId,
|
|
|
+ AppSecret: WxAppId,
|
|
|
Token: "",
|
|
|
EncodingAESKey: "",
|
|
|
Cache: memory,
|
|
|
}
|
|
|
officialAccount = wc.GetOfficialAccount(conf)
|
|
|
- wechatAccessToken := &WechatAccessToken{WxAppId: wxAppId,WxAppSecret: wxAppSecret}
|
|
|
+ wechatAccessToken := &WechatAccessToken{}
|
|
|
officialAccount.SetAccessTokenHandle(wechatAccessToken)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// GetUserInfo 获取微信用户详情
|
|
|
-func GetUserInfo(openid, wxAppId, wxAppSecret string) (userInfo *user.Info, err error) {
|
|
|
- wechatClient := GetWxChat(wxAppId, wxAppSecret)
|
|
|
+func GetUserInfo(openid string) (userInfo *user.Info, err error) {
|
|
|
+ wechatClient := GetWxChat()
|
|
|
userClient := wechatClient.GetUser()
|
|
|
userInfo, err = userClient.GetUserInfo(openid)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
// GetJsConfig 获取公众号jsConfig
|
|
|
-func GetJsConfig(signUrl, wxAppId, wxAppSecret string) (jsConf *js.Config, err error) {
|
|
|
- wechatClient := GetWxChat(wxAppId, wxAppSecret)
|
|
|
+func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
|
|
|
+ wechatClient := GetWxChat()
|
|
|
j := wechatClient.GetJs()
|
|
|
jsConf, err = j.GetConfig(signUrl)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
type WechatAccessToken struct {
|
|
|
- WxAppId string
|
|
|
- WxAppSecret string
|
|
|
}
|
|
|
|
|
|
|
|
|
// GetAccessToken 获取accessToken
|
|
|
func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
|
|
|
- if wechat.WxAppId != WxAppId {
|
|
|
- return WxGetRedisAccessToken(wechat.WxAppId, wechat.WxAppSecret)
|
|
|
- }
|
|
|
wxToken, err := wx_token.GetById()
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
//如果300s就要过期了,那么就去刷新accessToken
|
|
|
if wxToken.ExpiresIn < time.Now().Unix()+300 {
|
|
|
- tmpAccessToken, expires, tmpErr := getTokenFromServer(wechat.WxAppId, wechat.WxAppSecret)
|
|
|
+ tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)
|
|
|
if tmpErr != nil {
|
|
|
err = tmpErr
|
|
|
return
|
|
@@ -136,26 +129,7 @@ func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error)
|
|
|
accessToken = wxToken.AccessToken
|
|
|
return
|
|
|
}
|
|
|
-// WxGetRedisAccessToken 从redis中获取token
|
|
|
-func WxGetRedisAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error) {
|
|
|
- //从redis中获取token校验验证码
|
|
|
- accessToken, err = global.Redis.Get(context.TODO(), utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId).Result()
|
|
|
- if err != nil {
|
|
|
- err = nil
|
|
|
- token, expires, tErr := getTokenFromServer(wxAppId, wxAppSecret)
|
|
|
- if tErr != nil {
|
|
|
- err = tErr
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
- //更新redis的accessToken(过期时间提前十分钟)
|
|
|
- redisTimeExpire := time.Duration(expires - 600) * time.Second
|
|
|
- global.Redis.SetEX(context.TODO(), utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId, token, redisTimeExpire)
|
|
|
- accessToken = token
|
|
|
- return
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
// getTokenFromServer 服务端获取accessToken
|
|
|
func getTokenFromServer(appid, wxSecret string) (accessToken string, expires int64, err error) {
|
|
|
apiUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
|