popular.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "go-common/app/admin/main/feed/model/common"
  8. "go-common/app/admin/main/feed/model/show"
  9. "go-common/app/admin/main/feed/util"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. )
  14. func eventTopicList(c *bm.Context) {
  15. var (
  16. err error
  17. pager *show.EventTopicPager
  18. )
  19. res := map[string]interface{}{}
  20. req := &show.EventTopicLP{}
  21. if err = c.Bind(req); err != nil {
  22. return
  23. }
  24. if pager, err = popularSvc.EventTopicList(req); err != nil {
  25. res["message"] = "列表获取失败 " + err.Error()
  26. c.JSONMap(res, ecode.RequestErr)
  27. return
  28. }
  29. c.JSON(pager, nil)
  30. }
  31. func addEventTopic(c *bm.Context) {
  32. var (
  33. err error
  34. //title string
  35. )
  36. res := map[string]interface{}{}
  37. req := &show.EventTopicAP{}
  38. if err = c.Bind(req); err != nil {
  39. return
  40. }
  41. uid, name := util.UserInfo(c)
  42. if name == "" {
  43. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  44. c.Abort()
  45. return
  46. }
  47. req.Person = name
  48. if err = popularSvc.AddEventTopic(c, req, name, uid); err != nil {
  49. res["message"] = "卡片创建失败 " + err.Error()
  50. c.JSONMap(res, ecode.RequestErr)
  51. return
  52. }
  53. c.JSON(nil, nil)
  54. }
  55. func upEventTopic(c *bm.Context) {
  56. var (
  57. err error
  58. )
  59. res := map[string]interface{}{}
  60. req := &show.EventTopicUP{}
  61. if err = c.Bind(req); err != nil {
  62. return
  63. }
  64. uid, name := util.UserInfo(c)
  65. if name == "" {
  66. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  67. c.Abort()
  68. return
  69. }
  70. if req.ID <= 0 {
  71. c.JSONMap(map[string]interface{}{"message": "ID 参数不合法"}, ecode.RequestErr)
  72. c.Abort()
  73. return
  74. }
  75. if err = popularSvc.UpdateEventTopic(c, req, name, uid); err != nil {
  76. res["message"] = "卡片创建失败 " + err.Error()
  77. c.JSONMap(res, ecode.RequestErr)
  78. return
  79. }
  80. c.JSON(nil, nil)
  81. }
  82. func delEventTopic(c *bm.Context) {
  83. var (
  84. err error
  85. )
  86. res := map[string]interface{}{}
  87. req := &struct {
  88. ID int64 `form:"id" validate:"required"`
  89. }{}
  90. if err = c.Bind(req); err != nil {
  91. return
  92. }
  93. uid, name := util.UserInfo(c)
  94. if name == "" {
  95. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  96. c.Abort()
  97. return
  98. }
  99. if req.ID <= 0 {
  100. c.JSONMap(map[string]interface{}{"message": "ID 参数不合法"}, ecode.RequestErr)
  101. c.Abort()
  102. return
  103. }
  104. if err = popularSvc.DeleteEventTopic(req.ID, name, uid); err != nil {
  105. res["message"] = "卡片创建失败 " + err.Error()
  106. c.JSONMap(res, ecode.RequestErr)
  107. return
  108. }
  109. c.JSON(nil, nil)
  110. }
  111. func popularStarsList(c *bm.Context) {
  112. var (
  113. err error
  114. pager *show.PopularStarsPager
  115. )
  116. res := map[string]interface{}{}
  117. req := &show.PopularStarsLP{}
  118. if err = c.Bind(req); err != nil {
  119. return
  120. }
  121. if pager, err = popularSvc.PopularStarsList(req); err != nil {
  122. res["message"] = "列表获取失败 " + err.Error()
  123. c.JSONMap(res, ecode.RequestErr)
  124. return
  125. }
  126. c.JSON(pager, nil)
  127. }
  128. //ValidateUpid .
  129. func ValidateUpid(c context.Context, idStr string) (name string, err error) {
  130. var (
  131. id int64
  132. )
  133. if id, err = strconv.ParseInt(idStr, 10, 64); err != nil {
  134. return
  135. }
  136. if name, err = commonSvc.CardPreview(c, common.CardUp, id); err != nil {
  137. return
  138. }
  139. return
  140. }
  141. //ValidateAvid .
  142. func ValidateAvid(c context.Context, values string) (err error) {
  143. type Content struct {
  144. ID int64 `json:"id"`
  145. Title string `json:"title"`
  146. }
  147. var contents []*Content
  148. if err = json.Unmarshal([]byte(values), &contents); err != nil {
  149. return
  150. }
  151. dup := make(map[int64]bool)
  152. for _, v := range contents {
  153. if v == nil {
  154. return fmt.Errorf("不能提交空数据!")
  155. }
  156. if dup[v.ID] {
  157. return fmt.Errorf("重复视频ID (%d)", v.ID)
  158. }
  159. dup[v.ID] = true
  160. if _, err = commonSvc.CardPreview(c, common.CardAv, v.ID); err != nil {
  161. return
  162. }
  163. }
  164. if len(contents) < 3 {
  165. return fmt.Errorf("视频组成最少三个")
  166. }
  167. if len(contents) > 5 {
  168. return fmt.Errorf("视频组成不能超过5个")
  169. }
  170. return
  171. }
  172. func addPopularStars(c *bm.Context) {
  173. var (
  174. err error
  175. upName string
  176. )
  177. res := map[string]interface{}{}
  178. req := &show.PopularStarsAP{}
  179. if err = c.Bind(req); err != nil {
  180. return
  181. }
  182. uid, name := util.UserInfo(c)
  183. if name == "" {
  184. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  185. c.Abort()
  186. return
  187. }
  188. if err = ValidateAvid(c, req.Content); err != nil {
  189. c.JSONMap(map[string]interface{}{"message": "视频ID 校验失败:" + err.Error()}, ecode.RequestErr)
  190. c.Abort()
  191. return
  192. }
  193. req.Value = util.TrimStrSpace(req.Value)
  194. if upName, err = ValidateUpid(c, req.Value); err != nil {
  195. c.JSONMap(map[string]interface{}{"message": "up主ID 校验失败:" + err.Error()}, ecode.RequestErr)
  196. c.Abort()
  197. return
  198. }
  199. req.Person = name
  200. req.UID = uid
  201. req.LongTitle = upName
  202. if err = popularSvc.AddPopularStars(c, req, name, uid); err != nil {
  203. res["message"] = "卡片创建失败 " + err.Error()
  204. c.JSONMap(res, ecode.RequestErr)
  205. return
  206. }
  207. c.JSON(nil, nil)
  208. }
  209. func updatePopularStars(c *bm.Context) {
  210. var (
  211. err error
  212. upName string
  213. )
  214. res := map[string]interface{}{}
  215. req := &show.PopularStarsUP{}
  216. if err = c.Bind(req); err != nil {
  217. return
  218. }
  219. uid, name := util.UserInfo(c)
  220. if name == "" {
  221. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  222. c.Abort()
  223. return
  224. }
  225. if req.ID <= 0 {
  226. c.JSONMap(map[string]interface{}{"message": "ID 参数不合法"}, ecode.RequestErr)
  227. c.Abort()
  228. return
  229. }
  230. if err = ValidateAvid(c, req.Content); err != nil {
  231. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  232. c.Abort()
  233. return
  234. }
  235. req.Value = util.TrimStrSpace(req.Value)
  236. if upName, err = ValidateUpid(c, req.Value); err != nil {
  237. c.JSONMap(map[string]interface{}{"message": err.Error()}, ecode.RequestErr)
  238. c.Abort()
  239. return
  240. }
  241. req.LongTitle = upName
  242. if err = popularSvc.UpdatePopularStars(c, req, name, uid); err != nil {
  243. res["message"] = "卡片创建失败 " + err.Error()
  244. c.JSONMap(res, ecode.RequestErr)
  245. return
  246. }
  247. c.JSON(nil, nil)
  248. }
  249. func deletePopularStars(c *bm.Context) {
  250. var (
  251. err error
  252. )
  253. res := map[string]interface{}{}
  254. req := &struct {
  255. ID int64 `form:"id" validate:"required"`
  256. }{}
  257. if err = c.Bind(req); err != nil {
  258. return
  259. }
  260. uid, name := util.UserInfo(c)
  261. if name == "" {
  262. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  263. c.Abort()
  264. return
  265. }
  266. if req.ID <= 0 {
  267. c.JSONMap(map[string]interface{}{"message": "ID 参数不合法"}, ecode.RequestErr)
  268. c.Abort()
  269. return
  270. }
  271. if err = popularSvc.DeletePopularStars(req.ID, name, uid); err != nil {
  272. res["message"] = "卡片创建失败 " + err.Error()
  273. c.JSONMap(res, ecode.RequestErr)
  274. return
  275. }
  276. c.JSON(nil, nil)
  277. }
  278. func rejectPopularStars(c *bm.Context) {
  279. var (
  280. err error
  281. )
  282. res := map[string]interface{}{}
  283. req := &struct {
  284. ID int64 `form:"id" validate:"required"`
  285. }{}
  286. if err = c.Bind(req); err != nil {
  287. return
  288. }
  289. uid, name := util.UserInfo(c)
  290. if name == "" {
  291. c.JSONMap(map[string]interface{}{"message": "请重新登录"}, ecode.Unauthorized)
  292. c.Abort()
  293. return
  294. }
  295. if req.ID <= 0 {
  296. c.JSONMap(map[string]interface{}{"message": "ID 参数不合法"}, ecode.RequestErr)
  297. c.Abort()
  298. return
  299. }
  300. if err = popularSvc.RejectPopularStars(req.ID, name, uid); err != nil {
  301. res["message"] = "卡片创建失败 " + err.Error()
  302. c.JSONMap(res, ecode.RequestErr)
  303. return
  304. }
  305. c.JSON(nil, nil)
  306. }
  307. func aiAddPopularStars(c *bm.Context) {
  308. var (
  309. err error
  310. aids []byte
  311. addPopStar []*show.PopularStarsAP
  312. )
  313. res := map[string]interface{}{}
  314. req := &struct {
  315. Data string `form:"data" validate:"required"`
  316. }{}
  317. if err = c.Bind(req); err != nil {
  318. return
  319. }
  320. log.Info("aiAddPopularStars value(%v)", req.Data)
  321. values := make([]*show.PopularStarsAIAP, 0)
  322. if err = json.Unmarshal([]byte(req.Data), &values); err != nil {
  323. log.Error("aiAddPopularStars.Unmarshal value(%v) error(%v)", req.Data, err)
  324. res["message"] = "数据解析失败 " + err.Error()
  325. c.JSONMap(res, ecode.RequestErr)
  326. return
  327. }
  328. for _, v := range values {
  329. var (
  330. AiValues []*show.AiValue
  331. upName string
  332. )
  333. for _, aid := range v.Aids {
  334. aiValue := &show.AiValue{
  335. ID: aid,
  336. }
  337. AiValues = append(AiValues, aiValue)
  338. }
  339. if aids, err = json.Marshal(AiValues); err != nil {
  340. log.Error("aiAddPopularStars.Marshal value(%v) error(%v)", v.Aids, err)
  341. res["message"] = "数据encode失败 " + err.Error()
  342. c.JSONMap(res, ecode.RequestErr)
  343. }
  344. mid := strconv.FormatInt(v.Mid, 10)
  345. if upName, err = ValidateUpid(c, mid); err != nil {
  346. c.JSONMap(map[string]interface{}{"message": "up主ID 校验失败:" + err.Error()}, ecode.RequestErr)
  347. c.Abort()
  348. return
  349. }
  350. tmp := &show.PopularStarsAP{
  351. LongTitle: upName,
  352. Value: strconv.FormatInt(v.Mid, 10),
  353. Content: string(aids),
  354. }
  355. addPopStar = append(addPopStar, tmp)
  356. }
  357. if err = popularSvc.AIAddPopularStars(c, addPopStar); err != nil {
  358. res["message"] = "卡片创建失败 " + err.Error()
  359. c.JSONMap(res, ecode.RequestErr)
  360. return
  361. }
  362. c.JSON(nil, nil)
  363. }