Просмотр исходного кода

优化路由文件、图片上传

hsun 1 год назад
Родитель
Сommit
3084a8f4ae
2 измененных файлов с 25 добавлено и 121 удалено
  1. 25 17
      controllers/banner.go
  2. 0 104
      routers/router.go

+ 25 - 17
controllers/banner.go

@@ -2,6 +2,7 @@ package controllers
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
+	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"hongze/hz_crm_api/models"
 	"hongze/hz_crm_api/models"
 	"hongze/hz_crm_api/services"
 	"hongze/hz_crm_api/services"
@@ -211,34 +212,41 @@ func (this *BannerController) Upload() {
 		return
 		return
 	}
 	}
 	ext := path.Ext(h.Filename)
 	ext := path.Ext(h.Filename)
-	dateDir := time.Now().Format("20060102")
-	uploadDir := utils.STATIC_DIR + "hongze/" + dateDir
-	err = os.MkdirAll(uploadDir, 777)
-	if err != nil {
-		br.Msg = "存储目录创建失败"
-		br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
-		return
+
+	uploadDir := "static/images/"
+	if !utils.FileIsExist(uploadDir) {
+		err = os.MkdirAll(uploadDir, 777)
+		if err != nil {
+			br.Msg = "存储目录创建失败"
+			br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
+			return
+		}
 	}
 	}
-	randStr := utils.GetRandStringNoSpecialChar(28)
-	fileName := randStr + ext
-	fpath := uploadDir + "/" + fileName
-	defer f.Close() //关闭上传文件
-	err = this.SaveToFile("file", fpath)
+
+	fileName := fmt.Sprintf("%s%s", utils.GetRandStringNoSpecialChar(28), ext)
+	savePath := uploadDir + "/" + fileName
+	defer func() {
+		_ = f.Close()
+	}()
+	err = this.SaveToFile("file", savePath)
 	if err != nil {
 	if err != nil {
 		br.Msg = "文件上传失败"
 		br.Msg = "文件上传失败"
-		br.ErrMsg = "文件上传失败,Err:" + err.Error()
+		br.ErrMsg = "SaveToFile, Err:" + err.Error()
 		return
 		return
 	}
 	}
-	//上传到阿里云
-	resourceUrl, err := services.UploadAliyun(fileName, fpath)
+
+	hzUploadDir := "static/images/"
+	saveToOssPath := fmt.Sprintf("%s%s%s", hzUploadDir, time.Now().Format("200601/20060102/"), fileName)
+	err = services.UploadFileToAliyun("", savePath, saveToOssPath)
 	if err != nil {
 	if err != nil {
 		br.Msg = "文件上传失败"
 		br.Msg = "文件上传失败"
-		br.ErrMsg = "文件上传失败,Err:" + err.Error()
+		br.ErrMsg = "UploadFileToAliyun, Err:" + err.Error()
 		return
 		return
 	}
 	}
+	resourceUrl := fmt.Sprintf("%s%s", "https://hzstatic.hzinsights.com/", saveToOssPath)
 
 
 	defer func() {
 	defer func() {
-		os.Remove(fpath)
+		_ = os.Remove(savePath)
 	}()
 	}()
 
 
 	item := new(models.Resource)
 	item := new(models.Resource)

+ 0 - 104
routers/router.go

@@ -42,8 +42,6 @@ func init() {
 		web.NSNamespace("/sysuser",
 		web.NSNamespace("/sysuser",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.SysUserController{},
 				&controllers.SysUserController{},
-			),
-			web.NSInclude(
 				&controllers.SysUserAuthController{},
 				&controllers.SysUserAuthController{},
 			),
 			),
 		),
 		),
