作者 tangxvhui

文件上传

@@ -20,6 +20,9 @@ type MyConfig struct { @@ -20,6 +20,9 @@ type MyConfig struct {
20 UcenterBaseUrl string 20 UcenterBaseUrl string
21 UcenterSecret string 21 UcenterSecret string
22 UcenterAppKey string 22 UcenterAppKey string
  23 + FileSavePath string
  24 + FileHost string
  25 + FileHostPath string
23 } 26 }
24 27
25 //MConfig 28 //MConfig
@@ -48,6 +51,9 @@ func RestMyConfig() *MyConfig { @@ -48,6 +51,9 @@ func RestMyConfig() *MyConfig {
48 UcenterBaseUrl: beego.AppConfig.String("ucenter_base_url"), 51 UcenterBaseUrl: beego.AppConfig.String("ucenter_base_url"),
49 UcenterSecret: beego.AppConfig.String("ucenter_secret"), 52 UcenterSecret: beego.AppConfig.String("ucenter_secret"),
50 UcenterAppKey: beego.AppConfig.String("ucenter_app_key"), 53 UcenterAppKey: beego.AppConfig.String("ucenter_app_key"),
  54 + FileSavePath: beego.AppConfig.String("file_save_path"),
  55 + FileHost: beego.AppConfig.String("file_host"),
  56 + FileHostPath: beego.AppConfig.String("file_host_path"),
51 } 57 }
52 return MConfig 58 return MConfig
53 } 59 }
@@ -33,9 +33,9 @@ ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b" @@ -33,9 +33,9 @@ ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
33 ucenter_secret = "cykbjnfqgctn" 33 ucenter_secret = "cykbjnfqgctn"
34 34
35 # 上传文件保存路径 35 # 上传文件保存路径
36 -file_save_path = "${aliyun_file_access||~/www/opp}"  
37 -file_host = "http://192.168.139.137:8080/"  
38 -file_host_path = "file/opp" 36 +file_save_path = "/var/www/opp/file"
  37 +file_host = "http://mmm-opp-dev.fjmaimaimai.com"
  38 +file_host_path = "/file/opp"
39 # 审批流程修改 消息发布 39 # 审批流程修改 消息发布
40 message_publish = "audit_change" 40 message_publish = "audit_change"
41 #---自定义配置 结束---- 41 #---自定义配置 结束----
@@ -33,9 +33,9 @@ ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b" @@ -33,9 +33,9 @@ ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
33 ucenter_secret = "cykbjnfqgctn" 33 ucenter_secret = "cykbjnfqgctn"
34 34
35 # 上传文件保存路径 35 # 上传文件保存路径
36 -file_save_path = "${aliyun_file_access||~/www/opp}"  
37 -file_host = "http://192.168.139.137:8080/"  
38 -file_host_path = "file/opp" 36 +file_save_path = "/var/www/opp/file"
  37 +file_host = "http://mmm-opp-dev.fjmaimaimai.com"
  38 +file_host_path = "/file/opp"
39 # 审批流程修改 消息发布 39 # 审批流程修改 消息发布
40 message_publish = "audit_change" 40 message_publish = "audit_change"
41 #---自定义配置 结束---- 41 #---自定义配置 结束----
@@ -390,25 +390,26 @@ func (c *CompanyController) InitCompany() { @@ -390,25 +390,26 @@ func (c *CompanyController) InitCompany() {
390 msg = protocol.BadRequestParam("1") 390 msg = protocol.BadRequestParam("1")
391 return 391 return
392 } 392 }
393 - if len(param.CompanyName) == 0 {  
394 - log.Error("公司名称空")  
395 - msg = protocol.BadRequestParam("1")  
396 - return  
397 - }  
398 - if len(param.AdminAccount) == 0 {  
399 - log.Error("人员账号空")  
400 - msg = protocol.BadRequestParam("1")  
401 - return  
402 - }  
403 - if len(param.AdminName) == 0 {  
404 - log.Error("人员名称空")  
405 - msg = protocol.BadRequestParam("1")  
406 - return  
407 - } 393 +
408 var ( 394 var (
409 err error 395 err error
410 ) 396 )
411 if ok := param.IsEnable(); ok { 397 if ok := param.IsEnable(); ok {
  398 + if len(param.CompanyName) == 0 {
  399 + log.Error("公司名称空")
  400 + msg = protocol.BadRequestParam("1")
  401 + return
  402 + }
  403 + if len(param.AdminAccount) == 0 {
  404 + log.Error("人员账号空")
  405 + msg = protocol.BadRequestParam("1")
  406 + return
  407 + }
  408 + if len(param.AdminName) == 0 {
  409 + log.Error("人员名称空")
  410 + msg = protocol.BadRequestParam("1")
  411 + return
  412 + }
412 err = servecompany.InitCompanyInfo(param) 413 err = servecompany.InitCompanyInfo(param)
413 } else if ok := param.IsForbid(); ok { 414 } else if ok := param.IsForbid(); ok {
414 err = servecompany.ForbidCompany(param.CompanyId) 415 err = servecompany.ForbidCompany(param.CompanyId)
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
  4 + "fmt"
4 "io" 5 "io"
5 "mime/multipart" 6 "mime/multipart"
  7 + "oppmg/common/config"
6 "oppmg/common/log" 8 "oppmg/common/log"
7 "oppmg/protocol" 9 "oppmg/protocol"
  10 + "oppmg/utils"
8 "os" 11 "os"
  12 + "path"
  13 + "path/filepath"
  14 + "time"
9 ) 15 )
10 16
11 //UploadController 文件上传 17 //UploadController 文件上传
@@ -13,6 +19,10 @@ type UploadController struct { @@ -13,6 +19,10 @@ type UploadController struct {
13 BaseController 19 BaseController
14 } 20 }
15 21
  22 +type ResponseUploadImage struct {
  23 + Urls []string `json:"url"`
  24 +}
  25 +
