package services import ( "encoding/json" "errors" "fmt" "hongze/hongze_api/models" "hongze/hongze_api/utils" "rdluck_tools/http" "time" ) type WxAccessToken struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` RefreshToken string `json:"refresh_token"` Openid string `json:"openid"` Scope string `json:"scope"` Errcode string `json:"errcode"` Errmsg string `json:"errmsg"` } func WxGetUserOpenIdByCode(code string) (item *WxAccessToken, err error) { if code == "" { err = errors.New("code is empty") return nil, err } requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code` requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret, code) fmt.Println("requestUrl:", requestUrl) result, err := http.Get(requestUrl) if err != nil { return nil, err } err = json.Unmarshal(result, &item) return } type WxToken struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` Errcode string `json:"errcode"` Errmsg string `json:"errmsg"` } func WxGetToken() (item *WxToken, err error) { requestUrl := `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s` requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret) fmt.Println("requestUrl:", requestUrl) result, err := http.Get(requestUrl) if err != nil { return nil, err } err = json.Unmarshal(result, &item) return } func WxGetAccessToken() (accessToken string, err error) { wxToken, err := models.GetWxToken() if err != nil && err.Error() != utils.ErrNoRow() { return } //wx_token 不存在 if wxToken == nil || (err != nil && err.Error() == utils.ErrNoRow()) { token, err := WxGetToken() if err != nil { return accessToken, err } if token.Errcode != "" { err = errors.New("获取access_token 失败 errcode:" + token.Errcode + " ;errmsg:" + token.Errmsg) return "", err } expiresIn := time.Now().Add(time.Duration(token.ExpiresIn) * time.Second).Unix() err = models.AddWxToken(token.AccessToken, expiresIn) if err != nil { err = errors.New("新增wx_token失败" + err.Error()) return accessToken, err } accessToken = token.AccessToken } else { if wxToken.ExpiresIn <= time.Now().Unix() { token, err := WxGetToken() if err != nil { return accessToken, err } if token.Errcode != "" { err = errors.New("获取access_token 失败 errcode:" + token.Errcode + " ;errmsg:" + token.Errmsg) return "", err } expiresIn := time.Now().Add(time.Duration(wxToken.ExpiresIn) * time.Second).Unix() err = models.UpdateWxToken(token.AccessToken, expiresIn, wxToken.Id) if err != nil { err = errors.New("修改wx_token失败" + err.Error()) return accessToken, err } accessToken = token.AccessToken } else { accessToken = wxToken.AccessToken } } return } type WxUserInfo struct { Openid string `json:"openid"` Nickname string `json:"nickname"` Sex int `json:"sex"` Language string `json:"language"` City string `json:"city"` Province string `json:"province"` Country string `json:"country"` Headimgurl string `json:"headimgurl"` SubscribeTime int `json:"subscribe_time"` Unionid string `json:"unionid"` Remark string `json:"remark"` Groupid int `json:"groupid"` SubscribeScene string `json:"subscribe_scene"` Errcode string `json:"errcode"` Errmsg string `json:"errmsg"` } func WxGetUserInfo(openId, accessToken string) (item *WxUserInfo, err error) { requestUrl := `https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s` requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret) result, err := http.Get(requestUrl) if err != nil { return } err = json.Unmarshal(result, &item) return } /* public static function wxGetUserInfo($openid,$accessToken){ $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$accessToken}&openid={$openid}"; $html = @file_get_contents($url); $result = json_decode($html, true); return $result; } */