recruit.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package controllers
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/service"
  5. "cc-officialweb/unity"
  6. "encoding/json"
  7. beego "github.com/beego/beego/v2/server/web"
  8. "github.com/go-playground/validator/v10"
  9. "strconv"
  10. )
  11. type RecruitController struct {
  12. beego.Controller
  13. }
  14. func (r *RecruitController) Get() {
  15. recruit, err := service.GetRecruit("recruit")
  16. var success []string
  17. //获取成功案例轮播图
  18. exams := service.GetResource("example")
  19. for _, v := range exams {
  20. success = append(success, v.Url)
  21. }
  22. if err == nil {
  23. r.Data["recruit"] = recruit
  24. }
  25. r.Data["example"] = success
  26. r.TplName = "recruit.html"
  27. }
  28. // AddRecruit 添加招募或联系我们
  29. func (r *RecruitController) AddRecruit() {
  30. var content models.ContentDto
  31. err := json.Unmarshal(r.Ctx.Input.RequestBody, &content)
  32. if err != nil {
  33. r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  34. r.ServeJSON()
  35. return
  36. }
  37. var cont models.Content
  38. cont.Title = content.Title
  39. cont.Synopsis = content.Synopsis
  40. cont.Detail = content.Detail
  41. cont.Types = content.Types
  42. add, err := unity.Add(cont)
  43. if err != nil {
  44. r.Data["json"] = &JSON{Code: 101, Msg: "添加失败"}
  45. r.ServeJSON()
  46. return
  47. } else {
  48. r.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add}
  49. r.ServeJSON()
  50. }
  51. }
  52. // UpdateRecruit 更新招募和联系我们
  53. func (r *RecruitController) UpdateRecruit() {
  54. var content models.ContentDto
  55. err := json.Unmarshal(r.Ctx.Input.RequestBody, &content)
  56. if err != nil {
  57. r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  58. r.ServeJSON()
  59. return
  60. }
  61. var cont models.Content
  62. cont.Title = content.Title
  63. cont.Synopsis = content.Synopsis
  64. cont.Detail = content.Detail
  65. cont.Types = content.Types
  66. id, err := unity.UpdateById(content.ID, cont)
  67. if err != nil {
  68. r.Data["json"] = &JSON{Code: 101, Msg: "更新失败"}
  69. r.ServeJSON()
  70. return
  71. } else {
  72. r.Data["json"] = &JSON{Code: 200, Msg: "更新成功", Data: id}
  73. r.ServeJSON()
  74. }
  75. }
  76. // DeleteRecruit 删除招募和联系我们
  77. func (r *RecruitController) DeleteRecruit() {
  78. getString := r.GetString("id")
  79. id, _ := strconv.Atoi(getString)
  80. validate := validator.New()
  81. err2 := validate.Var(id, "required")
  82. if err2 != nil {
  83. r.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  84. r.ServeJSON()
  85. return
  86. }
  87. _, err := unity.DeleteById(id, &models.Content{})
  88. if err != nil {
  89. r.Data["json"] = &JSON{Code: 101, Msg: "删除失败"}
  90. r.ServeJSON()
  91. return
  92. } else {
  93. r.Data["json"] = &JSON{Code: 200, Msg: "删除成功"}
  94. r.ServeJSON()
  95. }
  96. }
  97. // GetAllRecruit 获取所有招募和联系我们
  98. func (r *RecruitController) GetAllRecruit() {
  99. var params unity.PageParams
  100. err := json.Unmarshal(r.Ctx.Input.RequestBody, &params)
  101. if err != nil {
  102. r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  103. r.ServeJSON()
  104. return
  105. }
  106. result, total, current, err := unity.Paginate(params, &models.Content{})
  107. if err != nil {
  108. r.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  109. r.ServeJSON()
  110. return
  111. } else {
  112. r.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: JSONS{
  113. Current: current,
  114. Size: total,
  115. Data: result,
  116. }}
  117. r.ServeJSON()
  118. return
  119. }
  120. }
  121. // GetRecruit 获取单个招募和联系我们
  122. func (r *RecruitController) GetRecruit() {
  123. getString := r.GetString("id")
  124. id, _ := strconv.Atoi(getString)
  125. validate := validator.New()
  126. err2 := validate.Var(id, "required")
  127. if err2 != nil {
  128. r.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  129. r.ServeJSON()
  130. return
  131. }
  132. result, err := unity.GetById(id, &models.Content{})
  133. if err != nil {
  134. r.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  135. r.ServeJSON()
  136. return
  137. } else {
  138. r.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  139. r.ServeJSON()
  140. return
  141. }
  142. }