Explorar el Código

Merge branch 'bzq/dev' of eta_mini/eta_mini_api into debug

鲍自强 hace 11 meses
padre
commit
bef99c190e
Se han modificado 6 ficheros con 155 adiciones y 31 borrados
  1. 47 0
      controllers/base_auth.go
  2. 45 1
      controllers/base_common.go
  3. 55 25
      controllers/wechat.go
  4. 3 0
      services/sms.go
  5. 2 5
      utils/config.go
  6. 3 0
      utils/email.go

+ 47 - 0
controllers/base_auth.go

@@ -4,7 +4,10 @@ import (
 	"encoding/json"
 	"eta/eta_mini_api/models"
 	"eta/eta_mini_api/utils"
+	"fmt"
 	"net/http"
+	"net/url"
+	"strings"
 	"time"
 
 	"github.com/beego/beego/v2/server/web"
@@ -123,6 +126,9 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 		http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
 		return err
 	}
+	ip := c.Ctx.Input.IP()
+	requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
+	c.logUri(content, requestBody, ip)
 	if utils.RunMode != "debug" {
 		content = utils.DesBase64Encrypt(content, utils.DesKey)
 		content = []byte(`"` + string(content) + `"`)
@@ -132,3 +138,44 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 	}
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseAuthController) logUri(respContent []byte, requestBody, ip string) {
+	authorization := ""
+	method := c.Ctx.Input.Method()
+	uri := c.Ctx.Input.URI()
+	if method != "HEAD" {
+		if method == "POST" || method == "GET" {
+			authorization = c.Ctx.Input.Header("authorization")
+			if authorization == "" {
+				authorization = c.Ctx.Input.Header("Authorization")
+			}
+			if authorization == "" {
+				newAuthorization := c.GetString("authorization")
+				if newAuthorization != `` {
+					authorization = "authorization=" + newAuthorization
+				} else {
+					newAuthorization = c.GetString("Authorization")
+					authorization = "authorization=" + newAuthorization
+				}
+			} else {
+				if strings.Contains(authorization, ";") {
+					authorization = strings.Replace(authorization, ";", "$", 1)
+				}
+			}
+			if authorization == "" {
+				strArr := strings.Split(uri, "?")
+				for k, v := range strArr {
+					fmt.Println(k, v)
+				}
+				if len(strArr) > 1 {
+					authorization = strArr[1]
+					authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
+					fmt.Println(authorization)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 45 - 1
controllers/base_common.go

@@ -5,8 +5,10 @@ import (
 	"eta/eta_mini_api/models"
 	"eta/eta_mini_api/services"
 	"eta/eta_mini_api/utils"
+	"fmt"
 	"net/http"
 	"net/url"
+	"strings"
 
 	"github.com/beego/beego/v2/server/web"
 )
@@ -65,7 +67,8 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 	}
 	ip := c.Ctx.Input.IP()
 	requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
-	utils.ApiLog.Info("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
+
+	c.logUri(content, requestBody, ip)
 
 	if coding {
 		content = []byte(utils.StringsToJSON(string(content)))
@@ -80,3 +83,44 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
+	authorization := ""
+	method := c.Ctx.Input.Method()
+	uri := c.Ctx.Input.URI()
+	if method != "HEAD" {
+		if method == "POST" || method == "GET" {
+			authorization = c.Ctx.Input.Header("authorization")
+			if authorization == "" {
+				authorization = c.Ctx.Input.Header("Authorization")
+			}
+			if authorization == "" {
+				newAuthorization := c.GetString("authorization")
+				if newAuthorization != `` {
+					authorization = "authorization=" + newAuthorization
+				} else {
+					newAuthorization = c.GetString("Authorization")
+					authorization = "authorization=" + newAuthorization
+				}
+			} else {
+				if strings.Contains(authorization, ";") {
+					authorization = strings.Replace(authorization, ";", "$", 1)
+				}
+			}
+			if authorization == "" {
+				strArr := strings.Split(uri, "?")
+				for k, v := range strArr {
+					fmt.Println(k, v)
+				}
+				if len(strArr) > 1 {
+					authorization = strArr[1]
+					authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
+					fmt.Println(authorization)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 55 - 25
controllers/wechat.go

@@ -19,6 +19,16 @@ type WechatController struct {
 	BaseCommonController
 }
 
+type Notify struct {
+	ToUserName   string `xml:"ToUserName"`
+	FromUserName string `xml:"FromUserName"`
+	CreateTime   int    `xml:"CreateTime"`
+	MsgType      string `xml:"MsgType"`
+	Event        string `xml:"Event"`
+	EventKey     string `xml:"EventKey"`
+	Content      string `xml:"Content"`
+}
+
 // @Title 微信获取签名接口
 // @Description 微信获取签名接口
 // @Param   Url   query   string  true       "url地址"
@@ -28,15 +38,6 @@ func (this *WechatController) Notify() {
 	echostr := this.GetString("echostr")
 	method := this.Ctx.Input.Method()
 
-	type Notify struct {
-		ToUserName   string `xml:"ToUserName"`
-		FromUserName string `xml:"FromUserName"`
-		CreateTime   int    `xml:"CreateTime"`
-		MsgType      string `xml:"MsgType"`
-		Event        string `xml:"Event"`
-		EventKey     string `xml:"EventKey"`
-		Content      string `xml:"Content"`
-	}
 	if method == "POST" {
 		body := this.Ctx.Input.RequestBody
 		utils.FileLog.Info("wechat notify:" + string(body))
@@ -89,22 +90,43 @@ func (this *WechatController) Notify() {
 func subscribe(openId string) {
 	userRecord, err := models.GetUserRecordByOpenId(openId)
 	if err != nil && err.Error() != utils.ErrNoRow() {
-		fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
+		utils.FileLog.Info("通过openid获取user_record记录失败,err:" + err.Error())
 		return
 	}
-	err = nil
-
 	// openId已存在
 	if userRecord != nil {
 		if userRecord.UserId > 0 && userRecord.UnionId != "" { //已经绑定了的话,那么就去修改用户状态
-			models.UserSubscribe(1, openId)
+			err = models.UserSubscribe(1, openId)
+			if err != nil {
+				utils.FileLog.Info("关注后,通过openid修改user_record异常,err:" + err.Error())
+			}
+			user, err := models.GetUserByUnionId(userRecord.UnionId)
+			if err != nil {
+				utils.FileLog.Info("关注后,通过unionid获取user记录失败,err:" + err.Error())
+				return
+			}
+			if user != nil {
+				user.IsSubscribed = true
+				user.ModifyTime = time.Now()
+				err = user.Update([]string{"modify_time", "is_subscribed"})
+				if err != nil {
+					utils.FileLog.Info("关注后,修改绑定用户状态异常,err:" + err.Error())
+					return
+				}
+				userRecord.UserId = user.UserId
+				err = userRecord.Update([]string{"user_id"})
+				if err != nil {
+					utils.FileLog.Info("关注后,修改公众号绑定用户异常,err:" + err.Error())
+					return
+				}
+			}
 		} else {
 			// 没有绑定的话,那么校验下unionid,然后再去修改
 			unionId := userRecord.UnionId
 			if unionId == `` {
 				wxUserItem, err := wechat.GetUserInfo(openId)
 				if err != nil {
-					fmt.Println("获取用户信息失败,err:" + err.Error())
+					utils.FileLog.Info("获取用户信息失败,err:" + err.Error())
 					return
 				}
 				if wxUserItem.UnionID != `` {
@@ -116,12 +138,12 @@ func subscribe(openId string) {
 			userRecord.SubscribeTime = time.Now()
 			err = userRecord.Update([]string{"union_id", "subscribe", "subscribe_time"})
 			if err != nil {
-				fmt.Println("关注后,通过openid更新user_record异常,err:", err)
+				utils.FileLog.Info("关注后,通过openid更新user_record异常,err:" + err.Error())
 				return
 			}
 			user, err := models.GetUserByUnionId(unionId)
 			if err != nil {
-				fmt.Println("关注后,通过unionid获取user记录失败,err:" + err.Error())
+				utils.FileLog.Info("关注后,通过unionid获取user记录失败,err:" + err.Error())
 				return
 			}
 			if user != nil {
@@ -129,11 +151,15 @@ func subscribe(openId string) {
 				user.ModifyTime = time.Now()
 				err = user.Update([]string{"modify_time", "is_subscribed"})
 				if err != nil {
-					fmt.Println("关注后,修改绑定用户状态异常,err:", err)
+					utils.FileLog.Info("关注后,修改绑定用户状态异常,err:" + err.Error())
 					return
 				}
 				userRecord.UserId = user.UserId
-				userRecord.Update([]string{"user_id"})
+				err = userRecord.Update([]string{"user_id"})
+				if err != nil {
+					utils.FileLog.Info("关注后,修改公众号绑定用户异常,err:" + err.Error())
+					return
+				}
 			}
 		}
 		return
@@ -142,7 +168,7 @@ func subscribe(openId string) {
 	// 没有记录,那么需要获取下unionid
 	wxUserItem, err := wechat.GetUserInfo(openId)
 	if err != nil {
-		fmt.Println("获取用户信息失败,err:" + err.Error())
+		utils.FileLog.Info("获取用户信息失败,err:" + err.Error())
 		return
 	}
 	newUserRecord := &models.UserRecord{
@@ -161,25 +187,29 @@ func subscribe(openId string) {
 	insertId, err := newUserRecord.Insert()
 	newUserRecord.UserRecordId = int(insertId)
 	if err != nil {
-		fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
+		utils.FileLog.Info("关注后,添加user_record信息失败,err:" + err.Error())
 		return
 	}
 	// 检测用户是否绑定小程序,如果绑定修改用户状态
 	user, err := models.GetUserByUnionId(newUserRecord.UnionId)
 	if err != nil {
-		fmt.Println("修改绑定用户状态异常,ERR:", err)
+		utils.FileLog.Info("修改绑定用户状态异常,err:" + err.Error())
 		return
 	}
 	if user != nil {
-		user.IsRegistered = true
+		user.IsSubscribed = true
 		user.ModifyTime = time.Now()
-		err = user.Update([]string{"is_registered", "modify_time"})
+		err = user.Update([]string{"is_subscribed", "modify_time"})
 		if err != nil {
-			fmt.Println("关注后,修改绑定用户状态异常,ERR:", err)
+			utils.FileLog.Info("关注后,修改绑定用户状态异常,err:" + err.Error())
 			return
 		}
 		newUserRecord.UserId = user.UserId
-		newUserRecord.Update([]string{"user_id"})
+		err = newUserRecord.Update([]string{"user_id"})
+		if err != nil {
+			utils.FileLog.Info("关注后,修改公众号绑定用户异常,err:" + err.Error())
+			return
+		}
 	}
 
 }

+ 3 - 0
services/sms.go

@@ -12,6 +12,9 @@ import (
 func SendSmsCode(mobile, vcode string) bool {
 	flag := false
 	tplId := utils.SMS_TPLID
+	if tplId == "" {
+		tplId = "262642"
+	}
 	expiresTime := "15"
 	result, err := sendSms(mobile, tplId, vcode, expiresTime)
 	if err != nil {

+ 2 - 5
utils/config.go

@@ -65,13 +65,14 @@ func init() {
 	RunMode = tmpRunMode
 	fmt.Println("RunMode:", RunMode)
 	if RunMode == "" {
-		RunMode = "debug"
 		configPath := `/home/code/config/eta_mini_api/conf/app.conf`
 		fmt.Println("configPath:", configPath)
 		err = web.LoadAppConfig("ini", configPath)
 		if err != nil {
 			fmt.Println("web.LoadAppConfig Err:" + err.Error())
 		}
+		tmpRunMode, _ := web.AppConfig.String("run_mode")
+		RunMode = tmpRunMode
 	}
 
 	config, err := web.AppConfig.GetSection(RunMode)
@@ -89,10 +90,6 @@ func init() {
 	WX_MINI_APPID = config["wx_mini_appid"]
 	WX_MINI_APP_SECRET = config["wx_mini_app_secret"]
 	// 仅测试
-	SMS_TPLID = "262642"
-	AlarmMsgUrl = `http://127.0.0.1:8606/api/alarm/send`
-	WX_MINI_APPID = "wx1c6d59a9ca4b42b3"
-	WX_MINI_APP_SECRET = "090716fa7b7fd89172cb26065fa4e6af"
 
 	DW_WX_Id = config["dw_wx_id"]
 	DW_WX_APPID = config["dw_wx_appid"]

+ 3 - 0
utils/email.go

@@ -1,6 +1,7 @@
 package utils
 
 import (
+	"crypto/tls"
 	"strings"
 
 	"gopkg.in/gomail.v2"
@@ -49,6 +50,8 @@ func SendEmailByDw(title, content string, touser string) (result bool, err error
 	m.SetHeader("Subject", title)
 	m.SetBody("text/html", content)
 	d := gomail.NewDialer("mail.dwqh88.com", 465, "lvan@dwqh88.com", "Dwqh20248888")
+	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
+
 	if err := d.DialAndSend(m); err != nil {
 		result = false
 		return result, err