add_friend_by_phone_logic.go 5.6 KB

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