@@ -60,97 +58,32 @@ func init() {
 		web.NSNamespace("/report",
 		web.NSNamespace("/report",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.ReportController{},
 				&controllers.ReportController{},
-				//&controllers.ReportChapterTypeController{},
-			),
-			web.NSInclude(
-			//&controllers.ReportCommonController{},
-			),
-			web.NSInclude(
-			//&controllers.ReportUploadCommonController{},
 			),
 			),
 		),
 		),
 		web.NSNamespace("/statistic_report",
 		web.NSNamespace("/statistic_report",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.StatisticReportController{},
 				&controllers.StatisticReportController{},
 				&controllers.StatisticYbLogController{},
 				&controllers.StatisticYbLogController{},
-			),
-			web.NSInclude(
 				&controllers.StatisticReportCommonController{},
 				&controllers.StatisticReportCommonController{},
 			),
 			),
 		),
 		),
 		web.NSNamespace("/voice",
 		web.NSNamespace("/voice",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.VoiceController{},
 				&controllers.VoiceController{},
-			),
-			web.NSInclude(
 				&controllers.VoiceCommonController{},
 				&controllers.VoiceCommonController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/ppt",
-		//	web.NSInclude(
-		//		&controllers.PptController{},
-		//	),
-		//	web.NSInclude(
-		//		&controllers.PptCommonController{},
-		//	),
-		//),
-		//web.NSNamespace("/pptv2",
-		//	web.NSInclude(
-		//		&controllers.PptV2Controller{},
-		//		&controllers.PptV2GroupController{},
-		//	),
-		//	web.NSInclude(
-		//		&controllers.PptV2CommonController{},
-		//	),
-		//),
-		//web.NSNamespace("/ppt_english",
-		//	web.NSInclude(
-		//		&controllers.PptEnglishController{},
-		//		&controllers.PptEnglishGroupController{},
-		//	),
-		//	web.NSInclude(
-		//		&controllers.PptEnglishCommonController{},
-		//	),
-		//),
-		//web.NSNamespace("/entry",
-		//	web.NSInclude(
-		//		&controllers.TargetController{},
-		//	),
-		//	web.NSInclude(
-		//		&controllers.TargetCommonController{},
-		//	),
-		//	web.NSInclude(
-		//		&controllers.TradeCommonController{},
-		//	),
-		//),
-
 		web.NSNamespace("/system",
 		web.NSNamespace("/system",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.SysDepartmentController{},
 				&controllers.SysDepartmentController{},
-			),
-			web.NSInclude(
 				&controllers.SysGroupController{},
 				&controllers.SysGroupController{},
-			),
-			web.NSInclude(
 				&controllers.SysTeamController{},
 				&controllers.SysTeamController{},
-			),
-			web.NSInclude(
 				&controllers.SysAdminController{},
 				&controllers.SysAdminController{},
-			),
-			web.NSInclude(
 				&controllers.SysRoleController{},
 				&controllers.SysRoleController{},
 				&controllers.SysRoleAdminController{},
 				&controllers.SysRoleAdminController{},
-			),
-			web.NSInclude(
 				&controllers.SysMenuController{},
 				&controllers.SysMenuController{},
-			),
-			web.NSInclude(
 				&controllers.SysMenuButtonController{},
 				&controllers.SysMenuButtonController{},
-			),
-			web.NSInclude(
 				&controllers.SysFlowController{},
 				&controllers.SysFlowController{},
-			),
-			web.NSInclude(
 				&controllers.ResearchGroupController{},
 				&controllers.ResearchGroupController{},
 			),
 			),
 		),
 		),
@@ -248,11 +181,6 @@ func init() {
 				&controllers.GroupSendMsgController{},
 				&controllers.GroupSendMsgController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/php_serve",
-		//	web.NSInclude(
-		//		&php_serve.PhpServeController{},
-		//	),
-		//),
 		web.NSNamespace("/my_chart",
 		web.NSNamespace("/my_chart",
 			web.NSInclude(
 			web.NSInclude(
 				&data_manage.MyChartController{},
 				&data_manage.MyChartController{},
@@ -291,11 +219,6 @@ func init() {
 				&roadshow.CalendarController{},
 				&roadshow.CalendarController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/sandbox",
-		//	web.NSInclude(
-		//		&sandbox.SandboxController{},
-		//	),
-		//),
 		web.NSNamespace("/taglib",
 		web.NSNamespace("/taglib",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.VarietyTagController{},
 				&controllers.VarietyTagController{},
@@ -318,14 +241,6 @@ func init() {
 				&english_report.EnglishVideoController{},
 				&english_report.EnglishVideoController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/report_author",
-		//	web.NSInclude(
-		//		&controllers.ReportAuthorCommonController{},
-		//	),
-		//	web.NSInclude(
-		//		&controllers.ReportAuthorController{},
-		//	),
-		//),
 		web.NSNamespace("/future_good",
 		web.NSNamespace("/future_good",
 			web.NSInclude(
 			web.NSInclude(
 				&future_good2.FutureGoodEdbInfoController{},
 				&future_good2.FutureGoodEdbInfoController{},
@@ -351,11 +266,6 @@ func init() {
 				&line_feature.LineFeaturesChartInfoController{},
 				&line_feature.LineFeaturesChartInfoController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/cloud_disk",
-		//	web.NSInclude(
-		//		&controllers.CloudDiskController{},
-		//	),
-		//),
 		web.NSNamespace("/eta_trial",
 		web.NSNamespace("/eta_trial",
 			web.NSInclude(
 			web.NSInclude(
 				&controllers.ETATrialController{},
 				&controllers.ETATrialController{},
@@ -372,15 +282,6 @@ func init() {
 				&controllers.MaycurOpenController{},
 				&controllers.MaycurOpenController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/semantic_analysis",
-		//	web.NSInclude(
-		//		&semantic_analysis.SaLabelController{},
-		//		&semantic_analysis.SaDocClassifyController{},
-		//		&semantic_analysis.SaDocController{},
-		//		&semantic_analysis.SaCompareClassifyController{},
-		//		&semantic_analysis.SaCompareController{},
-		//	),
-		//),
 		web.NSNamespace("/supply_analysis",
 		web.NSNamespace("/supply_analysis",
 			web.NSInclude(
 			web.NSInclude(
 				&supply_analysis.VarietyController{},
 				&supply_analysis.VarietyController{},
@@ -396,11 +297,6 @@ func init() {
 				&english_report.EnPermissionController{},
 				&english_report.EnPermissionController{},
 			),
 			),
 		),
 		),
-		//web.NSNamespace("/english_classify",
-		//	web.NSInclude(
-		//		&english_report.EnglishClassifyController{},
-		//	),
-		//),
 	)
 	)
 	web.AddNamespace(ns)
 	web.AddNamespace(ns)
 }
 }