Compare commits

..

No commits in common. "88d4039ff7a38d61044ed0407b34e569cc3a31fd" and "86516dc98e03b35bea4015bb3384fbf82650e87a" have entirely different histories.

8 changed files with 13 additions and 24 deletions

View File

@ -19,7 +19,7 @@ type bootstrap struct {
// =========================================================
type iris struct {
Session session `yaml:"session"`
Session session `yaml:"iris"`
}
type session struct {

View File

@ -3,7 +3,7 @@ iris:
session:
address: localhost:6379
db: 0
prefix: "iris:session:"
prefix: iris:session
expires: 10
database:
sqlite:
@ -11,4 +11,4 @@ database:
filePath: ./db_file.db
redis:
addr: localhost:6379
db: 0
db: 1

View File

@ -1,12 +1,13 @@
package adm_controller
import (
"Blog/internal/model"
"Blog/internal/model/result"
"Blog/internal/utils"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/sessions"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
)
@ -20,8 +21,10 @@ type AdminController struct {
func (ctrl *AdminController) Get() {
session := ctrl.Session
user := utils.SessionUtil.GetUser(session)
if user.Id != "" {
var userMap map[string]any
if err := session.Decode("user", &userMap); err == nil {
var user model.SysUser
mapstructure.Decode(userMap, &user)
logrus.Info("[", user.Username, "]进入后台管理")
}
ctrl.Ctx.View("/admin/index.html")

View File

@ -129,7 +129,7 @@ func (ctrl *LoginController) Post() {
session.Man.Destroy(ctrl.Ctx)
newSession := session.Man.Start(ctrl.Ctx)
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")
logrus.Info("用户:", user.Username, "登录,IP为:", xFrowardedFor)

View File

@ -66,7 +66,7 @@ func (ctrl *ArticleController) GetLatest() {
func (ctrl *ArticleController) ViewArticle() {
articleId := ctrl.Ctx.Params().Get("id")
article := service.ArticleService.GetArticle(articleId)
if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH {
if article.Id == "" {
ctrl.Ctx.View("404.html")
return
}

View File

@ -5,7 +5,6 @@ import (
"Blog/internal/controller/adm_controller"
"Blog/internal/controller/cli_controller"
"Blog/internal/middleware"
"Blog/internal/utils"
"net/http"
"time"
@ -55,17 +54,6 @@ func Router() {
m.Party("/diary").Handle(new(cli_controller.DiaryController))
m.Party("/file").Handle(new(cli_controller.FileController))
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("当前用户没有登录状态,无法退出登录")
})
})

View File

@ -28,7 +28,7 @@ func (repository *baseRep[T]) GetById(id string) (ret *T) {
logrus.Debug("执行的SQL:", sql)
err := repository.Table(repository.TableName).First(ret, "id = ?", id).Error
if err != nil {
logrus.Error(err)
logrus.Info(err)
return nil
}

View File

@ -49,10 +49,8 @@ func (*articleService) GetArticle(id string) *model.BlogArticle {
if err != nil {
logrus.Infoln(id, "文章的缓存不存在,读取数据库")
ret = repository.ArticleRepository.GetById(id)
if ret != nil {
client.RedisClient.Set(context.Background(), key, ret, time.Duration(0))
}
}
return ret
}