未发布和不存在的文章跳转至404页面

This commit is contained in:
sysnix 2023-11-23 05:38:11 +08:00
parent 4aabb57ff9
commit c64f52ed19
3 changed files with 5 additions and 3 deletions

View File

@ -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.Id == "" { if article == nil || article.State != consts.ARTICLE_STATE_PUBLISH {
ctrl.Ctx.View("404.html") ctrl.Ctx.View("404.html")
return return
} }

View File

@ -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.Info(err) logrus.Error(err)
return nil return nil
} }

View File

@ -49,8 +49,10 @@ 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
} }