作者 tangxvhui

文件上传

@@ -31,6 +31,11 @@ ucenter_check_alt = "rsF0pL!6DwjBO735" @@ -31,6 +31,11 @@ ucenter_check_alt = "rsF0pL!6DwjBO735"
31 ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com" 31 ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b" 32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
33 ucenter_secret = "cykbjnfqgctn" 33 ucenter_secret = "cykbjnfqgctn"
  34 +
  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"
34 # 审批流程修改 消息发布 39 # 审批流程修改 消息发布
35 message_publish = "audit_change" 40 message_publish = "audit_change"
36 #---自定义配置 结束---- 41 #---自定义配置 结束----
  1 +package controllers
  2 +
  3 +import (
  4 + "io"
  5 + "mime/multipart"
  6 + "oppmg/common/log"
  7 + "oppmg/protocol"
  8 + "os"
  9 +)
  10 +
  11 +//UploadController 文件上传
  12 +type UploadController struct {
  13 + BaseController
  14 +}
  15 +
  16 +//UploadImage 图片上传
  17 +//@router /image
  18 +func (c *UploadController) UploadImage() {
  19 + var msg *protocol.ResponseMessage
  20 + defer func() {
  21 + c.ResposeJson(msg)
  22 + }()
  23 + filehead, err := c.GetFiles("file")
  24 + if err != nil {
  25 + log.Error("获取上传文件失败:%s", err)
  26 + }
  27 + for i := range filehead {
  28 + err := saveFile(filehead[i], "")
  29 + if err != nil {
  30 + msg = protocol.BadRequestParam("1")
  31 + return
  32 + }
  33 + }
  34 + msg = protocol.NewReturnResponse(nil, nil)
  35 + return
  36 +}
  37 +
  38 +func saveFile(fileData *multipart.FileHeader, src string) error {
  39 + os.MkdirAll("./file/opp", 0777)
  40 + file, err := fileData.Open()
  41 + defer file.Close()
  42 + if err != nil {
  43 + log.Error("文件获取失败%s", err)
  44 + return err
  45 + }
  46 + dst, err := os.Create("./file/opp/" + fileData.Filename)
  47 + defer dst.Close()
  48 + if err != nil {
  49 + log.Error("目录访问失败:%s", err)
  50 +
  51 + return err
  52 + }
  53 + if _, err := io.Copy(dst, file); err != nil {
  54 + log.Error("文件保存失败:%s", err)
  55 + return err
  56 + }
  57 + return nil
  58 +}
