package services import ( "encoding/json" "errors" "fmt" "github.com/beego/beego/v2/client/orm" "github.com/rdlucklib/rdluck_tools/http" "hongze/hongze_mfyx_gzh/models" "hongze/hongze_mfyx_gzh/utils" "strconv" "strings" "time" ) type WxAccessToken struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` RefreshToken string `json:"refresh_token"` Openid string `json:"openid"` Unionid string `json:"unionid"` Scope string `json:"scope"` Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` } 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 int `json:"errcode"` Errmsg string `json:"errmsg"` SessionKey string `json:"session_key"` } type WxToken struct { AccessToken string ExpiresIn int64 Id int64 } 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) result, err := http.Get(requestUrl) if err != nil { return nil, err } err = json.Unmarshal(result, &item) return } // mfyx公众号 func WxGetUserOpenIdByCodeGzh(code string) (item *WxAccessToken, err error) { if code == "" { err = errors.New("code is empty") return nil, err } //requestUrl := `https://api.weixin.qq.com/cgi-bin/token?appid=%s&secret=%s&code=%s&grant_type=client_credential` 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.WxPublicAppId, utils.WxPublicAppSecret, code) result, err := http.Get(requestUrl) if err != nil { return nil, err } err = json.Unmarshal(result, &item) return } func WxGetToken() (item *WxAccessToken, err error) { requestUrl := `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s` requestUrl = fmt.Sprintf(requestUrl, utils.WxPublicAppId, utils.WxPublicAppSecret) result, err := http.Get(requestUrl) if err != nil { utils.FileLog.Info("获取wxToken失败,Err:%s", err.Error()) return nil, err } err = json.Unmarshal(result, &item) if err != nil { fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error())) return } if item.Errmsg != "" { err = fmt.Errorf(item.Errmsg) utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result))) } return } 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, accessToken, openId) result, err := http.Get(requestUrl) if err != nil { return } //fmt.Println("result:", string(result)) utils.FileLog.Info("WxGetUserInfo:%s openId:%s,accessToken:%s ", string(result), openId, accessToken) utils.FileLog.Info("WxGetUserInfo Result:%s ", string(result)) err = json.Unmarshal(result, &item) return } func GetWxSignature(ticket, url, noncestr string) (string, string, int64) { timestamp := time.Now().Unix() signStr := strings.Join([]string{"jsapi_ticket=", ticket, "&noncestr=", noncestr, "×tamp=", strconv.FormatInt(timestamp, 10), "&url=", url}, "") signature := utils.Sha1(signStr) fmt.Println("signStr", signStr) return signature, noncestr, timestamp } type WxUserDetail struct { Unionid string Headimgurl string Nickname string } func GetWxAccessTokenByGzh() (accessTokenStr string, err error) { //缓存校验 if utils.RunMode == "release" { cacheKey := "mfyx_wxtoken" accessTokenStr, _ = utils.Rc.RedisString(cacheKey) if accessTokenStr != "" { return } else { WxAccessToken, errWx := WxGetToken() if errWx != nil { err = errWx return } accessTokenStr = WxAccessToken.AccessToken utils.Rc.Put(cacheKey, WxAccessToken.AccessToken, time.Second*7000) } } else { accessTokenStr, err = GetWxAccessToken() if err != nil { return } } return } func GetWxTicket(accessToken string) (string, error) { Url := strings.Join([]string{"https://api.weixin.qq.com/cgi-bin/ticket/getticket", "?access_token=", accessToken, "&type=jsapi"}, "") infoBody, err := http.Get(Url) if err != nil { return "", err } atr := models.WxTicket{} err = json.Unmarshal(infoBody, &atr) fmt.Println("ticket result:", string(infoBody)) if err != nil { return atr.Errmsg, err } else { return atr.Ticket, nil } } // 获取测试环境的AccessToken func GetWxAccessToken() (accessTokenStr string, err error) { o := orm.NewOrmUsingDB("weekly_report") sql := `SELECT * FROM wx_token LIMIT 1` wxToken := new(WxToken) err = o.Raw(sql).QueryRow(&wxToken) if err != nil && err.Error() != utils.ErrNoRow() { return } //Token不存在 if wxToken == nil { fmt.Println("wxToken is empty") accessToken, err := WxGetToken() if err != nil { return "", err } if accessToken.AccessToken != "" { expiresIn := time.Now().Add(time.Duration(accessToken.ExpiresIn) * time.Second).Unix() addSql := "insert into wx_token (access_token,expires_in) values (?,?)" _, err = o.Raw(addSql, accessToken.AccessToken, expiresIn).Exec() accessTokenStr = accessToken.AccessToken } return accessTokenStr, err } else { //判断token是否过期 if time.Now().Unix() > wxToken.ExpiresIn { accessToken, err := WxGetToken() fmt.Println(accessToken) if err != nil { return "", err } if accessToken.AccessToken != "" { expiresIn := time.Now().Add(time.Duration(accessToken.ExpiresIn) * time.Second).Unix() updateSql := "update wx_token set access_token = ?,expires_in = ? " _, err = o.Raw(updateSql, accessToken.AccessToken, expiresIn).Exec() accessTokenStr = accessToken.AccessToken fmt.Println("更新 TOKEN:", err) } return accessTokenStr, err } else { return wxToken.AccessToken, nil } } }