mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2026-04-24 10:01:37 +08:00
又调整了结构
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package commonController
|
||||
|
||||
import (
|
||||
"x_admin/app/schema/commonSchema"
|
||||
"x_admin/app/service/commonService"
|
||||
"x_admin/config"
|
||||
"x_admin/core/request"
|
||||
"x_admin/core/response"
|
||||
"x_admin/middleware"
|
||||
"x_admin/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func AlbumRoute(rg *gin.RouterGroup) {
|
||||
|
||||
handle := albumHandler{}
|
||||
|
||||
rg = rg.Group("/common", middleware.TokenAuth())
|
||||
|
||||
rg.GET("/album/albumList", handle.albumList)
|
||||
rg.POST("/album/albumRename", middleware.RecordLog("相册文件重命名"), handle.albumRename)
|
||||
rg.POST("/album/albumMove", middleware.RecordLog("相册文件移动"), handle.albumMove)
|
||||
rg.POST("/album/albumDel", middleware.RecordLog("相册文件删除"), handle.albumDel)
|
||||
rg.GET("/album/cateList", handle.cateList)
|
||||
rg.POST("/album/cateAdd", middleware.RecordLog("相册分类新增"), handle.cateAdd)
|
||||
rg.POST("/album/cateRename", middleware.RecordLog("相册分类重命名"), handle.cateRename)
|
||||
rg.POST("/album/cateDel", middleware.RecordLog("相册分类删除"), handle.cateDel)
|
||||
}
|
||||
|
||||
type albumHandler struct{}
|
||||
|
||||
// albumList 相册文件列表
|
||||
func (ah albumHandler) albumList(c *gin.Context) {
|
||||
var page request.PageReq
|
||||
var listReq commonSchema.CommonAlbumListReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
|
||||
return
|
||||
}
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
|
||||
return
|
||||
}
|
||||
var adminId = config.AdminConfig.GetAdminId(c)
|
||||
res, err := commonService.AlbumService.AlbumList(adminId, page, listReq)
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
|
||||
// albumRename 相册文件重命名
|
||||
func (ah albumHandler) albumRename(c *gin.Context) {
|
||||
var rnReq commonSchema.CommonAlbumRenameReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &rnReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, commonService.AlbumService.AlbumRename(rnReq.ID, rnReq.Name))
|
||||
}
|
||||
|
||||
// albumMove 相册文件移动
|
||||
func (ah albumHandler) albumMove(c *gin.Context) {
|
||||
var mvReq commonSchema.CommonAlbumMoveReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &mvReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, commonService.AlbumService.AlbumMove(mvReq.Ids, mvReq.Cid))
|
||||
}
|
||||
|
||||
// albumDel 相册文件删除
|
||||
func (ah albumHandler) albumDel(c *gin.Context) {
|
||||
var delReq commonSchema.CommonAlbumDelReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &delReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, commonService.AlbumService.AlbumDel(delReq.Ids))
|
||||
}
|
||||
|
||||
// cateList 类目列表
|
||||
func (ah albumHandler) cateList(c *gin.Context) {
|
||||
var listReq commonSchema.CommonCateListReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
|
||||
return
|
||||
}
|
||||
var adminId = config.AdminConfig.GetAdminId(c)
|
||||
res, err := commonService.AlbumService.CateList(adminId, listReq)
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
|
||||
// cateAdd 类目新增
|
||||
func (ah albumHandler) cateAdd(c *gin.Context) {
|
||||
var addReq commonSchema.CommonCateAddReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &addReq)) {
|
||||
return
|
||||
}
|
||||
var adminId = config.AdminConfig.GetAdminId(c)
|
||||
response.CheckAndResp(c, commonService.AlbumService.CateAdd(adminId, addReq))
|
||||
}
|
||||
|
||||
// cateRename 类目命名
|
||||
func (ah albumHandler) cateRename(c *gin.Context) {
|
||||
var rnReq commonSchema.CommonCateRenameReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &rnReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, commonService.AlbumService.CateRename(rnReq.ID, rnReq.Name))
|
||||
}
|
||||
|
||||
// cateDel 类目删除
|
||||
func (ah albumHandler) cateDel(c *gin.Context) {
|
||||
var delReq commonSchema.CommonCateDelReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &delReq)) {
|
||||
return
|
||||
}
|
||||
response.CheckAndResp(c, commonService.AlbumService.CateDel(delReq.ID))
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package commonController
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"x_admin/app/schema/commonSchema"
|
||||
"x_admin/app/service/commonService"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CaptchaRoute(rg *gin.RouterGroup) {
|
||||
|
||||
rg = rg.Group("/common/captcha")
|
||||
rg.POST("/get", func(c *gin.Context) {
|
||||
var captchaGet commonSchema.CaptchaGetParams
|
||||
if err := c.ShouldBind(&captchaGet); err != nil {
|
||||
// 返回错误信息
|
||||
c.JSON(200, errorRes(err))
|
||||
return
|
||||
}
|
||||
if captchaGet.CaptchaType == "" {
|
||||
c.JSON(200, errorRes(errors.New("验证码类型不能为空")))
|
||||
return
|
||||
}
|
||||
// 根据参数类型获取不同服务即可
|
||||
data, err := commonService.CaptchaGet(captchaGet.CaptchaType)
|
||||
if err != nil {
|
||||
c.JSON(200, errorRes(err))
|
||||
return
|
||||
}
|
||||
//输出json结果给调用方
|
||||
c.JSON(200, successRes(data))
|
||||
})
|
||||
rg.POST("/check", func(c *gin.Context) {
|
||||
var params commonSchema.ClientParams
|
||||
if err := c.ShouldBind(¶ms); err != nil {
|
||||
// 返回错误信息
|
||||
c.JSON(200, errorRes(err))
|
||||
return
|
||||
}
|
||||
err := commonService.CaptchaCheck(params)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(200, errorRes(err))
|
||||
return
|
||||
}
|
||||
//输出json结果给调用方
|
||||
c.JSON(200, successRes(nil))
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func successRes(data interface{}) map[string]interface{} {
|
||||
ret := make(map[string]interface{})
|
||||
ret["error"] = false
|
||||
ret["repCode"] = "0000"
|
||||
ret["repData"] = data
|
||||
ret["repMsg"] = nil
|
||||
ret["successRes"] = true
|
||||
|
||||
return ret
|
||||
}
|
||||
func errorRes(err error) map[string]interface{} {
|
||||
ret := make(map[string]interface{})
|
||||
ret["error"] = true
|
||||
ret["repCode"] = "0001"
|
||||
ret["repData"] = nil
|
||||
ret["repMsg"] = err.Error()
|
||||
ret["successRes"] = false
|
||||
return ret
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package commonController
|
||||
|
||||
import (
|
||||
"x_admin/app/service/commonService"
|
||||
"x_admin/core/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GeTuiRoute(rg *gin.RouterGroup) {
|
||||
handle := geTuiHandler{}
|
||||
|
||||
rg = rg.Group("/common")
|
||||
rg.GET("/push", handle.push)
|
||||
|
||||
}
|
||||
|
||||
type geTuiHandler struct{}
|
||||
|
||||
// push 推送
|
||||
func (ih geTuiHandler) push(c *gin.Context) {
|
||||
var req []commonService.PushMessage
|
||||
// if err := c.ShouldBindJSON(&req); err != nil {
|
||||
// response.CheckAndResp(c, err)
|
||||
// return
|
||||
// }
|
||||
// "cid":"ca416f34681c49d2ee14a192c28de537", #华为
|
||||
// "cid": "43e9228f90aec7b37be97d4c5250829f",# 荣耀
|
||||
// "cid": "525420466c409f555b5a461d7c981a36",# 小米
|
||||
req = append(req, commonService.PushMessage{
|
||||
CID: "ca416f34681c49d2ee14a192c28de537",
|
||||
RequestID: "",
|
||||
NotifyID: 0,
|
||||
Title: "气体报警",
|
||||
Body: "气体报警",
|
||||
Payload: nil,
|
||||
})
|
||||
|
||||
res, err := commonService.GeTuiService.PushToSingleBatchCID(req)
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package commonController
|
||||
|
||||
import (
|
||||
"x_admin/app/service/commonService"
|
||||
"x_admin/core/response"
|
||||
"x_admin/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func IndexRoute(rg *gin.RouterGroup) {
|
||||
handle := indexHandler{}
|
||||
|
||||
rg = rg.Group("/common", middleware.TokenAuth())
|
||||
rg.GET("/index/console", handle.console)
|
||||
rg.GET("/index/config", handle.config)
|
||||
|
||||
}
|
||||
|
||||
type indexHandler struct{}
|
||||
|
||||
// console 控制台
|
||||
func (ih indexHandler) console(c *gin.Context) {
|
||||
res, err := commonService.IndexService.Console()
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
|
||||
// config 公共配置
|
||||
func (ih indexHandler) config(c *gin.Context) {
|
||||
res, err := commonService.IndexService.Config()
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package commonController
|
||||
|
||||
import (
|
||||
"x_admin/app/schema/commonSchema"
|
||||
"x_admin/app/service/commonService"
|
||||
"x_admin/config"
|
||||
"x_admin/core/response"
|
||||
"x_admin/middleware"
|
||||
"x_admin/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func UploadRoute(rg *gin.RouterGroup) {
|
||||
handle := uploadHandler{}
|
||||
|
||||
rg = rg.Group("/common", middleware.TokenAuth())
|
||||
rg.POST("/upload/preUploadFile", middleware.RecordLog("文件预上传", middleware.RequestFile), handle.preUploadFile)
|
||||
rg.POST("/upload/file", middleware.RecordLog("上传文件", middleware.RequestFile), handle.uploadFile)
|
||||
}
|
||||
|
||||
type uploadHandler struct{}
|
||||
|
||||
// 文件预上传
|
||||
func (uh uploadHandler) preUploadFile(c *gin.Context) {
|
||||
// md5,fileName,fileSize,cid
|
||||
// 检查MD5是否已存在
|
||||
// 检查名称是否合规安全
|
||||
// 检查文件大小是否超过限制
|
||||
// 检查文件类型是否符合要求
|
||||
|
||||
// 如果文件存在,复制到cid对应目录
|
||||
|
||||
}
|
||||
|
||||
// uploadFile 上传文件
|
||||
func (uh uploadHandler) uploadFile(c *gin.Context) {
|
||||
var uReq commonSchema.CommonUploadImageReq
|
||||
if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &uReq)) {
|
||||
return
|
||||
}
|
||||
file, ve := util.VerifyUtil.VerifyFile(c, "file")
|
||||
if response.IsFailWithResp(c, ve) {
|
||||
return
|
||||
}
|
||||
res, err := commonService.UploadService.UploadFile(file, uReq.Cid, config.AdminConfig.GetAdminId(c))
|
||||
response.CheckAndRespWithData(c, res, err)
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package commonController
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"x_admin/app/service/commonService"
|
||||
"x_admin/core/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func UploadChunkRoute(rg *gin.RouterGroup) {
|
||||
handle := uploadChunkHandler{
|
||||
uploadPath: "./uploads",
|
||||
tmpPath: "./uploads/.tmp",
|
||||
}
|
||||
os.MkdirAll(handle.uploadPath, 0755)
|
||||
os.MkdirAll(handle.tmpPath, 0755)
|
||||
|
||||
rg = rg.Group("/common")
|
||||
rg.GET("/uploadChunk/CheckFileExist", handle.CheckFileExist)
|
||||
// rg.GET("/uploadChunk/CheckChunkExist", handle.CheckChunkExist)
|
||||
rg.GET("/uploadChunk/HasChunk", handle.HasChunk)
|
||||
|
||||
rg.POST("/uploadChunk/UploadChunk", handle.UploadChunk)
|
||||
rg.POST("/uploadChunk/MergeChunk", handle.MergeChunk)
|
||||
}
|
||||
|
||||
type uploadChunkHandler struct {
|
||||
uploadPath string
|
||||
tmpPath string
|
||||
}
|
||||
|
||||
func (uh uploadChunkHandler) getFilePath(fileMd5 string, fileName string) string {
|
||||
// 获取文件后缀
|
||||
ext := filepath.Ext(fileName)
|
||||
|
||||
return fmt.Sprintf("%s/%s%s", uh.uploadPath, fileMd5, ext)
|
||||
}
|
||||
func (uh uploadChunkHandler) getChunkDir(fileMd5 string, chunkSize string) string {
|
||||
return fmt.Sprintf("%s/%s_%s", uh.tmpPath, fileMd5, chunkSize)
|
||||
}
|
||||
func (uh uploadChunkHandler) getChunkPath(fileMd5 string, chunkSize string, index string) string {
|
||||
return fmt.Sprintf("%s/%s_%s/%s", uh.tmpPath, fileMd5, chunkSize, index)
|
||||
}
|
||||
|
||||
func (uh uploadChunkHandler) CheckFileExist(c *gin.Context) {
|
||||
var fileMd5 = c.Query("fileMd5")
|
||||
var fileName = c.Query("fileName")
|
||||
if fileMd5 == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件hash错误")
|
||||
return
|
||||
}
|
||||
if fileName == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件名错误")
|
||||
return
|
||||
}
|
||||
//文件类型白名单
|
||||
var whiteList = []string{
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".png",
|
||||
".gif",
|
||||
".bmp",
|
||||
".svg",
|
||||
".webp",
|
||||
".avif",
|
||||
".txt",
|
||||
".mp4",
|
||||
".avi",
|
||||
".mov",
|
||||
".wmv",
|
||||
".flv",
|
||||
".mkv",
|
||||
".mp3",
|
||||
".wav",
|
||||
".aac",
|
||||
".flac",
|
||||
".ogg",
|
||||
".m4a",
|
||||
".pdf",
|
||||
".doc",
|
||||
".docx",
|
||||
".xls",
|
||||
".xlsx",
|
||||
".ppt",
|
||||
".pptx",
|
||||
".zip",
|
||||
".rar",
|
||||
".7z",
|
||||
".tar",
|
||||
".gz",
|
||||
".bz2",
|
||||
".xz",
|
||||
".msi",
|
||||
".exe",
|
||||
".dmg",
|
||||
".iso",
|
||||
".app",
|
||||
".deb",
|
||||
".rpm",
|
||||
".pkg",
|
||||
".apk",
|
||||
}
|
||||
var fileExt = ""
|
||||
for _, ext := range whiteList {
|
||||
if strings.HasSuffix(fileName, ext) {
|
||||
fileExt = ext
|
||||
break
|
||||
}
|
||||
}
|
||||
if fileExt == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件类型错误")
|
||||
return
|
||||
}
|
||||
|
||||
// var fileName = url.QueryEscape(c.Query("fileName"))
|
||||
// 正则检查MD5
|
||||
reg := regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
|
||||
if !reg.MatchString(fileMd5) {
|
||||
response.FailWithMsg(c, response.SystemError, "文件hash错误")
|
||||
return
|
||||
}
|
||||
var filePath = uh.getFilePath(fileMd5, fileName)
|
||||
// 检查文件是否存在
|
||||
if commonService.UploadChunkService.CheckFileExist(filePath) {
|
||||
response.OkWithData(c, filePath)
|
||||
return
|
||||
}
|
||||
response.OkWithData(c, nil)
|
||||
}
|
||||
func (uh uploadChunkHandler) HasChunk(c *gin.Context) {
|
||||
var fileMd5 = c.Query("fileMd5")
|
||||
var chunkSize = c.Query("chunkSize")
|
||||
if fileMd5 == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件hash错误")
|
||||
return
|
||||
}
|
||||
if chunkSize == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "分片大小错误")
|
||||
return
|
||||
}
|
||||
var chunkDir = uh.getChunkDir(fileMd5, chunkSize)
|
||||
var HasChunk = commonService.UploadChunkService.HasChunk(chunkDir)
|
||||
response.OkWithData(c, HasChunk)
|
||||
}
|
||||
|
||||
// 检查chunk是否存在
|
||||
// func (uh uploadChunkHandler) CheckChunkExist(c *gin.Context) {
|
||||
// fileMd5 := c.Query("fileMd5") // 上传文件的md5
|
||||
// chunkSize := c.Query("chunkSize")
|
||||
// index := c.Query("index") // 分片序号
|
||||
// // chunkPath := fmt.Sprintf("%s/%s/%s", uh.tmpPath, fileMd5, index)
|
||||
// chunkPath := uh.getChunkPath(fileMd5, chunkSize, index)
|
||||
// if commonService.UploadChunkService.CheckFileExist(chunkPath) {
|
||||
// response.OkWithData(c, 1)
|
||||
// return
|
||||
// }
|
||||
// response.OkWithData(c, 0)
|
||||
// }
|
||||
|
||||
// UploadChunk 上传分片
|
||||
func (uh uploadChunkHandler) UploadChunk(c *gin.Context) {
|
||||
chunk, _ := c.FormFile("chunk") // 分片文件
|
||||
chunkSize := c.PostForm("chunkSize") // 分片分割的大小
|
||||
index := c.PostForm("index") // 分片序号
|
||||
fileMd5 := c.PostForm("fileMd5") // 上传文件的md5
|
||||
if fileMd5 == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件hash错误")
|
||||
return
|
||||
}
|
||||
if chunkSize == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "分片大小错误")
|
||||
return
|
||||
}
|
||||
if index == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "分片序号错误")
|
||||
return
|
||||
}
|
||||
if chunk == nil {
|
||||
response.FailWithMsg(c, response.SystemError, "分片文件错误")
|
||||
return
|
||||
}
|
||||
|
||||
chunkDir := uh.getChunkDir(fileMd5, chunkSize)
|
||||
chunkPath := uh.getChunkPath(fileMd5, chunkSize, index)
|
||||
err := commonService.UploadChunkService.UploadChunk(chunkDir, chunkPath, chunk)
|
||||
if err != nil {
|
||||
response.FailWithMsg(c, response.SystemError, err.Error())
|
||||
return
|
||||
}
|
||||
response.Ok(c)
|
||||
}
|
||||
func (uh uploadChunkHandler) MergeChunk(c *gin.Context) {
|
||||
var MergeChunk struct {
|
||||
FileMd5 string `json:"fileMd5"` // 上传文件的md5
|
||||
FileName string `json:"fileName"` // 文件名
|
||||
ChunkCount int `json:"chunkCount"` // 分片数量
|
||||
ChunkSize int `json:"chunkSize"` // 分片分割的大小,作用:确保不同分片大小不放在同一目录
|
||||
}
|
||||
bindErr := c.ShouldBindJSON(&MergeChunk)
|
||||
if bindErr != nil {
|
||||
response.FailWithMsg(c, response.SystemError, bindErr.Error())
|
||||
return
|
||||
}
|
||||
if MergeChunk.FileMd5 == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件hash错误")
|
||||
return
|
||||
}
|
||||
if MergeChunk.FileName == "" {
|
||||
response.FailWithMsg(c, response.SystemError, "文件名错误")
|
||||
return
|
||||
}
|
||||
if MergeChunk.ChunkCount <= 0 {
|
||||
response.FailWithMsg(c, response.SystemError, "分片数量错误")
|
||||
return
|
||||
}
|
||||
if MergeChunk.ChunkSize <= 0 {
|
||||
response.FailWithMsg(c, response.SystemError, "分片大小错误")
|
||||
return
|
||||
}
|
||||
var filePath = uh.getFilePath(MergeChunk.FileMd5, MergeChunk.FileName)
|
||||
var chunkDir = uh.getChunkDir(MergeChunk.FileMd5, fmt.Sprintf("%d", MergeChunk.ChunkSize))
|
||||
err := commonService.UploadChunkService.MergeChunk(chunkDir, filePath, MergeChunk.ChunkCount)
|
||||
if err != nil {
|
||||
response.FailWithMsg(c, response.SystemError, err.Error())
|
||||
return
|
||||
}
|
||||
response.OkWithData(c, filePath)
|
||||
}
|
||||
Reference in New Issue
Block a user