contact.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 ContactController struct {
  12. beego.Controller
  13. }
  14. func (c *ContactController) Get() {
  15. c.Data["IsContact"] = true
  16. productType := service.GetProductType()
  17. if len(productType) == 0 {
  18. c.Data["productFirst"] = "暂无产品分类"
  19. } else {
  20. c.Data["productFirst"] = productType[0].Name
  21. }
  22. c.Data["productType"] = productType
  23. c.TplName = "contact.html"
  24. }
  25. // AddContact 提交联系我们信息
  26. func (c *ContactController) AddContact() {
  27. var contact models.Contact
  28. name := c.GetString("name")
  29. phone := c.GetString("phone")
  30. message := c.GetString("message")
  31. validate := validator.New()
  32. err2 := validate.Var(name, "required")
  33. err2 = validate.Var(phone, "required")
  34. err2 = validate.Var(message, "required")
  35. if err2 != nil {
  36. c.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
  37. c.ServeJSON()
  38. return
  39. }
  40. contact.Name = name
  41. contact.Phone = phone
  42. contact.Message = message
  43. add, err := unity.Add(&contact)
  44. if err != nil {
  45. c.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
  46. c.ServeJSON()
  47. return
  48. } else {
  49. c.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add}
  50. c.ServeJSON()
  51. }
  52. }
  53. // DeleteContactById 根据id删除联系消息
  54. func (c *ContactController) DeleteContactById() {
  55. id := c.GetString("id")
  56. atoi, _ := strconv.Atoi(id)
  57. validate := validator.New()
  58. err2 := validate.Var(atoi, "required")
  59. if err2 != nil {
  60. c.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  61. c.ServeJSON()
  62. return
  63. }
  64. _, err := unity.DeleteById(atoi, models.Contact{})
  65. if err != nil {
  66. c.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
  67. c.ServeJSON()
  68. return
  69. } else {
  70. c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
  71. c.ServeJSON()
  72. }
  73. }
  74. // GetAllContact 获取所有联系方式
  75. func (c *ContactController) GetAllContact() {
  76. var page unity.PageParams
  77. err := json.Unmarshal(c.Ctx.Input.RequestBody, &page)
  78. if err != nil {
  79. c.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  80. c.ServeJSON()
  81. return
  82. }
  83. result, total, current, err := unity.Paginate(page, models.Contact{})
  84. if err != nil {
  85. c.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  86. c.ServeJSON()
  87. return
  88. } else {
  89. c.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  90. Current: current,
  91. Size: total,
  92. Data: result,
  93. }}
  94. c.ServeJSON()
  95. return
  96. }
  97. }