image.go 8.0 KB

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