add_friend_by_phone_logic.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, phone := range req.Phones {
  71. info := AddFriendByPhoneInfo{Type: req.Type, WeChatId: req.WeChatIds[idx],
  72. Phone: phone, 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. /*
  102. for i := 0; i < lenWechatIds-lenPhones; i++ {
  103. phones = append(phones, GetRandWxid(phones))
  104. }
  105. */
  106. }
  107. return true, weChatIds, phones, nil
  108. }
  109. func (l *AddFriendByPhoneLogic) AddFriendByPhone(req *types.AddFriendByPhoneReq) (resp *types.BaseMsgResp, err error) {
  110. // todo: add your logic here and delete this line
  111. if len(req.Phones) == 0 {
  112. return nil, errors.New("miss phone")
  113. }
  114. orgID, ok := l.ctx.Value("organizationId").(uint64)
  115. if !ok || orgID == 0 {
  116. return nil, errors.New("content get oid info err")
  117. }
  118. //fmt.Println("before weChatIds:", req.WeChatIds)
  119. //fmt.Println("before phones:", req.Phones)
  120. //apiKeyObj.OrganizationID = 1
  121. if req.Type != 3 {
  122. req.Type = 1
  123. }
  124. infos, err := l.rebuildAddFriendByPhoneInfos(req, orgID)
  125. if err != nil {
  126. return nil, err
  127. }
  128. //fmt.Println("after weChatIds:", req.WeChatIds)
  129. //fmt.Println("after phones:", req.Phones)
  130. //fmt.Println(typekit.PrettyPrint(infos))
  131. var id int64
  132. for _, info := range infos {
  133. id, err = l.AppendWechaFriendAddInfo(&info)
  134. if err != nil {
  135. logx.Errorf("chat.AppendWechaFriendAddLog err : %s", err)
  136. return nil, err
  137. }
  138. logx.Infof("chat.AppendWechaFriendAddLog succ,get id:%d", id)
  139. if req.Type != 1 {
  140. continue //企微忽略后面
  141. }
  142. ret := addfriend.NewAddWechatFriendService(l.ctx, l.svcCtx).
  143. FindFriendByContent(info.WeChatId, info.Phone)
  144. retStr := "fail"
  145. if ret {
  146. retStr = "succ"
  147. }
  148. logx.Infof("call addfriend.NewAddWechatFriendService.FindFriendByContent('%s','%s') maybe %s", info.WeChatId, info.Phone, retStr)
  149. }
  150. return &types.BaseMsgResp{Msg: errormsg.Success, Code: errorcode.OK}, nil
  151. }
  152. func (l *AddFriendByPhoneLogic) AppendWechaFriendAddInfo(addInfo *AddFriendByPhoneInfo) (int64, error) {
  153. isCanAdd := 0
  154. if addInfo.Type != 1 {
  155. isCanAdd = 1
  156. }
  157. res, err := l.svcCtx.DB.AddWechatFriendLog.Create().
  158. SetNotNilOwnerWxID(&addInfo.WeChatId).
  159. SetNotNilOwnerWxType(&addInfo.Type).
  160. SetNotNilFindContent(&addInfo.Phone).
  161. SetNotNilMessage(&addInfo.Message).
  162. SetIsCanAdd(isCanAdd).
  163. Save(l.ctx)
  164. id := int64(0)
  165. if err == nil {
  166. id = res.ID
  167. }
  168. return id, err
  169. }