Compare commits
	
		
			No commits in common. "88d4039ff7a38d61044ed0407b34e569cc3a31fd" and "86516dc98e03b35bea4015bb3384fbf82650e87a" have entirely different histories.
		
	
	
		
			88d4039ff7
			...
			86516dc98e
		
	
		|  | @ -19,7 +19,7 @@ type bootstrap struct { | ||||||
| // =========================================================
 | // =========================================================
 | ||||||
| 
 | 
 | ||||||
| type iris struct { | type iris struct { | ||||||
| 	Session session `yaml:"session"` | 	Session session `yaml:"iris"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type session struct { | type session struct { | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ iris: | ||||||
|     session: |     session: | ||||||
|         address: localhost:6379 |         address: localhost:6379 | ||||||
|         db: 0 |         db: 0 | ||||||
|         prefix: "iris:session:" |         prefix: iris:session | ||||||
|         expires: 10 |         expires: 10 | ||||||
| database: | database: | ||||||
|     sqlite: |     sqlite: | ||||||
|  | @ -11,4 +11,4 @@ database: | ||||||
|         filePath: ./db_file.db |         filePath: ./db_file.db | ||||||
|     redis: |     redis: | ||||||
|         addr: localhost:6379 |         addr: localhost:6379 | ||||||
|         db: 0 |         db: 1 | ||||||
|  |  | ||||||
|  | @ -1,12 +1,13 @@ | ||||||
| package adm_controller | package adm_controller | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"Blog/internal/model" | ||||||
| 	"Blog/internal/model/result" | 	"Blog/internal/model/result" | ||||||
| 	"Blog/internal/utils" |  | ||||||
| 
 | 
 | ||||||
| 	"github.com/kataras/iris/v12" | 	"github.com/kataras/iris/v12" | ||||||
| 	"github.com/kataras/iris/v12/mvc" | 	"github.com/kataras/iris/v12/mvc" | ||||||
| 	"github.com/kataras/iris/v12/sessions" | 	"github.com/kataras/iris/v12/sessions" | ||||||
|  | 	"github.com/mitchellh/mapstructure" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -20,8 +21,10 @@ type AdminController struct { | ||||||
| 
 | 
 | ||||||
| func (ctrl *AdminController) Get() { | func (ctrl *AdminController) Get() { | ||||||
| 	session := ctrl.Session | 	session := ctrl.Session | ||||||
| 	user := utils.SessionUtil.GetUser(session) | 	var userMap map[string]any | ||||||
| 	if user.Id != "" { | 	if err := session.Decode("user", &userMap); err == nil { | ||||||
|  | 		var user model.SysUser | ||||||
|  | 		mapstructure.Decode(userMap, &user) | ||||||
| 		logrus.Info("[", user.Username, "]进入后台管理") | 		logrus.Info("[", user.Username, "]进入后台管理") | ||||||
| 	} | 	} | ||||||
| 	ctrl.Ctx.View("/admin/index.html") | 	ctrl.Ctx.View("/admin/index.html") | ||||||
|  |  | ||||||
|  | @ -129,7 +129,7 @@ func (ctrl *LoginController) Post() { | ||||||
| 	session.Man.Destroy(ctrl.Ctx) | 	session.Man.Destroy(ctrl.Ctx) | ||||||
| 	newSession := session.Man.Start(ctrl.Ctx) | 	newSession := session.Man.Start(ctrl.Ctx) | ||||||
| 	newSession.Set("user", user) | 	newSession.Set("user", user) | ||||||
| 	ctrl.Ctx.SetCookieKV("session_id_cookie", newSession.ID(), iris.CookieAllowSubdomains("www")) | 	ctrl.Ctx.SetCookieKV("session_id_cookie", newSession.ID()) | ||||||
| 
 | 
 | ||||||
| 	xFrowardedFor := ctrl.Ctx.GetHeader("X-Forwarded-For") | 	xFrowardedFor := ctrl.Ctx.GetHeader("X-Forwarded-For") | ||||||
| 	logrus.Info("用户:", user.Username, "登录,IP为:", xFrowardedFor) | 	logrus.Info("用户:", user.Username, "登录,IP为:", xFrowardedFor) | ||||||
|  |  | ||||||
|  | @ -66,7 +66,7 @@ func (ctrl *ArticleController) GetLatest() { | ||||||
| func (ctrl *ArticleController) ViewArticle() { | func (ctrl *ArticleController) ViewArticle() { | ||||||
| 	articleId := ctrl.Ctx.Params().Get("id") | 	articleId := ctrl.Ctx.Params().Get("id") | ||||||
| 	article := service.ArticleService.GetArticle(articleId) | 	article := service.ArticleService.GetArticle(articleId) | ||||||
| 	if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH { | 	if article.Id == "" { | ||||||
| 		ctrl.Ctx.View("404.html") | 		ctrl.Ctx.View("404.html") | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -5,7 +5,6 @@ import ( | ||||||
| 	"Blog/internal/controller/adm_controller" | 	"Blog/internal/controller/adm_controller" | ||||||
| 	"Blog/internal/controller/cli_controller" | 	"Blog/internal/controller/cli_controller" | ||||||
| 	"Blog/internal/middleware" | 	"Blog/internal/middleware" | ||||||
| 	"Blog/internal/utils" |  | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
|  | @ -55,17 +54,6 @@ func Router() { | ||||||
| 		m.Party("/diary").Handle(new(cli_controller.DiaryController)) | 		m.Party("/diary").Handle(new(cli_controller.DiaryController)) | ||||||
| 		m.Party("/file").Handle(new(cli_controller.FileController)) | 		m.Party("/file").Handle(new(cli_controller.FileController)) | ||||||
| 		m.Party("/admin/login").Handle(new(adm_controller.LoginController)) | 		m.Party("/admin/login").Handle(new(adm_controller.LoginController)) | ||||||
| 		m.Router.Get("/logout", func(ctx iris.Context) { |  | ||||||
| 			session := sessions.Get(ctx) |  | ||||||
| 			user := utils.SessionUtil.GetUser(session) |  | ||||||
| 			if user.Id != "" { |  | ||||||
| 				logrus.Info("[", user.Username, "]退出登录,清除Session") |  | ||||||
| 				session.Man.Destroy(ctx) |  | ||||||
| 				ctx.Redirect("") |  | ||||||
| 				return |  | ||||||
| 			} |  | ||||||
| 			logrus.Error("当前用户没有登录状态,无法退出登录") |  | ||||||
| 		}) |  | ||||||
| 
 | 
 | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -28,7 +28,7 @@ func (repository *baseRep[T]) GetById(id string) (ret *T) { | ||||||
| 	logrus.Debug("执行的SQL:", sql) | 	logrus.Debug("执行的SQL:", sql) | ||||||
| 	err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error | 	err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logrus.Error(err) | 		logrus.Info(err) | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -49,9 +49,7 @@ func (*articleService) GetArticle(id string) *model.BlogArticle { | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logrus.Infoln(id, "文章的缓存不存在,读取数据库") | 		logrus.Infoln(id, "文章的缓存不存在,读取数据库") | ||||||
| 		ret = repository.ArticleRepository.GetById(id) | 		ret = repository.ArticleRepository.GetById(id) | ||||||
| 		if ret != nil { | 		client.RedisClient.Set(context.Background(), key, ret, time.Duration(0)) | ||||||
| 			client.RedisClient.Set(context.Background(), key, ret, time.Duration(0)) |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 	return ret | 	return ret | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue