privilege.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package http
  2. import (
  3. "io/ioutil"
  4. "mime/multipart"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. "go-common/app/admin/main/vip/model"
  9. "go-common/library/ecode"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/http/blademaster/binding"
  12. )
  13. const (
  14. _maxnamelen = 30
  15. _maxtitlelen = 60
  16. _maxexplainlen = 1200
  17. )
  18. // regexp utf8 char 0x0e0d~0e4A
  19. var (
  20. _emptyUnicodeReg = []*regexp.Regexp{
  21. regexp.MustCompile(`[\x{202e}]+`), // right-to-left override
  22. regexp.MustCompile(`[\x{200b}]+`), // zeroWithChar
  23. regexp.MustCompile(`[\x{1f6ab}]+`), // no_entry_sign
  24. }
  25. // trim
  26. returnReg = regexp.MustCompile(`[\n]{3,}`)
  27. returnReg2 = regexp.MustCompile(`(\r\n){3,}`)
  28. spaceReg = regexp.MustCompile(`[ ]{5,}`) // Chinese quanjiao space character
  29. )
  30. func privileges(c *bm.Context) {
  31. var err error
  32. arg := new(struct {
  33. Langtype int8 `form:"lang_type"`
  34. })
  35. if err = c.Bind(arg); err != nil {
  36. return
  37. }
  38. c.JSON(vipSvc.Privileges(c, arg.Langtype))
  39. }
  40. func updatePrivilegeState(c *bm.Context) {
  41. var err error
  42. arg := new(model.ArgStatePrivilege)
  43. if err = c.Bind(arg); err != nil {
  44. return
  45. }
  46. c.JSON(nil, vipSvc.UpdatePrivilegeState(c, &model.Privilege{
  47. ID: arg.ID,
  48. State: arg.Status,
  49. }))
  50. }
  51. func deletePrivilege(c *bm.Context) {
  52. var err error
  53. arg := new(model.ArgPivilegeID)
  54. if err = c.Bind(arg); err != nil {
  55. return
  56. }
  57. c.JSON(nil, vipSvc.DeletePrivilege(c, arg.ID))
  58. }
  59. func updateOrder(c *bm.Context) {
  60. var err error
  61. arg := new(model.ArgOrder)
  62. if err = c.Bind(arg); err != nil {
  63. return
  64. }
  65. c.JSON(nil, vipSvc.UpdateOrder(c, arg))
  66. }
  67. func addPrivilege(c *bm.Context) {
  68. var err error
  69. arg := new(model.ArgAddPrivilege)
  70. username, ok := c.Get("username")
  71. if !ok {
  72. c.JSON(nil, ecode.AccessDenied)
  73. return
  74. }
  75. arg.Operator = username.(string)
  76. if err = c.BindWith(arg, binding.FormMultipart); err != nil {
  77. return
  78. }
  79. if len(arg.Name) > _maxnamelen {
  80. c.JSON(nil, ecode.VipPrivilegeNameTooLongErr)
  81. return
  82. }
  83. if len(arg.Title) > _maxtitlelen {
  84. c.JSON(nil, ecode.VipPrivilegeTitleTooLongErr)
  85. return
  86. }
  87. if len(arg.Explain) > _maxexplainlen {
  88. c.JSON(nil, ecode.VipPrivilegeExplainTooLongErr)
  89. return
  90. }
  91. img := new(model.ArgImage)
  92. if img.IconBody, img.IconFileType, err = file(c, "icon"); err != nil {
  93. c.JSON(nil, err)
  94. return
  95. }
  96. if img.IconFileType == "" {
  97. c.JSON(nil, ecode.VipFileImgEmptyErr)
  98. return
  99. }
  100. if img.IconGrayBody, img.IconGrayFileType, err = file(c, "gray_icon"); err != nil {
  101. c.JSON(nil, err)
  102. return
  103. }
  104. if img.IconGrayFileType == "" {
  105. c.JSON(nil, ecode.VipFileImgEmptyErr)
  106. return
  107. }
  108. if img.WebImageBody, img.WebImageFileType, err = file(c, "web_image"); err != nil {
  109. c.JSON(nil, err)
  110. return
  111. }
  112. if img.AppImageBody, img.AppImageFileType, err = file(c, "app_image"); err != nil {
  113. c.JSON(nil, err)
  114. return
  115. }
  116. arg.Explain = filterContent(arg.Explain)
  117. c.JSON(nil, vipSvc.AddPrivilege(c, arg, img))
  118. }
  119. func updatePrivilege(c *bm.Context) {
  120. var (
  121. err error
  122. )
  123. arg := new(model.ArgUpdatePrivilege)
  124. username, ok := c.Get("username")
  125. if !ok {
  126. c.JSON(nil, ecode.AccessDenied)
  127. return
  128. }
  129. arg.Operator = username.(string)
  130. if err = c.BindWith(arg, binding.FormMultipart); err != nil {
  131. return
  132. }
  133. if len(arg.Name) > _maxnamelen {
  134. c.JSON(nil, ecode.VipPrivilegeNameTooLongErr)
  135. return
  136. }
  137. if len(arg.Title) > _maxtitlelen {
  138. c.JSON(nil, ecode.VipPrivilegeTitleTooLongErr)
  139. return
  140. }
  141. if len(arg.Explain) > _maxexplainlen {
  142. c.JSON(nil, ecode.VipPrivilegeExplainTooLongErr)
  143. return
  144. }
  145. img := new(model.ArgImage)
  146. if img.IconBody, img.IconFileType, err = file(c, "icon"); err != nil {
  147. c.JSON(nil, err)
  148. return
  149. }
  150. if img.IconGrayBody, img.IconGrayFileType, err = file(c, "gray_icon"); err != nil {
  151. c.JSON(nil, err)
  152. return
  153. }
  154. if img.WebImageBody, img.WebImageFileType, err = file(c, "web_image"); err != nil {
  155. c.JSON(nil, err)
  156. return
  157. }
  158. if img.AppImageBody, img.AppImageFileType, err = file(c, "app_image"); err != nil {
  159. c.JSON(nil, err)
  160. return
  161. }
  162. arg.Explain = filterContent(arg.Explain)
  163. c.JSON(nil, vipSvc.UpdatePrivilege(c, arg, img))
  164. }
  165. func file(c *bm.Context, name string) (body []byte, filetype string, err error) {
  166. var file multipart.File
  167. if file, _, err = c.Request.FormFile(name); err != nil {
  168. if err == http.ErrMissingFile {
  169. err = nil
  170. return
  171. }
  172. err = ecode.RequestErr
  173. return
  174. }
  175. if file == nil {
  176. return
  177. }
  178. defer file.Close()
  179. if body, err = ioutil.ReadAll(file); err != nil {
  180. err = ecode.RequestErr
  181. return
  182. }
  183. filetype = http.DetectContentType(body)
  184. if err = checkImgFileType(filetype); err != nil {
  185. return
  186. }
  187. err = checkFileBody(body)
  188. return
  189. }
  190. func checkImgFileType(filetype string) error {
  191. switch filetype {
  192. case "image/jpeg", "image/jpg":
  193. case "image/png":
  194. default:
  195. return ecode.VipFileTypeErr
  196. }
  197. return nil
  198. }
  199. func checkFileBody(body []byte) error {
  200. if len(body) == 0 {
  201. return ecode.FileNotExists
  202. }
  203. if len(body) > cf.Bfs.MaxFileSize {
  204. return ecode.FileTooLarge
  205. }
  206. return nil
  207. }
  208. func filterContent(str string) string {
  209. tmp := str
  210. // check params
  211. tmp = strings.TrimSpace(tmp)
  212. tmp = spaceReg.ReplaceAllString(tmp, "   ")
  213. tmp = returnReg.ReplaceAllString(tmp, "\n\n\n")
  214. tmp = returnReg2.ReplaceAllString(tmp, "\n\n\n")
  215. // checkout empty
  216. for _, reg := range _emptyUnicodeReg {
  217. tmp = reg.ReplaceAllString(tmp, "")
  218. }
  219. return tmp
  220. }