浏览代码

执行添加好友的列表

lichangdong 3 天之前
父节点
当前提交
db861d88f2

+ 46 - 1
desc/wechat/add_wechat_friend_log.api

@@ -1,6 +1,5 @@
 import "../base.api"
 import "../base.api"
 
 
-
 type (
 type (
     
     
     //add_friend_by_phone api接口请求值
     //add_friend_by_phone api接口请求值
@@ -11,6 +10,43 @@ type (
 		Message string `json:"message"`
 		Message string `json:"message"`
 		CallbackURL string `json:"callback_url,optional"`
 		CallbackURL string `json:"callback_url,optional"`
     }
     }
+
+    AddFriendListReq {
+        PageInfo
+        OwnerWxId *string `json:"wxid,optional"`
+        OwnerWxType *int `json:"wxType,optional"`
+        FindContent *string `json:"phone,optional"`
+        status *int `json:"status,optional"`
+    }
+
+     AddFriendListResp {
+        BaseDataInfo
+        Data FriendListInfo `json:"data"`
+     }
+
+    FriendListInfo {
+        BaseListInfo
+        Data []FriendList `json:"data"`
+    }
+
+    FriendList{
+        Id int64 `json:"id,optional"`
+        // 微信id 公众号微信ID
+        OwnerWxId  *string `json:"wxId,optional"`
+        OwnerWxType  *string `json:"wxType,optional"`
+        FindContent  *string `json:"phone,optional"`
+        Status *string `json:"status,optional"`
+        ErrMessage  *string `json:"err_message,optional"`
+        TaskCount  *int `json:"err_message,optional"`
+        // 微信昵称 群备注名称
+        Message  *string `json:"message,optional"`
+        CreateTime  *string `json:"create_time,optional"`
+        UpdateTime  *string `json:"update_time,optional"`
+    }
+
+    CancelByIdsReq {
+        Ids []int64 `json:"ids" validate:"nonzero"`
+    }
 )
 )
 
 
 @server(
 @server(
@@ -23,4 +59,13 @@ service Wechat {
     // 手机号加好友接口
     // 手机号加好友接口
     @handler AddFriendByPhone
     @handler AddFriendByPhone
     post /add_friend/add_friend_by_phone (AddWechatFriendLogInfo) returns (BaseMsgResp)
     post /add_friend/add_friend_by_phone (AddWechatFriendLogInfo) returns (BaseMsgResp)
+
+    // 添加好友的列表接口
+    @handler AddFriendList
+    post /add_friend/add_friend_list (AddFriendListReq) returns (AddFriendListResp)
+
+    // cancel task add friend
+    @handler CancelByIds
+    post /add_friend/cancel_by_ids (CancelByIdsReq) returns (BaseMsgResp)
+
 } 
 } 

+ 1 - 1
ent/schema/add_wechat_friend_log.go

@@ -55,7 +55,7 @@ func (AddWechatFriendLog) Fields() []ent.Field {
 			Comment("任务执行次数"),
 			Comment("任务执行次数"),
 		field.Int("task_count").
 		field.Int("task_count").
 			Default(0).
 			Default(0).
-			Comment("是否可以添加好友(与属主账号非好友且其数据查询正常时 0 不可以,1 可以 2成功添加申请)"),
+			Comment("是否可以添加好友(与属主账号非好友且其数据查询正常时 0数据准备中,1 待执行 2成功添加申请 3timeout及其他错误 4用户不存在 5手动取消)"),
 		field.Int64("task_id").Default(0).Comment("添加时候的请求体"),
 		field.Int64("task_id").Default(0).Comment("添加时候的请求体"),
 		field.JSON("add_request", map[string]interface{}{}).
 		field.JSON("add_request", map[string]interface{}{}).
 			Optional().Comment("添加时候的请求体"),
 			Optional().Comment("添加时候的请求体"),

+ 42 - 0
internal/handler/add_friend/add_friend_list_handler.go

@@ -0,0 +1,42 @@
+package add_friend
+
+import (
+	"net/http"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+
+	"wechat-api/internal/logic/add_friend"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+)
+
+// swagger:route post /add_friend/add_friend_list add_friend AddFriendList
+//
+// 添加好友的列表接口
+//
+// Parameters:
+//  + name: body
+//    require: true
+//    in: body
+//    type: AddFriendListReq
+//
+// Responses:
+//  200: AddFriendListResp
+
+func AddFriendListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		var req types.AddFriendListReq
+		if err := httpx.Parse(r, &req, true); err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+			return
+		}
+
+		l := add_friend.NewAddFriendListLogic(r.Context(), svcCtx)
+		resp, err := l.AddFriendList(&req)
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 44 - 0
internal/handler/add_friend/cancel_by_ids_handler.go

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

+ 11 - 1
internal/handler/routes.go

@@ -1,5 +1,5 @@
 // Code generated by goctl. DO NOT EDIT.
 // Code generated by goctl. DO NOT EDIT.
-// goctls v1.10.1
+// goctls v1.10.4
 
 
 package handler
 package handler
 
 
@@ -2351,6 +2351,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
 					Path:    "/add_friend/add_friend_by_phone",
 					Path:    "/add_friend/add_friend_by_phone",
 					Handler: add_friend.AddFriendByPhoneHandler(serverCtx),
 					Handler: add_friend.AddFriendByPhoneHandler(serverCtx),
 				},
 				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/add_friend/add_friend_list",
