|
@@ -5,10 +5,12 @@ import (
|
|
|
"errors"
|
|
|
|
|
|
"wechat-api/ent"
|
|
|
+ "wechat-api/ent/predicate"
|
|
|
+ "wechat-api/ent/wx"
|
|
|
"wechat-api/internal/service/addfriend"
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
|
- "wechat-api/internal/utils/contextkey"
|
|
|
+ "wechat-api/internal/utils/typekit"
|
|
|
|
|
|
"github.com/suyuan32/simple-admin-common/enum/errorcode"
|
|
|
"github.com/suyuan32/simple-admin-common/msg/errormsg"
|
|
@@ -28,47 +30,157 @@ 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]
|
|
|
+ }
|
|
|
+ return id
|
|
|
+}
|
|
|
+
|
|
|
+func (l *AddFriendByPhoneLogic) getWxInfoList(preds ...predicate.Wx) ([]*ent.Wx, error) {
|
|
|
+ preds = append(preds, wx.StatusEQ(1))
|
|
|
+ return l.svcCtx.DB.Wx.Query().Where(preds...).All(l.ctx)
|
|
|
+}
|
|
|
+
|
|
|
+func (l *AddFriendByPhoneLogic) getWxIdListByOid(Oid uint64, Type uint64) ([]string, error) {
|
|
|
+ preds := []predicate.Wx{}
|
|
|
+ preds = append(preds, wx.OrganizationIDEQ(Oid))
|
|
|
+ if Type == 1 || Type == 3 {
|
|
|
+ preds = append(preds, wx.CtypeEQ(Type))
|
|
|
+ }
|
|
|
+ wxIdList := []string{}
|
|
|
+ wxInfoList, err := l.getWxInfoList(preds...)
|
|
|
+ if err == nil {
|
|
|
+ for _, wxInfo := range wxInfoList {
|
|
|
+ wxIdList = append(wxIdList, wxInfo.Wxid)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return wxIdList, err
|
|
|
+}
|
|
|
+
|
|
|
+type AddFriendByPhoneInfo struct {
|
|
|
+ Type int `json:"type"`
|
|
|
+ WeChatId string `json:"wechat_id"`
|
|
|
+ Phone string `json:"phone"`
|
|
|
+ Message string `json:"message"`
|
|
|
+ CallbackURL string `json:"callback_url"`
|
|
|
+}
|
|
|
+
|
|
|
+func (l *AddFriendByPhoneLogic) rebuildAddFriendByPhoneInfos(req *types.AddFriendByPhoneReq,
|
|
|
+ Oid uint64) ([]AddFriendByPhoneInfo, error) {
|
|
|
+
|
|
|
+ var err error
|
|
|
+ _, req.WeChatIds, req.Phones, err = l.alignWxidsAndPhones(Oid,
|
|
|
+ uint64(req.Type), req.WeChatIds, req.Phones)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ infos := []AddFriendByPhoneInfo{}
|
|
|
+ for idx, weChatid := range req.WeChatIds {
|
|
|
+ info := AddFriendByPhoneInfo{Type: req.Type, WeChatId: weChatid,
|
|
|
+ Phone: req.Phones[idx], Message: req.Message, CallbackURL: req.CallbackURL}
|
|
|
+ infos = append(infos, info)
|
|
|
+ }
|
|
|
+ return infos, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64,
|
|
|
+ weChatIds []string, phones []string) (bool, []string, []string, error) {
|
|
|
+
|
|
|
+ lenWechatIds := len(weChatIds)
|
|
|
+ lenPhones := len(phones)
|
|
|
+ //case1: 输入的wxids和phones大小一样,不需要对齐
|
|
|
+ if lenWechatIds == lenPhones {
|
|
|
+ return true, weChatIds, phones, nil
|
|
|
+ }
|
|
|
+ //case2: 输入的wxids比phones小或者空
|
|
|
+ if lenWechatIds < lenPhones {
|
|
|
+ allWxIds, err := l.getWxIdListByOid(Oid, CType)
|
|
|
+ if err != nil {
|
|
|
+ return false, weChatIds, phones, err
|
|
|
+ }
|
|
|
+ if len(allWxIds) == 0 {
|
|
|
+ if len(weChatIds) == 0 {
|
|
|
+ err = errors.New("db wxIds empty")
|
|
|
+ }
|
|
|
+ return false, weChatIds, phones, err
|
|
|
+ }
|
|
|
+ for i := 0; i < lenPhones-lenWechatIds; i++ {
|
|
|
+ weChatIds = append(weChatIds, GetRandWxid(allWxIds))
|
|
|
+ }
|
|
|
+ } else { //case 3: 输入的wxids比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
|
|
|
|
|
|
- var (
|
|
|
- apiKeyObj *ent.ApiKey
|
|
|
- ok bool
|
|
|
- )
|
|
|
+ if len(req.Phones) == 0 {
|
|
|
+ return nil, errors.New("miss phone")
|
|
|
+ }
|
|
|
|
|
|
- //从上下文中获取鉴权中间件埋下的apiAuthInfo
|
|
|
- apiKeyObj, ok = contextkey.AuthTokenInfoKey.GetValue(l.ctx)
|
|
|
- if !ok {
|
|
|
- return nil, errors.New("content get auth info err")
|
|
|
+ orgID, ok := l.ctx.Value("organizationId").(uint64)
|
|
|
+ if !ok || orgID == 0 {
|
|
|
+ return nil, errors.New("content get oid info err")
|
|
|
}
|
|
|
- err = l.AppendWechaFriendAddReq(apiKeyObj, req)
|
|
|
+
|
|
|
+ //fmt.Println("before weChatIds:", req.WeChatIds)
|
|
|
+ //fmt.Println("before phones:", req.Phones)
|
|
|
+ //apiKeyObj.OrganizationID = 1
|
|
|
+
|
|
|
+ if req.Type != 3 {
|
|
|
+ req.Type = 1
|
|
|
+ }
|
|
|
+ infos, err := l.rebuildAddFriendByPhoneInfos(req, orgID)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- //addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).AddNewFriend(req.WeChatId, req.Phone, req.Message)
|
|
|
- addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).FindFriendByContent(req.WeChatId, req.Phone)
|
|
|
+ //fmt.Println("after weChatIds:", req.WeChatIds)
|
|
|
+ //fmt.Println("after phones:", req.Phones)
|
|
|
+ //fmt.Println(typekit.PrettyPrint(infos))
|
|
|
|
|
|
- resp = &types.BaseMsgResp{
|
|
|
- Msg: errormsg.Success,
|
|
|
- Code: errorcode.OK,
|
|
|
- }
|
|
|
+ var id int64
|
|
|
+ for _, info := range infos {
|
|
|
+ id, err = l.AppendWechaFriendAddInfo(&info)
|
|
|
+ if err != nil {
|
|
|
+ logx.Errorf("chat.AppendWechaFriendAddLog err : %s", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ logx.Infof("chat.AppendWechaFriendAddLog succ,get id:%d", id)
|
|
|
|
|
|
- return resp, nil
|
|
|
+ if req.Type != 1 {
|
|
|
+ continue //企微忽略后面
|
|
|
+ }
|
|
|
+ ret := addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).
|
|
|
+ FindFriendByContent(info.WeChatId, info.Phone)
|
|
|
+ retStr := "fail"
|
|
|
+ if ret {
|
|
|
+ retStr = "succ"
|
|
|
+ }
|
|
|
+ logx.Infof("call addfriend.NewAddWechatFriendService.FindFriendByContent('%s','%s') maybe %s", info.WeChatId, info.Phone, retStr)
|
|
|
+ }
|
|
|
+ return &types.BaseMsgResp{Msg: errormsg.Success, Code: errorcode.OK}, nil
|
|
|
}
|
|
|
|
|
|
-func AppendWechaFriendAddLog(ctx context.Context, svcCtx *svc.ServiceContext, addLog *types.AddFriendByPhoneReq) (int64, error) {
|
|
|
+func (l *AddFriendByPhoneLogic) AppendWechaFriendAddInfo(addInfo *AddFriendByPhoneInfo) (int64, error) {
|
|
|
|
|
|
isCanAdd := 0
|
|
|
- if addLog.Type != 1 {
|
|
|
+ if addInfo.Type != 1 {
|
|
|
isCanAdd = 1
|
|
|
}
|
|
|
- res, err := svcCtx.DB.AddWechatFriendLog.Create().
|
|
|
- SetNotNilOwnerWxID(&addLog.WeChatId).
|
|
|
- SetNotNilOwnerWxType(&addLog.Type).
|
|
|
- SetNotNilFindContent(&addLog.Phone).
|
|
|
- SetNotNilMessage(&addLog.Message).
|
|
|
+ res, err := l.svcCtx.DB.AddWechatFriendLog.Create().
|
|
|
+ SetNotNilOwnerWxID(&addInfo.WeChatId).
|
|
|
+ SetNotNilOwnerWxType(&addInfo.Type).
|
|
|
+ SetNotNilFindContent(&addInfo.Phone).
|
|
|
+ SetNotNilMessage(&addInfo.Message).
|
|
|
SetIsCanAdd(isCanAdd).
|
|
|
- Save(ctx)
|
|
|
+ Save(l.ctx)
|
|
|
|
|
|
id := int64(0)
|
|
|
if err == nil {
|
|
@@ -76,12 +188,3 @@ func AppendWechaFriendAddLog(ctx context.Context, svcCtx *svc.ServiceContext, ad
|
|
|
}
|
|
|
return id, err
|
|
|
}
|
|
|
-
|
|
|
-func (l *AddFriendByPhoneLogic) AppendWechaFriendAddReq(apiKeyObj *ent.ApiKey, req *types.AddFriendByPhoneReq) error {
|
|
|
-
|
|
|
- id, err := AppendWechaFriendAddLog(l.ctx, l.svcCtx, req)
|
|
|
- if err == nil {
|
|
|
- logx.Infof("AppendWechaFriendAddReq succ,get id:%d", id)
|
|
|
- }
|
|
|
- return err
|
|
|
-}
|