108 lines
3.3 KiB
Go
108 lines
3.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"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() {
|
|
// Debug: print the raw request body
|
|
body := string(c.Ctx.Input.RequestBody)
|
|
fmt.Printf("Received request body: %s\n", body)
|
|
|
|
// Try unmarshaling to a map first to see the raw data
|
|
var rawData map[string]interface{}
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &rawData); err != nil {
|
|
fmt.Printf("JSON unmarshal error (raw): %v\n", err)
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误", "debug": body}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
fmt.Printf("Raw data: %+v\n", rawData)
|
|
|
|
// Now try unmarshaling to the struct
|
|
var m models.Contact
|
|
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &m); err != nil {
|
|
fmt.Printf("JSON unmarshal error (struct): %v\n", err)
|
|
c.Data["json"] = map[string]interface{}{"code": 1, "message": "请求参数格式错误", "debug": body, "error": err.Error()}
|
|
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()
|
|
}
|