Browse Source

Merge branch 'bzq/dev' of eta_mini/eta_mini_bridge into master

鲍自强 9 months ago
parent
commit
9df3623b62
5 changed files with 119 additions and 27 deletions
  1. 62 2
      controllers/report.go
  2. 9 0
      routers/commentsRouter.go
  3. 3 9
      services/template_msg.go
  4. 36 15
      services/wechat.go
  5. 9 1
      utils/constants.go

+ 62 - 2
controllers/report.go

@@ -179,7 +179,15 @@ func (this *ReportController) Detail() {
 		return
 	}
 	// 有效期是否到期
-	IsVail := user.ValidEndTime.After(time.Now())
+	var IsVail bool
+	if user.Status == utils.UserStatusFormal {
+		if user.ValidEndTime.After(time.Now()) && user.ValidStartTime.Before(time.Now()) {
+			IsVail = true
+		} else {
+			IsVail = false
+			user.Status = utils.UserStatusNo
+		}
+	}
 
 	report, err := models.GetReportById(reportId)
 	if err != nil {
@@ -224,7 +232,7 @@ func (this *ReportController) Detail() {
 	resp := new(response.ReportDetailResp)
 	if !IsPublic {
 		// 如果被禁用或是潜在用户,直接返回无阅读报告权限
-		if user.Status == 0 || user.Status == 1 {
+		if user.Status == utils.UserStatusNo || user.Status == utils.UserStatusPotential {
 			resp.Report = report
 			resp.Status = utils.ReportPermissionStatusNo
 			report.Content = ""
@@ -300,6 +308,58 @@ func (this *ReportController) Detail() {
 	br.Msg = "获取成功"
 }
 
+// @Title 研报详情
+// @Description 研报详情接口
+// @Param   ReportId   query   int  true       "报告id"
+// @Success 200 {object} models.ReportDetailResp
+// @router /detail/noUser [get]
+func (this *ReportController) DetailNoUser() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	reportId, err := this.GetInt("ReportId")
+	if err != nil {
+		br.Msg = "参数获取失败"
+		br.ErrMsg = "参数获取失败,Err:" + err.Error()
+		return
+	}
+	if reportId <= 0 {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,报告id小于等于0"
+		return
+	}
+
+	report, err := models.GetReportById(reportId)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			br.Ret = 200
+			br.Data = new(response.ReportDetailResp)
+			br.Success = true
+			br.Msg = "该报告已删除或不存在"
+			return
+		}
+		br.Msg = "该报告已删除"
+		br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
+		return
+	}
+	report.ContentSub = html.UnescapeString(report.ContentSub)
+	report.Content = ""
+	if report == nil {
+		br.Msg = "报告不存在"
+		return
+	}
+	resp := new(response.ReportDetailResp)
+	resp.Report = report
+	resp.Status = utils.ReportPermissionStatusNoUser
+
+	br.Data = resp
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+}
+
 // @Title List
 // @Description create users
 // @Param	body		body 	models.User	true		"body for user content"

+ 9 - 0
routers/commentsRouter.go

@@ -97,6 +97,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ReportController"] = append(beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ReportController"],
+        beego.ControllerComments{
+            Method: "DetailNoUser",
+            Router: `/detail/noUser`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ReportController"] = append(beego.GlobalControllerRouter["eta/eta_mini_bridge/controllers:ReportController"],
         beego.ControllerComments{
             Method: "List",

+ 3 - 9
services/template_msg.go

@@ -5,7 +5,6 @@ import (
 	"encoding/json"
 	"errors"
 	"eta/eta_mini_bridge/models"
-	"eta/eta_mini_bridge/services/alarm_msg"
 	"eta/eta_mini_bridge/utils"
 	"fmt"
 	"io"
@@ -139,22 +138,17 @@ func SendMultiTemplateMsg(sendData map[string]interface{}, items []*OpenIdList,
 	ws := GetWxChat()
 	accessToken, err := ws.GetAccessToken()
 	if err != nil {
-		alarm_msg.SendAlarmMsg("获取微信token失败, Err:"+err.Error(), 1)
+		utils.ApiLog.Info("获取微信token失败, Err:"+err.Error(), 1)
+		// alarm_msg.SendAlarmMsg("获取微信token失败, Err:"+err.Error(), 1)
 		return
 	}
 
-	var wxAppPath string
-	if utils.RunMode == "debug" {
-		wxAppPath = fmt.Sprintf("pages-question/answerDetail?id=%d", 3800)
-	} else {
-		wxAppPath = fmt.Sprintf("pages-report/reportDetail/index?id=%d", reportId)
-	}
 	sendMap := make(map[string]interface{})
 	for _, item := range items {
 		sendMap["template_id"] = utils.TEMPLATE_ID_BY_PRODUCT
 		sendMap["miniprogram"] = map[string]interface{}{
 			"appid":    utils.WX_MINI_APPID,
-			"pagepath": wxAppPath,
+			"pagepath": fmt.Sprintf("pages-report/reportDetail/index?id=%d", reportId),
 		}
 		sendMap["touser"] = item.OpenId
 		sendMap["data"] = sendData

+ 36 - 15
services/wechat.go

@@ -1,7 +1,7 @@
 package services
 
 import (
-	"errors"
+	"eta/eta_mini_bridge/models"
 	"eta/eta_mini_bridge/utils"
 	"fmt"
 	"time"
@@ -36,27 +36,48 @@ func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
 
 // GetAccessToken 获取accessToken
 func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
-	accessToken, err = utils.Rc.RedisString(utils.CACHE_WX_ACCESS_TOKEN_HZ)
-	if accessToken != "" {
+	//如果300s就要过期了,那么就去刷新accessToken
+	wxToken, err := models.GetWxTokenById()
+	if err != nil {
 		return
 	}
+	//如果300s就要过期了,那么就去刷新accessToken
+	if wxToken.ExpiresIn < time.Now().Unix()+300 {
+		tmpAccessToken, expires, tmpErr := getTokenFromServer(utils.DW_WX_APPID, utils.DW_WX_APP_SECRET)
+		if tmpErr != nil {
+			err = tmpErr
+			return
+		}
 
-	// 缓存中没有取到数据,那么就需要强制刷新的accessToken
-	tmpAccessToken, expires, tmpErr := getTokenFromServer(utils.DW_WX_APPID, utils.DW_WX_APP_SECRET)
-	if tmpAccessToken == "" {
-		err = errors.New("获取微信token失败,Err:" + tmpErr.Error())
-		return
-	}
-	redisTimeExpire := time.Duration(expires-600) * time.Second
-	err = utils.Rc.Put(utils.CACHE_WX_ACCESS_TOKEN_HZ, tmpAccessToken, redisTimeExpire)
-	if err != nil {
-		err = errors.New("更新微信token失败")
-		return
+		var updateCols = []string{"access_token", "expires_in"}
+		wxToken.AccessToken = tmpAccessToken
+		wxToken.ExpiresIn = time.Now().Unix() + expires - 600 //快过期前10分钟就刷新掉
+		wxToken.Update(updateCols)
 	}
-	accessToken = tmpAccessToken
+	accessToken = wxToken.AccessToken
 	return
 }
 
+// accessToken, err = utils.Rc.RedisString(utils.CACHE_WX_ACCESS_TOKEN_DW)
+// if accessToken != "" {
+// 	return
+// }
+
+// // 缓存中没有取到数据,那么就需要强制刷新的accessToken
+// tmpAccessToken, _, tmpErr := getTokenFromServer(utils.DW_WX_APPID, utils.DW_WX_APP_SECRET)
+// if tmpAccessToken == "" {
+// 	err = errors.New("获取微信token失败,Err:" + tmpErr.Error())
+// 	return
+// }
+
+// redisTimeExpire := time.Duration(expires-600) * time.Second
+// err = utils.Rc.Put(utils.CACHE_WX_ACCESS_TOKEN_HZ, tmpAccessToken, redisTimeExpire)
+// if err != nil {
+// 	err = errors.New("更新微信token失败")
+// 	return
+// }
+// accessToken = tmpAccessToken
+// return
 // //如果300s就要过期了,那么就去刷新accessToken
 // if wxToken.ExpiresIn < time.Now().Unix()+300 {
 // 	tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)

+ 9 - 1
utils/constants.go

@@ -21,12 +21,20 @@ const (
 	ReportPermissionStatusNoPermission = 2 //没有该品种权限
 	ReportPermissionStatusNo           = 3 //没有权限
 	ReportPermissionStatusHas          = 4 //有该品种权限
+	ReportPermissionStatusNoUser       = 5 //没有获得用户信息
 )
 
 // 缓存key
 const (
-	CACHE_WX_ACCESS_TOKEN_HZ = "wx:accesstoken:dw" //东吴公众号 微信accessToken
+	CACHE_WX_ACCESS_TOKEN_DW = "wx:accesstoken:dw" //东吴公众号 微信accessToken
 )
 const (
 	APPNAME = "东吴小程序桥接服务"
 )
+
+// 用户状态定义
+const (
+	UserStatusNo        = 0 //禁用
+	UserStatusPotential = 1 //潜在用户
+	UserStatusFormal    = 2 //正式用户
+)