+					Handler: add_friend.AddFriendListHandler(serverCtx),
+				},
+				{
+					Method:  http.MethodPost,
+					Path:    "/add_friend/cancel_by_ids",
+					Handler: add_friend.CancelByIdsHandler(serverCtx),
+				},
 			}...,
 			}...,
 		),
 		),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
 		rest.WithJwt(serverCtx.Config.Auth.AccessSecret),

+ 114 - 0
internal/logic/add_friend/add_friend_list_logic.go

@@ -0,0 +1,114 @@
+package add_friend
+
+import (
+	"context"
+	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/errorx"
+	"github.com/zeromicro/go-zero/core/logx"
+	"wechat-api/ent/addwechatfriendlog"
+	"wechat-api/ent/predicate"
+	"wechat-api/ent/wx"
+	"wechat-api/internal/svc"
+	"wechat-api/internal/types"
+	"wechat-api/internal/utils"
+)
+
+type AddFriendListLogic struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+}
+
+func NewAddFriendListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFriendListLogic {
+	return &AddFriendListLogic{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx}
+}
+
+func (l *AddFriendListLogic) AddFriendList(req *types.AddFriendListReq) (resp *types.AddFriendListResp, err error) {
+	//获取上下文的组织id
+	organizationId := l.ctx.Value("organizationId").(uint64)
+	//通过organizationId去wx表拿微信机器人的数
+
+	query := l.svcCtx.DB.Wx.Query().Where(wx.OrganizationIDEQ(organizationId), wx.StatusEQ(1))
+	// 动态添加条件示例:假设 req.OwnerWxId 存在时添加额外的 Where 条件
+	if utils.IsNonEmptyString(req.OwnerWxId) {
+		query = query.Where(wx.WxidEQ(*req.OwnerWxId))
+	}
+	wxIds, err := query.Select(wx.FieldWxid).Strings(l.ctx)
+	if err != nil {
+		//数据库错误,不直接返回错误,防止暴露数据库错误
+		return nil, errorx.NewInvalidArgumentError("请联系管理员")
+	}
+	//机器人列表为空,直接返回
+	if len(wxIds) == 0 {
+		return nil, errorx.NewInvalidArgumentError("当前组织架构下,没有wx机器人")
+	}
+	var predicates []predicate.AddWechatFriendLog
+
+	predicates = append(predicates, addwechatfriendlog.OwnerWxIDIn(wxIds...))
+	ownerWxType := req.OwnerWxType
+	//查询企业微信机器人加的好友
+	if *ownerWxType > 0 {
+		predicates = append(predicates, addwechatfriendlog.OwnerWxTypeEQ(*ownerWxType))
+	}
+	//查找添加好友
+	if utils.IsNonEmptyString(req.FindContent) {
+		predicates = append(predicates, addwechatfriendlog.FindContent(*req.FindContent))
+	}
+
+	var IsCanAdd []int
+	if req.Status != nil {
+		switch *req.Status {
+		case 1:
+			IsCanAdd = append(IsCanAdd, 0, 1)
+		case 2:
+			IsCanAdd = append(IsCanAdd, 3, 4)
+		default:
+			IsCanAdd = append(IsCanAdd, 2)
+		}
+		predicates = append(predicates, addwechatfriendlog.IsCanAddIn(IsCanAdd...))
+	}
+	data, err := l.svcCtx.DB.AddWechatFriendLog.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
+
+	if err != nil {
+		return nil, errorx.NewInvalidArgumentError("请联系管理员")
+	}
+	resp = &types.AddFriendListResp{}
+	resp.Msg = errormsg.Success
+	resp.Data.Total = data.PageDetails.Total
+	for _, v := range data.List {
+		var wxString string
+		if v.OwnerWxType == 1 {
+			wxString = "个微"
+		} else {
+			wxString = "企微"
+		}
+		resp.Data.Data = append(resp.Data.Data,
+			types.FriendList{
+				Id:          v.ID,
+				OwnerWxId:   &v.OwnerWxID,
+				OwnerWxType: &wxString,
+				FindContent: &v.FindContent,
+				Status: func() *string {
+					statusMap := map[int]string{
+						0: "数据准备中",
+						1: "待执行",
+						2: "成功邀请",
+						3: "timeout及其他错误",
+						4: "用户不存在",
+						5: "后台取消",
+					}
+					statusStr := statusMap[v.IsCanAdd]
+					return &statusStr
+				}(),
+				TaskCount:  &v.TaskCount,
+				Message:    &v.Message,
+				CreateTime: utils.UnixTimeToBeijing(v.CreatedAt),
+				UpdateTime: utils.UnixTimeToBeijing(v.UpdatedAt),
+			},
+		)
+	}
+	return resp, nil
+}

