notice.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/app/admin/main/reply/conf"
  6. "go-common/app/admin/main/reply/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. xtime "go-common/library/time"
  11. )
  12. //panigate notice list
  13. func listNotice(c *bm.Context) {
  14. var (
  15. err error
  16. page = int64(1)
  17. pageSize = int64(conf.Conf.Reply.PageSize)
  18. )
  19. params := c.Request.Form
  20. pageStr := params.Get("page")
  21. pageSizeStr := params.Get("pagesize")
  22. if pageStr != "" {
  23. if page, err = strconv.ParseInt(pageStr, 10, 64); err != nil {
  24. log.Warn("strconv.ParseInt(page:%s) error(%v)", pageStr, err)
  25. c.JSON(nil, ecode.RequestErr)
  26. return
  27. }
  28. if page < 1 {
  29. page = 1
  30. }
  31. }
  32. if pageSizeStr != "" {
  33. if pageSize, err = strconv.ParseInt(pageSizeStr, 10, 64); err != nil || pageSize < 1 {
  34. log.Warn("strconv.ParseInt(pagesize:%s) error(%v)", pageSizeStr, err)
  35. c.JSON(nil, ecode.RequestErr)
  36. return
  37. }
  38. }
  39. data, total, err := rpSvc.ListNotice(c, page, pageSize)
  40. if err != nil {
  41. log.Error("svc.ListNotcie(%d,%d) error(%v)", page, pageSize, err)
  42. c.JSON(nil, err)
  43. return
  44. }
  45. res := map[string]interface{}{}
  46. res["data"] = data
  47. res["pager"] = map[string]interface{}{
  48. "num": page,
  49. "size": pageSize,
  50. "total": total,
  51. }
  52. c.JSONMap(res, nil)
  53. return
  54. }
  55. //panigate notice list
  56. func listNotice2(c *bm.Context) {
  57. var (
  58. err error
  59. page int64 = 1
  60. pageSize = int64(conf.Conf.Reply.PageSize)
  61. )
  62. params := c.Request.Form
  63. pageStr := params.Get("page")
  64. pageSizeStr := params.Get("pagesize")
  65. if pageStr != "" {
  66. if page, err = strconv.ParseInt(pageStr, 10, 64); err != nil {
  67. log.Warn("strconv.ParseInt(page:%s) error(%v)", pageStr, err)
  68. c.JSON(nil, ecode.RequestErr)
  69. return
  70. }
  71. if page < 1 {
  72. page = 1
  73. }
  74. }
  75. if pageSizeStr != "" {
  76. if pageSize, err = strconv.ParseInt(pageSizeStr, 10, 64); err != nil || pageSize < 1 {
  77. log.Warn("strconv.ParseInt(pagesize:%s) error(%v)", pageSizeStr, err)
  78. c.JSON(nil, ecode.RequestErr)
  79. return
  80. }
  81. }
  82. data, total, err := rpSvc.ListNotice(c, page, pageSize)
  83. if err != nil {
  84. log.Error("svc.ListNotcie(%d,%d) error(%v)", page, pageSize, err)
  85. c.JSON(nil, err)
  86. return
  87. }
  88. res := map[string]interface{}{}
  89. res["data"] = data
  90. res["pager"] = model.Pager{Page: page, PageSize: pageSize, Total: total}
  91. c.JSONMap(res, nil)
  92. return
  93. }
  94. //return a notice detail
  95. func getNotice(c *bm.Context) {
  96. var (
  97. err error
  98. id uint64
  99. )
  100. params := c.Request.Form
  101. idStr := params.Get("id")
  102. if idStr == "" {
  103. c.JSON(nil, ecode.RequestErr)
  104. return
  105. }
  106. if id, err = strconv.ParseUint(idStr, 10, 32); err != nil {
  107. log.Warn("strconv.ParseUint(id:%s) error(%v)", idStr, err)
  108. c.JSON(nil, ecode.RequestErr)
  109. return
  110. }
  111. data, err := rpSvc.GetNotice(c, uint32(id))
  112. if err != nil {
  113. log.Error("svc.GetNotice(%d) error(%v)", id, err)
  114. c.JSON(nil, err)
  115. return
  116. }
  117. if data == nil {
  118. c.JSON(nil, ecode.NothingFound)
  119. return
  120. }
  121. c.JSON(data, nil)
  122. return
  123. }
  124. //update or create a notice
  125. func editNotice(c *bm.Context) {
  126. var (
  127. err error
  128. id uint64
  129. plat uint64
  130. condition uint64
  131. version string
  132. build uint64
  133. title string
  134. content string
  135. link string
  136. stime int64
  137. etime int64
  138. clientType string
  139. )
  140. params := c.Request.Form
  141. idStr := params.Get("id")
  142. platStr := params.Get("plat")
  143. conditionStr := params.Get("condi")
  144. version = params.Get("version")
  145. buildStr := params.Get("build")
  146. title = params.Get("title")
  147. content = params.Get("content")
  148. link = params.Get("link")
  149. stimeStr := params.Get("stime")
  150. etimeStr := params.Get("etime")
  151. clientType = params.Get("client_type")
  152. if platStr == "" || title == "" || content == "" || stimeStr == "" || etimeStr == "" {
  153. c.JSON(nil, ecode.RequestErr)
  154. return
  155. }
  156. if idStr != "" {
  157. if id, err = strconv.ParseUint(idStr, 10, 32); err != nil {
  158. log.Warn("strconv.ParseUint(id:%s) error(%v)", idStr, err)
  159. c.JSON(nil, ecode.RequestErr)
  160. return
  161. }
  162. }
  163. if plat, err = strconv.ParseUint(platStr, 10, 8); err != nil {
  164. log.Warn("strconv.ParseUint(plat:%s) error(%v)", platStr, err)
  165. c.JSON(nil, ecode.RequestErr)
  166. return
  167. }
  168. if conditionStr != "" {
  169. if condition, err = strconv.ParseUint(conditionStr, 10, 8); err != nil {
  170. log.Warn("strconv.ParseUint(condition:%s) error(%v)", conditionStr, err)
  171. c.JSON(nil, ecode.RequestErr)
  172. return
  173. }
  174. }
  175. if buildStr != "" {
  176. if build, err = strconv.ParseUint(buildStr, 10, 32); err != nil {
  177. log.Warn("strconv.ParseUint(build:%s) error(%v)", buildStr, err)
  178. c.JSON(nil, ecode.RequestErr)
  179. return
  180. }
  181. }
  182. var tempTime time.Time
  183. if tempTime, err = time.Parse("2006-01-02 15:04:05", stimeStr); err != nil {
  184. //error,so try to parse as unix timestamp again
  185. stime, err = strconv.ParseInt(stimeStr, 10, 64)
  186. if err != nil {
  187. log.Warn("strconv.ParseUint(stime:%s) error(%v)", stimeStr, err)
  188. c.JSON(nil, ecode.RequestErr)
  189. return
  190. }
  191. } else {
  192. //标准库的time.parse默认以UTC为标准转换成unix时间戳,所以需要减去8小时
  193. stime = tempTime.Unix() - 8*3600
  194. }
  195. if tempTime, err = time.Parse("2006-01-02 15:04:05", etimeStr); err != nil {
  196. //error,so try to parse as unix timestamp again
  197. etime, err = strconv.ParseInt(etimeStr, 10, 64)
  198. if err != nil {
  199. log.Warn("strconv.ParseUint(etime:%s) error(%v)", etimeStr, err)
  200. c.JSON(nil, ecode.RequestErr)
  201. return
  202. }
  203. } else {
  204. //标准库的time.parse默认以UTC为标准转换成unix时间戳,所以需要减去8小时
  205. etime = tempTime.Unix() - 8*3600
  206. }
  207. //开始时间必须小于等于结束时间
  208. if stime > etime {
  209. c.JSON(nil, ecode.RequestErr)
  210. return
  211. }
  212. nt := &model.Notice{
  213. ID: uint32(id),
  214. Plat: model.NoticePlat(plat),
  215. Version: version,
  216. Condition: model.NoticeCondition(condition),
  217. Build: uint32(build),
  218. Title: title,
  219. Content: content,
  220. Link: link,
  221. StartTime: xtime.Time(stime),
  222. EndTime: xtime.Time(etime),
  223. ClientType: clientType,
  224. }
  225. if idStr == "" {
  226. _, err = rpSvc.CreateNotice(c, nt)
  227. if err != nil {
  228. log.Error("svc.CreateNotice(%v) error(%v)", *nt, err)
  229. c.JSON(nil, err)
  230. return
  231. }
  232. } else {
  233. err = rpSvc.UpdateNotice(c, nt)
  234. if err != nil {
  235. log.Error("svc.UpdateNotice(%v) error(%v)", *nt, err)
  236. c.JSON(nil, err)
  237. return
  238. }
  239. }
  240. c.JSON(nil, nil)
  241. return
  242. }
  243. func deleteNotice(c *bm.Context) {
  244. var (
  245. err error
  246. id uint64
  247. )
  248. params := c.Request.Form
  249. idStr := params.Get("id")
  250. if idStr == "" {
  251. c.JSON(nil, ecode.RequestErr)
  252. return
  253. }
  254. if id, err = strconv.ParseUint(idStr, 10, 32); err != nil {
  255. log.Warn("strconv.ParseUint(id:%s) error(%v)", idStr, err)
  256. c.JSON(nil, ecode.RequestErr)
  257. return
  258. }
  259. err = rpSvc.DeleteNotice(c, uint32(id))
  260. if err != nil {
  261. log.Error("svc.DeleteNotice(%d) error(%v)", id, err)
  262. c.JSON(nil, err)
  263. return
  264. }
  265. c.JSON(nil, nil)
  266. return
  267. }
  268. func offlineNotice(c *bm.Context) {
  269. var (
  270. err error
  271. id uint64
  272. )
  273. params := c.Request.Form
  274. idStr := params.Get("id")
  275. if idStr == "" {
  276. c.JSON(nil, ecode.RequestErr)
  277. return
  278. }
  279. if id, err = strconv.ParseUint(idStr, 10, 32); err != nil {
  280. log.Warn("strconv.ParseUint(id:%s) error(%v)", idStr, err)
  281. c.JSON(nil, ecode.RequestErr)
  282. return
  283. }
  284. err = rpSvc.UpdateNoticeStatus(c, model.StatusOffline, uint32(id))
  285. if err != nil {
  286. log.Error("svc.UpdateNoticeStatus(%d) error(%v)", id, err)
  287. c.JSON(nil, err)
  288. return
  289. }
  290. c.JSON(nil, nil)
  291. return
  292. }
  293. func onlineNotice(c *bm.Context) {
  294. var (
  295. err error
  296. id uint64
  297. )
  298. params := c.Request.Form
  299. idStr := params.Get("id")
  300. if idStr == "" {
  301. c.JSON(nil, ecode.RequestErr)
  302. return
  303. }
  304. if id, err = strconv.ParseUint(idStr, 10, 32); err != nil {
  305. log.Warn("strconv.ParseUint(id:%s) error(%v)", idStr, err)
  306. c.JSON(nil, ecode.RequestErr)
  307. return
  308. }
  309. err = rpSvc.UpdateNoticeStatus(c, model.StatusOnline, uint32(id))
  310. if err != nil {
  311. log.Error("svc.UpdateNoticeStatus(%d) error(%v)", id, err)
  312. c.JSON(nil, err)
  313. return
  314. }
  315. c.JSON(nil, nil)
  316. return
  317. }