123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package models
- import (
- "encoding/json"
- "eta/eta_pub/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/http"
- )
- type WxToken struct {
- AccessToken string
- ExpiresIn int64
- Id int64
- }
- type WxAccessToken struct {
- AccessToken string `json:"access_token"`
- ExpiresIn int `json:"expires_in"`
- Errcode int `json:"errcode"`
- Errmsg string `json:"errmsg"`
- }
- func GetWxToken(wxAppId, wxAppSecret string) (item *WxAccessToken, err error) {
- getUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + wxAppId + "&secret=" + wxAppSecret
- result, err := http.Get(getUrl)
- if err != nil {
- utils.FileLog.Info("GetWxToken Err:", err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("GetWxAccessToken :%s", string(result)))
- err = json.Unmarshal(result, &item)
- if err != nil {
- fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
- return
- }
- if item.Errmsg != "" {
- utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
- }
- return
- }
- func GetWxTokenCygx() (item *WxAccessToken, err error) {
- var getUrl string
- if utils.RunMode == "debug" {
- getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppId + "&secret=" + utils.WxAppSecret
- } else {
- getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppIdCygx + "&secret=" + utils.WxAppSecretCygx
- }
- result, err := http.Get(getUrl)
- if err != nil {
- utils.FileLog.Info("GetWxToken Err:", err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("GetWxAccessToken :%s", string(result)))
- err = json.Unmarshal(result, &item)
- if err != nil {
- fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
- return
- }
- if item.Errmsg != "" {
- utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
- }
- return
- }
- // ModifyAccessToken 修改wx_access_token
- func ModifyAccessToken(accessToken string, expiresIn int64) (err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM wx_token LIMIT 1`
- wxToken := new(WxToken)
- err = o.Raw(sql).QueryRow(&wxToken)
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info("Get wxToken Err:", err.Error())
- return
- }
- if wxToken == nil {
- fmt.Println("wxToken is empty")
- addSql := "insert into wx_token (access_token,expires_in) values (?,?)"
- _, err = o.Raw(addSql, accessToken, expiresIn).Exec()
- } else {
- //判断token是否过期
- updateSql := "update wx_token set access_token = ?,expires_in = ? "
- _, err = o.Raw(updateSql, accessToken, expiresIn).Exec()
- fmt.Println("更新 TOKEN:", err)
- }
- return
- }
- func GetWxTokenMfyx() (item *WxAccessToken, err error) {
- var getUrl string
- if utils.RunMode == "debug"{
- getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppId + "&secret=" + utils.WxAppSecret
- }else{
- getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppIdMfyx + "&secret=" + utils.WxAppSecretMfyx
- }
- result, err := http.Get(getUrl)
- if err != nil {
- utils.FileLog.Info("GetWxToken Err:", err.Error())
- return
- }
- utils.FileLog.Info(fmt.Sprintf("GetWxAccessToken :%s", string(result)))
- err = json.Unmarshal(result, &item)
- if err != nil {
- fmt.Println(fmt.Sprintf("GetWxToken Unmarshal Err:%s", err.Error()))
- return
- }
- if item.Errmsg != "" {
- utils.FileLog.Info(fmt.Sprintf("GetWxToken fail result:%s", string(result)))
- }
- return
- }
|