浏览代码

编辑帮助文档,上传附件接口

xyxie 2 周之前
父节点
当前提交
48689cf2c1
共有 4 个文件被更改,包括 150 次插入0 次删除
  1. 91 0
      controllers/report.go
  2. 45 0
      models/resource.go
  3. 9 0
      routers/commentsRouter.go
  4. 5 0
      routers/router.go

+ 91 - 0
controllers/report.go

@@ -0,0 +1,91 @@
+package controllers
+
+import (
+	"eta/eta_forum_admin/models"
+	"eta/eta_forum_admin/services"
+	"eta/eta_forum_admin/utils"
+	"os"
+	"path"
+	"time"
+)
+
+type ReportController struct {
+	BaseAuthController
+}
+
+// @Title 图片上传
+// @Description 图片上传接口
+// @Param   File   query   file  true       "文件"
+// @Success 200 上传成功
+// @router /uploadImg [post]
+func (this *ReportController) UploadImg() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		if br.ErrMsg == "" {
+			br.IsSendEmail = false
+		}
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	f, h, err := this.GetFile("file")
+	if err != nil {
+		br.Msg = "获取资源信息失败"
+		br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
+		return
+	}
+	defer f.Close() //关闭上传文件
+	ext := path.Ext(h.Filename)
+	dateDir := time.Now().Format("20060102")
+	uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
+	err = os.MkdirAll(uploadDir, 777)
+	if err != nil {
+		return
+	}
+	randStr := utils.GetRandStringNoSpecialChar(28)
+	fileName := randStr + ext
+	fpath := uploadDir + "/" + fileName
+	
+	err = this.SaveToFile("file", fpath)
+	if err != nil {
+		return
+	}
+
+	defer func() {
+		os.Remove(fpath)
+	}()
+
+	// 上传文件
+	resourceUrl := ``
+	ossClient := services.NewOssClient()
+	if ossClient == nil {
+		br.Msg = "上传失败"
+		br.ErrMsg = "初始化OSS服务失败"
+		return
+	}
+	// 上传到阿里云
+	ossDir := utils.UploadDir + "images/"
+	savePath := ossDir + time.Now().Format("200601/20060102/") + fileName
+	resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
+	if err != nil {
+		br.Msg = "文件上传失败"
+		br.ErrMsg = "文件上传失败,Err:" + err.Error()
+		return
+	}
+
+	item := new(models.Resource)
+	item.ResourceUrl = resourceUrl
+	item.ResourceType = 1
+	item.CreateTime = time.Now()
+	newId, err := models.AddResource(item)
+	if err != nil {
+		return
+	}
+	resp := new(models.ResourceResp)
+	resp.Id = newId
+	resp.ResourceUrl = resourceUrl
+	br.Msg = "上传成功"
+	br.Ret = 200
+	br.Success = true
+	br.Data = resp
+}

+ 45 - 0
models/resource.go

@@ -0,0 +1,45 @@
+package models
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+type Resource struct {
+	Id           int       `orm:"column(id);" description:"资源id"`
+	ResourceUrl  string    `description:"资源地址"`
+	CreateTime   time.Time `description:"创建时间"`
+	ResourceType int       `description:"资源类型,1:图片,2:音频,3:视频 ,4:ppt"`
+}
+
+type ResourceResp struct {
+	Id           int64  `orm:"column(id);" description:"用户id"`
+	ResourceUrl  string `description:"资源地址"`
+	PlaySeconds  uint32 `description:"播放时长,单位秒"`
+	Source       string
+	CacheKey     string
+	ResourceName string `description:"资源名称"`
+}
+
+func AddResource(item *Resource) (newId int64, err error) {
+	o := orm.NewOrm()
+	newId, err = o.Insert(item)
+	return
+}
+
+func GetResourceById(id string) (item *Resource, err error) {
+	o := orm.NewOrm()
+	sql := "SELECT * FROM resource WHERE id=? "
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}
+
+type ResourceBase64Resp struct {
+	Image string `description:"图片,base64字符串"`
+}
+
+type PptResourceResp struct {
+	Id          int64    `orm:"column(id);" description:"用户id"`
+	ResourceUrl []string `description:"资源地址"`
+	PlaySeconds uint32   `description:"播放时长,单位秒"`
+}

+ 9 - 0
routers/commentsRouter.go

@@ -979,6 +979,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ReportController"] = append(beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ReportController"],
+        beego.ControllerComments{
+            Method: "UploadImg",
+            Router: `/uploadImg`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ResourceController"] = append(beego.GlobalControllerRouter["eta/eta_forum_admin/controllers:ResourceController"],
         beego.ControllerComments{
             Method: "ImageUpload",

+ 5 - 0
routers/router.go

@@ -96,6 +96,11 @@ func init() {
 				&controllers.CompanySellerController{},
 			),
 		),
+		web.NSNamespace("/report",
+			web.NSInclude(
+				&controllers.ReportController{},
+			),
+		),
 	)
 	web.AddNamespace(ns)
 }