代码推送
This commit is contained in:
116
internal/handler/customer.go
Normal file
116
internal/handler/customer.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gotest/internal/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CustomerHandler 客户信息收集处理器
|
||||
type CustomerHandler struct {
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// NewCustomerHandler 创建客户信息收集处理器
|
||||
func NewCustomerHandler(cfg *config.Config) *CustomerHandler {
|
||||
return &CustomerHandler{cfg: cfg}
|
||||
}
|
||||
|
||||
// CustomerRequest 客户信息请求
|
||||
type CustomerRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
Company string `json:"company"`
|
||||
Email string `json:"email"`
|
||||
Needs string `json:"needs" binding:"required"`
|
||||
}
|
||||
|
||||
// Submit 提交客户信息 POST /api/v1/customer/submit
|
||||
func (h *CustomerHandler) Submit(c *gin.Context) {
|
||||
var req CustomerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
FailBadRequest(c, "请填写完整的联系信息")
|
||||
return
|
||||
}
|
||||
|
||||
// 构建企业微信消息内容
|
||||
content := fmt.Sprintf("【新客户咨询】\n"+
|
||||
"━━━━━━━━━━━━━━━━━━━━\n"+
|
||||
"👤 姓名:%s\n"+
|
||||
"📱 电话:%s\n"+
|
||||
"📧 邮箱:%s\n"+
|
||||
"🏢 公司:%s\n"+
|
||||
"💼 需求:%s\n"+
|
||||
"⏰ 时间:%s\n"+
|
||||
"━━━━━━━━━━━━━━━━━━━━",
|
||||
req.Name, req.Phone, req.Email, req.Company, req.Needs, time.Now().Format("2006-01-02 15:04:05"))
|
||||
|
||||
// 发送到企业微信webhook
|
||||
if err := h.sendToWechatWebhook(content); err != nil {
|
||||
// 即使webhook失败,也记录下来并返回成功(避免暴露内部错误)
|
||||
fmt.Printf("发送企业微信webhook失败: %v\n", err)
|
||||
}
|
||||
|
||||
Success(c, gin.H{
|
||||
"message": "提交成功,我们会尽快联系您!",
|
||||
})
|
||||
}
|
||||
|
||||
// sendToWechatWebhook 发送消息到企业微信群机器人
|
||||
func (h *CustomerHandler) sendToWechatWebhook(content string) error {
|
||||
if h.cfg.WechatWebhook.URL == "" {
|
||||
return fmt.Errorf("企业微信webhook URL未配置")
|
||||
}
|
||||
|
||||
// 构建消息体
|
||||
message := map[string]interface{}{
|
||||
"msgtype": "text",
|
||||
"text": map[string]string{
|
||||
"content": content,
|
||||
},
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化消息失败: %w", err)
|
||||
}
|
||||
|
||||
// 发送POST请求
|
||||
resp, err := http.Post(h.cfg.WechatWebhook.URL, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("发送请求失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取响应失败: %w", err)
|
||||
}
|
||||
|
||||
// 检查响应状态
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("请求失败,状态码: %d,响应: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// 解析响应JSON
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return fmt.Errorf("解析响应失败: %w", err)
|
||||
}
|
||||
|
||||
// 检查企业微信返回的错误码
|
||||
if errcode, ok := result["errcode"].(float64); ok && errcode != 0 {
|
||||
errmsg, _ := result["errmsg"].(string)
|
||||
return fmt.Errorf("企业微信返回错误: %v - %s", errcode, errmsg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user