Browse Source

Merge branch 'encrypt_v1' into chart2.0

hsun 3 years ago
parent
commit
d0e82e9d31

+ 2 - 1
controller/report/research_report.go

@@ -36,7 +36,8 @@ func GetResearchReportInfo(c *gin.Context) {
 
 	reportInfo, hasPermission, err := report.GetResearchReportInfo(uint64(researchReportId), userInfo.UserID)
 	if err != nil {
-		response.Fail(err.Error(), c)
+		response.Fail("获取报告失败", c)
+		return
 	}
 	if !hasPermission {
 		response.Fail("无权限", c)

+ 30 - 16
controller/response/base.go

@@ -2,8 +2,11 @@ package response
 
 import (
 	"encoding/json"
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/utils"
+	"strings"
 )
 
 var (
@@ -15,23 +18,33 @@ var (
 )
 
 type ResultData struct {
-	Code int         `json:"code" description:"状态码"`
-	Msg  string      `json:"msg" description:"提示信息"`
-	Data interface{} `json:"data" description:"返回数据"`
+	Code   int         `json:"code" description:"状态码"`
+	Msg    string      `json:"msg" description:"提示信息"`
+	Data   interface{} `json:"data" description:"返回数据"`
+	ErrMsg string      `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
 }
 
 func result(code int, resultData ResultData, c *gin.Context) {
 	jsonByte, _ := json.Marshal(resultData)
-	global.LOG.Debug("resultData:", string(jsonByte))
+	token := c.Request.Header.Get("Authorization")
+	if token == "" {
+		token = c.DefaultQuery("authorization", "")
+		if token == "" {
+			token = c.DefaultQuery("Authorization", "")
+		}
+	}
+	logSlice := make([]string, 0)
+	logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
+	logSlice = append(logSlice, fmt.Sprint("Token:", token))
+	logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte)))
 
-	//测试环境,数据不进行加密
-	/*if global.CONFIG.Serve.RunMode == "debug" {
-		c.JSON(code, resultData)
-	} else {
-		responseResult := utils.DesBase64Encrypt(jsonByte)
-		c.JSON(code, responseResult)
-	}*/
-	c.JSON(code, resultData)
+	//记录错误日志
+	if resultData.ErrMsg != "" {
+		logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
+	}
+	global.LOG.Info(strings.Join(logSlice, ";"))
+	encryptResult := utils.DesBase64Encrypt(jsonByte)
+	c.JSON(code, string(encryptResult))
 	c.Abort()
 }
 
@@ -93,11 +106,12 @@ func CustomData(code int, msg string, data interface{}, c *gin.Context) {
 }
 
 // TokenError token异常
-func TokenError(data interface{}, message string, c *gin.Context) {
+func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
 	resultData := ResultData{
-		Code: TOKEN_ERROR_CODE,
-		Msg:  message,
-		Data: data,
+		Code:   TOKEN_ERROR_CODE,
+		Msg:    message,
+		Data:   data,
+		ErrMsg: errMsg,
 	}
 	result(200, resultData, c)
 }

+ 0 - 4
go.mod

@@ -9,9 +9,7 @@ require (
 	github.com/gin-gonic/gin v1.7.4
 	github.com/go-playground/validator/v10 v10.9.0 // indirect
 	github.com/go-redis/redis/v8 v8.11.4
-	github.com/go-sql-driver/mysql v1.6.0 // indirect
 	github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
-	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/jonboulle/clockwork v0.2.2 // indirect
 	github.com/json-iterator/go v1.1.12 // indirect
 	github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
@@ -28,10 +26,8 @@ require (
 	golang.org/x/image v0.0.0-20190802002840-cff245a6509b
 	golang.org/x/sys v0.0.0-20211107104306-e0b2ad06fe42 // indirect
 	golang.org/x/text v0.3.7 // indirect
-	google.golang.org/protobuf v1.27.1 // indirect
 	gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
 	gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
-	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gorm.io/driver/mysql v1.1.3
 	gorm.io/gorm v1.22.2
 	github.com/nosixtools/solarlunar v0.0.0-20211112060703-1b6dea7b4a19

+ 5 - 2
logic/report/research_report.go

@@ -3,7 +3,7 @@ package report
 import (
 	"hongze/hongze_yb/models/tables/company_report_permission"
 	"hongze/hongze_yb/models/tables/research_report"
-	"strings"
+	"hongze/hongze_yb/utils"
 )
 
 func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchReportInfo, hasPermission bool, err error) {
@@ -14,7 +14,8 @@ func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchRepo
 	}
 	reportType := reportInfo.Type
 	//这些个报告需要做权限校验
-	if strings.Contains("month,two_week,other", reportInfo.Type) {
+
+	if utils.InArray(reportInfo.Type, []string{"month", "two_week", "other"}) {
 		list, tmpErr := company_report_permission.GetReportVarietyList(userId, reportType)
 		if tmpErr != nil {
 			err = tmpErr
@@ -34,6 +35,8 @@ func GetResearchReportInfo(researchReportId, userId uint64) (result ResearchRepo
 			//}
 			return
 		}
+	} else {
+		hasPermission = true
 	}
 
 	researchReportTypeList := make([]*company_report_permission.ResearchReportTypeList, 0)

+ 1 - 1
logic/user/user.go

@@ -46,7 +46,7 @@ func SendSmsCode(openid, mobile, areaNum string) (err error, errMsg string) {
 		}
 		err = item.Create()
 	} else {
-		err = errors.New("发送失败,Err:" + err.Error())
+		err = errors.New("短信发送失败")
 	}
 	return
 }

+ 3 - 1
main.go

@@ -1,6 +1,8 @@
 package main
 
-import "hongze/hongze_yb/core"
+import (
+	"hongze/hongze_yb/core"
+)
 
 // @title 弘则研报API接口文档
 // @version 1.0

+ 8 - 8
middleware/token.go

@@ -18,24 +18,24 @@ func Token() gin.HandlerFunc {
 			}
 		}
 		if token == "" {
-			response.TokenError(nil, "未登录或非法访问", c)
+			response.TokenError(nil, "未登录或非法访问", "未登录或非法访问", c)
 			c.Abort()
 			return
 		}
 		sessionInfo, err := session.GetTokenByToken(token)
 		if err != nil {
 			if err == utils.ErrNoRow {
-				response.TokenError(nil, "信息已变更,请重新登陆!", c)
+				response.TokenError(nil, "信息已变更,请重新登陆!", "找不到对应session", c)
 				c.Abort()
 				return
 			}
-			response.TokenError(nil, "网络异常,请稍后重试!", c)
+			response.TokenError(nil, "网络异常,请稍后重试!", err.Error(), c)
 			c.Abort()
 			return
 		}
 
 		if sessionInfo == nil {
-			response.TokenError(nil, "网络异常,请稍后重试!", c)
+			response.TokenError(nil, "网络异常,请稍后重试1038!", "找不到对应session", c)
 			c.Abort()
 			return
 		}
@@ -47,7 +47,7 @@ func Token() gin.HandlerFunc {
 			userInfo = tmpUserInfo
 			err = tmpErr
 		} else {
-			response.TokenError(nil, "数据异常!", c)
+			response.TokenError(nil, "数据异常!", "openid为空", c)
 			c.Abort()
 			return
 		}
@@ -55,18 +55,18 @@ func Token() gin.HandlerFunc {
 		if err != nil {
 			//用户openid查询出来发现没有绑定用户
 			if err == services.ERR_USER_NOT_BIND {
-				response.TokenError(nil, "信息已变更,请重新登陆!", c)
+				response.TokenError(nil, "信息已变更,请重新登陆1058!", err.Error(), c)
 				c.Abort()
 				return
 			}
 			//没有找到记录
 			if err == utils.ErrNoRow {
-				response.TokenError(nil, "信息已变更,请重新登陆!", c)
+				response.TokenError(nil, "信息已变更,请重新登陆2064!", err.Error(), c)
 				c.Abort()
 				return
 			}
 
-			response.TokenError(nil, "网络异常,请稍后重试!", c)
+			response.TokenError(nil, "网络异常,请稍后重试3069!", err.Error(), c)
 			c.Abort()
 			return
 		}

+ 7 - 7
middleware/token_no_login.go

@@ -18,24 +18,24 @@ func TokenNoLogin() gin.HandlerFunc {
 			}
 		}
 		if token == "" {
-			response.TokenError(nil, "未登录或非法访问", c)
+			response.TokenError(nil, "未登录或非法访问", "token为空", c)
 			c.Abort()
 			return
 		}
 		sessionInfo, err := session.GetTokenByToken(token)
 		if err != nil {
 			if err == utils.ErrNoRow {
-				response.TokenError(nil, "信息已变更,请重新登陆!", c)
+				response.TokenError(nil, "信息已变更,请重新登陆!", "找不到session", c)
 				c.Abort()
 				return
 			}
-			response.TokenError(nil, "网络异常,请稍后重试!", c)
+			response.TokenError(nil, "网络异常,请稍后重试!", err.Error(), c)
 			c.Abort()
 			return
 		}
 
 		if sessionInfo == nil {
-			response.TokenError(nil, "网络异常,请稍后重试!", c)
+			response.TokenError(nil, "网络异常,请稍后重试!", "session为空", c)
 			c.Abort()
 			return
 		}
@@ -47,7 +47,7 @@ func TokenNoLogin() gin.HandlerFunc {
 			userInfo = tmpUserInfo
 			err = tmpErr
 		} else {
-			response.TokenError(nil, "数据异常!", c)
+			response.TokenError(nil, "数据异常!", "openid为空", c)
 			c.Abort()
 			return
 		}
@@ -56,12 +56,12 @@ func TokenNoLogin() gin.HandlerFunc {
 		if err != nil && err != services.ERR_USER_NOT_BIND {
 			//没有找到记录
 			if err == utils.ErrNoRow {
-				response.TokenError(nil, "信息已变更,请重新登陆!", c)
+				response.TokenError(nil, "信息已变更,请重新登陆!", err.Error(), c)
 				c.Abort()
 				return
 			}
 
-			response.TokenError(nil, "网络异常,请稍后重试!", c)
+			response.TokenError(nil, "网络异常,请稍后重试!", err.Error(), c)
 			c.Abort()
 			return
 		}

+ 8 - 8
models/tables/company_report_permission/custom_query.go

@@ -33,24 +33,24 @@ func GetReportVarietyList(userId uint64, reportType string) (list []*ReportChapt
 }
 
 type ResearchReportTypeList struct {
-	ResearchReportTypeId    int
-	ResearchReportId        int
+	ResearchReportTypeId    uint64
+	ResearchReportId        uint64
 	ResearchReportTypeTitle string
 	TypeId                  int
-	Edit                    int
-	Trend                   int
+	Edit                    int8
+	Trend                   string
 	ReportChapterTypeKey    string
 	ReportChapterTypeThumb  string
 	BannerUrl               string
 	ReportChapterTypeName   string
-	Sort                    string
+	Sort                    int
 	EditImgUrl              string
 	PauseStartTime          time.Time
 	PauseEndTime            time.Time
 	LastUpdatedTime         time.Time
 }
 
-// 获取研究报告的章节详情
+// GetResearchReportType 获取研究报告的章节详情
 func GetResearchReportType(researchReportId, userId uint64, reportType string) (list []*ResearchReportTypeList, err error) {
 	var condition string
 	whereVals := make([]interface{}, 0)
@@ -68,8 +68,8 @@ func GetResearchReportType(researchReportId, userId uint64, reportType string) (
 			for _, v := range reportChapterTypeList {
 				reportChapterTypeIdList = append(reportChapterTypeIdList, fmt.Sprint(v.ReportChapterTypeId))
 			}
-			condition += ` and rct.report_chapter_type_id in ( ` + `) `
-			whereVals = append(whereVals, strings.Join(reportChapterTypeIdList, ","))
+			condition += ` and rct.report_chapter_type_id in (` + strings.Join(reportChapterTypeIdList, ",") + `) `
+			//whereVals = append(whereVals, strings.Join(reportChapterTypeIdList, ","))
 
 		}
 	}

+ 2 - 2
models/tables/research_report/custom_query.go

@@ -17,8 +17,8 @@ type ResearchReportTypeContent struct {
 	LastUpdatedTime             time.Time `json:"last_updated_time" description:"最近一次更新时间"`
 }
 
-//获取研究报告章节详情
-func GetResearchReportTypeContent(researchReportTypeId int) (items []*ResearchReportTypeContent, err error) {
+// GetResearchReportTypeContent 获取研究报告章节详情
+func GetResearchReportTypeContent(researchReportTypeId uint64) (items []*ResearchReportTypeContent, err error) {
 	sql := `select rrt.research_report_type_title,rrtc.*,rrt.research_report_type_id
 from research_report_type rrt
 inner join research_report_type_content rrtc on rrtc.research_report_type_id = rrt.research_report_type_id

+ 4 - 4
services/sms.go

@@ -10,10 +10,10 @@ import (
 )
 
 // SendSmsCode 发送国内短信
-func SendSmsCode(mobile, vcode string) bool {
+func SendSmsCode(mobile, vCode string) bool {
 	flag := false
 	tplId := "65692"
-	result, err := sendSms(mobile, tplId, vcode)
+	result, err := sendSms(mobile, tplId, vCode)
 	if err != nil {
 		fmt.Println("发送短信失败")
 		return false
@@ -64,9 +64,9 @@ func sendSms(mobile, tplId, code string) (rs []byte, err error) {
 }
 
 // SendSmsCodeGj 发送国际短信
-func SendSmsCodeGj(mobile, vcode, areaNum string) bool {
+func SendSmsCodeGj(mobile, vCode, areaNum string) bool {
 	flag := false
-	result, err := sendSmsGj(mobile, vcode, areaNum)
+	result, err := sendSmsGj(mobile, vCode, areaNum)
 	if err != nil {
 		fmt.Println("发送短信失败")
 		return false

+ 9 - 3
services/user/user.go

@@ -239,12 +239,15 @@ QUERY_WX_USER:
 				if platformUser.CountryCode != "" {
 					countryCode, _ = strconv.Atoi(platformUser.CountryCode)
 				}
-				_, _, tempErr, errMsg := BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
+				tempToken, tempUser, tempErr, _ := BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
 				if tempErr != nil {
-					err = errors.New("自动绑定公众号用户失败" + errMsg)
+					err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error())
 					return
 				}
+				token = tempToken
+				userId = int(tempUser.UserID)
 				isBind = true
+				return
 			}
 		}
 	} else if wxUserErr != nil {
@@ -297,7 +300,10 @@ QUERY_WX_USER:
 	} else {
 		token = tokenItem.AccessToken
 		//如果联系人编号不为空,且联系人编号与session里面的联系人编号不一致的时候,需要做session变更
-		if userId > 0 && tokenItem.UserID != int64(userId) {
+		//if userId > 0 && tokenItem.UserID != int64(userId) {
+		//	_ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
+		//}
+		if userId > 0 {
 			_ = tokenItem.UpdateSession(int64(userId), time.Now().AddDate(0, 1, 0))
 		}
 	}

+ 27 - 0
utils/common.go

@@ -933,3 +933,30 @@ func getMonthDay(year, month int) (days int) {
 	}
 	return
 }
+
+// InArray 是否在切片(数组/map)中含有该值,目前只支持:string、int 、 int64,其他都是返回false
+func InArray(needle interface{}, hyStack interface{}) bool {
+	switch key := needle.(type) {
+	case string:
+		for _, item := range hyStack.([]string) {
+			if key == item {
+				return true
+			}
+		}
+	case int:
+		for _, item := range hyStack.([]int) {
+			if key == item {
+				return true
+			}
+		}
+	case int64:
+		for _, item := range hyStack.([]int64) {
+			if key == item {
+				return true
+			}
+		}
+	default:
+		return false
+	}
+	return false
+}