|
@@ -7,6 +7,7 @@ import (
|
|
|
"eta/eta_api/services/alarm_msg"
|
|
|
"eta/eta_api/services/data"
|
|
|
"fmt"
|
|
|
+ "github.com/sirupsen/logrus"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
"strconv"
|
|
@@ -17,12 +18,8 @@ import (
|
|
|
|
|
|
"eta/eta_api/models"
|
|
|
"eta/eta_api/utils"
|
|
|
-
|
|
|
- "github.com/rdlucklib/rdluck_tools/log"
|
|
|
)
|
|
|
|
|
|
-var apiLog *log.Log
|
|
|
-
|
|
|
type AfterHandle func(bodyByte []byte) error
|
|
|
|
|
|
// AfterHandlerUrlMap 结束后待处理的url
|
|
@@ -51,37 +48,28 @@ var AdminOperateRecordMap = map[string]string{
|
|
|
"/adminapi/system/menu/list": "/adminapi/system/menu/list",
|
|
|
}
|
|
|
|
|
|
-func init() {
|
|
|
- if utils.RunMode == "release" {
|
|
|
- logDir := `/data/rdlucklog/eta_api`
|
|
|
- apiLog = log.Init("20060102.api", logDir)
|
|
|
- } else {
|
|
|
- apiLog = log.Init("20060102.api")
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
type BaseAuthController struct {
|
|
|
web.Controller
|
|
|
SysUser *system.Admin
|
|
|
}
|
|
|
|
|
|
-func (this *BaseAuthController) Prepare() {
|
|
|
+func (c *BaseAuthController) Prepare() {
|
|
|
fmt.Println("enter prepare")
|
|
|
- method := this.Ctx.Input.Method()
|
|
|
- uri := this.Ctx.Input.URI()
|
|
|
+ method := c.Ctx.Input.Method()
|
|
|
+ uri := c.Ctx.Input.URI()
|
|
|
fmt.Println("Url:", uri)
|
|
|
if method != "HEAD" {
|
|
|
if method == "POST" || method == "GET" {
|
|
|
- authorization := this.Ctx.Input.Header("authorization")
|
|
|
+ authorization := c.Ctx.Input.Header("authorization")
|
|
|
if authorization == "" {
|
|
|
- authorization = this.Ctx.Input.Header("Authorization")
|
|
|
+ authorization = c.Ctx.Input.Header("Authorization")
|
|
|
}
|
|
|
if authorization == "" {
|
|
|
- newAuthorization := this.GetString("authorization")
|
|
|
+ newAuthorization := c.GetString("authorization")
|
|
|
if newAuthorization != `` {
|
|
|
authorization = "authorization=" + newAuthorization
|
|
|
} else {
|
|
|
- newAuthorization = this.GetString("Authorization")
|
|
|
+ newAuthorization = c.GetString("Authorization")
|
|
|
authorization = "authorization=" + newAuthorization
|
|
|
}
|
|
|
} else {
|
|
@@ -101,14 +89,14 @@ func (this *BaseAuthController) Prepare() {
|
|
|
}
|
|
|
}
|
|
|
if authorization == "" {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
authorizationArr := strings.Split(authorization, "$")
|
|
|
if len(authorizationArr) <= 1 {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
tokenStr := authorizationArr[0]
|
|
@@ -121,51 +109,51 @@ func (this *BaseAuthController) Prepare() {
|
|
|
//校验token是否合法
|
|
|
// JWT校验Token和Account
|
|
|
if !utils.CheckToken(account, token) {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "鉴权失败,请重新登录!", ErrMsg: "登录失效,请重新登陆!,CheckToken Fail"}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "鉴权失败,请重新登录!", ErrMsg: "登录失效,请重新登陆!,CheckToken Fail"}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
session, err := system.GetSysSessionByToken(token)
|
|
|
if err != nil {
|
|
|
if err.Error() == utils.ErrNoRow() {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + token}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + token}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
if session == nil {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
if time.Now().After(session.ExpiredTime) {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新登录!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "请重新登录!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
admin, err := system.GetSysUserById(session.SysUserId)
|
|
|
if err != nil {
|
|
|
if err.Error() == utils.ErrNoRow() {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.SysUserId)}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.SysUserId)}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取admin信息异常,Eerr:" + err.Error()}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取admin信息异常,Eerr:" + err.Error()}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
if admin == nil {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
//如果不是启用状态
|
|
|
if admin.Enabled != 1 {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "账户信息异常!", ErrMsg: "账户被禁用,不允许登陆!,CheckToken Fail"}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "账户信息异常!", ErrMsg: "账户被禁用,不允许登陆!,CheckToken Fail"}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -174,15 +162,15 @@ func (this *BaseAuthController) Prepare() {
|
|
|
loginKey := fmt.Sprint(utils.CACHE_ACCESS_TOKEN_LOGIN, session.Id)
|
|
|
loginInfo, _ := utils.Rc.RedisString(loginKey)
|
|
|
if loginInfo == `` {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "超时未操作,系统自动退出!", ErrMsg: "超时未操作,系统自动退出!"}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "超时未操作,系统自动退出!", ErrMsg: "超时未操作,系统自动退出!"}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if loginInfo != "1" {
|
|
|
msg := `该账号于` + admin.LastLoginTime + "在其他网络登录。此客户端已退出登录。"
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: msg, ErrMsg: msg}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: msg, ErrMsg: msg}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -196,10 +184,10 @@ func (this *BaseAuthController) Prepare() {
|
|
|
}
|
|
|
|
|
|
admin.RoleTypeCode = GetSysUserRoleTypeCode(admin.RoleTypeCode)
|
|
|
- this.SysUser = admin
|
|
|
+ c.SysUser = admin
|
|
|
} else {
|
|
|
- this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
|
|
|
- this.StopRun()
|
|
|
+ c.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
|
|
|
+ c.StopRun()
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -305,8 +293,7 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
|
|
|
if requestBody == "" {
|
|
|
requestBody = c.Ctx.Input.URI()
|
|
|
}
|
|
|
- apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
|
|
|
-
|
|
|
+ c.logUri(data, requestBody, ip)
|
|
|
// 如果不是debug分支的话,那么需要加密返回
|
|
|
if utils.RunMode != "debug" {
|
|
|
content = utils.DesBase64Encrypt(content)
|
|
@@ -368,3 +355,63 @@ func GetSysUserRoleTypeCode(roleTypeCode string) string {
|
|
|
}
|
|
|
return ""
|
|
|
}
|
|
|
+
|
|
|
+func (c *BaseAuthController) logUri(data interface{}, requestBody, ip string) {
|
|
|
+ authorization := ""
|
|
|
+ method := c.Ctx.Input.Method()
|
|
|
+ uri := c.Ctx.Input.URI()
|
|
|
+ fmt.Println("Url:", 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var reqData interface{}
|
|
|
+ err := json.Unmarshal([]byte(requestBody), &reqData)
|
|
|
+ if err != nil {
|
|
|
+ utils.ApiLog.WithFields(logrus.Fields{
|
|
|
+ "uri": c.Ctx.Input.URI(),
|
|
|
+ "authorization": authorization,
|
|
|
+ "requestBody": requestBody,
|
|
|
+ "responseBody": data,
|
|
|
+ "ip": ip,
|
|
|
+ }).Info("请求详情")
|
|
|
+ } else {
|
|
|
+ utils.ApiLog.WithFields(logrus.Fields{
|
|
|
+ "uri": c.Ctx.Input.URI(),
|
|
|
+ "authorization": authorization,
|
|
|
+ "requestBody": reqData,
|
|
|
+ "responseBody": data,
|
|
|
+ "ip": ip,
|
|
|
+ }).Info("请求详情")
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|