add_friend_by_phone_logic.go 5.5 KB

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