123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "hongze/hongze_api/models"
- "hongze/hongze_api/services/wechat"
- "hongze/hongze_api/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"`
- }
- 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
- }
- utils.FileLog.Info("WxGetUserOpenIdByCode:%s", string(result))
- err = json.Unmarshal(result, &item)
- return
- }
- type WxToken struct {
- AccessToken string `json:"access_token"`
- ExpiresIn int `json:"expires_in"`
- Errcode int `json:"errcode"`
- Errmsg string `json:"errmsg"`
- }
- func WxGetToken(wxAppId, wxAppSecret string) (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, wxAppId, wxAppSecret)
- fmt.Println("requestUrl:", requestUrl)
- result, err := http.Get(requestUrl)
- if err != nil {
- return nil, err
- }
- err = json.Unmarshal(result, &item)
- fmt.Println("WxGetToken start")
- fmt.Println(string(result))
- fmt.Println("WxGetToken end")
- return
- }
- /*func WxGetAccessToken() (accessToken string, err error) {
- wxToken, err := models.GetWxToken()
- fmt.Println(err, wxToken)
- 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.Errmsg != "" {
- err = errors.New("获取access_token 失败 errcode:" + token.Errmsg + " ;errmsg:" + token.Errmsg)
- return "", err
- }
- expiresIn := time.Now().Add(110 * time.Minute).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.Errmsg != "" {
- err = errors.New("获取access_token 失败 errcode:" + token.Errmsg + " ;errmsg:" + token.Errmsg)
- return "", err
- }
- expiresIn := time.Now().Add(110 * time.Minute).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 int `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, 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 GetWxTicket(accessToken string) (int, 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 0, "", err
- }
- atr := models.WxTicket{}
- err = json.Unmarshal(infoBody, &atr)
- fmt.Println("ticket result:", string(infoBody))
- if err != nil {
- return atr.Errcode, atr.Errmsg, err
- } else {
- return 0, atr.Ticket, nil
- }
- }
- 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 FixUnionId() {
- accessToken := `41_fMVI8r0fzoDjGOsKjMBhVN9b0j34TWTFNHIX1cQ4IOJ8SnJ2Cjz6MhvHvtH9gU_ztKo-rRQopCgSjNSZB5z25KgB5o61822LDH1sR5ow_Rfoa_Ro6KwcdP2Gp_naNM0AwMGZaF5OMifMny-FFEXfAJAXWP`
- users, err := models.GetOpenIdAll()
- if err != nil {
- fmt.Println("GetOpenIdAll Err:" + err.Error())
- return
- }
- for _, v := range users {
- openId := v.OpenId
- fmt.Println("openId:", openId, v.UserId)
- userInfoUrl := `https://api.weixin.qq.com/cgi-bin/user/info?access_token=` + accessToken + `&openid=` + openId + `&lang=zh_CN`
- result, err := http.Get(userInfoUrl)
- fmt.Println("err:", err)
- utils.FileLog.Info("%s", string(result))
- item := new(WxUserDetail)
- err = json.Unmarshal(result, &item)
- if err != nil {
- fmt.Println("json.Unmarshal Err:" + err.Error())
- return
- }
- if item.Unionid != "" {
- err = models.ModifyWxUserUnionId(item.Unionid, v.UserId)
- if err != nil {
- fmt.Println("ModifyWxUserUnionId Err:" + err.Error())
- return
- }
- //return
- }
- }
- }
- // GetDefaultWxAccessToken 获取微信token
- func GetDefaultWxAccessToken() (accessToken string, err error, errMsg string) {
- // 微信配置信息
- conf, e := models.GetBusinessConf()
- if e != nil {
- errMsg = "获取配置信息失败, Err: " + e.Error()
- err = errors.New(errMsg)
- return
- }
- appId := ""
- appSecret := ""
- if v, ok := conf[models.BusinessConfWxAppId]; ok {
- appId = v
- }
- if v, ok := conf[models.BusinessConfWxAppSecret]; ok {
- appSecret = v
- }
- if appId == "" || appSecret == "" {
- errMsg = "获取微信公众号未配置"
- err = errors.New(errMsg)
- return
- }
- req := wechat.WxTokenReq{
- WxAppId: appId,
- WxAppSecret: appSecret,
- }
- accessToken, err, errMsg = wechat.GetAccessToken(req)
- return
- }
|