image.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/services"
  8. "eta/eta_mini_crm_ht/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "os"
  12. "path"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. type ImageController struct {
  18. BaseAuthController
  19. }
  20. // UploadImage @Title 上传图片
  21. // @Description 上传视频
  22. // @Param File query file true "文件"
  23. // @Success 200 {object} models.ReportAuthorResp
  24. // @router /uploadImage [post]
  25. func (this *ImageController) UploadImage() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. f, h, err := this.GetFile("File")
  32. if err != nil {
  33. br.Msg = "获取资源信息失败"
  34. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  35. return
  36. }
  37. defer f.Close()
  38. size, err := strconv.Atoi(utils.UPLOAD_IMG_SIZE)
  39. if err != nil {
  40. size = 100
  41. }
  42. if h.Size > 1024*1024*int64(size) {
  43. br.Msg = fmt.Sprintf("图片大小不能超过%dK", size)
  44. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  45. return
  46. }
  47. ext := path.Ext(h.Filename)
  48. //if ext != ".mp3" {
  49. // br.Msg = "音频格式不正确"
  50. // br.ErrMsg = "音频上传失败,Err:" + err.Error()
  51. // return
  52. //}
  53. dateDir := time.Now().Format("20060102")
  54. uploadDir := utils.STATIC_DIR + "ht/audio" + dateDir
  55. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  56. if err != nil {
  57. br.Msg = "存储目录创建失败"
  58. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  59. return
  60. }
  61. randStr := utils.GetRandStringNoSpecialChar(28)
  62. fileName := randStr + ext
  63. fpath := uploadDir + "/" + fileName
  64. err = this.SaveToFile("File", fpath)
  65. if err != nil {
  66. br.Msg = "图片上传失败"
  67. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  68. return
  69. }
  70. audioUploadDir := utils.RESOURCE_DIR + "img/"
  71. savePdfToOssPath := audioUploadDir + time.Now().Format("200601/20060102/")
  72. audioName := utils.GetRandStringNoSpecialChar(28)
  73. savePdfToOssPath += audioName + ext
  74. defer func() {
  75. err = os.Remove(fpath)
  76. fmt.Sprintf("删除文件失败:%v", err)
  77. }()
  78. ossClient := services.NewOssClient()
  79. if ossClient == nil {
  80. br.Msg = "图片上传失败"
  81. br.ErrMsg = "初始化OSS服务失败"
  82. return
  83. }
  84. mp3Url, err := ossClient.UploadFile("", fpath, savePdfToOssPath)
  85. if err != nil {
  86. br.Msg = "图片上传失败"
  87. br.ErrMsg = "图片上传失败,Err:" + err.Error()
  88. return
  89. }
  90. base := path.Base(h.Filename)
  91. resp := new(response.MediaUploadResp)
  92. resp.Url = mp3Url
  93. resp.FileName = base
  94. br.Data = resp
  95. br.Msg = "上传成功"
  96. br.Ret = 200
  97. br.Success = true
  98. }
  99. // AddImage @Title 新增图片
  100. // @Description 新增图片
  101. // @Param File query file true "文件"
  102. // @router /addImage [post]
  103. func (this *ImageController) AddImage() {
  104. br := new(models.BaseResponse).Init()
  105. defer func() {
  106. this.Data["json"] = br
  107. this.ServeJSON()
  108. }()
  109. var req request.ImageReq
  110. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  111. br.Msg = "参数解析失败"
  112. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  113. return
  114. }
  115. if req.ImageName == "" {
  116. br.Msg = "图片名称为空"
  117. return
  118. }
  119. if req.SrcUrl == "" {
  120. br.Msg = "音频地址为空"
  121. return
  122. }
  123. if req.PermissionId <= 0 {
  124. br.Msg = "品种标签为空"
  125. return
  126. }
  127. var err error
  128. imageInsert := &models.ImageSource{
  129. ImgName: req.ImageName,
  130. SrcUrl: req.SrcUrl,
  131. PermissionId: req.PermissionId,
  132. Deleted: 0,
  133. CreatedTime: time.Now(),
  134. }
  135. _, err = imageInsert.Insert()
  136. if err != nil {
  137. br.Msg = "添加失败"
  138. br.ErrMsg = "图片新增失败,Err:" + err.Error()
  139. return
  140. }
  141. br.Msg = "添加成功"
  142. br.Ret = 200
  143. br.Success = true
  144. }
  145. // EditImage
  146. //
  147. // @Title 编辑音频
  148. //
  149. // @Description 编辑音频
  150. // @Success 200 {object} models.ReportAuthorResp
  151. // @router /editImage [post]
  152. func (this *ImageController) EditImage() {
  153. br := new(models.BaseResponse).Init()
  154. defer func() {
  155. this.Data["json"] = br
  156. this.ServeJSON()
  157. }()
  158. var req request.ImageReq
  159. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  160. br.Msg = "参数解析失败"
  161. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  162. return
  163. }
  164. if req.ImageId <= 0 {
  165. br.Msg = "图片ID为空"
  166. return
  167. }
  168. if req.ImageName == "" {
  169. br.Msg = "图片名称为空"
  170. return
  171. }
  172. if req.SrcUrl == "" {
  173. br.Msg = "图片src为空"
  174. return
  175. }
  176. if req.PermissionId <= 0 {
  177. br.Msg = "品种标签为空"
  178. return
  179. }
  180. var err error
  181. imageUpdate := &models.ImageSource{
  182. Id: req.ImageId,
  183. ImgName: req.ImageName,
  184. SrcUrl: req.SrcUrl,
  185. PermissionId: req.PermissionId,
  186. Deleted: 0,
  187. CreatedTime: time.Now(),
  188. }
  189. err = imageUpdate.Update()
  190. if err != nil {
  191. br.Msg = "更新失败"
  192. br.ErrMsg = "图片更新失败,Err:" + err.Error()
  193. return
  194. }
  195. br.Msg = "编辑成功"
  196. br.Ret = 200
  197. br.Success = true
  198. }
  199. // DeleteImage
  200. // @Title 删除音频
  201. //
  202. // @Description 删除音频
  203. // @Success 200 {object} models.ReportAuthorResp
  204. // @router /deleteImage [post]
  205. func (this *ImageController) DeleteImage() {
  206. br := new(models.BaseResponse).Init()
  207. defer func() {
  208. this.Data["json"] = br
  209. this.ServeJSON()
  210. }()
  211. var req request.ImageReq
  212. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  213. br.Msg = "参数解析失败"
  214. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  215. return
  216. }
  217. if req.ImageId <= 0 {
  218. br.Msg = "图片ID为空"
  219. return
  220. }
  221. var err error
  222. imageDelete := &models.ImageSource{
  223. Id: req.ImageId,
  224. Deleted: 1,
  225. }
  226. err = imageDelete.Delete()
  227. if err != nil {
  228. br.Msg = "删除失败"
  229. br.ErrMsg = "图片删除失败,Err:" + err.Error()
  230. return
  231. }
  232. br.Msg = "删除成功"
  233. br.Ret = 200
  234. br.Success = true
  235. }
  236. // ImageList
  237. // @Title 研报列表
  238. // @Description pdf研报列表
  239. // @Param PageSize query int true "每页数据条数"
  240. // @Param CurrentIndex query int true "当前页页码,从1开始"
  241. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  242. // @Param KeyWord query string true "报告标题/创建人"
  243. // @Param SortType query string true "排序方式"
  244. // @Success 200 {object} models.ReportAuthorResp
  245. // @router /imageList [get]
  246. func (this *ImageController) ImageList() {
  247. br := new(models.BaseResponse).Init()
  248. defer func() {
  249. this.Data["json"] = br
  250. this.ServeJSON()
  251. }()
  252. pageSize, _ := this.GetInt("PageSize")
  253. currentIndex, _ := this.GetInt("CurrentIndex")
  254. permissionIds := this.GetString("PermissionIds")
  255. sortType := this.GetString("SortType")
  256. KeyWord := this.GetString("KeyWord")
  257. var condition string
  258. var pars []interface{}
  259. if pageSize <= 0 {
  260. pageSize = utils.PageSize20
  261. }
  262. if currentIndex <= 0 {
  263. currentIndex = 1
  264. }
  265. if KeyWord != "" {
  266. condition += " AND img_name like '%" + KeyWord + "%'"
  267. }
  268. if permissionIds != "" {
  269. permissionArr := strings.Split(permissionIds, ",")
  270. for _, permissionId := range permissionArr {
  271. perId, _ := strconv.Atoi(permissionId)
  272. pars = append(pars, perId)
  273. }
  274. condition += " AND permission_id in (" + utils.GetOrmReplaceHolder(len(pars)) + ")"
  275. }
  276. sortCondition := " ORDER BY created_time "
  277. if sortType == "" {
  278. sortType = "DESC"
  279. }
  280. sortCondition = sortCondition + sortType
  281. total, err := models.GetImageCountByCondition(condition, pars)
  282. if err != nil {
  283. br.Msg = "获取图片列表失败"
  284. br.ErrMsg = "获取图片列表统计失败,Err:" + err.Error()
  285. return
  286. }
  287. startSize := utils.StartIndex(currentIndex, pageSize)
  288. imageList, err := models.GetImageByCondition(condition, sortCondition, pars, startSize, pageSize)
  289. var results []*models.ImageSourceView
  290. for _, image := range imageList {
  291. var view *models.ImageSourceView
  292. view, err = image.ToView()
  293. if err != nil {
  294. br.Msg = "获取图片列表失败"
  295. br.ErrMsg = "获取图片品种名称失败,Err:" + err.Error()
  296. return
  297. }
  298. results = append(results, view)
  299. }
  300. if err != nil {
  301. br.Msg = "获取图片列表失败"
  302. br.ErrMsg = "获取图片列表失败,Err:" + err.Error()
  303. return
  304. }
  305. page := paging.GetPaging(currentIndex, pageSize, total)
  306. resp := new(response.ImageSourceListResp)
  307. resp.List = results
  308. resp.Paging = page
  309. br.Ret = 200
  310. br.Success = true
  311. br.Data = resp
  312. br.Msg = "获取成功"
  313. }