91 lines
2.6 KiB
Go
91 lines
2.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"server/models"
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
type ContactController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// List GET /api/crm/contact/list?tenant_id=&related_type=&related_id=
|
|
func (c *ContactController) List() {
|
|
tenantId := c.GetString("tenant_id")
|
|
relatedType, _ := strconv.Atoi(c.GetString("related_type"))
|
|
relatedId := c.GetString("related_id")
|
|
if tenantId == "" || relatedType == 0 || relatedId == "" {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "缺少必要参数"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
list, err := services.ListContacts(tenantId, relatedType, relatedId)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": list}
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// Add POST /api/crm/contact/add
|
|
func (c *ContactController) Add() {
|
|
var m models.Contact
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
if err := services.CreateContact(&m); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok", "data": m}
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// Edit POST /api/crm/contact/edit
|
|
func (c *ContactController) Edit() {
|
|
var m models.Contact
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
if m.Id == 0 {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "id不能为空"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
if err := services.UpdateContact(&m); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok"}
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// Delete POST /api/crm/contact/delete
|
|
func (c *ContactController) Delete() {
|
|
var body struct {
|
|
Id int64 `json:"id"`
|
|
TenantId string `json:"tenant_id"`
|
|
}
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &body); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误"}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
if err := services.DeleteContact(body.Id, body.TenantId); err != nil {
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": err.Error()}
|
|
} else {
|
|
c.Data["json"] = map[string]interface{}{"code": 0, "message": "ok"}
|
|
}
|
|
c.ServeJSON()
|
|
}
|