keyword.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package http
  2. import (
  3. "errors"
  4. "strconv"
  5. "time"
  6. "go-common/app/service/main/antispam/conf"
  7. "go-common/app/service/main/antispam/dao"
  8. "go-common/app/service/main/antispam/model"
  9. "go-common/app/service/main/antispam/util"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. )
  14. var (
  15. // ErrMissID .
  16. ErrMissID = errors.New("error: id is required")
  17. // ErrIllegalOp .
  18. ErrIllegalOp = errors.New("error: this operation on keyword is not allowed")
  19. )
  20. // GetKeywordSenders return keyword's sender list
  21. func GetKeywordSenders(c *bm.Context) {
  22. params := c.Request.Form
  23. if _, err := getAdminID(params); err != nil {
  24. errResp(c, ecode.RequestErr, err)
  25. return
  26. }
  27. id, err := strconv.ParseInt(params.Get(ProtocolKeywordID), 10, 64)
  28. if err != nil {
  29. log.Error("%v", err)
  30. errResp(c, ecode.RequestErr, err)
  31. return
  32. }
  33. k, err := Svr.GetKeywordByID(c, id)
  34. if err != nil {
  35. errResp(c, ecode.ServerErr, err)
  36. return
  37. }
  38. if k.State == model.StateDeleted {
  39. errResp(c, ecode.ServerErr, dao.ErrResourceNotExist)
  40. return
  41. }
  42. l, err := Svr.GetSenderIDsByKeywordID(c, id)
  43. if err != nil {
  44. errResp(c, ecode.ServerErr, err)
  45. return
  46. }
  47. c.JSON(l, nil)
  48. }
  49. // DeleteKeywords .
  50. func DeleteKeywords(c *bm.Context) {
  51. params := c.Request.Form
  52. if _, err := getAdminID(params); err != nil {
  53. errResp(c, ecode.RequestErr, err)
  54. return
  55. }
  56. if params.Get(ProtocolKeywordIDs) == "" {
  57. errResp(c, ecode.RequestErr, errors.New("empty ids"))
  58. return
  59. }
  60. ids, err := util.StrToIntSli(params.Get(ProtocolKeywordIDs), ",")
  61. if err != nil {
  62. log.Error("%v", err)
  63. errResp(c, ecode.RequestErr, err)
  64. return
  65. }
  66. if len(ids) == 0 {
  67. errResp(c, ecode.RequestErr, nil)
  68. return
  69. }
  70. ks, err := Svr.DeleteKeywords(c, ids)
  71. if err != nil {
  72. errResp(c, ecode.ServerErr, err)
  73. return
  74. }
  75. c.JSON(ks, nil)
  76. }
  77. // UpdateKeyword .
  78. func UpdateKeyword(c *bm.Context) {
  79. params := c.Request.Form
  80. _, err := getAdminID(params)
  81. if err != nil {
  82. errResp(c, ecode.RequestErr, err)
  83. return
  84. }
  85. op := params.Get(ProtocolKeywordOperation)
  86. if op != ProtocolKeywordOpDefaultLimit &&
  87. op != ProtocolKeywordOpRestrictLimit &&
  88. op != ProtocolKeywordOpBlack &&
  89. op != ProtocolKeywordOpWhite {
  90. log.Error("%v", ErrIllegalOp)
  91. errResp(c, ecode.RequestErr, ErrIllegalOp)
  92. return
  93. }
  94. id, err := strconv.ParseInt(params.Get(ProtocolKeywordID), 10, 64)
  95. if err != nil {
  96. log.Error("%v", err)
  97. errResp(c, ecode.RequestErr, err)
  98. return
  99. }
  100. k, err := Svr.GetKeywordByID(c, id)
  101. if err != nil {
  102. errResp(c, ecode.ServerErr, err)
  103. return
  104. }
  105. if k.State == model.StateDeleted {
  106. errResp(c, ecode.ServerErr, dao.ErrResourceNotExist)
  107. return
  108. }
  109. if k.Tag == op {
  110. c.JSON(k, nil)
  111. return
  112. }
  113. k, err = Svr.OpKeyword(c, id, op)
  114. if err != nil {
  115. errResp(c, ecode.ServerErr, err)
  116. return
  117. }
  118. c.JSON(k, nil)
  119. }
  120. // GetKeyword .
  121. func GetKeyword(c *bm.Context) {
  122. params := c.Request.Form
  123. _, _, err := getAdminIDAndArea(params)
  124. if err != nil {
  125. errResp(c, ecode.RequestErr, err)
  126. return
  127. }
  128. if params.Get(ProtocolKeywordID) == "" {
  129. errResp(c, ecode.RequestErr, ErrMissID)
  130. return
  131. }
  132. id, err := strconv.ParseInt(params.Get(ProtocolKeywordID), 10, 64)
  133. if err != nil {
  134. log.Error("%v", err)
  135. errResp(c, ecode.RequestErr, err)
  136. return
  137. }
  138. keyword, err := Svr.GetKeywordByID(c, id)
  139. if err != nil {
  140. errResp(c, ecode.ServerErr, err)
  141. return
  142. }
  143. c.JSON(keyword, nil)
  144. }
  145. // GetKeywords .
  146. func GetKeywords(c *bm.Context) {
  147. params := c.Request.Form
  148. _, area, err := getAdminIDAndArea(params)
  149. if err != nil {
  150. errResp(c, ecode.RequestErr, err)
  151. return
  152. }
  153. var (
  154. ctimeStart, ctimeEnd string
  155. startTime, endTime time.Time
  156. )
  157. cond := &Condition{
  158. Pagination: &util.Pagination{},
  159. Area: area,
  160. State: model.StateDefault,
  161. Search: params.Get(ProtocolSearch),
  162. Order: params.Get(ProtocolOrder),
  163. OrderBy: params.Get(ProtocolOrderBy),
  164. Tag: params.Get(ProtocolKeywordTag),
  165. }
  166. if ctimeStart = params.Get(ProtocolCTimeStart); ctimeStart != "" {
  167. startTime, err = time.Parse(util.TimeFormat, ctimeStart)
  168. if err != nil {
  169. log.Error("%v", err)
  170. errResp(c, ecode.RequestErr, err)
  171. return
  172. }
  173. cond.StartTime = &startTime
  174. }
  175. if ctimeEnd = params.Get(ProtocolCTimeEnd); ctimeEnd != "" {
  176. endTime, err = time.Parse(util.TimeFormat, ctimeEnd)
  177. if err != nil {
  178. log.Error("%v", err)
  179. errResp(c, ecode.RequestErr, err)
  180. return
  181. }
  182. cond.EndTime = &endTime
  183. }
  184. if ctimeStart != "" && ctimeEnd != "" {
  185. if startTime.After(endTime) {
  186. err = errors.New("start_time cannot be bigger than end_time")
  187. log.Error("%v", err)
  188. errResp(c, ecode.RequestErr, err)
  189. return
  190. }
  191. }
  192. var export bool
  193. if params.Get(ProtocolKeywordExport) != "" {
  194. export, err = strconv.ParseBool(params.Get(ProtocolKeywordExport))
  195. if err != nil {
  196. log.Error("%v", err)
  197. errResp(c, ecode.RequestErr, err)
  198. return
  199. }
  200. }
  201. if export {
  202. cond.PerPage = conf.Conf.ServiceOption.MaxExportRows
  203. } else {
  204. if params.Get(ProtocolCurPage) != "" {
  205. cond.CurPage, err = strconv.ParseInt(params.Get(ProtocolCurPage), 10, 64)
  206. if err != nil {
  207. log.Error("%v", err)
  208. errResp(c, ecode.RequestErr, err)
  209. return
  210. }
  211. }
  212. if perPage := params.Get(ProtocolPerPage); perPage != "" {
  213. if cond.PerPage, err = strconv.ParseInt(perPage, 10, 64); err != nil {
  214. log.Error("%v", err)
  215. errResp(c, ecode.RequestErr, err)
  216. return
  217. }
  218. }
  219. }
  220. if err = cond.Valid(); err != nil {
  221. log.Error("%v", err)
  222. errResp(c, ecode.RequestErr, err)
  223. return
  224. }
  225. keywords, total, err := Svr.GetKeywordsByCond(c, ToServiceCond(cond))
  226. if err != nil {
  227. errResp(c, ecode.ServerErr, err)
  228. return
  229. }
  230. res := map[string]interface{}{}
  231. res[ProtocolTotalCounts] = total
  232. res[ProtocolData] = keywords
  233. c.JSONMap(res, nil)
  234. }