@@ -6,6 +6,7 @@ import ( @@ -6,6 +6,7 @@ import (
6 "oppmg/protocol" 6 "oppmg/protocol"
7 serveauth "oppmg/services/auth" 7 serveauth "oppmg/services/auth"
8 "oppmg/storage/redisdata" 8 "oppmg/storage/redisdata"
  9 + "strings"
9 10
10 "github.com/astaxie/beego" 11 "github.com/astaxie/beego"
11 12
@@ -38,11 +39,17 @@ var AuthToken = func(ctx *context.Context) { @@ -38,11 +39,17 @@ var AuthToken = func(ctx *context.Context) {
38 mtoken *serveauth.MyToken 39 mtoken *serveauth.MyToken
39 ) 40 )
40 accesstoken := ctx.Input.Header(protocol.HeaderAccessToken) 41 accesstoken := ctx.Input.Header(protocol.HeaderAccessToken)
41 - // refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken)  
42 - mtoken, err = serveauth.ValidJWTToken(accesstoken) 42 + //解出需要的jwt串 例:头【Authorization】:Bearer 123token456
  43 + s := strings.Split(accesstoken, "\u0020")
  44 + // strings.TrimSpace()
  45 + if len(s) > 0 {
  46 + accesstoken = s[len(s)-1]
  47 + }
  48 + fmt.Println("===>", accesstoken)
43 if accesstoken == "123456" && beego.BConfig.RunMode != "prod" { 49 if accesstoken == "123456" && beego.BConfig.RunMode != "prod" {
44 return 50 return
45 } 51 }
  52 + mtoken, err = serveauth.ValidJWTToken(accesstoken)
46 if err == nil { 53 if err == nil {
47 storetoken, err = redisdata.GetLoginToken(mtoken.UID) 54 storetoken, err = redisdata.GetLoginToken(mtoken.UID)
48 if err != nil { 55 if err != nil {
1 package protocol 1 package protocol
2 2
  3 +var NullData = struct{}{}
  4 +
3 //CustomErrParse 解析自定义错误结构体 5 //CustomErrParse 解析自定义错误结构体
4 type CustomErrParse interface { 6 type CustomErrParse interface {
5 ParseToMessage() *ResponseMessage 7 ParseToMessage() *ResponseMessage
@@ -37,7 +39,7 @@ func NewMessage(code string) *ResponseMessage { @@ -37,7 +39,7 @@ func NewMessage(code string) *ResponseMessage {
37 rsp := &ResponseMessage{ 39 rsp := &ResponseMessage{
38 Errno: transformCode(ecode.Errno), 40 Errno: transformCode(ecode.Errno),
39 Errmsg: ecode.Errmsg, 41 Errmsg: ecode.Errmsg,
40 - Data: nil, 42 + Data: NullData,
41 } 43 }
42 return rsp 44 return rsp
43 } 45 }
@@ -89,8 +91,12 @@ func SearchErr(code string) ErrorCode { @@ -89,8 +91,12 @@ func SearchErr(code string) ErrorCode {
89 //NewReturnResponse 控制层响应返回 91 //NewReturnResponse 控制层响应返回
90 func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) { 92 func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) {
91 // var msg *ResponseMessage 93 // var msg *ResponseMessage
  94 + if data == nil {
  95 + data = NullData
  96 + }
92 if eRR == nil { 97 if eRR == nil {
93 msg = NewMessage("0") 98 msg = NewMessage("0")
  99 +
94 msg.Data = data 100 msg.Data = data
95 return msg 101 return msg
96 } 102 }
@@ -108,6 +114,9 @@ func BadRequestParam(code string) *ResponseMessage { @@ -108,6 +114,9 @@ func BadRequestParam(code string) *ResponseMessage {
108 114
109 //NewPageDataResponse 控制层分页数据响应返回 115 //NewPageDataResponse 控制层分页数据响应返回
110 func NewPageDataResponse(data interface{}, eRR error) (msg *ResponseMessage) { 116 func NewPageDataResponse(data interface{}, eRR error) (msg *ResponseMessage) {
  117 + if data == nil {
  118 + data = NullData
  119 + }
111 if eRR != nil { 120 if eRR != nil {
112 if x, ok := eRR.(CustomErrParse); ok { 121 if x, ok := eRR.(CustomErrParse); ok {
113 return x.ParseToMessage() 122 return x.ParseToMessage()
@@ -80,6 +80,9 @@ func init() { @@ -80,6 +80,9 @@ func init() {
80 beego.NSRouter("/deleteCategory", &controllers.TemplateController{}, "post:TemplateDeleteCategory"), 80 beego.NSRouter("/deleteCategory", &controllers.TemplateController{}, "post:TemplateDeleteCategory"),
81 beego.NSRouter("/sortCategory", &controllers.TemplateController{}, "post:CategoryEditSort"), 81 beego.NSRouter("/sortCategory", &controllers.TemplateController{}, "post:CategoryEditSort"),
82 ), 82 ),
  83 + beego.NSNamespace("/upload",
  84 + beego.NSRouter("/image", &controllers.UploadController{}, "post:UploadImage"),
  85 + ),
83 ) 86 )
84 87
85 nsAuth := beego.NewNamespace("/auth", 88 nsAuth := beego.NewNamespace("/auth",