+ 0 - 6
internal/pkg/util/strings.go

@@ -1,6 +0,0 @@
-package util
-
-// IsNonEmptyString 判断字符串指针不为 nil 且不为空
-func IsNonEmptyString(s *string) bool {
-	return s != nil && *s != ""
-}

+ 8 - 2
internal/service/MessageHandlers/task_result_notice.go

@@ -61,8 +61,14 @@ func (f *TaskResultNoticeHandler) Handle(ctx context.Context, msg *wechat_ws.Msg
 		SetAddResult(m).
 		SetAddResult(m).
 		SetUpdatedAt(time.Now().Unix())
 		SetUpdatedAt(time.Now().Unix())
 
 
-	if common.ErrMsg == "该用户不存在" {
-		update.SetIsCanAdd(0) // 重置添加为不能添加好友
+	if common.Code == "InternalError" {
+		if common.ErrMsg == "timeout" {
+			update.SetIsCanAdd(3) // 重置添加为不能添加好友
+		} else if common.ErrMsg == "用户不存在" {
+			update.SetIsCanAdd(4) // 重置添加为不能添加好友
+		} else {
+			update.SetIsCanAdd(3)
+		} // 重置添加为不能添加好友
 	}
 	}
 
 
 	if common.Success {
 	if common.Success {

+ 41 - 0
internal/types/types.go

@@ -4726,3 +4726,44 @@ type AddWechatFriendLogInfo struct {
 	Message     string   `json:"message"`
 	Message     string   `json:"message"`
 	CallbackURL string   `json:"callback_url,optional"`
 	CallbackURL string   `json:"callback_url,optional"`
 }
 }
+
+// swagger:model AddFriendListReq
+type AddFriendListReq struct {
+	PageInfo
+	OwnerWxId   *string `json:"wxid,optional"`
+	OwnerWxType *int    `json:"wxType,optional"`
+	FindContent *string `json:"phone,optional"`
+	Status      *int    `json:"status,optional"`
+}
+
+// swagger:model AddFriendListResp
+type AddFriendListResp struct {
+	BaseDataInfo
+	Data FriendListInfo `json:"data"`
+}
+
+// swagger:model FriendListInfo
+type FriendListInfo struct {
+	BaseListInfo
+	Data []FriendList `json:"data"`
+}
+
+type FriendList struct {
+	Id int64 `json:"id,optional"`
+	// 微信id 公众号微信ID
+	OwnerWxId   *string `json:"wxId,optional"`
+	OwnerWxType *string `json:"wxType,optional"`
+	FindContent *string `json:"phone,optional"`
+	Status      *string `json:"status,optional"`
+	ErrMessage  *string `json:"err_message,optional"`
+	TaskCount   *int    `json:"err_message,optional"`
+	// 微信昵称 群备注名称
+	Message    *string `json:"message,optional"`
+	CreateTime *string `json:"create_time,optional"`
+	UpdateTime *string `json:"update_time,optional"`
+}
+
+// swagger:model CancelByIdsReq
+type CancelByIdsReq struct {
+	Ids []int64 `json:"ids" validate:"nonzero"`
+}

+ 26 - 0
internal/utils/strings.go

@@ -0,0 +1,26 @@
+package utils
+
+import "time"
+
+// IsNonEmptyString 判断字符串指针不为 nil 且不为空
+func IsNonEmptyString(s *string) bool {
+	return s != nil && *s != ""
+}
+
+// UnixTimeToBeijing 将 Unix 时间戳转换为北京时间字符串
+func UnixTimeToBeijing(i any) *string {
+	// 断言输入为 int64 类型
+	unixTime, ok := i.(int64)
+	if !ok {
+		return nil
+	}
+
+	// 转换为北京时间(UTC+8)
+	beijingTime := time.Unix(unixTime, 0).In(time.FixedZone("CST", 8*3600))
+
+	// 格式化时间字符串
+	formattedTime := beijingTime.Format("2006-01-02 15:04:05")
+
+	// 返回字符串指针
+	return &formattedTime
+}