feat: vue-mobile => 完成用户信息修改功能,前后端都添加文件上传功能。

This commit is contained in:
RockYang
2023-06-27 12:11:55 +08:00
parent c3d62bb8d8
commit 3efd5fb77a
20 changed files with 353 additions and 73 deletions

View File

@@ -17,6 +17,7 @@ import (
)
type AppServer struct {
Debug bool
AppConfig *types.AppConfig
Engine *gin.Engine
ChatContexts *types.LMap[string, []types.Message] // 聊天上下文 Map [chatId] => []Message
@@ -33,6 +34,7 @@ func NewServer(appConfig *types.AppConfig) *AppServer {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard
return &AppServer{
Debug: false,
AppConfig: appConfig,
Engine: gin.Default(),
ChatContexts: types.NewLMap[string, []types.Message](),
@@ -44,9 +46,11 @@ func NewServer(appConfig *types.AppConfig) *AppServer {
func (s *AppServer) Init(debug bool) {
if debug { // 调试模式允许跨域请求 API
s.Debug = debug
logger.Info("Enabled debug mode")
s.Engine.Use(corsMiddleware())
}
s.Engine.Use(sessionMiddleware(s.AppConfig))
s.Engine.Use(authorizeMiddleware(s))
s.Engine.Use(errorHandler)

View File

@@ -19,6 +19,7 @@ func NewDefaultConfig() *types.AppConfig {
ProxyURL: "",
Manager: types.Manager{Username: "admin", Password: "admin123"},
StaticDir: "./static",
StaticUrl: "http://localhost/5678/static",
Session: types.Session{
SecretKey: utils.RandString(64),

View File

@@ -12,6 +12,7 @@ type AppConfig struct {
MysqlDns string // mysql 连接地址
Manager Manager // 后台管理员账户信息
StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL
}
// Manager 管理员

View File

@@ -182,7 +182,10 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
}
}
}
logger.Info("聊天上下文:", chatCtx)
if h.App.Debug { // 调试打印聊天上下文
logger.Info("聊天上下文:", chatCtx)
}
}
req.Messages = append(chatCtx, types.Message{
Role: "user",

View File

@@ -0,0 +1,67 @@
package handler
import (
"chatplus/core"
"chatplus/utils/resp"
"fmt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"os"
"path/filepath"
"time"
)
type UploadHandler struct {
BaseHandler
db *gorm.DB
}
func NewUploadHandler(app *core.AppServer, db *gorm.DB) *UploadHandler {
handler := &UploadHandler{db: db}
handler.App = app
return handler
}
func (h *UploadHandler) Upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
resp.ERROR(c, fmt.Sprintf("文件上传失败: %s", err.Error()))
return
}
filePath, err := h.genFilePath(file.Filename)
if err != nil {
resp.ERROR(c, fmt.Sprintf("文件上传失败: %s", err.Error()))
return
}
// 将文件保存到指定路径
err = c.SaveUploadedFile(file, filePath)
if err != nil {
resp.ERROR(c, fmt.Sprintf("文件保存失败: %s", err.Error()))
return
}
resp.SUCCESS(c, h.genFileUrl(filePath))
}
// 生成上传文件路径
func (h *UploadHandler) genFilePath(filename string) (string, error) {
now := time.Now()
dir := fmt.Sprintf("%s/upload/%d/%d", h.App.AppConfig.StaticDir, now.Year(), now.Month())
_, err := os.Stat(dir)
if err != nil {
err = os.MkdirAll(dir, 0755)
if err != nil {
return "", fmt.Errorf("创建上传目录失败:%s", err)
}
}
fileExt := filepath.Ext(filename)
return fmt.Sprintf("%s/%d%s", dir, now.UnixMilli(), fileExt), nil
}
// 生成上传文件 URL
func (h *UploadHandler) genFileUrl(filePath string) string {
now := time.Now()
filename := filepath.Base(filePath)
return fmt.Sprintf("%s/upload/%d/%d/%s", h.App.AppConfig.StaticUrl, now.Year(), now.Month(), filename)
}

View File

@@ -233,8 +233,38 @@ func (h *UserHandler) Session(c *gin.Context) {
}
type userProfile struct {
Id uint `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
ChatConfig types.ChatConfig `json:"chat_config"`
Calls int `json:"calls"`
Tokens int `json:"tokens"`
}
func (h *UserHandler) Profile(c *gin.Context) {
user, err := utils.GetLoginUser(c, h.db)
if err != nil {
resp.NotAuth(c)
return
}
h.db.First(&user, user.Id)
var profile userProfile
err = utils.CopyObject(user, &profile)
if err != nil {
logger.Error("对象拷贝失败:", err.Error())
resp.ERROR(c, "获取用户信息失败")
return
}
profile.Id = user.Id
resp.SUCCESS(c, profile)
}
func (h *UserHandler) ProfileUpdate(c *gin.Context) {
var data vo.User
var data userProfile
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
@@ -272,26 +302,6 @@ func (h *UserHandler) ProfileUpdate(c *gin.Context) {
resp.SUCCESS(c)
}
func (h *UserHandler) Profile(c *gin.Context) {
user, err := utils.GetLoginUser(c, h.db)
if err != nil {
resp.NotAuth(c)
return
}
h.db.First(&user, user.Id)
var userVo vo.User
err = utils.CopyObject(user, &userVo)
if err != nil {
logger.Error("对象拷贝失败:", err.Error())
resp.ERROR(c, "获取用户信息失败")
return
}
userVo.Id = user.Id
resp.SUCCESS(c, userVo)
}
// Password 更新密码
func (h *UserHandler) Password(c *gin.Context) {
var data struct {

View File

@@ -93,8 +93,9 @@ func main() {
fx.Provide(handler.NewChatRoleHandler),
fx.Provide(handler.NewUserHandler),
fx.Provide(handler.NewChatHandler),
fx.Provide(admin.NewConfigHandler),
fx.Provide(handler.NewUploadHandler),
fx.Provide(admin.NewConfigHandler),
fx.Provide(admin.NewAdminHandler),
fx.Provide(admin.NewApiKeyHandler),
fx.Provide(admin.NewUserHandler),
@@ -126,8 +127,11 @@ func main() {
group.GET("tokens", h.Tokens)
group.GET("stop", h.StopGenerate)
}),
fx.Invoke(func(s *core.AppServer, h *handler.UploadHandler) {
s.Engine.POST("/api/upload", h.Upload)
}),
//
// 管理后台控制器
fx.Invoke(func(s *core.AppServer, h *admin.ConfigHandler) {
group := s.Engine.Group("/api/admin/config/")
group.POST("update", h.Update)

View File

@@ -4,16 +4,16 @@ import "chatplus/core/types"
type User struct {
BaseVo
Username string `json:"username,omitempty"`
Nickname string `json:"nickname,omitempty"`
Avatar string `json:"avatar,omitempty"`
Salt string `json:"salt,omitempty"` // 密码盐
Tokens int64 `json:"tokens,omitempty"` // 剩余tokens
Calls int `json:"calls,omitempty"` // 剩余对话次数
ChatConfig types.ChatConfig `json:"chat_config,omitempty"` // 聊天配置
ChatRoles []string `json:"chat_roles,omitempty"` // 聊天角色集合
ExpiredTime int64 `json:"expired_time,omitempty"` // 账户到期时间
Status bool `json:"status,omitempty"` // 当前状态
LastLoginAt int64 `json:"last_login_at,omitempty"` // 最后登录时间
LastLoginIp string `json:"last_login_ip,omitempty"` // 最后登录 IP
Username string `json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Salt string `json:"salt"` // 密码盐
Tokens int64 `json:"tokens"` // 剩余tokens
Calls int `json:"calls"` // 剩余对话次数
ChatConfig types.ChatConfig `json:"chat_config"` // 聊天配置
ChatRoles []string `json:"chat_roles"` // 聊天角色集合
ExpiredTime int64 `json:"expired_time"` // 账户到期时间
Status bool `json:"status"` // 当前状态
LastLoginAt int64 `json:"last_login_at"` // 最后登录时间
LastLoginIp string `json:"last_login_ip"` // 最后登录 IP
}