package model import ( "bytes" "encoding/json" "encoding/xml" "errors" "fmt" "github.com/codinl/go-logger" "github.com/gin-gonic/gin" "io/ioutil" "net/http" "strings" ) //go:generate goqueryset -in wx_msg.go // gen:qs type PublicRecord struct { Model OpenId string `json:"open_id" gorm:"index"` RedeemCodeId uint32 `json:"redeem_code_id" gorm:"index"` SerialCode string `json:"serial_code" gorm:"index;comment:'兑换编码'"` // 兑换编码 FocusState uint32 `json:"focus_state"` // 关注状态:1-关注 RedeemCodeState uint32 `json:"redeem_code_state"` // 兑换码状态:1-未领取 2-已领取 3-已使用 // public_record } type WxReplyTextMsg struct { XMLName xml.Name `xml:"xml"` ToUserName string `xml:"ToUserName"` FromUserName string `xml:"FromUserName"` CreateTime uint32 `xml:"CreateTime"` MsgType string `xml:"MsgType"` Content string `xml:"Content"` } type WxReplyTextReceiveMsg struct { XMLName xml.Name `xml:"xml"` ToUserName string `xml:"ToUserName"` FromUserName string `xml:"FromUserName"` CreateTime uint32 `xml:"CreateTime"` MsgType string `xml:"MsgType"` Event string `xml:"Event"` Content string `xml:"Content"` MsgId uint64 `xml:"MsgId"` } func SendCustomerServiceMessage(data []byte, c *gin.Context, accessToken string) error { client := http.Client{} SendCustomerServiceMessageUrl := "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken fmt.Println("客服消息 请求参数:", string(data)) req, err := http.NewRequest("POST", SendCustomerServiceMessageUrl, bytes.NewBuffer(data)) if err != nil { logger.Error(err) } req.Header.Set("Content-Type", "application/json; charset=utf-8") resp, err := client.Do(req) if err != nil { logger.Error(err) } body, err := ioutil.ReadAll(resp.Body) if err != nil { logger.Error(err) } defer resp.Body.Close() logger.Error("SendCustomerServiceMessage body:", string(body)) type CSResp struct { ErrCode int `json:"err_code"` ErrMsg string `json:"err_msg"` } var MsgResp CSResp err = json.Unmarshal(body, &MsgResp) if err != nil { logger.Error(err) } if MsgResp.ErrCode != 0 { logger.Error(MsgResp.ErrMsg) return errors.New(MsgResp.ErrMsg) //_, err := c.Writer.Write([]byte("success")) //if err != nil { // fmt.Println("响应失败") // logger.Error("Signature 响应失败", err) //} //logger.Error("SUCCESS") } return nil } func WipeStringSpace(s string) string { s = strings.Replace(s, " ", "", -1) s = strings.Replace(s, "\n", "", -1) return s }