Browse Source

实现企微发送

boweniac 1 month ago
parent
commit
e3ec08da4c

+ 2 - 1
desc/all.api

@@ -41,4 +41,5 @@ import "./wechat/credit_balance.api"
 import "./wechat/credit_usage.api"
 import "./wechat/pay_recharge.api"
 import "./wechat/whatsapp.api"
-import "./wechat/whatsapp_channel.api"
+import "./wechat/whatsapp_channel.api"
+import "./wp_wecom/send_msg.api"

+ 26 - 0
desc/wp_wecom/send_msg.api

@@ -0,0 +1,26 @@
+import "../base.api"
+
+type (
+    WpWecomMsgReq {
+        // 机器人微信id
+        WxId *string `json:"wx_id"`
+        // 接收方微信 id
+        ConvId *string `json:"conv_id"`
+        // 消息类型
+        ContentType *string `json:"content_type"`
+        // 消息内容
+        Content *string `json:"content"`
+    }
+)
+
+@server(
+	group: wp_wecom
+)
+
+service Wechat {
+    @handler sendMsg
+    post /wp/wecom/send_msg (WpWecomMsgReq) returns (BaseMsgResp)
+
+    @handler sendMsgByChan
+    post /wp/wecom/send_msg_by_chan (WpWecomMsgReq) returns (BaseMsgResp)
+}

+ 16 - 0
internal/handler/routes.go

@@ -44,6 +44,7 @@ import (
 	whatsapp "wechat-api/internal/handler/whatsapp"
 	whatsapp_channel "wechat-api/internal/handler/whatsapp_channel"
 	work_experience "wechat-api/internal/handler/work_experience"
+	wp_wecom "wechat-api/internal/handler/wp_wecom"
 	wxcard "wechat-api/internal/handler/wxcard"
 	wxcarduser "wechat-api/internal/handler/wxcarduser"
 	wxcardvisit "wechat-api/internal/handler/wxcardvisit"
@@ -2037,4 +2038,19 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
 	)
+
+	server.AddRoutes(
+		[]rest.Route{
+			{
+				Method:  http.MethodPost,
+				Path:    "/wp/wecom/send_msg",
+				Handler: wp_wecom.SendMsgHandler(serverCtx),
+			},
+			{
+				Method:  http.MethodPost,
+				Path:    "/wp/wecom/send_msg_by_chan",
+				Handler: wp_wecom.SendMsgByChanHandler(serverCtx),
+			},
+		},
+	)
 }

+ 44 - 0
internal/handler/wp_wecom/send_msg_by_chan_handler.go

@@ -0,0 +1,44 @@
+package wp_wecom
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/wp_wecom"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /wp/wecom/send_msg_by_chan wp_wecom SendMsgByChan
+//
+
+//
+
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: WpWecomMsgReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func SendMsgByChanHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.WpWecomMsgReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := wp_wecom.NewSendMsgByChanLogic(r.Context(), svcCtx)
+		resp, err := l.SendMsgByChan(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/wp_wecom/send_msg_handler.go

@@ -0,0 +1,44 @@
+package wp_wecom
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/wp_wecom"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route get /wp/wecom/send_msg wp_wecom SendMsg
+//
+
+//
+
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: WpWecomMsgReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func SendMsgHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.WpWecomMsgReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := wp_wecom.NewSendMsgLogic(r.Context(), svcCtx)
+		resp, err := l.SendMsg(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 29 - 0
internal/logic/wp_wecom/send_msg_by_chan_logic.go

@@ -0,0 +1,29 @@
+package wp_wecom
+
+import (
+	"context"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type SendMsgByChanLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewSendMsgByChanLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMsgByChanLogic {
+	return &SendMsgByChanLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *SendMsgByChanLogic) SendMsgByChan(req *types.WpWecomMsgReq) (resp *types.BaseMsgResp, err error) {
+	// todo: add your logic here and delete this line
+
+	return
+}

+ 44 - 0
internal/logic/wp_wecom/send_msg_logic.go

@@ -0,0 +1,44 @@
+package wp_wecom
+
+import (
+	"context"
+	"encoding/base64"
+	"encoding/json"
+
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type SendMsgLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewSendMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMsgLogic {
+	return &SendMsgLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *SendMsgLogic) SendMsg(req *types.WpWecomMsgReq) (resp *types.BaseMsgResp, err error) {
+	data := map[string]interface{}{
+		"MsgType": "TalkToFriendTask",
+		"Content": map[string]interface{}{
+			"WxId":        req.WxId,
+			"ConvId":      req.ConvId,
+			"ContentType": req.ContentType,
+			"Content":     base64.StdEncoding.EncodeToString([]byte(*req.Content)),
+		},
+	}
+	jsonStr, err := json.Marshal(data)
+	err = l.svcCtx.WechatWs["wecom"].SendMsg([]byte(jsonStr))
+	if err != nil {
+		return nil, err
+	}
+
+	return
+}

+ 1 - 1
internal/svc/service_context.go

@@ -43,7 +43,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
 
 	// 初始化微信ws客户端
 	// todo 现在配置是从 config.yaml中读取的,后续需要改成从数据库中读取,以便匹配不同的微信号
-	var wsClients map[string]*wechat_ws.WechatWsClient
+	wsClients := make(map[string]*wechat_ws.WechatWsClient)
 	for _, ws := range c.WeChatWs {
 		client, err := wechat_ws.NewWechatWsClient(ws.Url, ws.Appid)
 		if err != nil {

+ 12 - 0
internal/types/types.go

@@ -4017,3 +4017,15 @@ type WhatsappChannelInfoResp struct {
 	// WhatsappChannel information | WhatsappChannel数据
 	Data WhatsappChannelInfo `json:"data"`
 }
+
+// swagger:model WpWecomMsgReq
+type WpWecomMsgReq struct {
+	// 机器人微信id
+	WxId *string `json:"wx_id"`
+	// 接收方微信 id
+	ConvId *string `json:"conv_id"`
+	// 消息类型
+	ContentType *string `json:"content_type"`
+	// 消息内容
+	Content *string `json:"content"`
+}