Răsfoiți Sursa

获取群组信息
发送接口不进行验证

rowan 9 luni în urmă
părinte
comite
3d99f9ab89

+ 2 - 1
desc/all.api

@@ -9,4 +9,5 @@ import "./wechat/label_relationship.api"
 import "./wechat/sop_task.api"
 import "./wechat/sop_stage.api"
 import "./wechat/sop_node.api"
-import "./wechat/message_records.api"
+import "./wechat/message_records.api"
+import "./wechat/chatroom_member.api"

+ 69 - 0
desc/wechat/chatroom_member.api

@@ -0,0 +1,69 @@
+import "../base.api"
+
+type (
+    // The response data of chatroom member information | ChatroomMember信息
+    ChatroomMemberInfo {
+
+        // 名称 
+        Wxid  *string `json:"wxid,optional"`
+
+        // 群组
+        ChatRoom *string `json:"chatRoom,optional"`
+
+        // 属主Wxid
+        OwnerWxid *string `json:"ownerWxid,optional"`
+
+        // 账号
+        Account *string `json:"account,optional"`
+    }
+
+    // The response data of chatroom member list | ChatroomMember列表数据
+    ChatroomMemberListResp {
+        BaseDataInfo
+
+        // chatroom member list data | chatroom member 列表数据
+        Data ChatroomMemberListInfo `json:"data"`
+    }
+
+    // chatroom member list data | chatroom member列表数据
+    ChatroomMemberListInfo {
+        BaseListInfo
+
+        // The API list data | chatroom member列表数据
+        Data  []ChatroomMemberInfo  `json:"data"`
+    }
+
+    // Get chatroom member list request params | ChatroomMember列表请求参数
+    ChatroomMemberListReq {
+        PageInfo
+
+        // 属主Wxid
+        OwnerWxid *string `json:"ownerWxid,optional"`
+
+        // 群组
+        ChatRoom *string `json:"chatRoom,optional"`
+
+        
+    }
+
+    // chatroom member information response | ChatroomMember信息返回体
+    ChatroomMemberInfoResp {
+        BaseDataInfo
+
+        // Server information | Server数据
+        Data ChatroomMemberInfo `json:"data"`
+    }
+)
+
+@server(
+    jwt: Auth
+    group: ChatRoomMember
+    middleware: Authority
+)
+
+service Wechat {
+
+    // Get chatroom member list | 获取ChatroomMember列表
+    @handler getChatroomMemberList
+    post /chatroom_member/list (ChatroomMemberListReq) returns (ChatroomMemberListResp)
+}

+ 3 - 0
desc/wechat/contact.api

@@ -33,6 +33,9 @@ type (
 
         // 微信账号 
         Account  *string `json:"account,optional"`
+
+        // 联系人类型:1好友,2群组,3公众号,4企业微信联系人
+        Type  *int `json:"type,optional"`
     }
 
     // Contact information response | Contact信息返回体

+ 14 - 3
desc/wechat/wxhook.api

