Browse Source

回撤更改

xiexiaoyuan 2 years ago
parent
commit
d9f001e84a

+ 2 - 2
controller/wechat/wechat.go

@@ -19,7 +19,7 @@ func GetQrCode(c *gin.Context) {
 
 func GetUserInfo(c *gin.Context) {
 	openid := `oN0jD1S3P-FVosLhq-YiVOXldtRo`
-	userInfo, err := wechat.GetUserInfo(openid, wechat.WxAppId, wechat.WxAppSecret)
+	userInfo, err := wechat.GetUserInfo(openid)
 	if err != nil {
 		response.Fail("获取失败,Err:"+err.Error(), c)
 		return
@@ -147,7 +147,7 @@ func GetEncryptInfo(c *gin.Context) {
 // @router /getWxJsConf [get]
 func GetWxJsConf(c *gin.Context) {
 	getUrl := c.DefaultQuery("Url", "")
-	jsConf, err := wechat.GetJsConfig(getUrl, wechat.WxAppId, wechat.WxAppSecret)
+	jsConf, err := wechat.GetJsConfig(getUrl)
 	if err != nil {
 		response.Fail("获取失败,Err:"+err.Error(), c)
 		return

+ 0 - 1
models/tables/user_template_record/entity.go

@@ -12,7 +12,6 @@ type UserTemplateRecord struct {
 	CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"`
 	SendStatus int    `gorm:"column:send_status;type:tinyint(4);default:1" json:"sendStatus"` // 1:发送成功,0:发送失败
 	SendType   int    `gorm:"column:send_type;type:tinyint(4);default:1" json:"sendType"`     // 1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知
-	WxAppId    string `gorm:"column:wx_app_id;type:varchar(50)" json:"wx_app_id"` // 公众号appid
 }
 
 // TableName get sql table name.获取数据库表名

+ 7 - 8
services/wechat/template_msg.go

@@ -176,7 +176,7 @@ func (c *TemplateMsgSendClient) ClearQuota() (result *ClearQuotaResponse, err er
 }
 
 // AddUserTemplateRecord 新增模板消息推送记录
-func AddUserTemplateRecord(wxAppId string, userId, sendStatus, sendType int, openid, resource, sendData, result string) (err error) {
+func AddUserTemplateRecord(userId, sendStatus, sendType int, openid, resource, sendData, result string) (err error) {
 	item := &user_template_record.UserTemplateRecord{
 		UserID:     userId,
 		OpenID:     openid,
@@ -187,15 +187,14 @@ func AddUserTemplateRecord(wxAppId string, userId, sendStatus, sendType int, ope
 		CreateTime: time.Now().Format(utils.FormatDateTime),
 		SendStatus: sendStatus,
 		SendType:   sendType,
-		WxAppId:    wxAppId,
 	}
 	err = item.Create()
 	return
 }
 
 // SendMultiTemplateMsg 推送模板消息至多个用户
-func SendMultiTemplateMsg(wxAppId, wxAppSecret string, sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
-	ws := GetWxChat(wxAppId, wxAppSecret)
+func SendMultiTemplateMsg(sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
+	ws := GetWxChat()
 	accessToken, err := ws.GetAccessToken()
 	if err != nil {
 		return
@@ -223,7 +222,7 @@ func SendMultiTemplateMsg(wxAppId, wxAppSecret string, sendMap map[string]interf
 					sendStatus = 0
 				}
 				resultJson, _ := json.Marshal(result)
-				_ = AddUserTemplateRecord(wxAppId, v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
+				_ = AddUserTemplateRecord(v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
 			}(item)
 		}
 		if e != nil {
@@ -388,8 +387,8 @@ func SendVoiceBroadcastWxMsg(broadcastId int, sectionName, broadcastName string)
 }
 
 // SendMultiTemplateMsg 推送模板消息至多个用户中间出错不返回
-func SendMultiTemplateMsgNoReturn(wxAppId, wxAppSecret string, sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
-	ws := GetWxChat(wxAppId, wxAppSecret)
+func SendMultiTemplateMsgNoReturn(sendMap map[string]interface{}, items []*OpenIdList, resource string, sendType int) (err error) {
+	ws := GetWxChat()
 	accessToken, err := ws.GetAccessToken()
 	if err != nil {
 		return
@@ -417,7 +416,7 @@ func SendMultiTemplateMsgNoReturn(wxAppId, wxAppSecret string, sendMap map[strin
 					sendStatus = 0
 				}
 				resultJson, _ := json.Marshal(result)
-				_ = AddUserTemplateRecord(wxAppId, v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
+				_ = AddUserTemplateRecord(v.UserId, sendStatus, sendType, v.OpenId, resource, string(data), string(resultJson))
 			}(item)
 		}
 		if e != nil {

+ 9 - 35
services/wechat/wechat.go

@@ -1,7 +1,6 @@
 package wechat
 
 import (
-	"context"
 	"encoding/json"
 	"errors"
 	"fmt"
@@ -16,7 +15,6 @@ import (
 	"hongze/hongze_yb/global"
 	"hongze/hongze_yb/models/response/pc"
 	"hongze/hongze_yb/models/tables/wx_token"
-	"hongze/hongze_yb/utils"
 	"time"
 )
 
@@ -73,56 +71,51 @@ func init() {
 	}
 }
 
-func GetWxChat(wxAppId, wxAppSecret string) (officialAccount *officialaccount.OfficialAccount) {
+func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
 	wc := wechat.NewWechat()
 	memory := cache.NewMemory()
 	conf := &config.Config{
-		AppID:          wxAppId,
-		AppSecret:      wxAppSecret,
+		AppID:          WxAppId,
+		AppSecret:      WxAppId,
 		Token:          "",
 		EncodingAESKey: "",
 		Cache:          memory,
 	}
 	officialAccount = wc.GetOfficialAccount(conf)
-	wechatAccessToken := &WechatAccessToken{WxAppId: wxAppId,WxAppSecret: wxAppSecret}
+	wechatAccessToken := &WechatAccessToken{}
 	officialAccount.SetAccessTokenHandle(wechatAccessToken)
 	return
 }
 
 // GetUserInfo 获取微信用户详情
-func GetUserInfo(openid, wxAppId, wxAppSecret string) (userInfo *user.Info, err error) {
-	wechatClient := GetWxChat(wxAppId, wxAppSecret)
+func GetUserInfo(openid string) (userInfo *user.Info, err error) {
+	wechatClient := GetWxChat()
 	userClient := wechatClient.GetUser()
 	userInfo, err = userClient.GetUserInfo(openid)
 	return
 }
 
 // GetJsConfig 获取公众号jsConfig
-func GetJsConfig(signUrl, wxAppId, wxAppSecret string) (jsConf *js.Config, err error) {
-	wechatClient := GetWxChat(wxAppId, wxAppSecret)
+func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
+	wechatClient := GetWxChat()
 	j := wechatClient.GetJs()
 	jsConf, err = j.GetConfig(signUrl)
 	return
 }
 
 type WechatAccessToken struct {
-	WxAppId     string
-	WxAppSecret string
 }
 
 
 // GetAccessToken 获取accessToken
 func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
-	if wechat.WxAppId != WxAppId {
-		return WxGetRedisAccessToken(wechat.WxAppId, wechat.WxAppSecret)
-	}
 	wxToken, err := wx_token.GetById()
 	if err != nil {
 		return
 	}
 	//如果300s就要过期了,那么就去刷新accessToken
 	if wxToken.ExpiresIn < time.Now().Unix()+300 {
-		tmpAccessToken, expires, tmpErr := getTokenFromServer(wechat.WxAppId, wechat.WxAppSecret)
+		tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)
 		if tmpErr != nil {
 			err = tmpErr
 			return
@@ -136,26 +129,7 @@ func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error)
 	accessToken = wxToken.AccessToken
 	return
 }
-// WxGetRedisAccessToken 从redis中获取token
-func WxGetRedisAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error) {
-	//从redis中获取token校验验证码
-	accessToken, err = global.Redis.Get(context.TODO(), utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId).Result()
-	if err != nil {
-		err = nil
-		token, expires, tErr := getTokenFromServer(wxAppId, wxAppSecret)
-		if tErr != nil {
-			err = tErr
-			return
-		}
 
-		//更新redis的accessToken(过期时间提前十分钟)
-		redisTimeExpire := time.Duration(expires - 600) * time.Second
-		global.Redis.SetEX(context.TODO(), utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId, token, redisTimeExpire)
-		accessToken = token
-		return
-	}
-	return
-}
 // getTokenFromServer 服务端获取accessToken
 func getTokenFromServer(appid, wxSecret string) (accessToken string, expires int64, err error) {
 	apiUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"