瀏覽代碼

Merge branch 'feature/get_wx_token' of eta_server/eta_pub into master

xyxie 1 年之前
父節點
當前提交
79da96ed61

+ 3 - 2
.gitignore

@@ -1,8 +1,9 @@
-/hongze_public_api.exe
+/*.exe
 /lastupdate.tmp
 /rdlucklog
+/binlog/*
 /.idea
 /conf
 .DS_Store
-/hongze_public_api
+eta_pub
 hongze_public_api

+ 5 - 5
controllers/base_auth.go

@@ -2,21 +2,21 @@ package controllers
 
 import (
 	"encoding/json"
+	"eta/eta_pub/models"
+	"eta/eta_pub/services/alarm_msg"
+	"eta/eta_pub/utils"
 	"fmt"
+	"github.com/beego/beego/v2/server/web"
 	"github.com/rdlucklib/rdluck_tools/log"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services/alarm_msg"
-	"hongze/hongze_public_api/utils"
 	"net/http"
 	"net/url"
-	"github.com/beego/beego/v2/server/web"
 )
 
 var apiLog *log.Log
 
 func init() {
 	if utils.RunMode == "release" {
-		logDir := `/data/rdlucklog/hongze_public_api`
+		logDir := `/data/rdlucklog/eta_pub`
 		apiLog = log.Init("20060102.api", logDir)
 	} else {
 		apiLog = log.Init("20060102.api")

+ 3 - 3
controllers/base_common.go

@@ -2,10 +2,10 @@ package controllers
 
 import (
 	"encoding/json"
+	"eta/eta_pub/services/alarm_msg"
+	"eta/eta_pub/utils"
 	"fmt"
 	"github.com/beego/beego/v2/server/web"
-	"hongze/hongze_public_api/services/alarm_msg"
-	"hongze/hongze_public_api/utils"
 	"net/http"
 	"net/url"
 )
@@ -38,7 +38,7 @@ func (c *BaseCommonController) ServeJSON(encoding ...bool) {
 		hasEncoding = true
 	}
 	if c.Data["json"] == nil {
-		msg := "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值"
+		msg := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
 		go alarm_msg.SendAlarmMsg(msg, 3)
 		return
 	}

+ 31 - 32
controllers/image.go

@@ -2,11 +2,11 @@ package controllers
 
 import (
 	"encoding/json"
+	"eta/eta_pub/models"
+	"eta/eta_pub/services"
+	"eta/eta_pub/services/ppt2img"
+	"eta/eta_pub/utils"
 	"fmt"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services"
-	"hongze/hongze_public_api/services/ppt2img"
-	"hongze/hongze_public_api/utils"
 	"io/ioutil"
 	"os"
 	"os/exec"
@@ -161,7 +161,7 @@ func (this *ImageController) Base64Upload() {
 	}
 	outFileName = uploadDir + randStr + ".png"
 	fmt.Println("start convert", time.Now())
-	cd := exec.Command("highcharts-export-server", "--infile", saveSvgPath, "--constr", "Chart", "--scale", "2","--workers","10","--workLimit","5", "--outfile", outFileName)
+	cd := exec.Command("highcharts-export-server", "--infile", saveSvgPath, "--constr", "Chart", "--scale", "2", "--workers", "10", "--workLimit", "5", "--outfile", outFileName)
 	str, err := cd.Output()
 	fmt.Println(string(str))
 	if err != nil {
@@ -221,10 +221,10 @@ func (this *ImageController) Ppt2Image() {
 	}
 
 	// 下载ppt
-	pptPath,err := utils.DownloadImage(req.PptUrl)
-	if err != nil{
+	pptPath, err := utils.DownloadImage(req.PptUrl)
+	if err != nil {
 		br.Msg = "下载ppt失败"
-		br.ErrMsg = "下载ppt失败,Err:"+err.Error()
+		br.ErrMsg = "下载ppt失败,Err:" + err.Error()
 		return
 	}
 	defer func() {
@@ -234,9 +234,9 @@ func (this *ImageController) Ppt2Image() {
 	// ppt转pdf
 	converterType := "pdf"
 	pdfPath, err := ppt2img.FuncDocs2Pdf(utils.LibreOfficePath, pptPath, "./static/pdf", converterType)
-	if err != nil{
+	if err != nil {
 		br.Msg = "ppt转pdf失败"
-		br.ErrMsg = "ppt转pdf失败,Err:"+err.Error()
+		br.ErrMsg = "ppt转pdf失败,Err:" + err.Error()
 		return
 	}
 	defer func() {
@@ -245,13 +245,13 @@ func (this *ImageController) Ppt2Image() {
 	}()
 
 	// 生成临时存放图片的目录
-	uploadDir := "./static/pdf/png/"+utils.GetRandStringNoSpecialChar(16)+"/"
+	uploadDir := "./static/pdf/png/" + utils.GetRandStringNoSpecialChar(16) + "/"
 	{
 		if !utils.FileIsExist(uploadDir) {
 			err := os.MkdirAll(uploadDir, 0777)
 			if err != nil {
 				br.Msg = "创建存放图片的目录失败"
-				br.ErrMsg = "创建存放图片的目录失败,Err:"+err.Error()
+				br.ErrMsg = "创建存放图片的目录失败,Err:" + err.Error()
 				return
 			}
 		}
@@ -261,43 +261,42 @@ func (this *ImageController) Ppt2Image() {
 		os.RemoveAll(uploadDir)
 	}()
 
-	pdfPathList := strings.Split(pdfPath,"/")
-	pngName := strings.Split(path.Base(pdfPathList[len(pdfPathList)-1]), ".")[0]	//图片名称
-	convPngName := uploadDir + pngName+".png"
+	pdfPathList := strings.Split(pdfPath, "/")
+	pngName := strings.Split(path.Base(pdfPathList[len(pdfPathList)-1]), ".")[0] //图片名称
+	convPngName := uploadDir + pngName + ".png"
 
 	// pdf 转图片
-	_,err = ppt2img.Pdf2Img(pdfPath,convPngName)
-	if err != nil{
+	_, err = ppt2img.Pdf2Img(pdfPath, convPngName)
+	if err != nil {
 		br.Msg = "pdf转png失败"
-		br.ErrMsg = "pdf转png失败,Err:"+err.Error()
+		br.ErrMsg = "pdf转png失败,Err:" + err.Error()
 		return
 	}
 
 	// oss的图片存放地址
-	resourceUrlList := make([]string,0)
-
+	resourceUrlList := make([]string, 0)
 
 	// 获取生成的文件列表
-	fileInfoList,err := ioutil.ReadDir(uploadDir)
+	fileInfoList, err := ioutil.ReadDir(uploadDir)
 	if err != nil {
 		br.Msg = "获取生成的文件列表失败"
-		br.ErrMsg = "获取生成的文件列表失败,Err:"+err.Error()
+		br.ErrMsg = "获取生成的文件列表失败,Err:" + err.Error()
 		return
 	}
 	countFile := len(fileInfoList)
-	for i:=0;i<countFile;i++{
-		tmpFilePath := fmt.Sprintf("%s%s-%d.png",uploadDir,pngName,i)
-		if utils.FileIsExist(tmpFilePath){
-			resourceUrl,tmpErr := uploadToAliOss(tmpFilePath)
-			if tmpErr != nil{
+	for i := 0; i < countFile; i++ {
+		tmpFilePath := fmt.Sprintf("%s%s-%d.png", uploadDir, pngName, i)
+		if utils.FileIsExist(tmpFilePath) {
+			resourceUrl, tmpErr := uploadToAliOss(tmpFilePath)
+			if tmpErr != nil {
 				br.Msg = "上传图片至oss失败"
-				br.ErrMsg = "上传图片至oss失败,Err:"+err.Error()
+				br.ErrMsg = "上传图片至oss失败,Err:" + err.Error()
 				return
 			}
-			resourceUrlList = append(resourceUrlList,resourceUrl)
-		}else{
+			resourceUrlList = append(resourceUrlList, resourceUrl)
+		} else {
 			br.Msg = "上传图片至oss失败"
-			br.ErrMsg = "上传图片至oss失败,Err:"+err.Error()
+			br.ErrMsg = "上传图片至oss失败,Err:" + err.Error()
 			return
 		}
 	}
@@ -309,7 +308,7 @@ func (this *ImageController) Ppt2Image() {
 }
 
 // 上传文件到阿里云
-func uploadToAliOss(outFileName string) (resourceUrl string,err error) {
+func uploadToAliOss(outFileName string) (resourceUrl string, err error) {
 	var saveToOssPath string
 	randStr := utils.GetRandStringNoSpecialChar(28)
 	uploadDir := "static/images/"

+ 2 - 2
controllers/report.go

@@ -2,8 +2,8 @@ package controllers
 
 import (
 	"encoding/json"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services"
+	"eta/eta_pub/models"
+	"eta/eta_pub/services"
 )
 
 // ReportController 报告模块

+ 50 - 8
controllers/wechat.go

@@ -2,13 +2,13 @@ package controllers
 
 import (
 	"encoding/json"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services"
-	"hongze/hongze_public_api/services/alarm_msg"
-	"hongze/hongze_public_api/utils"
+	"eta/eta_pub/models"
+	"eta/eta_pub/services"
+	"eta/eta_pub/services/alarm_msg"
+	"eta/eta_pub/utils"
 )
 
-//发送模板消息
+// 发送模板消息
 type WechatController struct {
 	BaseAuthController
 }
@@ -42,12 +42,12 @@ func (this *WechatController) SendTemplateMsg() {
 		return
 	}
 	if sendItem.WxAppId == "" {
-		sendItem.WxAppId = utils.WxAppId   //默认是弘则研究公众号的appId
+		sendItem.WxAppId = utils.WxAppId //默认是弘则研究公众号的appId
 	}
 	go func() {
-		if 	sendItem.RedirectTarget == 3 {
+		if sendItem.RedirectTarget == 3 {
 			err = services.SendWxTemplateMsgCygx(sendItem)
-		}else{
+		} else {
 			err = services.SendWxTemplateMsg(sendItem)
 		}
 		if err != nil {
@@ -59,3 +59,45 @@ func (this *WechatController) SendTemplateMsg() {
 	br.Msg = "推送成功"
 	return
 }
+
+// GetAccessToken
+// @Title 获取微信token
+// @Description 获取微信token
+// @Success 200
+// @router /access_token [post]
+func (this *WechatController) GetAccessToken() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	body := this.Ctx.Input.RequestBody
+	req := new(models.WxTokenReq)
+	err := json.Unmarshal(body, &req)
+	if err != nil {
+		br.Msg = "参数解析失败"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+
+	if req.WxAppId == "" {
+		br.Msg = "参数错误,微信AppId为空"
+		return
+	}
+
+	if req.WxAppSecret == "" {
+		br.Msg = "参数错误,微信AppSecret为空"
+		return
+	}
+	token, err, errMsg := services.GetWxAccessToken(req.WxAppId, req.WxAppSecret)
+	if err != nil {
+		br.Msg = errMsg
+		br.ErrMsg = err.Error()
+		return
+	}
+	data := models.WxTokenResp{AccessToken: token}
+	br.Ret = 200
+	br.Msg = "推送成功"
+	br.Data = data
+	return
+}

+ 1 - 1
go.mod

@@ -1,4 +1,4 @@
-module hongze/hongze_public_api
+module eta/eta_pub
 
 go 1.19
 

二進制
hongze_public_api


+ 2 - 2
main.go

@@ -1,7 +1,7 @@
 package main
 
 import (
-	_ "hongze/hongze_public_api/routers"
+	_ "eta/eta_pub/routers"
 	"os"
 
 	"github.com/beego/beego/v2/server/web"
@@ -31,7 +31,7 @@ func main() {
 	svcConfig := &service.Config{
 		Name:        "Trim",         //服务显示名称
 		DisplayName: "Trim Service", //服务名称
-		Description: "微服务",       //服务描述
+		Description: "微服务",          //服务描述
 	}
 
 	prg := &program{}

+ 1 - 1
models/db.go

@@ -1,8 +1,8 @@
 package models
 
 import (
+	"eta/eta_pub/utils"
 	_ "github.com/go-sql-driver/mysql"
-	"hongze/hongze_public_api/utils"
 	"time"
 
 	"github.com/beego/beego/v2/client/orm"

+ 6 - 6
models/wechat.go

@@ -2,8 +2,8 @@ package models
 
 import (
 	"encoding/json"
+	"eta/eta_pub/utils"
 	"fmt"
-	"hongze/hongze_public_api/utils"
 
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/rdlucklib/rdluck_tools/http"
@@ -22,7 +22,7 @@ type WxAccessToken struct {
 	Errmsg      string `json:"errmsg"`
 }
 
-func GetWxToken(wxAppId,wxAppSecret string) (item *WxAccessToken, err error) {
+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 {
@@ -43,9 +43,9 @@ func GetWxToken(wxAppId,wxAppSecret string) (item *WxAccessToken, err error) {
 
 func GetWxTokenCygx() (item *WxAccessToken, err error) {
 	var getUrl string
-	if utils.RunMode == "debug"{
+	if utils.RunMode == "debug" {
 		getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppId + "&secret=" + utils.WxAppSecret
-	}else{
+	} else {
 		getUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxAppIdCygx + "&secret=" + utils.WxAppSecretCygx
 	}
 
@@ -67,7 +67,7 @@ func GetWxTokenCygx() (item *WxAccessToken, err error) {
 }
 
 // ModifyAccessToken 修改wx_access_token
-func ModifyAccessToken(accessToken string,expiresIn int64)(err error){
+func ModifyAccessToken(accessToken string, expiresIn int64) (err error) {
 	o := orm.NewOrm()
 	sql := `SELECT * FROM wx_token LIMIT 1`
 	wxToken := new(WxToken)
@@ -87,4 +87,4 @@ func ModifyAccessToken(accessToken string,expiresIn int64)(err error){
 		fmt.Println("更新 TOKEN:", err)
 	}
 	return
-}
+}

+ 10 - 1
models/wechat_send_msg.go

@@ -1,8 +1,8 @@
 package models
 
 import (
+	"eta/eta_pub/utils"
 	"github.com/beego/beego/v2/client/orm"
-	"hongze/hongze_public_api/utils"
 	"strings"
 )
 
@@ -223,3 +223,12 @@ type SendWxTemplate struct {
 	SendType       int      `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
 	OpenIdArr      []string `description:"消息接收者openid"`
 }
+
+type WxTokenReq struct {
+	WxAppId     string `description:"公众号appId"`
+	WxAppSecret string `description:"公众号appSecret"`
+}
+
+type WxTokenResp struct {
+	AccessToken string `description:"微信token"`
+}

+ 13 - 4
routers/commentsRouter.go

@@ -7,7 +7,7 @@ import (
 
 func init() {
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"],
+    beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"],
         beego.ControllerComments{
             Method: "Ppt2Image",
             Router: `/ppt2img`,
@@ -16,7 +16,7 @@ func init() {
             Filters: nil,
             Params: nil})
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"],
+    beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"],
         beego.ControllerComments{
             Method: "Base64Upload",
             Router: `/svg_to_png`,
@@ -25,7 +25,7 @@ func init() {
             Filters: nil,
             Params: nil})
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ReportController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ReportController"],
+    beego.GlobalControllerRouter["eta/eta_pub/controllers:ReportController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:ReportController"],
         beego.ControllerComments{
             Method: "HandleVideoDecibel",
             Router: `/handle_video_decibel`,
@@ -34,7 +34,16 @@ func init() {
             Filters: nil,
             Params: nil})
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:WechatController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:WechatController"],
+    beego.GlobalControllerRouter["eta/eta_pub/controllers:WechatController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:WechatController"],
+        beego.ControllerComments{
+            Method: "GetAccessToken",
+            Router: `/access_token`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_pub/controllers:WechatController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:WechatController"],
         beego.ControllerComments{
             Method: "SendTemplateMsg",
             Router: `/send_template_msg`,

+ 32 - 32
routers/commentsRouter_controllers.go

@@ -7,40 +7,40 @@ import (
 
 func init() {
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"],
-        beego.ControllerComments{
-            Method: "Ppt2Image",
-            Router: "/ppt2img",
-            AllowHTTPMethods: []string{"post"},
-            MethodParams: param.Make(),
-            Filters: nil,
-            Params: nil})
+	beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"],
+		beego.ControllerComments{
+			Method:           "Ppt2Image",
+			Router:           "/ppt2img",
+			AllowHTTPMethods: []string{"post"},
+			MethodParams:     param.Make(),
+			Filters:          nil,
+			Params:           nil})
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ImageController"],
-        beego.ControllerComments{
-            Method: "Base64Upload",
-            Router: "/svg_to_png",
-            AllowHTTPMethods: []string{"post"},
-            MethodParams: param.Make(),
-            Filters: nil,
-            Params: nil})
+	beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:ImageController"],
+		beego.ControllerComments{
+			Method:           "Base64Upload",
+			Router:           "/svg_to_png",
+			AllowHTTPMethods: []string{"post"},
+			MethodParams:     param.Make(),
+			Filters:          nil,
+			Params:           nil})
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ReportController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:ReportController"],
-        beego.ControllerComments{
-            Method: "HandleVideoDecibel",
-            Router: "/handle_video_decibel",
-            AllowHTTPMethods: []string{"post"},
-            MethodParams: param.Make(),
-            Filters: nil,
-            Params: nil})
+	beego.GlobalControllerRouter["eta/eta_pub/controllers:ReportController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:ReportController"],
+		beego.ControllerComments{
+			Method:           "HandleVideoDecibel",
+			Router:           "/handle_video_decibel",
+			AllowHTTPMethods: []string{"post"},
+			MethodParams:     param.Make(),
+			Filters:          nil,
+			Params:           nil})
 
-    beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:WechatController"] = append(beego.GlobalControllerRouter["hongze/hongze_public_api/controllers:WechatController"],
-        beego.ControllerComments{
-            Method: "SendTemplateMsg",
-            Router: "/send_template_msg",
-            AllowHTTPMethods: []string{"post"},
-            MethodParams: param.Make(),
-            Filters: nil,
-            Params: nil})
+	beego.GlobalControllerRouter["eta/eta_pub/controllers:WechatController"] = append(beego.GlobalControllerRouter["eta/eta_pub/controllers:WechatController"],
+		beego.ControllerComments{
+			Method:           "SendTemplateMsg",
+			Router:           "/send_template_msg",
+			AllowHTTPMethods: []string{"post"},
+			MethodParams:     param.Make(),
+			Filters:          nil,
+			Params:           nil})
 
 }

+ 1 - 1
routers/router.go

@@ -8,7 +8,7 @@
 package routers
 
 import (
-	"hongze/hongze_public_api/controllers"
+	"eta/eta_pub/controllers"
 
 	"github.com/beego/beego/v2/server/web"
 )

+ 1 - 1
services/alarm_msg/alarm_msg.go

@@ -2,8 +2,8 @@ package alarm_msg
 
 import (
 	"encoding/json"
+	"eta/eta_pub/utils"
 	"github.com/rdlucklib/rdluck_tools/http"
-	"hongze/hongze_public_api/utils"
 )
 
 // projectName-项目名称

+ 1 - 1
services/audio/audio.go

@@ -2,8 +2,8 @@ package audio
 
 import (
 	"bytes"
+	"eta/eta_pub/utils"
 	"fmt"
-	"hongze/hongze_public_api/utils"
 	"math"
 	"os"
 	"os/exec"

+ 1 - 1
services/oss.go

@@ -5,7 +5,7 @@ import (
 	"os"
 	"time"
 
-	"hongze/hongze_public_api/utils"
+	"eta/eta_pub/utils"
 )
 
 // UploadFileToAliyun 上传文件到阿里云

+ 3 - 3
services/report.go

@@ -1,10 +1,10 @@
 package services
 
 import (
+	"eta/eta_pub/models"
+	"eta/eta_pub/services/audio"
+	"eta/eta_pub/utils"
 	"fmt"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services/audio"
-	"hongze/hongze_public_api/utils"
 	"os"
 	"path"
 )

+ 24 - 37
services/wechat_send_msg.go

@@ -4,10 +4,10 @@ import (
 	"bytes"
 	"encoding/json"
 	"errors"
+	"eta/eta_pub/models"
+	"eta/eta_pub/services/alarm_msg"
+	"eta/eta_pub/utils"
 	"fmt"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services/alarm_msg"
-	"hongze/hongze_public_api/utils"
 	"io/ioutil"
 	"net/http"
 	"strconv"
@@ -181,7 +181,7 @@ func toSendTemplateMsg(wxAppId string, data []byte, resource string, sendType in
 	var accessToken string
 	var errMsg string
 
-	accessToken, err, errMsg = getWxAccessToken(wxAppId)
+	accessToken, err, errMsg = getWxAccessToken(wxAppId, "")
 	if err != nil {
 		utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
 		return
@@ -232,7 +232,7 @@ func toSendTemplateMsg(wxAppId string, data []byte, resource string, sendType in
 	//accessToken过期
 	if templateResponse.Errcode == 40001 {
 		//强刷token并重新推送
-		accessToken, err, errMsg = refreshWxAccessToken(wxAppId)
+		accessToken, err, errMsg = refreshWxAccessToken(wxAppId, "")
 		if err != nil {
 			utils.FileLog.Info(fmt.Sprintf("refreshWxAccessToken Err:%s", err.Error()))
 			return err
@@ -288,8 +288,18 @@ func toSendTemplateMsg(wxAppId string, data []byte, resource string, sendType in
 	return
 }
 
+// GetWxAccessToken 获取微信token
+func GetWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
+	accessToken, err, errMsg = getWxAccessToken(wxAppId, wxAppSecret)
+	if err != nil {
+		utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
+		return
+	}
+	return
+}
+
 // getWxAccessToken 获取微信token
-func getWxAccessToken(wxAppId string) (accessToken string, err error, errMsg string) {
+func getWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
 	redisKey := getRedisKeyByAppid(wxAppId)
 	if redisKey == `` {
 		errMsg = "未配置缓存key"
@@ -311,11 +321,11 @@ func getWxAccessToken(wxAppId string) (accessToken string, err error, errMsg str
 	}
 
 	//缓存中没有取到数据,那么需要去强制刷新新的accessToken
-	return refreshWxAccessToken(wxAppId)
+	return refreshWxAccessToken(wxAppId, wxAppSecret)
 }
 
 // refreshWxAccessToken 强制刷新微信token
-func refreshWxAccessToken(wxAppId string) (accessToken string, err error, errMsg string) {
+func refreshWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
 	fmt.Println("强制刷新" + wxAppId + "微信token")
 	defer func() {
 		if errMsg != `` {
@@ -329,8 +339,10 @@ func refreshWxAccessToken(wxAppId string) (accessToken string, err error, errMsg
 		return
 	}
 
-	wxAppSecret, ok := utils.WxAppList[wxAppId]
-	if !ok {
+	if wxAppSecret == "" {
+		wxAppSecret, _ = utils.WxAppList[wxAppId]
+	}
+	if wxAppSecret == "" {
 		err = errors.New("缺少密钥信息")
 		utils.FileLog.Info(fmt.Sprintf("获取Token失败, errMsg:%s", err.Error()))
 		return
@@ -373,33 +385,6 @@ func refreshWxAccessToken(wxAppId string) (accessToken string, err error, errMsg
 	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 := models.GetWxToken(wxAppId, wxAppSecret)
-		if tErr != nil {
-			return "", tErr
-		}
-		if token.Errmsg != "" {
-			err = errors.New("获取access_token 失败 errcode:" + token.Errmsg + " ;errmsg:" + token.Errmsg)
-			return "", err
-		}
-		//更新redis的accessToken(过期时间提前十分钟)
-		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
-}
-
 // 根据微信appid获取对应的缓存key
 func getRedisKeyByAppid(wxAppId string) (redisKey string) {
 	switch wxAppId {
@@ -407,6 +392,8 @@ func getRedisKeyByAppid(wxAppId string) (redisKey string) {
 		redisKey = utils.CACHE_WX_ACCESS_TOKEN_HZ
 	case utils.AdminWxAppId:
 		redisKey = utils.HZ_ADMIN_WX_ACCESS_TOEKN + wxAppId
+	default:
+		redisKey = utils.HZ_ADMIN_WX_ACCESS_TOEKN + wxAppId
 	}
 	return redisKey
 }

+ 24 - 26
services/wechat_send_msg_cygx.go

@@ -4,18 +4,16 @@ import (
 	"bytes"
 	"encoding/json"
 	"errors"
+	"eta/eta_pub/models"
+	"eta/eta_pub/services/alarm_msg"
+	"eta/eta_pub/utils"
 	"fmt"
-	"hongze/hongze_public_api/models"
-	"hongze/hongze_public_api/services/alarm_msg"
-	"hongze/hongze_public_api/utils"
 	"io/ioutil"
 	"net/http"
 	"strings"
 	"time"
 )
 
-
-
 func SendWxTemplateMsgCygx(sendInfo *models.SendWxTemplate) (err error) {
 	var msg string
 	defer func() {
@@ -114,9 +112,9 @@ func sendTemplateMsgCygx(sendMap map[string]interface{}, openIdArr []string, res
 func toSendTemplateMsgCygx(data []byte, resource string, sendType int, openId, uniqueCode string) (err error) {
 	utils.FileLog.Info("Send:" + string(data))
 	//获取accessToken
-	accessToken, err,errMsg := getWxAccessTokenCygx()
+	accessToken, err, errMsg := getWxAccessTokenCygx()
 	if err != nil {
-		utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s",err.Error(), errMsg))
+		utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
 		return
 	}
 	sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
@@ -161,10 +159,10 @@ func toSendTemplateMsgCygx(data []byte, resource string, sendType int, openId, u
 	}
 
 	//accessToken过期
-	if templateResponse.Errcode == 40001{
+	if templateResponse.Errcode == 40001 {
 		//强刷token并重新推送
-		accessToken,err,errMsg = refreshWxAccessTokenCygx()
-		if err != nil{
+		accessToken, err, errMsg = refreshWxAccessTokenCygx()
+		if err != nil {
 			utils.FileLog.Info(fmt.Sprintf("refreshWxAccessToken Err:%s", err.Error()))
 			return err
 		}
@@ -220,15 +218,15 @@ func toSendTemplateMsgCygx(data []byte, resource string, sendType int, openId, u
 }
 
 // getWxAccessTokenCygx 获取微信token
-func getWxAccessTokenCygx()(accessToken string,err error,errMsg string){
+func getWxAccessTokenCygx() (accessToken string, err error, errMsg string) {
 	var tokenKey string
-	if utils.RunMode == "debug"{
+	if utils.RunMode == "debug" {
 		tokenKey = utils.CACHE_WX_ACCESS_TOKEN_HZ
-	}else{
+	} else {
 		tokenKey = utils.CACHE_WX_ACCESS_TOKEN_CYGX
 	}
 	accessToken, err = utils.Rc.RedisString(tokenKey)
-	if accessToken != ""{
+	if accessToken != "" {
 		return
 	}
 
@@ -237,10 +235,10 @@ func getWxAccessTokenCygx()(accessToken string,err error,errMsg string){
 }
 
 // refreshWxAccessTokenCygx 强制刷新微信token
-func refreshWxAccessTokenCygx()(accessToken string,err error,errMsg string){
-	fmt.Println("强制刷新"+utils.WxAppIdCygx+"微信token")
-	if errMsg !=``{
-		utils.FileLog.Info(fmt.Sprintf("强制刷新%s微信token异常:%s", utils.WxAppIdCygx,errMsg))
+func refreshWxAccessTokenCygx() (accessToken string, err error, errMsg string) {
+	fmt.Println("强制刷新" + utils.WxAppIdCygx + "微信token")
+	if errMsg != `` {
+		utils.FileLog.Info(fmt.Sprintf("强制刷新%s微信token异常:%s", utils.WxAppIdCygx, errMsg))
 	}
 	//调用微信官方接口获取新的accessToken
 	wxAccessToken, tmpErr := models.GetWxTokenCygx()
@@ -258,23 +256,23 @@ func refreshWxAccessTokenCygx()(accessToken string,err error,errMsg string){
 	accessToken = wxAccessToken.AccessToken
 	//更新mysql的accessToken(如果是debug环境的话)
 	var tokenKey string
-	if utils.RunMode == "debug"{
+	if utils.RunMode == "debug" {
 		expiresIn := time.Now().Add(time.Duration(wxAccessToken.ExpiresIn) * time.Second).Unix()
-		err = models.ModifyAccessToken(wxAccessToken.AccessToken,expiresIn)
-		if err!=nil{
+		err = models.ModifyAccessToken(wxAccessToken.AccessToken, expiresIn)
+		if err != nil {
 			errMsg = "更新mysql中的accessToken失败 Err:" + err.Error()
 			return
 		}
 		tokenKey = utils.CACHE_WX_ACCESS_TOKEN_HZ
-	}else{
+	} else {
 		tokenKey = utils.CACHE_WX_ACCESS_TOKEN_CYGX
 	}
 	//更新redis的accessToken(过期时间提前十分钟)
-	redisTimeExpire := time.Duration(wxAccessToken.ExpiresIn - 600) * time.Second
-	err = utils.Rc.Put(tokenKey,accessToken,redisTimeExpire)
-	if err!=nil{
+	redisTimeExpire := time.Duration(wxAccessToken.ExpiresIn-600) * time.Second
+	err = utils.Rc.Put(tokenKey, accessToken, redisTimeExpire)
+	if err != nil {
 		errMsg = "更新redis中的accessToken失败 Err:" + err.Error()
 		return
 	}
 	return
-}
+}

+ 1 - 1
utils/config.go

@@ -87,7 +87,7 @@ func init() {
 			RunMode = "release"
 		}
 		fmt.Println("RunMode:", RunMode)
-		configPath := `/home/code/config/hongze_public_api/conf/app.conf`
+		configPath := `/home/code/config/eta_pub/conf/app.conf`
 		fmt.Println("configPath:", configPath)
 		err = web.LoadAppConfig("ini", configPath)
 		if err != nil {