Bladeren bron

修改空指针处理

wangkaiyue 3 jaren geleden
bovenliggende
commit
bd554512d3
5 gewijzigde bestanden met toevoegingen van 19 en 24 verwijderingen
  1. 1 0
      core/logs/output.go
  2. 10 8
      core/logs/saveDb.go
  3. 4 1
      core/router/ctx.go
  4. 4 1
      core/router/manager.go
  5. 0 14
      core/router/session.go

+ 1 - 0
core/logs/output.go

@@ -12,6 +12,7 @@ import (
 
 // RecordLogAndNotice 记录请求日志及提醒
 func (s *serverLog) RecordLogAndNotice(r *ghttp.Request, err error) {
+
 	if !s.Config.ServerAccessLogEnabled && //关闭成功日志
 		!s.Config.ServerErrorLogEnabled && //关闭错误日志
 		!s.Config.ServerRequestLogSaveDb && //关闭保存数据库日志

+ 10 - 8
core/logs/saveDb.go

@@ -46,14 +46,16 @@ func (s *serverLog) saveLogToDb(ctx *router.GContext, r *ghttp.Request) {
 		"method":    r.Method,
 		"url":       r.URL.String(),
 	}
-	if ctx.Sess.UserId != "" {
-		m["userid"] = ctx.Sess.UserId
-	}
-	if ctx.Sess.NewUid != 0 {
-		m["userid_new"] = ctx.Sess.NewUid
-	}
-	if ctx.Sess.EntId != 0 {
-		m["userid"] = ctx.Sess.EntId
+	if ctx.Sess != nil {
+		if ctx.Sess.UserId != "" {
+			m["userid"] = ctx.Sess.UserId
+		}
+		if ctx.Sess.NewUid != 0 {
+			m["userid_new"] = ctx.Sess.NewUid
+		}
+		if ctx.Sess.EntId != 0 {
+			m["userid"] = ctx.Sess.EntId
+		}
 	}
 
 	timeNow := time.Now()

+ 4 - 1
core/router/ctx.go

@@ -21,7 +21,10 @@ func UpdateGContext(r *ghttp.Request, GCtx *GContext) {
 
 // GetGContext 获取上下文GContext
 func GetGContext(ctx context.Context) (gCtx *GContext) {
-	gCtx = &GContext{}
+	gCtx = &GContext{
+		Sess:       &JySession{},
+		RouterRule: &Router{},
+	}
 	value := ctx.Value(GContextKey)
 	if value == nil {
 		return

+ 4 - 1
core/router/manager.go

@@ -85,7 +85,10 @@ func (m *Manager) GetRouterRule(url string) (*Router, error) {
 // InfusionContext 注入通用结构体gContext
 func (m *Manager) InfusionContext(r *ghttp.Request) (err error) {
 	var router *Router
-	var GCtx = &GContext{}
+	var GCtx = &GContext{
+		Sess:       &JySession{},
+		RouterRule: &Router{},
+	}
 	router, err = m.GetRouterRule(r.RequestURI)
 	if err != nil {
 		r.SetCtxVar(GContextKey, GCtx)

+ 0 - 14
core/router/session.go

@@ -2,7 +2,6 @@ package router
 
 import (
 	"app.yhyue.com/moapp/jybase/redis"
-	"context"
 	"encoding/json"
 	"fmt"
 	"github.com/gogf/gf/v2/frame/g"
@@ -25,19 +24,6 @@ type JySession struct {
 	Data    g.Map  // 当前Session管理对象
 }
 
-// GetJySession 获取剑鱼程序session内容
-func GetJySession(ctx context.Context) (jSession *JySession) {
-	jSession = &JySession{}
-	value := ctx.Value(JY_SESSIONNAME)
-	if value == nil {
-		return
-	}
-	if localCtx, ok := value.(*JySession); ok {
-		jSession = localCtx
-	}
-	return
-}
-
 // InitJySessionContext 获取用户session
 func InitJySessionContext(r *ghttp.Request) (jSession *JySession, err error) {
 	jSession = &JySession{}