作者 tangxvhui

文件上传

... ... @@ -31,6 +31,11 @@ ucenter_check_alt = "rsF0pL!6DwjBO735"
ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
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"
# 审批流程修改 消息发布
message_publish = "audit_change"
#---自定义配置 结束----
\ No newline at end of file
... ...
package controllers
import (
"io"
"mime/multipart"
"oppmg/common/log"
"oppmg/protocol"
"os"
)
//UploadController 文件上传
type UploadController struct {
BaseController
}
//UploadImage 图片上传
//@router /image
func (c *UploadController) UploadImage() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
filehead, err := c.GetFiles("file")
if err != nil {
log.Error("获取上传文件失败:%s", err)
}
for i := range filehead {
err := saveFile(filehead[i], "")
if err != nil {
msg = protocol.BadRequestParam("1")
return
}
}
msg = protocol.NewReturnResponse(nil, nil)
return
}
func saveFile(fileData *multipart.FileHeader, src string) error {
os.MkdirAll("./file/opp", 0777)
file, err := fileData.Open()
defer file.Close()
if err != nil {
log.Error("文件获取失败%s", err)
return err
}
dst, err := os.Create("./file/opp/" + fileData.Filename)
defer dst.Close()
if err != nil {
log.Error("目录访问失败:%s", err)
return err
}
if _, err := io.Copy(dst, file); err != nil {
log.Error("文件保存失败:%s", err)
return err
}
return nil
}
... ...
... ... @@ -6,6 +6,7 @@ import (
"oppmg/protocol"
serveauth "oppmg/services/auth"
"oppmg/storage/redisdata"
"strings"
"github.com/astaxie/beego"
... ... @@ -38,11 +39,17 @@ var AuthToken = func(ctx *context.Context) {
mtoken *serveauth.MyToken
)
accesstoken := ctx.Input.Header(protocol.HeaderAccessToken)
// refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken)
mtoken, err = serveauth.ValidJWTToken(accesstoken)
//解出需要的jwt串 例:头【Authorization】:Bearer 123token456
s := strings.Split(accesstoken, "\u0020")
// strings.TrimSpace()
if len(s) > 0 {
accesstoken = s[len(s)-1]
}
fmt.Println("===>", accesstoken)
if accesstoken == "123456" && beego.BConfig.RunMode != "prod" {
return
}
mtoken, err = serveauth.ValidJWTToken(accesstoken)
if err == nil {
storetoken, err = redisdata.GetLoginToken(mtoken.UID)
if err != nil {
... ...
package protocol
var NullData = struct{}{}
//CustomErrParse 解析自定义错误结构体
type CustomErrParse interface {
ParseToMessage() *ResponseMessage
... ... @@ -37,7 +39,7 @@ func NewMessage(code string) *ResponseMessage {
rsp := &ResponseMessage{
Errno: transformCode(ecode.Errno),
Errmsg: ecode.Errmsg,
Data: nil,
Data: NullData,
}
return rsp
}
... ... @@ -89,8 +91,12 @@ func SearchErr(code string) ErrorCode {
//NewReturnResponse 控制层响应返回
func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) {
// var msg *ResponseMessage
if data == nil {
data = NullData
}
if eRR == nil {
msg = NewMessage("0")
msg.Data = data
return msg
}
... ... @@ -108,6 +114,9 @@ func BadRequestParam(code string) *ResponseMessage {
//NewPageDataResponse 控制层分页数据响应返回
func NewPageDataResponse(data interface{}, eRR error) (msg *ResponseMessage) {
if data == nil {
data = NullData
}
if eRR != nil {
if x, ok := eRR.(CustomErrParse); ok {
return x.ParseToMessage()
... ...
... ... @@ -80,6 +80,9 @@ func init() {
beego.NSRouter("/deleteCategory", &controllers.TemplateController{}, "post:TemplateDeleteCategory"),
beego.NSRouter("/sortCategory", &controllers.TemplateController{}, "post:CategoryEditSort"),
),
beego.NSNamespace("/upload",
beego.NSRouter("/image", &controllers.UploadController{}, "post:UploadImage"),
),
)
nsAuth := beego.NewNamespace("/auth",
... ...