123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package controllers
- import (
- "cc-officialweb/models"
- "cc-officialweb/service"
- "cc-officialweb/unity"
- "encoding/json"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/go-playground/validator/v10"
- "strconv"
- )
- type ContactController struct {
- beego.Controller
- }
- func (c *ContactController) Get() {
- c.Data["IsContact"] = true
- productType := service.GetProductType()
- if len(productType) == 0 {
- c.Data["productFirst"] = "暂无产品分类"
- } else {
- c.Data["productFirst"] = productType[0].Name
- }
- c.Data["productType"] = productType
- c.TplName = "contact.html"
- }
- // AddContact 提交联系我们信息
- func (c *ContactController) AddContact() {
- var contact models.Contact
- name := c.GetString("name")
- phone := c.GetString("phone")
- message := c.GetString("message")
- validate := validator.New()
- err2 := validate.Var(name, "required")
- err2 = validate.Var(phone, "required")
- err2 = validate.Var(message, "required")
- if err2 != nil {
- c.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
- c.ServeJSON()
- return
- }
- contact.Name = name
- contact.Phone = phone
- contact.Message = message
- add, err := unity.Add(&contact)
- if err != nil {
- c.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
- c.ServeJSON()
- return
- } else {
- c.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add}
- c.ServeJSON()
- }
- }
- // DeleteContactById 根据id删除联系消息
- func (c *ContactController) DeleteContactById() {
- id := c.GetString("id")
- atoi, _ := strconv.Atoi(id)
- validate := validator.New()
- err2 := validate.Var(atoi, "required")
- if err2 != nil {
- c.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
- c.ServeJSON()
- return
- }
- _, err := unity.DeleteById(atoi, models.Contact{})
- if err != nil {
- c.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
- c.ServeJSON()
- return
- } else {
- c.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
- c.ServeJSON()
- }
- }
- // GetAllContact 获取所有联系方式
- func (c *ContactController) GetAllContact() {
- var page unity.PageParams
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &page)
- if err != nil {
- c.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
- c.ServeJSON()
- return
- }
- result, total, current, err := unity.Paginate(page, models.Contact{})
- if err != nil {
- c.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
- c.ServeJSON()
- return
- } else {
- c.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
- Current: current,
- Size: total,
- Data: result,
- }}
- c.ServeJSON()
- return
- }
- }
|