16 //UploadImage 图片上传 26 //UploadImage 图片上传
17 //@router /image 27 //@router /image
18 func (c *UploadController) UploadImage() { 28 func (c *UploadController) UploadImage() {
@@ -24,35 +34,42 @@ func (c *UploadController) UploadImage() { @@ -24,35 +34,42 @@ func (c *UploadController) UploadImage() {
24 if err != nil { 34 if err != nil {
25 log.Error("获取上传文件失败:%s", err) 35 log.Error("获取上传文件失败:%s", err)
26 } 36 }
  37 + var rsp ResponseUploadImage
27 for i := range filehead { 38 for i := range filehead {
28 - err := saveFile(filehead[i], "") 39 + hostpath, err := saveFile(filehead[i], "image")
29 if err != nil { 40 if err != nil {
30 msg = protocol.BadRequestParam("1") 41 msg = protocol.BadRequestParam("1")
31 return 42 return
32 } 43 }
  44 + rsp.Urls = append(rsp.Urls, hostpath)
33 } 45 }
34 - msg = protocol.NewReturnResponse(nil, nil) 46 + msg = protocol.NewReturnResponse(rsp, nil)
35 return 47 return
36 } 48 }
37 49
38 -func saveFile(fileData *multipart.FileHeader, src string) error {  
39 - os.MkdirAll("./file/opp", 0777) 50 +func saveFile(fileData *multipart.FileHeader, fileType string) (hostpath string, err error) {
  51 + savepath := filepath.Join(config.MConfig.FileSavePath, fileType)
  52 + os.MkdirAll(savepath, 0777)
40 file, err := fileData.Open() 53 file, err := fileData.Open()
41 defer file.Close() 54 defer file.Close()
42 if err != nil { 55 if err != nil {
43 log.Error("文件获取失败%s", err) 56 log.Error("文件获取失败%s", err)
44 - return err 57 + return "", err
45 } 58 }
46 - dst, err := os.Create("./file/opp/" + fileData.Filename) 59 + subfix := path.Ext(fileData.Filename)
  60 + mfileName := fmt.Sprint(utils.GenerateIDBySonyflake())
  61 + mfileName = fmt.Sprintf("%d_%s%s", time.Now().Unix(), mfileName, subfix)
  62 + dst, err := os.Create(filepath.Join(savepath, mfileName))
47 defer dst.Close() 63 defer dst.Close()
48 if err != nil { 64 if err != nil {
49 log.Error("目录访问失败:%s", err) 65 log.Error("目录访问失败:%s", err)
50 66
51 - return err 67 + return "", err
52 } 68 }
53 if _, err := io.Copy(dst, file); err != nil { 69 if _, err := io.Copy(dst, file); err != nil {
54 log.Error("文件保存失败:%s", err) 70 log.Error("文件保存失败:%s", err)
55 - return err 71 + return "", err
56 } 72 }
57 - return nil 73 + hostpath = config.MConfig.FileHost + filepath.Join(config.MConfig.FileHostPath, fileType, mfileName)
  74 + return hostpath, nil
58 } 75 }
@@ -14,12 +14,12 @@ import ( @@ -14,12 +14,12 @@ import (
14 14
15 func init() { 15 func init() {
16 beego.BConfig.WebConfig.Session.SessionOn = true 16 beego.BConfig.WebConfig.Session.SessionOn = true
17 -}  
18 -  
19 -func main() {  
20 common.ResetCommonConfig() 17 common.ResetCommonConfig()
21 log.Debug("加载配置%s", config.MConfig.ConfigName) 18 log.Debug("加载配置%s", config.MConfig.ConfigName)
22 orm.RegisterDataBase("default", "mysql", config.MConfig.SqlConn) 19 orm.RegisterDataBase("default", "mysql", config.MConfig.SqlConn)
  20 +}
  21 +
  22 +func main() {
23 // orm.Debug = true 23 // orm.Debug = true
24 // if beego.BConfig.RunMode == "dev" { 24 // if beego.BConfig.RunMode == "dev" {
25 // beego.BConfig.WebConfig.DirectoryIndex = true 25 // beego.BConfig.WebConfig.DirectoryIndex = true
@@ -102,7 +102,6 @@ func init() { @@ -102,7 +102,6 @@ func init() {
102 beego.AddNamespace(nsV1) 102 beego.AddNamespace(nsV1)
103 beego.AddNamespace(nsAuth) 103 beego.AddNamespace(nsAuth)
104 beego.AddNamespace(nsUcenter) 104 beego.AddNamespace(nsUcenter)
105 -  
106 nsTest := beego.NewNamespace("/test", 105 nsTest := beego.NewNamespace("/test",
107 beego.NSCond(func(ctx *context.Context) bool { 106 beego.NSCond(func(ctx *context.Context) bool {
108 if beego.BConfig.RunMode != "prod" { 107 if beego.BConfig.RunMode != "prod" {
@@ -114,4 +113,5 @@ func init() { @@ -114,4 +113,5 @@ func init() {
114 ) 113 )
115 beego.AddNamespace(nsTest) 114 beego.AddNamespace(nsTest)
116 beego.SetStaticPath("/log", beego.AppConfig.String("log_filename")) 115 beego.SetStaticPath("/log", beego.AppConfig.String("log_filename"))
  116 + beego.SetStaticPath("/file/opp", beego.AppConfig.String("file_save_path"))
117 } 117 }