浏览代码

优化代码

boweniac 8 月之前
父节点
当前提交
40ae775763
共有 3 个文件被更改,包括 74 次插入58 次删除
  1. 2 2
      desc/wechat/wx.api
  2. 70 54
      internal/logic/Wx/get_wx_allow_block_list_logic.go
  3. 2 2
      internal/types/types.go

+ 2 - 2
desc/wechat/wx.api

@@ -160,10 +160,10 @@ type (
     // AllowBlockData
     AllowBlockData {
         // 微信id 公众号微信ID
-        Wxid  *string `json:"wxid,optional"`
+        Wxid  string `json:"wxid,optional"`
 
         // 微信昵称 群备注名称
-        Nickname  *string `json:"nickname,optional"`
+        Nickname  string `json:"nickname,optional"`
     }
 )
 

+ 70 - 54
internal/logic/Wx/get_wx_allow_block_list_logic.go

@@ -51,75 +51,91 @@ func (l *GetWxAllowBlockListLogic) GetWxAllowBlockList(req *types.IDReq) (resp *
 	resp = &types.AllowBlockListResp{}
 	resp.Data = types.AllowBlockListRespData{}
 
-	if data.AllowList != nil {
-		allowList, err := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.AllowList...)).All(l.ctx)
-		if err != nil {
-			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
+	allowListData := make([]*types.AllowBlockData, 0)
+	if data.AllowList == nil || len(data.AllowList) == 0 || data.AllowList[0] == "ALL" {
+		allowListData = append(allowListData,
+			&types.AllowBlockData{
+				Wxid:     "ALL",
+				Nickname: "全部允许",
+			})
+	} else if len(data.AllowList) > 0 {
+		allowList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.AllowList...)).All(l.ctx)
+		if allowList != nil {
+			for _, av := range allowList {
+				allowListData = append(allowListData,
+					&types.AllowBlockData{
+						Wxid:     av.Wxid,
+						Nickname: av.Nickname,
+					})
+			}
 		}
-		// 将 allowList 转换为[]*ContactInfo
-		allowListData := make([]*types.AllowBlockData, 0)
-		for _, av := range allowList {
-			allowListData = append(allowListData,
-				&types.AllowBlockData{
-					Wxid:     &av.Wxid,
-					Nickname: &av.Nickname,
-				})
-		}
-		resp.Data.AllowList = allowListData
 	}
+	resp.Data.AllowList = allowListData
 
-	if data.GroupAllowList != nil {
-		groupAllowList, err := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.GroupAllowList...)).All(l.ctx)
-
-		if err != nil {
-			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-		}
-		// 将 allowList 转换为[]*ContactInfo
-		groupAllowListData := make([]*types.AllowBlockData, 0)
-		for _, av := range groupAllowList {
-			groupAllowListData = append(groupAllowListData,
-				&types.AllowBlockData{
-					Wxid:     &av.Wxid,
-					Nickname: &av.Nickname,
-				})
+	groupAllowListData := make([]*types.AllowBlockData, 0)
+	if data.GroupAllowList == nil || len(data.GroupAllowList) == 0 || data.GroupAllowList[0] == "ALL" {
+		groupAllowListData = append(groupAllowListData,
+			&types.AllowBlockData{
+				Wxid:     "ALL",
+				Nickname: "全部允许",
+			})
+	} else if len(data.GroupAllowList) > 0 {
+		groupAllowList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.GroupAllowList...)).All(l.ctx)
+		if groupAllowList != nil {
+			for _, av := range groupAllowList {
+				groupAllowListData = append(groupAllowListData,
+					&types.AllowBlockData{
+						Wxid:     av.Wxid,
+						Nickname: av.Nickname,
+					})
+			}
 		}
-		resp.Data.GroupAllowList = groupAllowListData
 	}
-
-	if data.BlockList != nil {
-		blockList, err := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.BlockList...)).All(l.ctx)
-
-		if err != nil {
-			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-		}
-		// 将 allowList 转换为[]*ContactInfo
-		blockListData := make([]*types.AllowBlockData, 0)
-		for _, av := range blockList {
+	resp.Data.GroupAllowList = groupAllowListData
+	blockListData := make([]*types.AllowBlockData, 0)
+	if data.BlockList != nil && len(data.BlockList) > 0 {
+		if data.BlockList[0] == "ALL" {
 			blockListData = append(blockListData,
 				&types.AllowBlockData{
-					Wxid:     &av.Wxid,
-					Nickname: &av.Nickname,
+					Wxid:     "ALL",
+					Nickname: "全部禁用",
 				})
+		} else {
+			blockList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.BlockList...)).All(l.ctx)
+			if blockList != nil {
+				for _, av := range blockList {
+					blockListData = append(blockListData,
+						&types.AllowBlockData{
+							Wxid:     av.Wxid,
+							Nickname: av.Nickname,
+						})
+				}
+			}
 		}
-		resp.Data.BlockList = blockListData
 	}
+	resp.Data.BlockList = blockListData
 
-	if data.GroupBlockList != nil {
-		groupBlockList, err := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.GroupBlockList...)).All(l.ctx)
-
-		if err != nil {
-			return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
-		}
-		groupBlockListData := make([]*types.AllowBlockData, 0)
-		for _, av := range groupBlockList {
+	groupBlockListData := make([]*types.AllowBlockData, 0)
+	if data.GroupBlockList != nil && len(data.GroupBlockList) > 0 {
+		if data.GroupBlockList[0] == "ALL" {
 			groupBlockListData = append(groupBlockListData,
 				&types.AllowBlockData{
-					Wxid:     &av.Wxid,
-					Nickname: &av.Nickname,
+					Wxid:     "ALL",
+					Nickname: "全部禁用",
 				})
+		} else {
+			groupBlockList, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(wxid), contact.WxidIn(data.GroupBlockList...)).All(l.ctx)
+			if groupBlockList != nil {
+				for _, av := range groupBlockList {
+					groupBlockListData = append(groupBlockListData,
+						&types.AllowBlockData{
+							Wxid:     av.Wxid,
+							Nickname: av.Nickname,
+						})
+				}
+			}
 		}
-		resp.Data.GroupBlockList = groupBlockListData
 	}
-
+	resp.Data.GroupBlockList = groupBlockListData
 	return resp, nil
 }

+ 2 - 2
internal/types/types.go

@@ -413,9 +413,9 @@ type AllowBlockListRespData struct {
 // AllowBlockData
 type AllowBlockData struct {
 	// 微信id 公众号微信ID
-	Wxid *string `json:"wxid,optional"`
+	Wxid string `json:"wxid,optional"`
 	// 微信昵称 群备注名称
-	Nickname *string `json:"nickname,optional"`
+	Nickname string `json:"nickname,optional"`
 }
 
 // The data of agent information | Agent信息