123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "hongze/hongze_cygx/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
- }
- err = json.Unmarshal(result, &item)
- return
- }
- //小助手
- func WxGetUserOpenIdByCodeXzs(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.WxPublicIdXzs, utils.WxPublicSecretXzs, code)
- 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 int `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)
- result, err := http.Get(requestUrl)
- if err != nil {
- utils.FileLog.Info("WxGetToken Result:%s ", string(result))
- return nil, err
- }
- err = json.Unmarshal(result, &item)
- 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"`
- SessionKey string `json:"session_key"`
- }
- 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)
- utils.FileLog.Info("WxGetUserInfo Result:%s ", string(result))
- if err != nil {
- return
- }
- fmt.Println("result:", 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
- }
|