mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-08 10:13:44 +08:00
feat: 优化后台UI
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -36,6 +37,13 @@ func (h *ManagerHandler) Login(c *gin.Context) {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
return
|
||||
}
|
||||
|
||||
// add captcha
|
||||
if !base64Captcha.DefaultMemStore.Verify(data.CaptchaId, data.Captcha, true) {
|
||||
resp.ERROR(c, "验证码错误,请重新输入!")
|
||||
return
|
||||
}
|
||||
|
||||
manager := h.App.Config.Manager
|
||||
if data.Username == manager.Username && data.Password == manager.Password {
|
||||
// 创建 token
|
||||
|
||||
@@ -64,11 +64,10 @@ func (h *SysUserHandler) List(c *gin.Context) {
|
||||
// Save 更新或者新增
|
||||
func (h *SysUserHandler) Save(c *gin.Context) {
|
||||
var data struct {
|
||||
Id uint `json:"id"`
|
||||
Password string `json:"password"`
|
||||
Username string `json:"username"`
|
||||
ExpiredTime string `json:"expired_time"`
|
||||
Status bool `json:"status"`
|
||||
Id uint `json:"id"`
|
||||
Password string `json:"password"`
|
||||
Username string `json:"username"`
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
resp.ERROR(c, types.InvalidArgs)
|
||||
@@ -81,18 +80,16 @@ func (h *SysUserHandler) Save(c *gin.Context) {
|
||||
user.Id = data.Id
|
||||
// 此处需要用 map 更新,用结构体无法更新 0 值
|
||||
res = h.db.Model(&user).Updates(map[string]interface{}{
|
||||
"username": data.Username,
|
||||
"status": data.Status,
|
||||
"expired_time": utils.Str2stamp(data.ExpiredTime),
|
||||
"username": data.Username,
|
||||
"status": data.Status,
|
||||
})
|
||||
} else {
|
||||
salt := utils.RandString(8)
|
||||
u := model.AdminUser{
|
||||
Username: data.Username,
|
||||
Password: utils.GenPassword(data.Password, salt),
|
||||
Salt: salt,
|
||||
Status: true,
|
||||
ExpiredTime: utils.Str2stamp(data.ExpiredTime),
|
||||
Username: data.Username,
|
||||
Password: utils.GenPassword(data.Password, salt),
|
||||
Salt: salt,
|
||||
Status: true,
|
||||
}
|
||||
res = h.db.Create(&u)
|
||||
_ = utils.CopyObject(u, &userVo)
|
||||
|
||||
41
api/handler/admin/captcha_handler.go
Normal file
41
api/handler/admin/captcha_handler.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"chatplus/core"
|
||||
"chatplus/handler"
|
||||
"chatplus/utils/resp"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
type CaptchaHandler struct {
|
||||
handler.BaseHandler
|
||||
}
|
||||
|
||||
func NewCaptchaHandler(app *core.AppServer) *CaptchaHandler {
|
||||
h := CaptchaHandler{}
|
||||
h.App = app
|
||||
return &h
|
||||
}
|
||||
|
||||
type CaptchaVo struct {
|
||||
CaptchaId string `json:"captcha_id"`
|
||||
PicPath string `json:"pic_path"`
|
||||
}
|
||||
|
||||
// GetCaptcha 获取验证码
|
||||
func (h *CaptchaHandler) GetCaptcha(c *gin.Context) {
|
||||
var captchaVo CaptchaVo
|
||||
driver := base64Captcha.NewDriverDigit(48, 130, 4, 0.4, 10)
|
||||
cp := base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore)
|
||||
// b64s是图片的base64编码
|
||||
id, b64s, err := cp.Generate()
|
||||
if err != nil {
|
||||
resp.ERROR(c, "生成验证码错误!")
|
||||
return
|
||||
}
|
||||
captchaVo.CaptchaId = id
|
||||
captchaVo.PicPath = b64s
|
||||
|
||||
resp.SUCCESS(c, captchaVo)
|
||||
}
|
||||
97
api/handler/admin/upload_handler.go
Normal file
97
api/handler/admin/upload_handler.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"chatplus/core"
|
||||
"chatplus/handler"
|
||||
"chatplus/service/oss"
|
||||
"chatplus/store/model"
|
||||
"chatplus/store/vo"
|
||||
"chatplus/utils"
|
||||
"chatplus/utils/resp"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UploadHandler struct {
|
||||
handler.BaseHandler
|
||||
db *gorm.DB
|
||||
uploaderManager *oss.UploaderManager
|
||||
}
|
||||
|
||||
func NewUploadHandler(app *core.AppServer, db *gorm.DB, manager *oss.UploaderManager) *UploadHandler {
|
||||
adminHandler := &UploadHandler{db: db, uploaderManager: manager}
|
||||
adminHandler.App = app
|
||||
return adminHandler
|
||||
}
|
||||
|
||||
func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
file, err := h.uploaderManager.GetUploadHandler().PutFile(c, "file")
|
||||
if err != nil {
|
||||
resp.ERROR(c, err.Error())
|
||||
return
|
||||
}
|
||||
userId := 0
|
||||
res := h.db.Create(&model.File{
|
||||
UserId: userId,
|
||||
Name: file.Name,
|
||||
ObjKey: file.ObjKey,
|
||||
URL: file.URL,
|
||||
Ext: file.Ext,
|
||||
Size: file.Size,
|
||||
CreatedAt: time.Time{},
|
||||
})
|
||||
if res.Error != nil || res.RowsAffected == 0 {
|
||||
resp.ERROR(c, "error with update database: "+res.Error.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp.SUCCESS(c, file)
|
||||
}
|
||||
|
||||
func (h *UploadHandler) List(c *gin.Context) {
|
||||
userId := 0
|
||||
var items []model.File
|
||||
var files = make([]vo.File, 0)
|
||||
h.db.Where("user_id = ?", userId).Find(&items)
|
||||
if len(items) > 0 {
|
||||
for _, v := range items {
|
||||
var file vo.File
|
||||
err := utils.CopyObject(v, &file)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
continue
|
||||
}
|
||||
file.CreatedAt = v.CreatedAt.Unix()
|
||||
files = append(files, file)
|
||||
}
|
||||
}
|
||||
|
||||
resp.SUCCESS(c, files)
|
||||
}
|
||||
|
||||
// Remove remove files
|
||||
func (h *UploadHandler) Remove(c *gin.Context) {
|
||||
userId := 0
|
||||
id := h.GetInt(c, "id", 0)
|
||||
var file model.File
|
||||
tx := h.db.Where("user_id = ? AND id = ?", userId, id).First(&file)
|
||||
if tx.Error != nil || file.Id == 0 {
|
||||
resp.ERROR(c, "file not existed")
|
||||
return
|
||||
}
|
||||
|
||||
// remove database
|
||||
tx = h.db.Model(&model.File{}).Delete("id = ?", id)
|
||||
if tx.Error != nil || tx.RowsAffected == 0 {
|
||||
resp.ERROR(c, "failed to update database")
|
||||
return
|
||||
}
|
||||
// remove files
|
||||
objectKey := file.ObjKey
|
||||
if objectKey == "" {
|
||||
objectKey = file.URL
|
||||
}
|
||||
_ = h.uploaderManager.GetUploadHandler().Delete(objectKey)
|
||||
resp.SUCCESS(c)
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
|
||||
userId := h.GetLoginUserId(c)
|
||||
res := h.db.Create(&model.File{
|
||||
UserId: userId,
|
||||
UserId: int(userId),
|
||||
Name: file.Name,
|
||||
ObjKey: file.ObjKey,
|
||||
URL: file.URL,
|
||||
|
||||
Reference in New Issue
Block a user