banner.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/services"
  8. "hongze/hz_crm_api/utils"
  9. "os"
  10. "path"
  11. "time"
  12. )
  13. // banner
  14. type BannerController struct {
  15. BaseAuthController
  16. }
  17. // @Title 新增banner接口
  18. // @Description 新增banner
  19. // @Param request body models.BannerAddReq true "type json string"
  20. // @Success 200 新增成功
  21. // @router /add [post]
  22. func (this *BannerController) Add() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.BannerAddReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. item := new(models.Banner)
  36. item.ClassifyId = req.ClassifyId
  37. item.ImageUrl = req.ImgUrl
  38. item.BannerType = req.BannerType
  39. item.CreateTime = time.Now()
  40. item.ModifyTime = time.Now()
  41. item.ClassifyName = req.ClassifyName
  42. item.JumpUrl = req.JumpUrl
  43. err = models.AddBanner(item)
  44. if err != nil {
  45. br.Msg = "新增失败."
  46. br.ErrMsg = "新增失败,Err:" + err.Error()
  47. return
  48. }
  49. br.Ret = 200
  50. br.Success = true
  51. br.Msg = "新增成功"
  52. }
  53. // @Title 编辑banner接口
  54. // @Description 编辑banner
  55. // @Param request body models.BannerEditReq true "type json string"
  56. // @Success 200 新增成功
  57. // @router /edit [post]
  58. func (this *BannerController) Edit() {
  59. br := new(models.BaseResponse).Init()
  60. defer func() {
  61. this.Data["json"] = br
  62. this.ServeJSON()
  63. }()
  64. var req models.BannerEditReq
  65. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  66. if err != nil {
  67. br.Msg = "参数解析异常!"
  68. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  69. return
  70. }
  71. err = models.EditBanner(&req)
  72. if err != nil {
  73. br.Msg = "编辑失败"
  74. br.ErrMsg = "编辑失败,Err:" + err.Error()
  75. return
  76. }
  77. br.Ret = 200
  78. br.Success = true
  79. br.Msg = "编辑成功"
  80. }
  81. // @Title 删除轮播图
  82. // @Description 删除轮播图
  83. // @Param request body models.BannerDeleteReq true "type json string"
  84. // @Success 200 新增成功
  85. // @router /delete [post]
  86. func (this *BannerController) Delete() {
  87. br := new(models.BaseResponse).Init()
  88. defer func() {
  89. this.Data["json"] = br
  90. this.ServeJSON()
  91. }()
  92. var req models.BannerDeleteReq
  93. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  94. if err != nil {
  95. br.Msg = "参数解析异常!"
  96. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  97. return
  98. }
  99. err = models.DeleteBanner(req.BannerId)
  100. if err != nil {
  101. br.Msg = "删除失败"
  102. br.ErrMsg = "删除失败,Err:" + err.Error()
  103. return
  104. }
  105. br.Ret = 200
  106. br.Success = true
  107. br.Msg = "删除成功"
  108. }
  109. // @Title 获取分类接口
  110. // @Description 获取分类
  111. // @Success 200 {object} models.Classify
  112. // @router /getClassify [get]
  113. func (this *BannerController) GetClassify() {
  114. br := new(models.BaseResponse).Init()
  115. defer func() {
  116. this.Data["json"] = br
  117. this.ServeJSON()
  118. }()
  119. items, err := models.GetClassify()
  120. if err != nil {
  121. br.Msg = "获取失败"
  122. br.ErrMsg = "获取失败,Err:" + err.Error()
  123. return
  124. }
  125. br.Ret = 200
  126. br.Success = true
  127. br.Msg = "获取成功"
  128. br.Data = items
  129. }
  130. // @Title 获取分类接口
  131. // @Description 获取分类
  132. // @Param PageSize query int true "每页数据条数"
  133. // @Param CurrentIndex query int true "当前页页码,从1开始"
  134. // @Param BannerType query int true "分类:1:轮播图,2:头部海报"
  135. // @Param KeyWord query string true "检索关键词"
  136. // @Success 200 {object} models.BannerListResp
  137. // @router /list [get]
  138. func (this *BannerController) ListBanner() {
  139. br := new(models.BaseResponse).Init()
  140. defer func() {
  141. this.Data["json"] = br
  142. this.ServeJSON()
  143. }()
  144. pageSize, _ := this.GetInt("PageSize")
  145. currentIndex, _ := this.GetInt("CurrentIndex")
  146. bannerType, _ := this.GetInt("BannerType")
  147. keyWord := this.GetString("KeyWord")
  148. var startSize int
  149. if pageSize <= 0 {
  150. pageSize = utils.PageSize20
  151. }
  152. if currentIndex <= 0 {
  153. currentIndex = 1
  154. }
  155. startSize = utils.StartIndex(currentIndex, pageSize)
  156. var condition string
  157. var pars []interface{}
  158. if bannerType > 0 {
  159. condition = " AND banner_type=? "
  160. pars = append(pars, bannerType)
  161. }
  162. if keyWord != "" {
  163. condition += ` AND classify_name LIKE '%` + keyWord + `%' `
  164. }
  165. total, err := models.GetBannerListCount(condition, pars)
  166. if err != nil {
  167. br.Msg = "获取失败"
  168. br.ErrMsg = "获取失败,Err:" + err.Error()
  169. return
  170. }
  171. list, err := models.GetBannerList(condition, pars, startSize, pageSize)
  172. if err != nil {
  173. br.Msg = "获取失败"
  174. br.ErrMsg = "获取失败,Err:" + err.Error()
  175. return
  176. }
  177. page := paging.GetPaging(currentIndex, pageSize, total)
  178. resp := new(models.BannerListResp)
  179. resp.Paging = page
  180. resp.List = list
  181. br.Ret = 200
  182. br.Success = true
  183. br.Msg = "获取成功"
  184. br.Data = resp
  185. }
  186. // @Title 图片上传
  187. // @Description 图片上传接口
  188. // @Param File query file true "文件"
  189. // @Success 200 新增成功
  190. // @router /upload [post]
  191. func (this *BannerController) Upload() {
  192. br := new(models.BaseResponse).Init()
  193. defer func() {
  194. this.Data["json"] = br
  195. this.ServeJSON()
  196. }()
  197. f, h, err := this.GetFile("file")
  198. if err != nil {
  199. br.Msg = "获取资源信息失败"
  200. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  201. return
  202. }
  203. ext := path.Ext(h.Filename)
  204. uploadDir := "static/images/"
  205. if !utils.FileIsExist(uploadDir) {
  206. err = os.MkdirAll(uploadDir, 777)
  207. if err != nil {
  208. br.Msg = "存储目录创建失败"
  209. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  210. return
  211. }
  212. }
  213. fileName := fmt.Sprintf("%s%s", utils.GetRandStringNoSpecialChar(28), ext)
  214. savePath := uploadDir + "/" + fileName
  215. defer func() {
  216. _ = f.Close()
  217. }()
  218. err = this.SaveToFile("file", savePath)
  219. if err != nil {
  220. br.Msg = "文件上传失败"
  221. br.ErrMsg = "SaveToFile, Err:" + err.Error()
  222. return
  223. }
  224. hzUploadDir := "static/images/"
  225. saveToOssPath := fmt.Sprintf("%s%s%s", hzUploadDir, time.Now().Format("200601/20060102/"), fileName)
  226. err = services.UploadFileToAliyun("", savePath, saveToOssPath)
  227. if err != nil {
  228. br.Msg = "文件上传失败"
  229. br.ErrMsg = "UploadFileToAliyun, Err:" + err.Error()
  230. return
  231. }
  232. resourceUrl := fmt.Sprintf("%s%s", "https://hzstatic.hzinsights.com/", saveToOssPath)
  233. defer func() {
  234. _ = os.Remove(savePath)
  235. }()
  236. item := new(models.Resource)
  237. item.ResourceUrl = resourceUrl
  238. item.ResourceType = 1
  239. item.CreateTime = time.Now()
  240. newId, err := models.AddResource(item)
  241. if err != nil {
  242. br.Msg = "资源上传失败"
  243. br.ErrMsg = "资源上传失败,Err:" + err.Error()
  244. return
  245. }
  246. resp := new(models.ResourceResp)
  247. resp.Id = newId
  248. resp.ResourceUrl = resourceUrl
  249. br.Msg = "上传成功"
  250. br.Ret = 200
  251. br.Success = true
  252. br.Data = resp
  253. return
  254. }
  255. // @Title
  256. // @Description banner统计
  257. // @Param PageSize query int true "每页数据条数"
  258. // @Param CurrentIndex query int true "当前页页码,从1开始"
  259. // @Param BannerType query int true "分类:1:轮播图,2:头部海报"
  260. // @Param KeyWord query string true "检索关键词"
  261. // @Success 200 {object} models.BannerListResp
  262. // @router /statistic [get]
  263. func (this *BannerController) BannerStatistic() {
  264. br := new(models.BaseResponse).Init()
  265. defer func() {
  266. this.Data["json"] = br
  267. this.ServeJSON()
  268. }()
  269. urlList, err := models.GetBannerUrlList()
  270. if err != nil {
  271. br.Msg = "获取失败"
  272. br.ErrMsg = "获取失败,Err:" + err.Error()
  273. return
  274. }
  275. list, err := models.GetBannerHistoryList()
  276. if err != nil {
  277. br.Msg = "获取失败"
  278. br.ErrMsg = "获取失败,Err:" + err.Error()
  279. return
  280. }
  281. //resp := new(models.BannerHistoryListResp)
  282. souceMap := make(map[string][]*models.SourcePv)
  283. for _, v := range list {
  284. item := &models.SourcePv{
  285. FirstSource: v.FirstSource,
  286. SecondSource: v.SecondSource,
  287. Pv: v.Pv,
  288. LastUpdatedTime: v.LastUpdatedTime,
  289. }
  290. souceMap[v.BannerUrl] = append(souceMap[v.BannerUrl], item)
  291. }
  292. for _, v := range urlList {
  293. v.SourceList = souceMap[v.BannerUrl]
  294. }
  295. br.Ret = 200
  296. br.Success = true
  297. br.Msg = "获取成功"
  298. br.Data = urlList
  299. }