package controllers
import (
"encoding/json"
"eta/eta_pub/models"
"eta/eta_pub/services"
"eta/eta_pub/services/alarm_msg"
"eta/eta_pub/utils"
)
// 发送模板消息
type WechatController struct {
BaseAuthController
}
// @Title 推送模板消息
// @Description 推送模板消息
// @Success 200 新增成功
// @router /send_template_msg [post]
func (this *WechatController) SendTemplateMsg() {
br := new(models.BaseResponse).Init()
defer func() {
this.Data["json"] = br
this.ServeJSON()
}()
body := this.Ctx.Input.RequestBody
sendItem := new(models.SendWxTemplate)
err := json.Unmarshal(body, &sendItem)
if err != nil {
br.Msg = "参数解析失败"
br.ErrMsg = "参数解析失败,Err:" + err.Error()
return
}
if sendItem.TemplateId == "" {
br.Msg = "参数错误,模板id为空"
return
}
if len(sendItem.OpenIdArr) <= 0 {
br.Msg = "openid为空"
return
}
if sendItem.WxAppId == "" {
sendItem.WxAppId = utils.WxAppId //默认是弘则研究公众号的appId
}
go func() {
if sendItem.RedirectTarget == 3 {
err = services.SendWxTemplateMsgCygx(sendItem)
} else {
err = services.SendWxTemplateMsg(sendItem)
}
if err != nil {
msg := "URI:" + this.Ctx.Input.URI() + "
" + "Params" + string(body) + "
" + "ErrMsg:" + err.Error() + ";
Body:" + string(body) + "
"
go alarm_msg.SendAlarmMsg(msg, 1)
}
}()
br.Ret = 200
br.Msg = "推送成功"
return
}
// GetAccessToken
// @Title 获取微信token
// @Description 获取微信token
// @Success 200
// @router /access_token [post]
func (this *WechatController) GetAccessToken() {
br := new(models.BaseResponse).Init()
defer func() {
this.Data["json"] = br
this.ServeJSON()
}()
body := this.Ctx.Input.RequestBody
req := new(models.WxTokenReq)
err := json.Unmarshal(body, &req)
if err != nil {
br.Msg = "参数解析失败"
br.ErrMsg = "参数解析失败,Err:" + err.Error()
return
}
if req.WxAppId == "" {
br.Msg = "参数错误,微信AppId为空"
return
}
if req.WxAppSecret == "" {
br.Msg = "参数错误,微信AppSecret为空"
return
}
token, err, errMsg := services.GetWxAccessToken(req.WxAppId, req.WxAppSecret)
if err != nil {
br.Msg = errMsg
br.ErrMsg = err.Error()
return
}
data := models.WxTokenResp{AccessToken: token}
br.Ret = 200
br.Msg = "推送成功"
br.Data = data
return
}
// @Title 推送类目模板消息
// @Description 推送模板消息
// @Success 200 新增成功
// @router /send_category_template_msg [post]
func (this *WechatController) SendCategoryTemplateMsg() {
br := new(models.BaseResponse).Init()
defer func() {
this.Data["json"] = br
this.ServeJSON()
}()
body := this.Ctx.Input.RequestBody
sendItem := new(models.SendWxCategoryTemplate)
err := json.Unmarshal(body, &sendItem)
if err != nil {
br.Msg = "参数解析失败"
br.ErrMsg = "参数解析失败,Err:" + err.Error()
return
}
if sendItem.TemplateId == "" {
br.Msg = "参数错误,模板id为空"
return
}
if len(sendItem.OpenIdArr) <= 0 {
br.Msg = "openid为空"
return
}
sendItem.WxAppId = utils.WxMfyxAppId //默认是买方研选公众号
go func() {
err = services.SendWxCategoryTemplateMsgCygx(sendItem)
if err != nil {
msg := "URI:" + this.Ctx.Input.URI() + "
" + "Params" + string(body) + "
" + "ErrMsg:" + err.Error() + ";
Body:" + string(body) + "
"
go alarm_msg.SendAlarmMsg(msg, 1)
}
}()
br.Ret = 200
br.Msg = "推送成功"
return
}