feat: file upload and remove
This commit is contained in:
+22
-1
@@ -1,8 +1,10 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crm/models"
|
||||||
"crm/response"
|
"crm/response"
|
||||||
"crm/service"
|
"crm/service"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -21,4 +23,23 @@ func NewCommonApi() *CommonApi {
|
|||||||
func (c *CommonApi) InitDatabase(context *gin.Context) {
|
func (c *CommonApi) InitDatabase(context *gin.Context) {
|
||||||
errCode := c.commonService.InitDatabase()
|
errCode := c.commonService.InitDatabase()
|
||||||
response.Result(errCode, nil, context)
|
response.Result(errCode, nil, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件上传
|
||||||
|
func (c *CommonApi) FileUpload(context *gin.Context) {
|
||||||
|
file, _ := context.FormFile("file")
|
||||||
|
fileInfo, errCode := c.commonService.FileUpload(file)
|
||||||
|
response.Result(errCode, fileInfo, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件移除
|
||||||
|
func (c *CommonApi) FileRemove(context *gin.Context) {
|
||||||
|
var param models.FileParam
|
||||||
|
if err := context.ShouldBind(¶m); err != nil {
|
||||||
|
response.Result(response.ErrCodeParamInvalid, nil, context)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
errCode := c.commonService.FileRemove(¶m)
|
||||||
|
response.Result(errCode, nil, context)
|
||||||
|
}
|
||||||
|
|||||||
+36
-2
@@ -2,10 +2,14 @@ package dao
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crm/common"
|
||||||
"crm/global"
|
"crm/global"
|
||||||
"crm/models"
|
"crm/models"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,8 +60,6 @@ func NewCommonDao() *CommonDao {
|
|||||||
func (c *CommonDao) InitDatabase() error {
|
func (c *CommonDao) InitDatabase() error {
|
||||||
env := global.Config.Server.Runenv
|
env := global.Config.Server.Runenv
|
||||||
if env == Prod {
|
if env == Prod {
|
||||||
// fn := "/home/ubuntu/crmapi/crm.sql"
|
|
||||||
// fn := "/Users/xuzxc/Downloads/crm.sql"
|
|
||||||
dbFile := global.Config.Mysql.DbFile
|
dbFile := global.Config.Mysql.DbFile
|
||||||
sql, err := os.ReadFile(dbFile)
|
sql, err := os.ReadFile(dbFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -79,3 +81,35 @@ func (c *CommonDao) InitDatabase() error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CommonDao) FileUpload(file *multipart.FileHeader) (*models.FileInfo, error) {
|
||||||
|
dist := global.Config.File.Path
|
||||||
|
name := common.GenUUID() + path.Ext(file.Filename)
|
||||||
|
dn := dist + name
|
||||||
|
src, err := file.Open()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer src.Close()
|
||||||
|
|
||||||
|
out, err := os.Create(dn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(out, src)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
flieInfo := models.FileInfo{
|
||||||
|
Url: dn,
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
return &flieInfo, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommonDao) FileRemove(fileName string) error {
|
||||||
|
file := global.Config.File.Path + fileName
|
||||||
|
return os.Remove(file)
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ func Router() {
|
|||||||
|
|
||||||
// 通用接口
|
// 通用接口
|
||||||
route.POST("/common/database/init", api.NewCommonApi().InitDatabase)
|
route.POST("/common/database/init", api.NewCommonApi().InitDatabase)
|
||||||
|
route.POST("/common/file/upload", api.NewCommonApi().FileUpload)
|
||||||
|
route.DELETE("/common/file/remove", api.NewCommonApi().FileRemove)
|
||||||
|
|
||||||
// Jwt中间件
|
// Jwt中间件
|
||||||
route.Use(middleware.JwtAuth())
|
route.Use(middleware.JwtAuth())
|
||||||
|
|||||||
+19
-7
@@ -8,11 +8,23 @@ type Page struct {
|
|||||||
|
|
||||||
// 发送邮件参数模型
|
// 发送邮件参数模型
|
||||||
type MailParam struct {
|
type MailParam struct {
|
||||||
Smtp string
|
Smtp string
|
||||||
Port int
|
Port int
|
||||||
AuthCode string
|
AuthCode string
|
||||||
Sender string
|
Sender string
|
||||||
Subject string
|
Subject string
|
||||||
Content string
|
Content string
|
||||||
Receiver string
|
Attachment string
|
||||||
|
Receiver string
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件参数模型
|
||||||
|
type FileParam struct {
|
||||||
|
Name string `json:"name" binding:"required,gt=0"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件信息
|
||||||
|
type FileInfo struct {
|
||||||
|
Url string `json:"url"`
|
||||||
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,15 @@ const (
|
|||||||
ErrCodeNoLogin = 3 // 未登录或非法访问
|
ErrCodeNoLogin = 3 // 未登录或非法访问
|
||||||
ErrCodeTokenExpire = 4 // Token过期
|
ErrCodeTokenExpire = 4 // Token过期
|
||||||
|
|
||||||
ErrCodeInitDataFailed = 10 // 初始化数据失败
|
ErrCodeInitDataFailed = 10 // 初始化数据失败
|
||||||
|
ErrCodeFileUploadFailed = 11 // 文件上传失败
|
||||||
|
ErrCodeFileRemoveFailed = 12 // 文件移除失败
|
||||||
|
|
||||||
ErrCodeUserHasExist = 10001 // 用户已经存在
|
ErrCodeUserHasExist = 10001 // 用户已经存在
|
||||||
ErrCodeUserNotExist = 10002 // 用户不存在
|
ErrCodeUserNotExist = 10002 // 用户不存在
|
||||||
ErrCOdeUserEmailOrPass = 10003 // 用户邮箱或密码错误
|
ErrCOdeUserEmailOrPass = 10003 // 用户邮箱或密码错误
|
||||||
ErrCodeVerityCodeSendFailed = 10004 // 验证码发送失败
|
ErrCodeVerityCodeSendFailed = 10004 // 验证码发送失败
|
||||||
ErrCodeVerityCodeInvalid = 10005 // 验证码无效
|
ErrCodeVerityCodeInvalid = 10005 // 验证码无效
|
||||||
ErrCodeCompanyCreateFailed = 10006 // 企业创建失败
|
|
||||||
ErrCodeCompanyIdNotExist = 10007 // 企业编号不存在
|
|
||||||
ErrCodeEmailFormatInvalid = 10008 // 邮箱格式无效
|
ErrCodeEmailFormatInvalid = 10008 // 邮箱格式无效
|
||||||
ErrCodeUserPassResetFailed = 10009 // 用户密码重置失败
|
ErrCodeUserPassResetFailed = 10009 // 用户密码重置失败
|
||||||
|
|
||||||
@@ -40,12 +40,12 @@ var msg = map[int]string{
|
|||||||
ErrCodeNoLogin: "no login",
|
ErrCodeNoLogin: "no login",
|
||||||
ErrCodeTokenExpire: "token expire",
|
ErrCodeTokenExpire: "token expire",
|
||||||
ErrCodeInitDataFailed: "data init failed",
|
ErrCodeInitDataFailed: "data init failed",
|
||||||
|
ErrCodeFileUploadFailed: "file upload failed",
|
||||||
ErrCodeUserHasExist: "user has exist",
|
ErrCodeUserHasExist: "user has exist",
|
||||||
ErrCodeUserNotExist: "user not exist",
|
ErrCodeUserNotExist: "user not exist",
|
||||||
ErrCOdeUserEmailOrPass: "user email or password error",
|
ErrCOdeUserEmailOrPass: "user email or password error",
|
||||||
ErrCodeVerityCodeSendFailed: "verify code send failed",
|
ErrCodeVerityCodeSendFailed: "verify code send failed",
|
||||||
ErrCodeVerityCodeInvalid: "verify code invalid",
|
ErrCodeVerityCodeInvalid: "verify code invalid",
|
||||||
ErrCodeCompanyCreateFailed: "company create failed",
|
|
||||||
ErrCodeEmailFormatInvalid: "email format invalid",
|
ErrCodeEmailFormatInvalid: "email format invalid",
|
||||||
ErrCodeUserPassResetFailed: "user password reset failed",
|
ErrCodeUserPassResetFailed: "user password reset failed",
|
||||||
ErrCodeFileExportFailed: "file export failed",
|
ErrCodeFileExportFailed: "file export failed",
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crm/dao"
|
"crm/dao"
|
||||||
|
"crm/models"
|
||||||
"crm/response"
|
"crm/response"
|
||||||
|
"mime/multipart"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -50,3 +52,20 @@ func (c *CommonService) InitDatabase() int {
|
|||||||
}
|
}
|
||||||
return response.ErrCodeSuccess
|
return response.ErrCodeSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件上传
|
||||||
|
func (c *CommonService) FileUpload(file *multipart.FileHeader) (*models.FileInfo, int) {
|
||||||
|
fileInfo, err := c.commonDao.FileUpload(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, response.ErrCodeFileUploadFailed
|
||||||
|
}
|
||||||
|
return fileInfo, response.ErrCodeSuccess
|
||||||
|
}
|
||||||
|
|
||||||
|
// 文件移除
|
||||||
|
func (c *CommonService) FileRemove(param *models.FileParam) int {
|
||||||
|
if err := c.commonDao.FileRemove(param.Name); err != nil {
|
||||||
|
return response.ErrCodeFileUploadFailed
|
||||||
|
}
|
||||||
|
return response.ErrCodeSuccess
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,3 +8,12 @@ export function initDatabase(param) {
|
|||||||
data: param,
|
data: param,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件移除
|
||||||
|
export function fileRemove(param) {
|
||||||
|
return request({
|
||||||
|
url: '/common/file/remove',
|
||||||
|
method: 'delete',
|
||||||
|
data: param,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user