MockService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Services\V1_1;
  3. use App\Generated\V1_1\Messages\Mock\DeleteBlackListMessage;
  4. use App\Generated\V1_1\Messages\Mock\AddBlackListMessage;
  5. use App\Generated\V1_1\Messages\Mock\SendSystemMessageMessage;
  6. use App\Generated\V1_1\Messages\Mock\ThumbPartnerMessage;
  7. use App\Exceptions\AlertException;
  8. use App\Generated\V1_1\Messages\Mock\SendFriendApplyMessage;
  9. use App\Managers\FriendManager;
  10. use App\Models\BlackListModel;
  11. use App\Models\Friends\FriendApply;
  12. use App\Models\Friends\FriendContact;
  13. use App\Models\NoticeModel;
  14. use App\Models\PartnerModel;
  15. use App\Models\RiskListModel;
  16. use App\Models\User\UserModel;
  17. use App\Services\IM\CustomElem;
  18. use App\Utils\ImUtil;
  19. class MockService
  20. {
  21. private $friendManager;
  22. public function __construct(FriendManager $friendManager)
  23. {
  24. $this->friendManager = $friendManager;
  25. }
  26. /**
  27. * 发送好友申请
  28. * @param SendFriendApplyMessage $message
  29. * @throws AlertException
  30. */
  31. public function sendFriendApply(SendFriendApplyMessage $message)
  32. {
  33. $uid = $message->getUid();
  34. $contactUid = $message->getContactUid();
  35. if (FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid), array('friend_level', 2)])->exists()) {
  36. throw new AlertException("你们已经是好友了", 401);
  37. }
  38. if (!$this->friendManager->canIm($uid, $contactUid)) {
  39. throw new AlertException("你们处于拉黑关系", 402);
  40. }
  41. FriendApply::updateOrCreate(array('uid' => $uid, 'apply_uid' => $contactUid), array('last_at' => time(), 'handle' => 0));
  42. $receiveUser = UserModel::find($contactUid, ['uid', 'im_account']);
  43. $message = new CustomElem('EventFriendApply', ['FromUid' => $uid, 'ToUid' => $contactUid]);
  44. $imUtil = new ImUtil();
  45. $imUtil->sendMessage("system_event", $receiveUser->im_account, [$message->toArray()]);
  46. }
  47. /**
  48. * 点赞卡片
  49. * @param ThumbPartnerMessage $message
  50. * @throws AlertException
  51. */
  52. public function thumbPartner(ThumbPartnerMessage $message)
  53. {
  54. $uid = $message->getUid();
  55. $partnerId = $message->getPartnerId();
  56. $thumb = $message->getThumb();
  57. $ps = new \App\Services\User\PartnerService();
  58. $data = $ps->thumb($uid, $partnerId, $thumb);
  59. $message->setResponse($data);
  60. }
  61. /**
  62. * 发送系统消息
  63. * @param SendSystemMessageMessage $message
  64. */
  65. public function sendSystemMessage(SendSystemMessageMessage $message)
  66. {
  67. $uid = $message->getUid();
  68. // 系统消息
  69. NoticeModel::create(['uid' => $uid, 'title' => '测试通知', 'content' => "测试。。。", 'type' => 5, 'type_id' => 0, 'create_at' => time(), 'update_at' => time(), 'tab_content' => '查看详情']);
  70. }
  71. /**
  72. * 添加黑名单
  73. * @param AddBlackListMessage $message
  74. */
  75. public function addBlackList(AddBlackListMessage $message)
  76. {
  77. $uid = $message->getUid();
  78. $end_time = 1893427200;
  79. $account_type = 'uid';
  80. $type = 'all';
  81. $state = 3;
  82. $data['end_at'] = $end_time;
  83. $data['account_type'] = $account_type;
  84. $data['account'] = $uid;
  85. BlackListModel::create($data);
  86. // 永久封禁
  87. if ($account_type == 'uid' && $type == 'all') {
  88. $user = UserModel::find($uid);
  89. $user->headimgurl = 'https://oss.pocketuniversity.cn/media/2019-08-14/5d53e27a8d81e.png';
  90. $user->save();
  91. // 封禁卡片
  92. if ($user->partner_id) {
  93. $partner = PartnerModel::find($user->partner_id);
  94. $partner->is_sell = -1;
  95. $partner->black_at = $end_time;
  96. $partner->save();
  97. }
  98. // 加入风险帐号列表
  99. RiskListModel::create(['uid' => $user->uid, 'weixin' => $user->weixin, 'qq' => $user->qq, 'phone' => $user->phone]);
  100. // 手机号码封禁
  101. if ($state == 3) {
  102. if ($user->phone) {
  103. BlackListModel::create(['type' => 'all', 'account_type' => 'phone', 'account' => $user->phone, 'end_at' => 1893427200, 'state' => 3]);
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * 删除黑名单
  110. * @param DeleteBlackListMessage $message
  111. */
  112. public function deleteBlackList(DeleteBlackListMessage $message)
  113. {
  114. $uid = $message->getUid();
  115. $black = BlackListModel::where('account_type','uid')
  116. ->where('account', $uid)
  117. ->first();
  118. $user = UserModel::find($uid);
  119. // 卡片解禁
  120. $partner = PartnerModel::find($user->partner_id);
  121. $partner->black_at = 0;
  122. $partner->save();
  123. $black->delete();
  124. if ($user->phone) {
  125. BlackListModel::where(['account_type' => 'phone', 'account' => $user->phone])->delete();
  126. }
  127. }
  128. }