模板还不行,需要改变。
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_blog/models"
|
||||
"go_blog/pkg/util"
|
||||
"go_blog/serializers"
|
||||
@@ -30,7 +29,6 @@ func UsersLoginHandler(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证用户逻辑不变
|
||||
user := &models.Account{Username: loginUser.Username}
|
||||
if err := models.DB.Where("username = ?", user.Username).First(user).Error; err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
@@ -90,50 +88,6 @@ func UsersRegisterHandler(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "注册成功"})
|
||||
}
|
||||
|
||||
// 修改用户信息
|
||||
func UsersSetInfoHandler(ctx *gin.Context) {
|
||||
|
||||
jsonData, err := util.GetBodyData(ctx)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"msg": "参数解析失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
fmt.Println(jsonData)
|
||||
if jsonData == nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"msg": "获取不到参数",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 从上下文中获取用户(假设 JWT 中间件已将用户存入 "user" 键)
|
||||
user, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||
"code": http.StatusUnauthorized,
|
||||
"msg": "未登录",
|
||||
})
|
||||
return
|
||||
}
|
||||
currentUser, ok := user.(*models.Account) // 明确类型为 models.Account
|
||||
if !ok {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"code": http.StatusInternalServerError,
|
||||
"msg": "用户类型错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
models.DB.Model(currentUser).Updates(jsonData)
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"msg": "更新成功",
|
||||
})
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
func UsersSetPwdHandler(ctx *gin.Context) {
|
||||
|
||||
|
||||
9
main.go
9
main.go
@@ -29,11 +29,6 @@ func main() {
|
||||
}
|
||||
slog.Info("配置加载成功", "config", conf)
|
||||
// 2. 初始化日志系统
|
||||
// if err := logger.Initialize(cfg.Log); err != nil {
|
||||
// slog.Error("日志初始化失败", "error", err)
|
||||
// os.Exit(1)
|
||||
// }
|
||||
// defer logger.Flush()
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
slog.SetDefault(logger)
|
||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||
@@ -74,7 +69,7 @@ func main() {
|
||||
gin.Logger(),
|
||||
gin.Recovery(),
|
||||
databaseMiddleware(models.DB),
|
||||
//configMiddleware(cfg),
|
||||
configMiddleware(conf),
|
||||
themeMiddleware(themeManager),
|
||||
sessions.Sessions("blog-session", store),
|
||||
)
|
||||
@@ -113,9 +108,7 @@ func themeMiddleware(manager *themes.ThemeManager) gin.HandlerFunc {
|
||||
func loggerMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
duration := time.Since(start)
|
||||
slog.Info("请求处理完成",
|
||||
"status", c.Writer.Status(),
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
@Time : 2020/6/29 14:40
|
||||
@Author : xuyiqing
|
||||
@File : body.py
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 反序列化request.body中的json数据为map
|
||||
func GetBodyData(ctx *gin.Context) (map[string]interface{}, error) {
|
||||
bdata := make([]byte, 1024)
|
||||
length, err := ctx.Request.Body.Read(bdata)
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
var data map[string]interface{}
|
||||
str := string(bdata[:length])
|
||||
decoder := json.NewDecoder(strings.NewReader(str))
|
||||
decoder.UseNumber()
|
||||
err1 := decoder.Decode(&data)
|
||||
if err1 != nil {
|
||||
return nil, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// 构建文件url连接主机端口全链接 "https://192.168.11.121:7889/meida/upload/..."
|
||||
func BuildAbsoluteUri(ctx *gin.Context, filePath string) string {
|
||||
host := ctx.Request.Host
|
||||
schema := ctx.Request.Header.Get("X-Forwarded-Proto")
|
||||
if schema == "https" {
|
||||
return fmt.Sprintf("https://%s/%s", host, filePath)
|
||||
} else {
|
||||
return fmt.Sprintf("http://%s/%s", host, filePath)
|
||||
}
|
||||
}
|
||||
@@ -178,7 +178,6 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
authAdmin.GET("/index", controllers.ShowAdminIndexPage)
|
||||
authAdmin.GET("/themes", controllers.ListThemes)
|
||||
authAdmin.POST("/themes/switch", controllers.SwitchTheme)
|
||||
authAdmin.POST("/setinfo", controllers.UsersSetInfoHandler)
|
||||
authAdmin.POST("/setpwd", controllers.UsersSetPwdHandler)
|
||||
admin.GET("/logout", controllers.UsersLogoutHandler) // 添加退出路由
|
||||
}
|
||||
|
||||
137
web/themes/default/templates/base.tmpl
Normal file
137
web/themes/default/templates/base.tmpl
Normal file
@@ -0,0 +1,137 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ .Title}}</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style type="text/css">
|
||||
body{
|
||||
background: #f8f8f8;
|
||||
}
|
||||
.layui-main {
|
||||
width: 1140px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.container {
|
||||
width: 1170px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.header .layui-nav {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
padding: 0;
|
||||
background: none;
|
||||
}
|
||||
.post-list {
|
||||
width: 75%;
|
||||
float: left;
|
||||
display: block;
|
||||
}
|
||||
.list-card {
|
||||
background: #fff;
|
||||
overflow: hidden;
|
||||
padding: 20px 20px 10px 20px;
|
||||
position: relative;
|
||||
border-radius: 10px;
|
||||
margin-bottom: 10px;
|
||||
height: 200px;
|
||||
}
|
||||
.sidebar {
|
||||
float: left;
|
||||
width: 25%;
|
||||
}
|
||||
|
||||
.header {}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
{{ template "header" . }}
|
||||
|
||||
<main>{{ block "content" . }}{{ end }}</main>
|
||||
|
||||
<div class="page-navigator">
|
||||
共 {{ .Total }} 条,每页 {{ .PageSize }} 条,当前第 {{ .Page }} 页</div>
|
||||
<div class="pagination">
|
||||
{{ if .PrevPage }}
|
||||
<a href="/page/{{ .PrevPage }}">上一页</a>
|
||||
{{ end }}
|
||||
{{ if .NextPage }}
|
||||
<a href="/page/{{ .NextPage }}">下一页</a>
|
||||
{{ end }}
|
||||
<a href="/">首页</a>
|
||||
<a href="/page/{{ .Total }}">尾页</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sidebar">
|
||||
<div class="column">
|
||||
<h3 class="title-sidebar"><i class="layui-icon"></i> 博客信息</h3>
|
||||
<div class="personal-information">
|
||||
<div class="user">
|
||||
<img src="https://www.hanxiaonuan.cn/usr/uploads/2021/07/3991382612.jpg" alt="韩小暖的博客的头像"
|
||||
class="rounded-circle avatar">
|
||||
<div class="p-2">
|
||||
<a class="user-name" target="_blank" href="https://www.hanxiaonuan.cn/">
|
||||
韩小暖的博客</a>
|
||||
<p class="introduction mt-1">这里是小暖的日常记录,欢迎!</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="component">
|
||||
<form class="layui-form" id="search" method="post" action="https://www.hanxiaonuan.cn/"
|
||||
role="search">
|
||||
<div class="layui-inline input">
|
||||
<input type="text" id="s" name="s" class="layui-input" required="" lay-verify="required"
|
||||
placeholder="输入关键字搜索">
|
||||
</div>
|
||||
<div class="layui-inline">
|
||||
<button class="layui-btn layui-btn-sm layui-btn-primary"><i
|
||||
class="layui-icon"></i></button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="column">
|
||||
<h3 class="title-sidebar"><i class="layui-icon"></i> 栏目分类</h3>
|
||||
<ul class="layui-row layui-col-space5">
|
||||
<li class="layui-col-md12 layui-col-xs6"><a
|
||||
href="https://www.hanxiaonuan.cn/category/default/"><i class="layui-icon"></i>
|
||||
默认分类<span class="layui-badge layui-bg-gray">12</span></a></li>
|
||||
<li class="layui-col-md12 layui-col-xs6"><a href="https://www.hanxiaonuan.cn/category/zc/"><i
|
||||
class="layui-icon"></i> 日常随想<span class="layui-badge layui-bg-gray">2</span></a>
|
||||
</li>
|
||||
<li class="layui-col-md12 layui-col-xs6"><a
|
||||
href="https://www.hanxiaonuan.cn/category/%E5%85%B3%E4%BA%8E%E6%88%BF%E5%AD%90/"><i
|
||||
class="layui-icon"></i> 关于房子<span class="layui-badge layui-bg-gray">1</span></a>
|
||||
</li>
|
||||
<li class="layui-col-md12 layui-col-xs6"><a
|
||||
href="https://www.hanxiaonuan.cn/category/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/"><i
|
||||
class="layui-icon"></i> 工作日志<span class="layui-badge layui-bg-gray">0</span></a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="tags">
|
||||
<h3 class="title-sidebar"><i class="layui-icon"></i>标签云</h3>
|
||||
<div>
|
||||
<a class="layui-btn layui-btn-xs layui-btn-primary" style="color: rgb(231, 229, 26)"
|
||||
href="https://www.hanxiaonuan.cn/tag/%E5%B0%8F%E6%9A%96/" title="小暖">小暖</a>
|
||||
<a class="layui-btn layui-btn-xs layui-btn-primary" style="color: rgb(125, 196, 207)"
|
||||
href="https://www.hanxiaonuan.cn/tag/%E6%84%9F%E6%82%9F/" title="感悟">感悟</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="tags">
|
||||
<h3 class="title-sidebar"><i class="layui-icon"></i>系统</h3>
|
||||
<div>
|
||||
<li class="last"><a href="https://www.hanxiaonuan.cn/admin/">进入后台 (admin)</a></li>
|
||||
<li><a href="https://www.hanxiaonuan.cn/action/logout?_=5b09b0e1048bbd45bdddc56fc43667b2">退出</a>
|
||||
</li>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,54 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ .Title}}</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style type="text/css">
|
||||
body{
|
||||
background: #f8f8f8;
|
||||
}
|
||||
.layui-main {
|
||||
width: 1140px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.container {
|
||||
width: 1170px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.header .layui-nav {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
padding: 0;
|
||||
background: none;
|
||||
}
|
||||
.post-list {
|
||||
width: 75%;
|
||||
float: left;
|
||||
display: block;
|
||||
}
|
||||
.list-card {
|
||||
background: #fff;
|
||||
overflow: hidden;
|
||||
padding: 20px 20px 10px 20px;
|
||||
position: relative;
|
||||
border-radius: 10px;
|
||||
margin-bottom: 10px;
|
||||
height: 200px;
|
||||
}
|
||||
.sidebar {
|
||||
float: left;
|
||||
width: 25%;
|
||||
}
|
||||
|
||||
.header {}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
{{ template "header" . }}
|
||||
<!-- templates/about.html -->
|
||||
{{ define "content" }}
|
||||
<div class="container">
|
||||
|
||||
<div class="post-list">
|
||||
@@ -144,6 +95,8 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
{{ end }}
|
||||
|
||||
<!--继承基础模板内容-->
|
||||
{{ template "base.html" . }}
|
||||
Reference in New Issue
Block a user