Kaynağa Gözat

增加联系人转人工/AI功能

boweniac 3 ay önce
ebeveyn
işleme
7d320709ea

+ 10 - 0
desc/wechat/contact.api

@@ -56,6 +56,13 @@ type (
         Desc string `json:"desc"`
         AddType string `json:"addType"`
     }
+
+    changeBlockListReq {
+        OwnerWxid string `json:"ownerWxid"`
+        Wxid string `json:"wxid"`
+        Type  *int `json:"type,optional"`
+        Ai *bool `json:"ai,optional"`
+    }
 )
 
 @server(
@@ -87,4 +94,7 @@ service Wechat {
 
     @handler addNewFriend
     post /contact/addNewFriend (AddNewFriendReq) returns (BaseMsgResp)
+
+    @handler changeBlockList
+    post /contact/changeBlockList (changeBlockListReq) returns (BaseMsgResp)
 }

+ 3 - 0
desc/wechat/label_relationship.api

@@ -70,6 +70,9 @@ type (
 
         // 组织ID
         OrganizationId *uint64 `json:"organizationId,optional"`
+
+        // 是否在黑名单中
+        IsInBlockList *bool `json:"isInBlockList,optional"`
     }
     // The response data of label information | Label信息
     LabelInfo {

+ 44 - 0
internal/handler/contact/change_block_list_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/changeBlockList contact ChangeBlockList
+//
+
+//
+
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: changeBlockListReq
+//
+// Responses:
+//  200: BaseMsgResp
+
+func ChangeBlockListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.ChangeBlockListReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := contact.NewChangeBlockListLogic(r.Context(), svcCtx)
+		resp, err := l.ChangeBlockList(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 5 - 0
internal/handler/routes.go

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

+ 88 - 0
internal/logic/contact/change_block_list_logic.go

@@ -0,0 +1,88 @@
+package contact
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"wechat-api/ent/wx"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+	"wechat-api/internal/utils/dberrorhandler"
+
+	"github.com/zeromicro/go-zero/core/logx"
+)
+
+type ChangeBlockListLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewChangeBlockListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeBlockListLogic {
+	return &ChangeBlockListLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *ChangeBlockListLogic) ChangeBlockList(req *types.ChangeBlockListReq) (resp *types.BaseMsgResp, err error) {
+	wxInfo, err := l.svcCtx.DB.Wx.Query().Where(wx.Wxid(req.OwnerWxid)).Only(l.ctx)
+	if wxInfo == nil {
+		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	}
+
+	// 根据请求类型选择对应的黑名单列表
+	blockList := wxInfo.BlockList
+	if *req.Type != 1 {
+		blockList = wxInfo.GroupBlockList
+	}
+
+	if contains(blockList, "ALL") {
+		return nil, errorx.NewDefaultError("请先在账号管理中,取消所属账号的全部黑名单设置")
+	}
+
+	isInBlockList := contains(blockList, req.Wxid)
+	shouldUpdateBlockList := (*req.Ai && isInBlockList) || (!*req.Ai && !isInBlockList)
+
+	if shouldUpdateBlockList {
+		// 更新黑名单: 如果是Ai,移除;如果不是Ai,添加
+		if *req.Ai {
+			blockList = removeElement(blockList, req.Wxid)
+		} else {
+			blockList = append(blockList, req.Wxid)
+		}
+
+		updateQuery := l.svcCtx.DB.Wx.Update().Where(wx.Wxid(req.OwnerWxid))
+		if *req.Type == 1 {
+			updateQuery.SetBlockList(blockList)
+		} else {
+			updateQuery.SetNotNilGroupBlockList(blockList)
+		}
+
+		err = updateQuery.Exec(l.ctx)
+		if err != nil {
+			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+		}
+		l.svcCtx.Rds.HDel(l.ctx, "wx_info", req.OwnerWxid)
+	}
+
+	return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
+}
+
+func contains(slice []string, element string) bool {
+	for _, elem := range slice {
+		if elem == element {
+			return true
+		}
+	}
+	return false
+}
+
+func removeElement(slice []string, element string) []string {
+	for i, elem := range slice {
+		if elem == element {
+			return append(slice[:i], slice[i+1:]...)
+		}
+	}
+	return slice
+}

+ 27 - 4
internal/logic/contact/get_contact_list_logic.go

@@ -89,16 +89,25 @@ func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.
 	resp.Data.Total = data.PageDetails.Total
 
 	wxWxids := []string{}
-	wxWxidsSet := make(map[string]string)
+	wxWxidsSet := make(map[string]*ent.Wx)
+	blockListSet := make(map[string]struct{})
+	groupBlockListSet := make(map[string]struct{})
 	for _, v := range data.List {
 		wxWxids = append(wxWxids, v.WxWxid)
 	}
 	wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
 	for _, w := range wxs {
-		wxWxidsSet[w.Wxid] = w.Nickname
+		wxWxidsSet[w.Wxid] = w
+		for _, b := range w.BlockList {
+			blockListSet[w.Wxid+"_"+b] = struct{}{}
+		}
+		for _, g := range w.GroupBlockList {
+			groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
+		}
 	}
 
 	for _, v := range data.List {
+		isInBlockList := false
 		labelRelationships := make([]types.ContactLabelList, 0)
 		if v.Edges.ContactRelationships != nil {
 			for _, lr := range v.Edges.ContactRelationships {
@@ -112,10 +121,23 @@ func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.
 			}
 		}
 		var wxNickname string
-		if wxWxidsSet[v.WxWxid] == "" {
+		if wxWxidsSet[v.WxWxid] == nil {
 			wxNickname = v.WxWxid
 		} else {
-			wxNickname = wxWxidsSet[v.WxWxid]
+			wxNickname = wxWxidsSet[v.WxWxid].Nickname
+		}
+		if v.Type == 1 {
+			if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
+				isInBlockList = true
+			} else {
+				_, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
+			}
+		} else {
+			if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
+				isInBlockList = true
+			} else {
+				_, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
+			}
 		}
 
 		resp.Data.Data = append(resp.Data.Data,
@@ -143,6 +165,7 @@ func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.
 				Gname:              &v.Gname,
 				V3:                 &v.V3,
 				LabelRelationships: labelRelationships,
+				IsInBlockList:      &isInBlockList,
 			})
 	}
 

+ 10 - 0
internal/types/types.go

@@ -770,6 +770,8 @@ type ContactInfo struct {
 	LabelRelationships []ContactLabelList `json:"labelRelationships,optional"`
 	// 组织ID
 	OrganizationId *uint64 `json:"organizationId,optional"`
+	// 是否在黑名单中
+	IsInBlockList *bool `json:"isInBlockList,optional"`
 }
 
 // The response data of label information | Label信息
@@ -978,6 +980,14 @@ type AddNewFriendReq struct {
 	AddType   string `json:"addType"`
 }
 
+// swagger:model changeBlockListReq
+type ChangeBlockListReq struct {
+	OwnerWxid string `json:"ownerWxid"`
+	Wxid      string `json:"wxid"`
+	Type      *int   `json:"type,optional"`
+	Ai        *bool  `json:"ai,optional"`
+}
+
 // The response data of message information | Message信息
 // swagger:model MessageInfo
 type MessageInfo struct {