add_friend_by_phone_logic.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package chat
  2. import (
  3. "context"
  4. "errors"
  5. "wechat-api/ent"
  6. "wechat-api/ent/predicate"
  7. "wechat-api/ent/wx"
  8. "wechat-api/internal/service/addfriend"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "wechat-api/internal/utils/typekit"
  12. "github.com/suyuan32/simple-admin-common/enum/errorcode"
  13. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type AddFriendByPhoneLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewAddFriendByPhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFriendByPhoneLogic {
  22. return &AddFriendByPhoneLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func GetRandWxid(idList []string) string {
  28. id := ""
  29. if len(idList) > 0 {
  30. typekit.ShuffleList(idList)
  31. id = idList[0]
  32. }
  33. return id
  34. }
  35. func (l *AddFriendByPhoneLogic) getWxInfoList(preds ...predicate.Wx) ([]*ent.Wx, error) {
  36. preds = append(preds, wx.StatusEQ(1))
  37. return l.svcCtx.DB.Wx.Query().Where(preds...).All(l.ctx)
  38. }
  39. func (l *AddFriendByPhoneLogic) getWxIdListByOid(Oid uint64, Type uint64) ([]string, error) {
  40. preds := []predicate.Wx{}
  41. preds = append(preds, wx.OrganizationIDEQ(Oid))
  42. if Type == 1 || Type == 3 {
  43. preds = append(preds, wx.CtypeEQ(Type))
  44. }
  45. wxIdList := []string{}
  46. wxInfoList, err := l.getWxInfoList(preds...)
  47. if err == nil {
  48. for _, wxInfo := range wxInfoList {
  49. wxIdList = append(wxIdList, wxInfo.Wxid)
  50. }
  51. }
  52. return wxIdList, err
  53. }
  54. type AddFriendByPhoneInfo struct {
  55. Type int `json:"type"`
  56. WeChatId string `json:"wechat_id"`
  57. Phone string `json:"phone"`
  58. Message string `json:"message"`
  59. CallbackURL string `json:"callback_url"`
  60. }
  61. func (l *AddFriendByPhoneLogic) rebuildAddFriendByPhoneInfos(req *types.AddFriendByPhoneReq,
  62. Oid uint64) ([]AddFriendByPhoneInfo, error) {
  63. var err error
  64. _, req.WeChatIds, req.Phones, err = l.alignWxidsAndPhones(Oid,
  65. uint64(req.Type), req.WeChatIds, req.Phones)
  66. if err != nil {
  67. return nil, err
  68. }
  69. infos := []AddFriendByPhoneInfo{}
  70. for idx, weChatid := range req.WeChatIds {
  71. info := AddFriendByPhoneInfo{Type: req.Type, WeChatId: weChatid,
  72. Phone: req.Phones[idx], Message: req.Message, CallbackURL: req.CallbackURL}
  73. infos = append(infos, info)
  74. }
  75. return infos, nil
  76. }
  77. func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64,
  78. weChatIds []string, phones []string) (bool, []string, []string, error) {
  79. lenWechatIds := len(weChatIds)
  80. lenPhones := len(phones)
  81. //case1: 输入的wxids和phones大小一样,不需要对齐
  82. if lenWechatIds == lenPhones {
  83. return true, weChatIds, phones, nil
  84. }
  85. //case2: 输入的wxids比phones小或者空
  86. if lenWechatIds < lenPhones {
  87. allWxIds, err := l.getWxIdListByOid(Oid, CType)
  88. if err != nil {
  89. return false, weChatIds, phones, err
  90. }
  91. if len(allWxIds) == 0 {
  92. if len(weChatIds) == 0 {
  93. err = errors.New("db wxIds empty")
  94. }
  95. return false, weChatIds, phones, err
  96. }
  97. for i := 0; i < lenPhones-lenWechatIds; i++ {
  98. weChatIds = append(weChatIds, GetRandWxid(allWxIds))
  99. }
  100. } else { //case 3: 输入的wxids比phones大
  101. for i := 0; i < lenWechatIds-lenPhones; i++ {
  102. phones = append(phones, GetRandWxid(phones))
  103. }
  104. }
  105. return true, weChatIds, phones, nil
  106. }
  107. func (l *AddFriendByPhoneLogic) AddFriendByPhone(req *types.AddFriendByPhoneReq) (resp *types.BaseMsgResp, err error) {
  108. // todo: add your logic here and delete this line
  109. if len(req.Phones) == 0 {
  110. return nil, errors.New("miss phone")
  111. }
  112. orgID, ok := l.ctx.Value("organizationId").(uint64)
  113. if !ok || orgID == 0 {
  114. return nil, errors.New("content get oid info err")
  115. }
  116. //fmt.Println("before weChatIds:", req.WeChatIds)
  117. //fmt.Println("before phones:", req.Phones)
  118. //apiKeyObj.OrganizationID = 1
  119. if req.Type != 3 {
  120. req.Type = 1
  121. }
  122. infos, err := l.rebuildAddFriendByPhoneInfos(req, orgID)
  123. if err != nil {
  124. return nil, err
  125. }
  126. //fmt.Println("after weChatIds:", req.WeChatIds)
  127. //fmt.Println("after phones:", req.Phones)
  128. //fmt.Println(typekit.PrettyPrint(infos))
  129. var id int64
  130. for _, info := range infos {
  131. id, err = l.AppendWechaFriendAddInfo(&info)
  132. if err != nil {
  133. logx.Errorf("chat.AppendWechaFriendAddLog err : %s", err)
  134. return nil, err
  135. }
  136. logx.Infof("chat.AppendWechaFriendAddLog succ,get id:%d", id)
  137. if req.Type != 1 {
  138. continue //企微忽略后面
  139. }
  140. ret := addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).
  141. FindFriendByContent(info.WeChatId, info.Phone)
  142. retStr := "fail"
  143. if ret {
  144. retStr = "succ"
  145. }
  146. logx.Infof("call addfriend.NewAddWechatFriendService.FindFriendByContent('%s','%s') maybe %s", info.WeChatId, info.Phone, retStr)
  147. }
  148. return &types.BaseMsgResp{Msg: errormsg.Success, Code: errorcode.OK}, nil
  149. }
  150. func (l *AddFriendByPhoneLogic) AppendWechaFriendAddInfo(addInfo *AddFriendByPhoneInfo) (int64, error) {
  151. isCanAdd := 0
  152. if addInfo.Type != 1 {
  153. isCanAdd = 1
  154. }
  155. res, err := l.svcCtx.DB.AddWechatFriendLog.Create().
  156. SetNotNilOwnerWxID(&addInfo.WeChatId).
  157. SetNotNilOwnerWxType(&addInfo.Type).
  158. SetNotNilFindContent(&addInfo.Phone).
  159. SetNotNilMessage(&addInfo.Message).
  160. SetIsCanAdd(isCanAdd).
  161. Save(l.ctx)
  162. id := int64(0)
  163. if err == nil {
  164. id = res.ID
  165. }
  166. return id, err
  167. }