123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/http"
- "hongze/hongze_mobile_admin/models/custom"
- "hongze/hongze_mobile_admin/models/tables/wx_token"
- "hongze/hongze_mobile_admin/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
- }
- func WxGetAdminOpenIdByCode(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.AdminWxAppId, utils.AdminWxAppSecret, code)
- result, err := http.Get(requestUrl)
- if err != nil {
- return nil, err
- }
- utils.FileLog.Info("WxGetAdminOpenIdByCode:%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
- }
- item = &WxToken{}
- 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 := wx_token.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(utils.WxAppId, utils.WxAppSecret)
- 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 = wx_token.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(utils.WxAppId, utils.WxAppSecret)
- 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 = wx_token.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
- }
- // WxGetRedisAccessToken 从redis中获取token
- func WxGetRedisAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error) {
- //从redis中获取token校验验证码
- accessToken, err = utils.Rc.RedisString(utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId)
- if err != nil {
- err = nil
- token, tErr := WxGetToken(wxAppId, wxAppSecret)
- if tErr != nil {
- return "", tErr
- }
- if token.Errmsg != "" {
- err = errors.New("获取access_token 失败 errcode:" + token.Errmsg + " ;errmsg:" + token.Errmsg)
- return "", err
- }
- redisTimeExpire := time.Duration(token.ExpiresIn - 600) * time.Second
- err = utils.Rc.Put(utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId, token.AccessToken, redisTimeExpire)
- if err != nil {
- err = errors.New("保存access_token失败 " + err.Error())
- return accessToken, err
- }
- accessToken = token.AccessToken
- return
- }
- 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"`
- SessionKey string `json:"session_key"`
- 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) (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 := custom.WxTicket{}
- err = json.Unmarshal(infoBody, &atr)
- fmt.Println("ticket result:", string(infoBody))
- if err != nil {
- return atr.Errmsg, err
- } else {
- return 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
- }
- //微信接口基础返回数据
- type WxBase struct {
- Errcode int
- Errmsg string
- }
- //微信用户数据
- type WxUsers struct {
- WxBase
- Total int
- Count int
- Data struct {
- Openid []string
- }
- NextOpenid string
- }
- func WxUsersGet() (openIdStr string) {
- accessToken, err := WxGetAccessToken()
- if err != nil {
- utils.FileLog.Info("GetWxAccessToken Err:%s", err.Error())
- return
- }
- url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken
- result, err := http.Get(url)
- if err != nil {
- utils.FileLog.Info("GetUser Err:", err.Error())
- return
- }
- item := new(WxUsers)
- err = json.Unmarshal(result, &item)
- if err != nil {
- fmt.Println("Unmarshal Err:", err.Error())
- return
- }
- if item.Errcode != 0 {
- utils.FileLog.Info("微信接口返回异常 Err:", item.Errmsg)
- return
- }
- err = errors.New(item.Errmsg)
- openIdStr = strings.Join(item.Data.Openid, "','")
- openIdStr = "'" + openIdStr + "'"
- return
- }
- func WxAdminOpenIdGet() (openIdStr string) {
- accessToken, err := WxGetRedisAccessToken(utils.AdminWxAppId, utils.AdminWxAppSecret)
- if err != nil {
- utils.FileLog.Info("GetWxAccessToken Err:%s", err.Error())
- return
- }
- url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken
- result, err := http.Get(url)
- if err != nil {
- utils.FileLog.Info("GetUser Err:", err.Error())
- return
- }
- item := new(WxUsers)
- err = json.Unmarshal(result, &item)
- if err != nil {
- fmt.Println("Unmarshal Err:", err.Error())
- return
- }
- if item.Errcode != 0 {
- utils.FileLog.Info("微信接口返回异常 Err:", item.Errmsg)
- return
- }
- err = errors.New(item.Errmsg)
- openIdStr = strings.Join(item.Data.Openid, "','")
- openIdStr = "'" + openIdStr + "'"
- return
- }
|