recruit.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //获取产品分类
  27. productType := service.GetProductType()
  28. if len(productType) == 0 {
  29. r.Data["productFirst"] = "暂无产品分类"
  30. } else {
  31. r.Data["productFirst"] = productType[0].Name
  32. }
  33. r.Data["productType"] = productType
  34. r.TplName = "recruit.html"
  35. }
  36. // AddRecruit 添加招募或联系我们
  37. func (r *RecruitController) AddRecruit() {
  38. var content models.ContentDto
  39. err := json.Unmarshal(r.Ctx.Input.RequestBody, &content)
  40. if err != nil {
  41. r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  42. r.ServeJSON()
  43. return
  44. }
  45. var cont models.Content
  46. cont.Title = content.Title
  47. cont.Synopsis = content.Synopsis
  48. cont.Detail = content.Detail
  49. cont.Types = content.Types
  50. add, err := unity.Add(cont)
  51. if err != nil {
  52. r.Data["json"] = &JSON{Code: 101, Msg: "添加失败"}
  53. r.ServeJSON()
  54. return
  55. } else {
  56. r.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add}
  57. r.ServeJSON()
  58. }
  59. }
  60. // UpdateRecruit 更新招募和联系我们
  61. func (r *RecruitController) UpdateRecruit() {
  62. var content models.ContentDto
  63. err := json.Unmarshal(r.Ctx.Input.RequestBody, &content)
  64. if err != nil {
  65. r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  66. r.ServeJSON()
  67. return
  68. }
  69. var cont models.Content
  70. cont.Title = content.Title
  71. cont.Synopsis = content.Synopsis
  72. cont.Detail = content.Detail
  73. cont.Types = content.Types
  74. id, err := unity.UpdateById(content.ID, cont)
  75. if err != nil {
  76. r.Data["json"] = &JSON{Code: 101, Msg: "更新失败"}
  77. r.ServeJSON()
  78. return
  79. } else {
  80. r.Data["json"] = &JSON{Code: 200, Msg: "更新成功", Data: id}
  81. r.ServeJSON()
  82. }
  83. }
  84. // DeleteRecruit 删除招募和联系我们
  85. func (r *RecruitController) DeleteRecruit() {
  86. getString := r.GetString("id")
  87. id, _ := strconv.Atoi(getString)
  88. validate := validator.New()
  89. err2 := validate.Var(id, "required")
  90. if err2 != nil {
  91. r.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  92. r.ServeJSON()
  93. return
  94. }
  95. _, err := unity.DeleteById(id, &models.Content{})
  96. if err != nil {
  97. r.Data["json"] = &JSON{Code: 101, Msg: "删除失败"}
  98. r.ServeJSON()
  99. return
  100. } else {
  101. r.Data["json"] = &JSON{Code: 200, Msg: "删除成功"}
  102. r.ServeJSON()
  103. }
  104. }
  105. // GetAllRecruit 获取所有招募和联系我们
  106. func (r *RecruitController) GetAllRecruit() {
  107. var params unity.PageParams
  108. err := json.Unmarshal(r.Ctx.Input.RequestBody, &params)
  109. if err != nil {
  110. r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  111. r.ServeJSON()
  112. return
  113. }
  114. result, total, current, err := unity.Paginate(params, &models.Content{})
  115. if err != nil {
  116. r.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  117. r.ServeJSON()
  118. return
  119. } else {
  120. r.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: JSONS{
  121. Current: current,
  122. Size: total,
  123. Data: result,
  124. }}
  125. r.ServeJSON()
  126. return
  127. }
  128. }
  129. // GetRecruit 获取单个招募和联系我们
  130. func (r *RecruitController) GetRecruit() {
  131. getString := r.GetString("id")
  132. id, _ := strconv.Atoi(getString)
  133. validate := validator.New()
  134. err2 := validate.Var(id, "required")
  135. if err2 != nil {
  136. r.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  137. r.ServeJSON()
  138. return
  139. }
  140. result, err := unity.GetById(id, &models.Content{})
  141. if err != nil {
  142. r.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  143. r.ServeJSON()
  144. return
  145. } else {
  146. r.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  147. r.ServeJSON()
  148. return
  149. }
  150. }