add_friend_by_phone_logic.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. Oid int64 `json:"oid"`
  58. Phone string `json:"phone"`
  59. Message string `json:"message"`
  60. CallbackURL string `json:"callback_url"`
  61. }
  62. func (l *AddFriendByPhoneLogic) rebuildAddFriendByPhoneInfos(req *types.AddFriendByPhoneReq,
  63. Oid uint64) ([]AddFriendByPhoneInfo, error) {
  64. var err error
  65. _, req.WeChatIds, req.Phones, err = l.alignWxidsAndPhones(Oid,
  66. uint64(req.Type), req.WeChatIds, req.Phones)
  67. if err != nil {
  68. return nil, err
  69. }
  70. infos := []AddFriendByPhoneInfo{}
  71. for idx, phone := range req.Phones {
  72. info := AddFriendByPhoneInfo{Type: req.Type, WeChatId: req.WeChatIds[idx],
  73. Phone: phone, Message: req.Message, CallbackURL: req.CallbackURL, Oid: int64(Oid)}
  74. infos = append(infos, info)
  75. }
  76. return infos, nil
  77. }
  78. func (l *AddFriendByPhoneLogic) alignWxidsAndPhones(Oid uint64, CType uint64,
  79. weChatIds []string, phones []string) (bool, []string, []string, error) {
  80. lenWechatIds := len(weChatIds)
  81. lenPhones := len(phones)
  82. //case1: 输入的wxids和phones大小一样,不需要对齐
  83. if lenWechatIds == lenPhones {
  84. return true, weChatIds, phones, nil
  85. }
  86. //case2: 输入的wxids比phones小或者空
  87. if lenWechatIds < lenPhones {
  88. allWxIds, err := l.getWxIdListByOid(Oid, CType)
  89. if err != nil {
  90. return false, weChatIds, phones, err
  91. }
  92. if len(allWxIds) == 0 {
  93. if len(weChatIds) == 0 {
  94. err = errors.New("db wxIds empty")
  95. }
  96. return false, weChatIds, phones, err
  97. }
  98. for i := 0; i < lenPhones-lenWechatIds; i++ {
  99. weChatIds = append(weChatIds, GetRandWxid(allWxIds))
  100. }
  101. } else { //case 3: 输入的wxids比phones大
  102. /*
  103. for i := 0; i < lenWechatIds-lenPhones; i++ {
  104. phones = append(phones, GetRandWxid(phones))
  105. }
  106. */
  107. }
  108. return true, weChatIds, phones, nil
  109. }
  110. func (l *AddFriendByPhoneLogic) AddFriendByPhone(req *types.AddFriendByPhoneReq) (resp *types.BaseMsgResp, err error) {
  111. // todo: add your logic here and delete this line
  112. if len(req.Phones) == 0 {
  113. return nil, errors.New("miss phone")
  114. }
  115. orgID, ok := l.ctx.Value("organizationId").(uint64)
  116. if !ok || orgID == 0 {
  117. return nil, errors.New("content get oid info err")
  118. }
  119. //fmt.Println("before weChatIds:", req.WeChatIds)
  120. //fmt.Println("before phones:", req.Phones)
  121. //apiKeyObj.OrganizationID = 1
  122. if req.Type != 3 {
  123. req.Type = 1
  124. }
  125. infos, err := l.rebuildAddFriendByPhoneInfos(req, orgID)
  126. if err != nil {
  127. return nil, err
  128. }
  129. //fmt.Println("after weChatIds:", req.WeChatIds)
  130. //fmt.Println("after phones:", req.Phones)
  131. //fmt.Println(typekit.PrettyPrint(infos))
  132. var id int64
  133. for _, info := range infos {
  134. id, err = l.AppendWechaFriendAddInfo(&info)
  135. if err != nil {
  136. logx.Errorf("chat.AppendWechaFriendAddLog err : %s", err)
  137. return nil, err
  138. }
  139. logx.Infof("chat.AppendWechaFriendAddLog succ,get id:%d", id)
  140. if req.Type != 1 {
  141. continue //企微忽略后面
  142. }
  143. ret := addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).
  144. FindFriendByContent(info.WeChatId, info.Phone)
  145. retStr := "fail"
  146. if ret {
  147. retStr = "succ"
  148. }
  149. logx.Infof("call addfriend.NewAddWechatFriendService.FindFriendByContent('%s','%s') maybe %s", info.WeChatId, info.Phone, retStr)
  150. }
  151. return &types.BaseMsgResp{Msg: errormsg.Success, Code: errorcode.OK}, nil
  152. }
  153. func (l *AddFriendByPhoneLogic) AppendWechaFriendAddInfo(addInfo *AddFriendByPhoneInfo) (int64, error) {
  154. isCanAdd := 0
  155. if addInfo.Type != 1 {
  156. isCanAdd = 1
  157. }
  158. res, err := l.svcCtx.DB.AddWechatFriendLog.Create().
  159. SetNotNilOwnerWxID(&addInfo.WeChatId).
  160. SetNotNilOwnerWxType(&addInfo.Type).
  161. SetNotNilFindContent(&addInfo.Phone).
  162. SetNotNilMessage(&addInfo.Message).
  163. SetIsCanAdd(isCanAdd).
  164. SetNotNilOrganizationID(&addInfo.Oid).
  165. Save(l.ctx)
  166. id := int64(0)
  167. if err == nil {
  168. id = res.ID
  169. }
  170. return id, err
  171. }