Pārlūkot izejas kodu

Add remaining changes

boweniac 8 mēneši atpakaļ
vecāks
revīzija
674bb09790

+ 32 - 0
hook/chatroom.go

@@ -2,6 +2,7 @@ package hook
 
 import "fmt"
 
+// 网络获取群成员wxid
 func (h *Hook) BatchGetChatRoomMemberWxid(gid string) (result GetBatchGetChatRoomMemberWxidReap, err error) {
 	resp, err := h.Client.R().SetBody(&GetBatchGetChatRoomMemberWxidReq{
 		Gid: gid,
@@ -15,3 +16,34 @@ func (h *Hook) BatchGetChatRoomMemberWxid(gid string) (result GetBatchGetChatRoo
 	}
 	return
 }
+
+// 批量获取群成员邀请信息
+func (h *Hook) GetChatrooMmemberDetail(gid string) (result GetChatrooMmemberDetailReap, err error) {
+	resp, err := h.Client.R().SetBody(&GetChatrooMmemberDetailReq{
+		Gid: gid,
+	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/GetChatrooMmemberDetail")
+	if err != nil {
+		return
+	}
+	if !resp.IsSuccessState() {
+		err = fmt.Errorf("GetChatrooMmemberDetail failed with status code %d", resp.StatusCode)
+		return
+	}
+	return
+}
+
+// 网络获取群成员详细信息
+func (h *Hook) GetChatroomMemberDetailInfo(gid string, wxid string) (result GetChatroomMemberDetailInfoReap, err error) {
+	resp, err := h.Client.R().SetBody(&GetChatroomMemberDetailInfoReq{
+		Gid:  gid,
+		Wxid: wxid,
+	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/GetChatroomMemberDetailInfo")
+	if err != nil {
+		return
+	}
+	if !resp.IsSuccessState() {
+		err = fmt.Errorf("GetChatroomMemberDetailInfo failed with status code %d", resp.StatusCode)
+		return
+	}
+	return
+}

+ 19 - 0
hook/contact.go

@@ -37,3 +37,22 @@ func (h *Hook) BatchGetContactBriefInfo(wxids []string) (result BatchGetContactB
 	}
 	return
 }
+
+// 添加新的好友
+func (h *Hook) AddNewFriend(v3_wxid string, v4 string, desc string, addType string, role string) (result AddNewFriendReap, err error) {
+	resp, err := h.Client.R().SetBody(&AddNewFriendReq{
+		V3Wxid:  v3_wxid,
+		V4:      v4,
+		Desc:    desc,
+		AddType: addType,
+		Role:    role,
+	}).SetSuccessResult(&result).Post("http://" + h.ServerIp + ":" + h.WxPort + "/AddNewFriend")
+	if err != nil {
+		return
+	}
+	if !resp.IsSuccessState() {
+		err = fmt.Errorf("AddNewFriend failed with status code %d", resp.StatusCode)
+		return
+	}
+	return
+}

+ 56 - 0
hook/type.go

@@ -1,5 +1,61 @@
 package hook
 
+type GetChatrooMmemberDetailReq struct {
+	Gid string `json:"gid"`
+}
+
+type GetChatrooMmemberDetailReap struct {
+	Member []ChatrooMmemberDetail `json:"member"`
+}
+
+type ChatrooMmemberDetail struct {
+	Wxid          string `json:"wxid"`
+	Nickname      string `json:"nickname"`
+	UserHeadBig   string `json:"user_head_big"`
+	UserHeadSmall string `json:"user_head_small"`
+	UserFlag      string `json:"user_flag"`
+	InviterWxid   string `json:"inviter_wxid"`
+}
+
+type GetChatroomMemberDetailInfoReq struct {
+	Gid  string `json:"gid"`
+	Wxid string `json:"wxid"`
+}
+
+type GetChatroomMemberDetailInfoReap struct {
+	Type          string `json:"type"`
+	Wxid          string `json:"wxid"`
+	Nickname      string `json:"nickname"`
+	Sex           string `json:"sex"`
+	Source        string `json:"source"`
+	Province      string `json:"province"`
+	Area          string `json:"area"`
+	Signinfo      string `json:"signinfo"`
+	Wxaccount     string `json:"wxaccount"`
+	Md5           string `json:"Md5"`
+	Timelinebgurl string `json:"timelinebgurl"`
+	Country       string `json:"country"`
+	Headurl       string `json:"headurl"`
+	HeadImgMd5    string `json:"headImgMd5"`
+	V3            string `json:"v3"`
+	Fromchatroom  string `json:"fromchatroom"`
+	V4            string `json:"v4"`
+}
+
+type AddNewFriendReq struct {
+	V3Wxid  string `json:"v3_wxid"`
+	V4      string `json:"v4"`
+	Desc    string `json:"desc"`
+	AddType string `json:"type"`
+	Role    string `json:"role"`
+}
+
+type AddNewFriendReap struct {
+	Status string `json:"Status"`
+	RetTxt string `json:"RetTxt"`
+	RetV3  string `json:"ret_v3"`
+}
+
 type BatchGetContactBriefInfoReq struct {
 	WxidList string `json:"wxidlist"`
 }

+ 44 - 0
internal/handler/contact/add_new_friend_handler.go

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

+ 5 - 0
internal/handler/routes.go

@@ -174,6 +174,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/contact",
 					Handler: contact.GetContactByIdHandler(serverCtx),
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/contact/addNewFriend",
+					Handler: contact.AddNewFriendHandler(serverCtx),
+				},
 			}...,
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 5 - 24
internal/logic/ChatRoomMember/get_chatroom_member_list_logic.go

@@ -42,25 +42,11 @@ func (l *GetChatroomMemberListLogic) GetChatroomMemberList(req *types.ChatroomMe
 
 	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)
+	chatrooMmemberDetail, err := hookClient.GetChatrooMmemberDetail(*req.ChatRoom)
+
 	if err != nil {
-		l.Error("获取微信信息失败", err)
+		l.Error("获取群成员详细信息失败", err)
 		return
-	} else {
-		for _, v := range wxinfos.Info {
-			wxlist[v.Wxid] = v
-		}
-		l.Info("获取微信信息成功", wxlist)
 	}
 
 	resp = &types.ChatroomMemberListResp{
@@ -70,17 +56,12 @@ func (l *GetChatroomMemberListLogic) GetChatroomMemberList(req *types.ChatroomMe
 		},
 	}
 
-	for _, v := range friendAndChatRoomList.Data {
-		nickname := ""
-		if _, ok := wxlist[v.Wxid]; ok {
-			nickname = wxlist[v.Wxid].Nickname
-		}
-
+	for _, v := range chatrooMmemberDetail.Member {
 		resp.Data.Data = append(resp.Data.Data, types.ChatroomMemberInfo{
 			Wxid:      &v.Wxid,
 			ChatRoom:  req.ChatRoom,
 			OwnerWxid: req.OwnerWxid,
-			Account:   &nickname,
+			Account:   &v.Nickname,
 		})
 	}
 

+ 75 - 0
internal/logic/contact/add_new_friend_logic.go

@@ -0,0 +1,75 @@
+package contact
+
+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 AddNewFriendLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewAddNewFriendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddNewFriendLogic {
+	return &AddNewFriendLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *AddNewFriendLogic) AddNewFriend(req *types.AddNewFriendReq) (resp *types.BaseMsgResp, err error) {
+
+	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)
+
+	//通过群id获取群成员详细信息
+	ChatroomMemberDetailInfo, err := hookClient.GetChatroomMemberDetailInfo(req.Gid, req.Wxid)
+
+	if err != nil {
+		l.Error("获取群成员详细信息失败", err)
+		return
+	}
+
+	v3 := req.Wxid
+	v4 := ""
+
+	if ChatroomMemberDetailInfo.V3 != "" {
+		v3 = ChatroomMemberDetailInfo.V3
+	}
+
+	if ChatroomMemberDetailInfo.V4 != "" {
+		v4 = ChatroomMemberDetailInfo.V4
+	}
+
+	AddNewFriendResult, err := hookClient.AddNewFriend(v3, v4, req.Desc, req.AddType, "0")
+	if err != nil {
+		l.Error("添加好友失败", err)
+		return
+	}
+
+	if AddNewFriendResult.Status != "0" {
+		l.Error("添加好友失败", err)
+		return
+	}
+
+	return &types.BaseMsgResp{Msg: errormsg.Success}, nil
+}

+ 9 - 0
internal/types/types.go

@@ -447,6 +447,15 @@ type ContactInfoResp struct {
 	Data ContactInfo `json:"data"`
 }
 
+// swagger:model AddNewFriendReq
+type AddNewFriendReq struct {
+	OwnerWxid string `json:"ownerWxid"`
+	Wxid      string `json:"wxid"`
+	Gid       string `json:"gid"`
+	Desc      string `json:"desc"`
+	AddType   string `json:"addType"`
+}
+
 // ContactLabelList | Contact标签列表
 type ContactLabelList struct {
 	// label