作者 tangxvhui

文件上传

... ... @@ -20,6 +20,9 @@ type MyConfig struct {
UcenterBaseUrl string
UcenterSecret string
UcenterAppKey string
FileSavePath string
FileHost string
FileHostPath string
}
//MConfig
... ... @@ -48,6 +51,9 @@ func RestMyConfig() *MyConfig {
UcenterBaseUrl: beego.AppConfig.String("ucenter_base_url"),
UcenterSecret: beego.AppConfig.String("ucenter_secret"),
UcenterAppKey: beego.AppConfig.String("ucenter_app_key"),
FileSavePath: beego.AppConfig.String("file_save_path"),
FileHost: beego.AppConfig.String("file_host"),
FileHostPath: beego.AppConfig.String("file_host_path"),
}
return MConfig
}
... ...
... ... @@ -33,9 +33,9 @@ ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "cykbjnfqgctn"
# 上传文件保存路径
file_save_path = "${aliyun_file_access||~/www/opp}"
file_host = "http://192.168.139.137:8080/"
file_host_path = "file/opp"
file_save_path = "/var/www/opp/file"
file_host = "http://mmm-opp-dev.fjmaimaimai.com"
file_host_path = "/file/opp"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
... ... @@ -33,9 +33,9 @@ ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "cykbjnfqgctn"
# 上传文件保存路径
file_save_path = "${aliyun_file_access||~/www/opp}"
file_host = "http://192.168.139.137:8080/"
file_host_path = "file/opp"
file_save_path = "/var/www/opp/file"
file_host = "http://mmm-opp-dev.fjmaimaimai.com"
file_host_path = "/file/opp"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
... ... @@ -390,25 +390,26 @@ func (c *CompanyController) InitCompany() {
msg = protocol.BadRequestParam("1")
return
}
if len(param.CompanyName) == 0 {
log.Error("公司名称空")
msg = protocol.BadRequestParam("1")
return
}
if len(param.AdminAccount) == 0 {
log.Error("人员账号空")
msg = protocol.BadRequestParam("1")
return
}
if len(param.AdminName) == 0 {
log.Error("人员名称空")
msg = protocol.BadRequestParam("1")
return
}
var (
err error
)
if ok := param.IsEnable(); ok {
if len(param.CompanyName) == 0 {
log.Error("公司名称空")
msg = protocol.BadRequestParam("1")
return
}
if len(param.AdminAccount) == 0 {
log.Error("人员账号空")
msg = protocol.BadRequestParam("1")
return
}
if len(param.AdminName) == 0 {
log.Error("人员名称空")
msg = protocol.BadRequestParam("1")
return
}
err = servecompany.InitCompanyInfo(param)
} else if ok := param.IsForbid(); ok {
err = servecompany.ForbidCompany(param.CompanyId)
... ...
package controllers
import (
"fmt"
"io"
"mime/multipart"
"oppmg/common/config"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/utils"
"os"
"path"
"path/filepath"
"time"
)
//UploadController 文件上传
... ... @@ -13,6 +19,10 @@ type UploadController struct {
BaseController
}
type ResponseUploadImage struct {
Urls []string `json:"url"`
}
//UploadImage 图片上传
//@router /image
func (c *UploadController) UploadImage() {
... ... @@ -24,35 +34,42 @@ func (c *UploadController) UploadImage() {
if err != nil {
log.Error("获取上传文件失败:%s", err)
}
var rsp ResponseUploadImage
for i := range filehead {
err := saveFile(filehead[i], "")
hostpath, err := saveFile(filehead[i], "image")
if err != nil {
msg = protocol.BadRequestParam("1")
return
}
rsp.Urls = append(rsp.Urls, hostpath)
}
msg = protocol.NewReturnResponse(nil, nil)
msg = protocol.NewReturnResponse(rsp, nil)
return
}
func saveFile(fileData *multipart.FileHeader, src string) error {
os.MkdirAll("./file/opp", 0777)
func saveFile(fileData *multipart.FileHeader, fileType string) (hostpath string, err error) {
savepath := filepath.Join(config.MConfig.FileSavePath, fileType)
os.MkdirAll(savepath, 0777)
file, err := fileData.Open()
defer file.Close()
if err != nil {
log.Error("文件获取失败%s", err)
return err
return "", err
}
dst, err := os.Create("./file/opp/" + fileData.Filename)
subfix := path.Ext(fileData.Filename)
mfileName := fmt.Sprint(utils.GenerateIDBySonyflake())
mfileName = fmt.Sprintf("%d_%s%s", time.Now().Unix(), mfileName, subfix)
dst, err := os.Create(filepath.Join(savepath, mfileName))
defer dst.Close()
if err != nil {
log.Error("目录访问失败:%s", err)
return err
return "", err
}
if _, err := io.Copy(dst, file); err != nil {
log.Error("文件保存失败:%s", err)
return err
return "", err
}
return nil
hostpath = config.MConfig.FileHost + filepath.Join(config.MConfig.FileHostPath, fileType, mfileName)
return hostpath, nil
}
... ...
... ... @@ -14,12 +14,12 @@ import (
func init() {
beego.BConfig.WebConfig.Session.SessionOn = true
}
func main() {
common.ResetCommonConfig()
log.Debug("加载配置%s", config.MConfig.ConfigName)
orm.RegisterDataBase("default", "mysql", config.MConfig.SqlConn)
}
func main() {
// orm.Debug = true
// if beego.BConfig.RunMode == "dev" {
// beego.BConfig.WebConfig.DirectoryIndex = true
... ...
... ... @@ -102,7 +102,6 @@ func init() {
beego.AddNamespace(nsV1)
beego.AddNamespace(nsAuth)
beego.AddNamespace(nsUcenter)
nsTest := beego.NewNamespace("/test",
beego.NSCond(func(ctx *context.Context) bool {
if beego.BConfig.RunMode != "prod" {
... ... @@ -114,4 +113,5 @@ func init() {
)
beego.AddNamespace(nsTest)
beego.SetStaticPath("/log", beego.AppConfig.String("log_filename"))
beego.SetStaticPath("/file/opp", beego.AppConfig.String("file_save_path"))
}
... ...