Browse Source

Merge branch 'hotfix/doc_add_attachment' of eta_forum/eta_forum_admin into master

xyxie 1 month ago
parent
commit
21fdeb519f
7 changed files with 180 additions and 1 deletions
  1. 29 0
      controllers/base_auth.go
  2. 90 0
      controllers/report.go
  3. 1 0
      models/db.go
  4. 45 0
      models/resource.go
  5. 9 0
      routers/commentsRouter.go
  6. 5 0
      routers/router.go
  7. 1 1
      utils/config.go

+ 29 - 0
controllers/base_auth.go

@@ -444,3 +444,32 @@ func (c *BaseAuthController) logUri(respContent []byte, requestBody, ip string)
 	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
 	return
 }
+
+func (c *BaseAuthController) ServeJSONOther(encoding ...bool) {
+	urlPath := c.Ctx.Request.URL.Path
+	// 方法处理完后,需要后置处理的业务逻辑
+	if handlerList, ok := AfterHandlerUrlMap[urlPath]; ok {
+		for _, handler := range handlerList {
+			handler(c.Ctx.Input.RequestBody)
+		}
+	}
+
+	var (
+		hasIndent   = false
+		hasEncoding = false
+	)
+	if web.BConfig.RunMode == web.PROD {
+		hasIndent = false
+	}
+	if len(encoding) > 0 && encoding[0] == true {
+		hasEncoding = true
+	}
+	if c.Data["json"] == nil {
+		//go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
+		body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
+		go alarm_msg.SendAlarmMsg(body, 1)
+		return
+	}
+	
+	c.JSON(c.Data["json"], hasIndent, hasEncoding)
+}

+ 90 - 0
controllers/report.go

@@ -0,0 +1,90 @@
+package controllers
+
+import (
+	"eta/eta_forum_admin/models"
+	"eta/eta_forum_admin/services"
+	"eta/eta_forum_admin/utils"
+	"fmt"
+	"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()
+	var err error
+	defer func() {
+		if err != nil {
+			utils.FileLog.Info("UploadImg", "err", err)
+		}
+	}()
+
+	f, h, err := this.GetFile("file")
+	if err != nil {
+		err = fmt.Errorf("获取文件失败,Err:%v", err)
+		return
+	}
+	defer f.Close() //关闭上传文件
+	ext := path.Ext(h.Filename)
+	dateDir := time.Now().Format("20060102")
+	uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
+	err = os.MkdirAll(uploadDir, utils.DIR_MOD)
+	if err != nil {
+		err = fmt.Errorf("创建文件夹失败,Err:%v", err)
+		return
+	}
+	randStr := utils.GetRandStringNoSpecialChar(28)
+	fileName := randStr + ext
+	fpath := uploadDir + "/" + fileName
+
+	err = this.SaveToFile("file", fpath)
+	if err != nil {
+		err = fmt.Errorf("保存文件失败,Err:%v", err)
+		return
+	}
+
+	defer func() {
+		os.Remove(fpath)
+	}()
+
+	// 上传文件
+	resourceUrl := ``
+	ossClient := services.NewOssClient()
+	if ossClient == nil {
+		err = fmt.Errorf("初始化OSS服务失败")
+		return
+	}
+	// 上传到阿里云
+	ossDir := utils.UploadDir + "images/"
+	savePath := ossDir + time.Now().Format("200601/20060102/") + fileName
+	resourceUrl, err = ossClient.UploadFile(fileName, fpath, savePath)
+	if err != nil {
+		err = fmt.Errorf("文件上传失败,Err:%v", err)
+		return
+	}
+
+	item := new(models.Resource)
+	item.ResourceUrl = resourceUrl
+	item.ResourceType = 1
+	item.CreateTime = time.Now()
+	newId, err := models.AddResource(item)
+	if err != nil {
+		err = fmt.Errorf("保存资源失败,Err:%v", err)
+		return
+	}
+	resp := new(models.ResourceResp)
+	resp.Id = newId
+	resp.ResourceUrl = resourceUrl
+
+	this.Data["json"] = map[string]string{"link": resourceUrl}
+	this.ServeJSONOther()
+}

+ 1 - 0
models/db.go

@@ -116,6 +116,7 @@ func initHelpDoc() {
 	orm.RegisterModel(
 		new(help_doc.HelpDocClassify), //分类
 		new(help_doc.HelpDoc),         //文章
+		new(Resource),        //文件
 	)
 }
 

+ 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)
 }

+ 1 - 1
utils/config.go

@@ -219,7 +219,7 @@ func init() {
 	STATIC_DIR = config["static_dir"]
 	if STATIC_DIR == "" {
 		// 静态文件目录
-		STATIC_DIR = "static"
+		STATIC_DIR = "static/"
 	}
 
 	{