web_tpl.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. func webTemplates(c *bm.Context) {
  10. // check user
  11. midI, ok := c.Get("mid")
  12. if !ok {
  13. c.JSON(nil, ecode.NoLogin)
  14. return
  15. }
  16. mid, _ := midI.(int64)
  17. tps, err := tplSvc.Templates(c, mid)
  18. if err != nil {
  19. c.JSON(nil, err)
  20. return
  21. }
  22. c.JSON(tps, nil)
  23. }
  24. func webAddTpl(c *bm.Context) {
  25. params := c.Request.Form
  26. typeidStr := params.Get("typeid")
  27. copyright := params.Get("arctype")
  28. name := params.Get("name")
  29. title := params.Get("title")
  30. tag := params.Get("keywords")
  31. content := params.Get("description")
  32. // check params
  33. midI, ok := c.Get("mid")
  34. if !ok {
  35. c.JSON(nil, ecode.NoLogin)
  36. return
  37. }
  38. mid, _ := midI.(int64)
  39. typeid, _ := strconv.Atoi(typeidStr)
  40. if typeid < 0 {
  41. typeid = 0
  42. }
  43. if name == "" {
  44. log.Error("name can not be empty")
  45. c.JSON(nil, ecode.RequestErr)
  46. return
  47. }
  48. if copyright == "" {
  49. copyright = "Original"
  50. }
  51. if err := tplSvc.AddTemplate(c, mid, int16(typeid), copyright, name, title, tag, content, time.Now()); err != nil {
  52. c.JSON(nil, err)
  53. return
  54. }
  55. c.JSON(nil, nil)
  56. }
  57. func webUpdateTpl(c *bm.Context) {
  58. params := c.Request.Form
  59. idStr := params.Get("tid")
  60. typeidStr := params.Get("typeid")
  61. copyright := params.Get("arctype")
  62. name := params.Get("name")
  63. title := params.Get("title")
  64. tag := params.Get("keywords")
  65. content := params.Get("description")
  66. // check params
  67. midI, ok := c.Get("mid")
  68. if !ok {
  69. c.JSON(nil, ecode.NoLogin)
  70. return
  71. }
  72. mid, _ := midI.(int64)
  73. id, err := strconv.ParseInt(idStr, 10, 64)
  74. if err != nil || id < 1 {
  75. log.Error("strconv.ParseInt(%s) error(%v)", idStr, err)
  76. c.JSON(nil, ecode.RequestErr)
  77. return
  78. }
  79. typeid, _ := strconv.Atoi(typeidStr)
  80. if typeid < 0 {
  81. typeid = 0
  82. }
  83. if name == "" {
  84. log.Error("name is empty error(%v)", err)
  85. c.JSON(nil, ecode.RequestErr)
  86. return
  87. }
  88. if copyright == "" {
  89. copyright = "Original"
  90. }
  91. // update
  92. c.JSON(nil, tplSvc.UpdateTemplate(c, id, mid, int16(typeid), copyright, name, title, tag, content, time.Now()))
  93. }
  94. func webDelTpl(c *bm.Context) {
  95. params := c.Request.Form
  96. idStr := params.Get("tid")
  97. // check params
  98. midI, ok := c.Get("mid")
  99. if !ok {
  100. c.JSON(nil, ecode.NoLogin)
  101. return
  102. }
  103. mid, _ := midI.(int64)
  104. id, err := strconv.ParseInt(idStr, 10, 64)
  105. if err != nil || id < 1 {
  106. log.Error("strconv.ParseInt(%s) error(%v)", idStr, err)
  107. c.JSON(nil, ecode.RequestErr)
  108. return
  109. }
  110. // del
  111. c.JSON(nil, tplSvc.DelTemplate(c, id, mid, time.Now()))
  112. }