浏览代码

Merge branch 'task_526_lichangdong_250424' into debug

lichangdong 3 天之前
父节点
当前提交
b0e0f6bb4a
共有 2 个文件被更改,包括 53 次插入13 次删除
  1. 1 1
      ent/schema/add_wechat_friend_log.go
  2. 52 12
      internal/logic/chat/add_friend_by_phone_logic.go

+ 1 - 1
ent/schema/add_wechat_friend_log.go

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

+ 52 - 12
internal/logic/chat/add_friend_by_phone_logic.go

@@ -3,6 +3,7 @@ package chat
 import (
 	"context"
 	"errors"
+	"fmt"
 
 	"wechat-api/ent"
 	"wechat-api/ent/predicate"
@@ -32,13 +33,13 @@ func NewAddFriendByPhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
 		svcCtx: svcCtx}
 }
 
-func GetRandWxid(idList []string) string {
-	id := ""
-	if len(idList) > 0 {
-		typekit.ShuffleList(idList)
-		id = idList[0]
+func GetListRandOne(strList []string) string {
+	one := ""
+	if len(strList) > 0 {
+		typekit.ShuffleList(strList)
+		one = strList[0]
 	}
-	return id
+	return one
 }
 
 func (l *AddFriendByPhoneLogic) getWxInfoList(preds ...predicate.Wx) ([]*ent.Wx, error) {
@@ -81,7 +82,7 @@ func (l *AddFriendByPhoneLogic) rebuildAddFriendByPhoneInfos(req *types.AddFrien
 		return nil, err
 	}
 	infos := []AddFriendByPhoneInfo{}
-	for idx, phone := range req.Phones {
+	for idx, phone := range req.Phones { //以phones为主,逐一绑定wxid,多余的wxid忽略
 		info := AddFriendByPhoneInfo{Type: req.Type, WeChatId: req.WeChatIds[idx],
 			Phone: phone, Message: req.Message, CallbackURL: req.CallbackURL, Oid: int64(Oid)}
 		infos = append(infos, info)
@@ -89,6 +90,44 @@ func (l *AddFriendByPhoneLogic) rebuildAddFriendByPhoneInfos(req *types.AddFrien
 	return infos, nil
 }
 
+/*
+1.手机号是必传的,至少有1个
+2.微信号可以不传为空,这种情况拿当前登录者同租户OID的微信ID随机绑定传入的每个手机号
+3.微信号数量等于或者大于手机号数量,则按输入前后顺序逐一绑定,多出来的微信号忽略
+4.当微信号数量小于手机号数量,多出来的手机号则从输入的微信号中随机找1个绑定
+*/
+
+func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64,
+	weChatIds []string, phones []string) (bool, []string, []string, error) {
+
+	lenWechatIds := len(weChatIds)
+	lenPhones := len(phones)
+
+	if lenPhones == 0 {
+		return false, nil, nil, errors.New("phones empty")
+	}
+
+	if lenWechatIds == 0 { //输入wxids为空,从当前库中同Oid的Wxid里随机选1个
+		allWxIds, err := l.getWxIdListByOid(Oid, CType)
+		if err != nil {
+			return false, weChatIds, phones, err
+		}
+		if len(allWxIds) == 0 {
+			return false, weChatIds, phones, fmt.Errorf("wxids empty(%d)", Oid)
+		}
+		for range lenPhones {
+			weChatIds = append(weChatIds, GetListRandOne(allWxIds))
+		}
+	} else if lenWechatIds < lenPhones { //当wxids数量小于phones数量,从已输入wxids随机选1个对齐phones
+		for range lenPhones - lenWechatIds {
+			weChatIds = append(weChatIds, GetListRandOne(weChatIds))
+		}
+	}
+	//剩下的case就是wxids数量等于或大于phones
+	return true, weChatIds, phones, nil
+}
+
+/*
 func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64,
 	weChatIds []string, phones []string) (bool, []string, []string, error) {
 
@@ -114,14 +153,15 @@ func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64,
 			weChatIds = append(weChatIds, GetRandWxid(allWxIds))
 		}
 	} else { //case 3: 输入的wxids比phones大
-		/*
-			for i := 0; i < lenWechatIds-lenPhones; i++ {
-				phones = append(phones, GetRandWxid(phones))
-			}
-		*/
+
+		//	for i := 0; i < lenWechatIds-lenPhones; i++ {
+		//		phones = append(phones, GetRandWxid(phones))
+		//	}
+
 	}
 	return true, weChatIds, phones, nil
 }
+*/
 
 func (l *AddFriendByPhoneLogic) AddFriendByPhone(req *types.AddFriendByPhoneReq) (resp *types.BaseMsgResp, err error) {
 	// todo: add your logic here and delete this line