@@ -75,6 +75,17 @@ type (
 )
 
 @server(
+    group: Wxhook
+)
+
+service Wechat {
+    // 发送微信文本消息
+    @handler sendTextMsg
+    post /wxhook/sendTextMsg (SendTextMsgReq) returns (BaseMsgResp)
+}
+
+
+@server(
     jwt: Auth
     group: Wxhook
     middleware: Authority
@@ -93,9 +104,9 @@ service Wechat {
     @handler getFriendsAndGroups
     post /wxhook/getFriendsAndGroups (IDReq) returns (BaseMsgResp)
 
-    // 发送微信文本消息
-    @handler sendTextMsg
-    post /wxhook/sendTextMsg (SendTextMsgReq) returns (BaseMsgResp)
+    // // 发送微信文本消息
+    // @handler sendTextMsg
+    // post /wxhook/sendTextMsg (SendTextMsgReq) returns (BaseMsgResp)
 
     // 发送微信图片
     @handler sendPicMsg

+ 17 - 0
hook/chatroom.go

@@ -0,0 +1,17 @@
+package hook
+
+import "fmt"
+
+func (h *Hook) BatchGetChatRoomMemberWxid(gid string) (result GetBatchGetChatRoomMemberWxidReap, err error) {
+	resp, err := h.Client.R().SetBody(&GetBatchGetChatRoomMemberWxidReq{
+		Gid: gid,
+	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/BatchGetChatRoomMemberWxid")
+	if err != nil {
+		return
+	}
+	if !resp.IsSuccessState() {
+		err = fmt.Errorf("BatchGetChatRoomMemberWxid failed with status code %d", resp.StatusCode)
+		return
+	}
+	return
+}

+ 22 - 1
hook/contact.go

@@ -1,6 +1,9 @@
 package hook
 
-import "fmt"
+import (
+	"fmt"
+	"strings"
+)
 
 // 获取好友和群信息
 func (h *Hook) GetFriendAndChatRoomList(typeStr string) (result GetFriendAndChatRoomListReap, err error) {
@@ -16,3 +19,21 @@ func (h *Hook) GetFriendAndChatRoomList(typeStr string) (result GetFriendAndChat
 	}
 	return
 }
+
+// 批量获取联系人简明信息(每次100个)
+func (h *Hook) BatchGetContactBriefInfo(wxids []string) (result BatchGetContactBriefInfoReap, err error) {
+
+	wxidList := strings.Join(wxids, ",")
+
+	resp, err := h.Client.R().SetBody(&BatchGetContactBriefInfoReq{
+		WxidList: wxidList,
+	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/BatchGetContactBriefInfo")
+	if err != nil {
+		return
+	}
+	if !resp.IsSuccessState() {
+		err = fmt.Errorf("BatchGetChatRoomMemberWxid failed with status code %d", resp.StatusCode)
+		return
+	}
+	return
+}

+ 34 - 0
hook/type.go

@@ -1,5 +1,39 @@
 package hook
 
+type BatchGetContactBriefInfoReq struct {
+	WxidList string `json:"wxidlist"`
+}
+
+type BriefInfo struct {
+	Wxid      string `json:"wxid"`
+	WXAccount string `json:"wx_account"`
+	Status    string `json:"status"`
+	Nickname  string `json:"nickname"`
+	Markname  string `json:"markname"`
+	Country   string `json:"country"`
+	Province  string `json:"province"`
+	City      string `json:"city"`
+	Sex       string `json:"sex"`
+	Bighead   string `json:"bighead"`
+	Smallhead string `json:"smallhead"`
+	Labeltag  string `json:"labeltag"`
+}
+
+type BatchGetContactBriefInfoReap struct {
+	Count string      `json:"count"`
+	Info  []BriefInfo `json:"info"`
+}
+
+type GetBatchGetChatRoomMemberWxidReq struct {
+	Gid string `json:"gid"`
+}
+
+type GetBatchGetChatRoomMemberWxidReap struct {
+	Data []struct {
+		Wxid string `json:"wxid"`
+	} `json:"data"`
+}
+
 type GetFriendAndChatRoomListReq struct {
 	Type string `json:"type"`
 }

+ 44 - 0
internal/handler/ChatRoomMember/get_chatroom_member_list_handler.go

@@ -0,0 +1,44 @@
+package ChatRoomMember
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/ChatRoomMember"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /chatroom_member/list ChatRoomMember GetChatroomMemberList
+//
+// Get chatroom member list | 获取ChatroomMember列表
+//
+// Get chatroom member list | 获取ChatroomMember列表
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: ChatroomMemberListReq
+//
+// Responses:
+//  200: ChatroomMemberListResp
+
+func GetChatroomMemberListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.ChatroomMemberListReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := ChatRoomMember.NewGetChatroomMemberListLogic(r.Context(), svcCtx)
+		resp, err := l.GetChatroomMemberList(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 25 - 5
internal/handler/routes.go

@@ -4,6 +4,7 @@ package handler
 import (
 	"net/http"
 
+	ChatRoomMember "wechat-api/internal/handler/ChatRoomMember"
 	Message "wechat-api/internal/handler/Message"
 	WechatServer "wechat-api/internal/handler/WechatServer"
 	Wx "wechat-api/internal/handler/Wx"
@@ -101,6 +102,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 	)
 
 	server.AddRoutes(
+		[]rest.Route{
+			{
+				Method:  http.MethodPost,
+				Path:    "/wxhook/sendTextMsg",
+				Handler: Wxhook.SendTextMsgHandler(serverCtx),
+			},
+		},
+	)
+
+	server.AddRoutes(
 		rest.WithMiddlewares(
 			[]rest.Middleware{serverCtx.Authority},
 			[]rest.Route{
@@ -121,11 +132,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 				},
 				{
 					Method:  http.MethodPost,
-					Path:    "/wxhook/sendTextMsg",
-					Handler: Wxhook.SendTextMsgHandler(serverCtx),
-				},
-				{
-					Method:  http.MethodPost,
 					Path:    "/wxhook/sendPicMsg",
 					Handler: Wxhook.SendPicMsgHandler(serverCtx),
 				},
@@ -440,4 +446,18 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
 	)
+
+	server.AddRoutes(
+		rest.WithMiddlewares(
+			[]rest.Middleware{serverCtx.Authority},
+			[]rest.Route{
+				{
+					Method:  http.MethodPost,
+					Path:    "/chatroom_member/list",
+					Handler: ChatRoomMember.GetChatroomMemberListHandler(serverCtx),
+				},
+			}...,
+		),
+		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
+	)
 }

+ 88 - 0
internal/logic/ChatRoomMember/get_chatroom_member_list_logic.go

@@ -0,0 +1,88 @@
+package ChatRoomMember
+
+import (
+	"context"
+
+	"wechat-api/ent/wx"
+	"wechat-api/hook"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type GetChatroomMemberListLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewGetChatroomMemberListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChatroomMemberListLogic {
+	return &GetChatroomMemberListLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *GetChatroomMemberListLogic) GetChatroomMemberList(req *types.ChatroomMemberListReq) (resp *types.ChatroomMemberListResp, err error) {
+	// todo: add your logic here and delete this line
+	wxInfo, err := l.svcCtx.DB.Wx.Query().Where(wx.Wxid(*req.OwnerWxid)).First(l.ctx)
+
+	if err != nil {
+		l.Error("查询微信信息失败", err)
+		return
+	}
+
+	serverInfo, err := l.svcCtx.DB.Server.Get(l.ctx, wxInfo.ServerID)
+	if err != nil {
+		l.Error("查询服务器信息失败", err)
+		return
+	}
+
+	hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, wxInfo.Port)
+
+	friendAndChatRoomList, err := hookClient.BatchGetChatRoomMemberWxid(*req.ChatRoom)
+	if err != nil {
+		l.Error("获取群成员列表失败", err)
+		return
+	}
+	wxlist := make(map[string]hook.BriefInfo)
+	var wxidList []string
+	for _, v := range friendAndChatRoomList.Data {
+		wxidList = append(wxidList, v.Wxid)
+	}
+	wxinfos, err := hookClient.BatchGetContactBriefInfo(wxidList)
+	if err != nil {
+		l.Error("获取微信信息失败", err)
+		return
+	} else {
+		for _, v := range wxinfos.Info {
+			wxlist[v.Wxid] = v
+		}
+		l.Info("获取微信信息成功", wxlist)
+	}
+
+	resp = &types.ChatroomMemberListResp{
+		BaseDataInfo: types.BaseDataInfo{
+			Code: 0,
+			Msg:  errormsg.Success,
+		},
+	}
+
+	for _, v := range friendAndChatRoomList.Data {
+		nickname := ""
+		if _, ok := wxlist[v.Wxid]; ok {
+			nickname = wxlist[v.Wxid].Nickname
+		}
+
+		resp.Data.Data = append(resp.Data.Data, types.ChatroomMemberInfo{
+			Wxid:      &v.Wxid,
+			ChatRoom:  req.ChatRoom,
+			OwnerWxid: req.OwnerWxid,
+			Account:   &nickname,
+		})
+	}
+
+	return resp, nil
+}

+ 3 - 0
internal/logic/contact/get_contact_list_logic.go

@@ -66,6 +66,9 @@ func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.
 	if req.Account != nil {
 		predicates = append(predicates, contact.AccountContains(*req.Account))
 	}
+	if req.Type != nil {
+		predicates = append(predicates, contact.TypeEQ(*req.Type))
+	}
 	data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
 		query.WithLabels()
 	}).Page(l.ctx, req.Page, req.PageSize)

+ 49 - 0
internal/types/types.go

@@ -435,6 +435,8 @@ type ContactListReq struct {
 	Wxid *string `json:"wxid,optional"`
 	// 微信账号
 	Account *string `json:"account,optional"`
+	// 联系人类型:1好友,2群组,3公众号,4企业微信联系人
+	Type *int `json:"type,optional"`
 }
 
 // Contact information response | Contact信息返回体
@@ -914,3 +916,50 @@ type MessageRecordsInfoResp struct {
 	// MessageRecords information | MessageRecords数据
 	Data MessageRecordsInfo `json:"data"`
 }
+
+// The response data of chatroom member information | ChatroomMember信息
+// swagger:model ChatroomMemberInfo
+type ChatroomMemberInfo struct {
+	// 名称
+	Wxid *string `json:"wxid,optional"`
+	// 群组
+	ChatRoom *string `json:"chatRoom,optional"`
+	// 属主Wxid
+	OwnerWxid *string `json:"ownerWxid,optional"`
+	// 账号
+	Account *string `json:"account,optional"`
+}
+
+// The response data of chatroom member list | ChatroomMember列表数据
+// swagger:model ChatroomMemberListResp
+type ChatroomMemberListResp struct {
+	BaseDataInfo
+	// chatroom member list data | chatroom member 列表数据
+	Data ChatroomMemberListInfo `json:"data"`
+}
+
+// chatroom member list data | chatroom member列表数据
+// swagger:model ChatroomMemberListInfo
+type ChatroomMemberListInfo struct {
+	BaseListInfo
+	// The API list data | chatroom member列表数据
+	Data []ChatroomMemberInfo `json:"data"`
+}
+
+// Get chatroom member list request params | ChatroomMember列表请求参数
+// swagger:model ChatroomMemberListReq
+type ChatroomMemberListReq struct {
+	PageInfo
+	// 属主Wxid
+	OwnerWxid *string `json:"ownerWxid,optional"`
+	// 群组
+	ChatRoom *string `json:"chatRoom,optional"`
+}
+
+// chatroom member information response | ChatroomMember信息返回体
+// swagger:model ChatroomMemberInfoResp
+type ChatroomMemberInfoResp struct {
+	BaseDataInfo
+	// Server information | Server数据
+	Data ChatroomMemberInfo `json:"data"`
+}