FriendService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace App\Http\Services\V1_1;
  3. use App\Generated\V1_1\Messages\Friend\GetEachLikeContactsMessage;
  4. use App\Generated\V1_1\Messages\Friend\ChangeContactNoteMessage;
  5. use App\Generated\V1_1\Messages\Friend\FriendApplyMsgOverviewMessage;
  6. use App\Generated\V1_1\Messages\Friend\GetAllFriendAppliesMessage;
  7. use App\Generated\V1_1\DTOs\ContactUserDTO;
  8. use App\Generated\V1_1\Messages\Friend\GetContactMessage;
  9. use App\Generated\V1_1\Messages\Friend\GetUnReadSayHelloContactListMessage;
  10. use App\Generated\V1_1\Messages\Friend\GetReadSayHelloContactListMessage;
  11. use App\Generated\V1_1\Messages\Friend\GetReceiveFriendApplyListMessage;
  12. use App\Generated\V1_1\Messages\Friend\GetSendFriendApplyListMessage;
  13. use App\Generated\V1_1\Messages\Friend\SendPairFriendApplyMessage;
  14. use App\Generated\V1_1\Messages\Friend\AgreePairFriendApplyMessage;
  15. use App\Generated\V1_1\Messages\Friend\RefusePairFriendApplyMessage;
  16. use App\Exceptions\AlertException;
  17. use App\Generated\V1_1\DTOs\ListFriendApplyDTO;
  18. use App\Generated\V1_1\DTOs\ListFriendDTO;
  19. use App\Generated\V1_1\DTOs\PageDTO;
  20. use App\Generated\V1_1\Messages\Friend\GetContactListMessage;
  21. use App\Generated\V1_1\Messages\Friend\AgreeFriendApplyMessage;
  22. use App\Http\Controllers\Core\Auth;
  23. use App\Managers\FriendManager;
  24. use App\Generated\V1_1\Messages\Friend\RefuseFriendApplyMessage;
  25. use App\Generated\V1_1\Messages\Friend\DeleteContactMessage;
  26. use App\Generated\V1_1\Messages\Friend\StarContactMessage;
  27. use App\Generated\V1_1\Messages\Friend\UnStarContactMessage;
  28. use App\Generated\V1_1\Messages\Friend\UnBlacklistContactMessage;
  29. use App\Generated\V1_1\Messages\Friend\SendFriendApplyMessage;
  30. use App\Generated\V1_1\Messages\Friend\GetStarContactListMessage;
  31. use App\Generated\V1_1\Messages\Friend\GetFriendListMessage;
  32. use App\Generated\V1_1\Messages\Friend\BlacklistContactMessage;
  33. use App\Managers\PairActivityManager;
  34. use App\Managers\PairManager;
  35. use App\Models\Fpdx\ActivityModel;
  36. use App\Models\Fpdx\PairModel;
  37. use App\Models\Friends\FriendApply;
  38. use App\Models\Friends\FriendContact;
  39. use App\Models\Friends\PairContactApply;
  40. use App\Models\NoticeModel;
  41. use App\Services\IM\CustomElem;
  42. use App\Models\User\UserModel;
  43. use App\Utils\ImUtil;
  44. use Illuminate\Support\Arr;
  45. use Illuminate\Support\Collection;
  46. use Illuminate\Support\Facades\DB;
  47. class FriendService
  48. {
  49. protected $friendManager;
  50. public function __construct(FriendManager $friendManager)
  51. {
  52. $this->friendManager = $friendManager;
  53. }
  54. public function refuseFriendApply(RefuseFriendApplyMessage $message)
  55. {
  56. $contactUid = $message->getContactUid();
  57. $uid = Auth::auth();
  58. FriendApply::where([array('uid', $contactUid), array('apply_uid', $uid)])->update(['handle' => -1]);
  59. }
  60. public function agreeFriendApply(AgreeFriendApplyMessage $message)
  61. {
  62. $contactUid = $message->getContactUid();
  63. $uid = Auth::auth();
  64. FriendApply::where([array('uid', $contactUid), array('apply_uid', $uid)])->update(['handle' => 1]);
  65. $this->friendManager->beFriend($uid, $contactUid);
  66. }
  67. public function unStarContact(UnStarContactMessage $message)
  68. {
  69. $contactUid = $message->getContactUid();
  70. $uid = Auth::auth();
  71. FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->update(['star_at' => 0]);
  72. }
  73. public function starContact(StarContactMessage $message)
  74. {
  75. $contactUid = $message->getContactUid();
  76. $uid = Auth::auth();
  77. FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->update(['star_at' => time()]);
  78. }
  79. public function deleteContact(DeleteContactMessage $message)
  80. {
  81. $contactUid = $message->getContactUid();
  82. $uid = Auth::auth();
  83. FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->orWhere([array('uid', $contactUid), array('friend_uid', $uid)])->delete();
  84. }
  85. public function unBlacklistContact(UnBlacklistContactMessage $message)
  86. {
  87. $contactUid = $message->getContactUid();
  88. $uid = Auth::auth();
  89. FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->update(['is_blacklist' => 0]);
  90. }
  91. public function blacklistContact(BlacklistContactMessage $message)
  92. {
  93. $contactUid = $message->getContactUid();
  94. $uid = Auth::auth();
  95. FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->update(['is_blacklist' => 1]);
  96. }
  97. public function sendFriendApply(SendFriendApplyMessage $message)
  98. {
  99. $contactUid = $message->getContactUid();
  100. $uid = Auth::auth();
  101. if (FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid), array('friend_level', 2)])->exists()) {
  102. throw new AlertException("你们已经是好友了", 401);
  103. }
  104. if (!$this->friendManager->canIm($uid, $contactUid)) {
  105. throw new AlertException("你们处于拉黑关系", 402);
  106. }
  107. FriendApply::updateOrCreate(array('uid' => $uid, 'apply_uid' => $contactUid), array('last_at' => time(), 'handle' => 0));
  108. $receiveUser = UserModel::find($contactUid, ['uid', 'im_account']);
  109. $message = new CustomElem('EventFriendApply', ['FromUid' => $uid, 'ToUid' => $contactUid]);
  110. $imUtil = new ImUtil();
  111. $imUtil->sendMessage("system_event", $receiveUser->im_account, [$message->toArray()]);
  112. }
  113. public function getContactList(GetContactListMessage $message)
  114. {
  115. $uid = Auth::auth();
  116. $lists = Collection::make([]);
  117. // 1. 好友联系人 + 普通联系人
  118. $contacts = FriendContact::where('uid', $uid)->whereIn('friend_level', [1, 2])->get();
  119. $contacts->map(function (&$item) {
  120. $item->list_type = "contact";
  121. if ($user = UserModel::whereUid($item->friend_uid)->first()) {
  122. $item->user = $user->toArray();
  123. } else {
  124. $item->user = $user;
  125. }
  126. });
  127. $lists = $lists->merge($contacts);
  128. // 2. 系统消息
  129. $systemMsg = NoticeModel::whereUid($uid)->orderByDesc('create_at')->first();
  130. if ($systemMsg) {
  131. $systemMsgUnreadCnt = NoticeModel::where([['uid', $uid], ['is_read', 0]])->count();
  132. $lists->push(['list_type' => 'systemNotice', 'last_at' => $systemMsg->create_at->timestamp, 'last_msg' => null, 'list_unread_msg_cnt' => $systemMsgUnreadCnt, 'star_at' => 0, 'user' => null]);
  133. }
  134. // 3. 好友申请
  135. $friendApply = FriendApply::where([array('apply_uid', $uid), array('receive_unread', 1)])->orWhere([array('uid', $uid), array('send_unread', 1)])->orderByDesc('last_at')->first();
  136. if ($friendApply) {
  137. $lists->push(['list_type' => 'friendApply', 'last_at' => $friendApply->last_at, 'last_msg' => null, 'list_unread_msg_cnt' => 1, 'star_at' => 0, 'user' => null]);
  138. }
  139. // 4. 打招呼
  140. $helloListUnreadMsgCnt = FriendContact::where([array('uid', $uid), array('friend_level', 0)])->sum('list_unread_msg_cnt');
  141. if ($helloListUnreadMsgCnt) {
  142. $helloContact = FriendContact::where([array('uid', $uid), array('friend_level', 0), array('list_unread_msg_cnt', '>', 0)])->orderByDesc('last_at')->first();
  143. $helloContact->list_unread_msg_cnt = $helloListUnreadMsgCnt;
  144. $helloContact->user = null;
  145. $helloContact->list_type = "hello";
  146. $lists->push($helloContact);
  147. }
  148. $lists = array_values($lists->sortByDesc('last_at')->toArray());
  149. $message->setResponse(ListFriendDTO::initFromModels($lists));
  150. }
  151. /**
  152. * 星标联系人列表
  153. * @param GetStarContactListMessage $message
  154. */
  155. public function getStarContactList(GetStarContactListMessage $message)
  156. {
  157. $uid = Auth::auth();
  158. // 好友联系人 + 普通联系人
  159. $lists = FriendContact::where('uid', $uid)->where('star_at', '>', 0)->whereIn('friend_level', [1, 2])->orderByDesc('last_at')->with('user')->get();
  160. $lists = array_values($lists->toArray());
  161. $message->setResponse(ListFriendDTO::initFromEloquents($lists));
  162. }
  163. public function getFriendList(GetFriendListMessage $message)
  164. {
  165. $uid = Auth::auth();
  166. // 好友联系人
  167. $lists = FriendContact::where('uid', $uid)->where('friend_level', 2)->with('user')->get();
  168. $lists = array_values($lists->sortByDesc('last_at')->toArray());
  169. $message->setResponse(ListFriendDTO::initFromEloquents($lists));
  170. }
  171. public function refusePairFriendApply(RefusePairFriendApplyMessage $message)
  172. {
  173. $uid = Auth::auth();
  174. $contactUid = $message->getContactUid();
  175. $activityManager = new PairActivityManager(new ActivityModel());
  176. $enrollingActivity = $activityManager->getEnrollingActivity();
  177. if (!$enrollingActivity) {
  178. return;
  179. }
  180. PairContactApply::where([array('activity_id', $enrollingActivity->stage_id), array('send_uid', $contactUid), array('receive_uid', $uid)])->update(['handle' => -1]);
  181. $receiveUser = UserModel::find($contactUid, ['uid', 'im_account']);
  182. $message = new CustomElem('EventRefuseRefuseFriendApply', ['FromUid' => $uid, 'ToUid' => $contactUid]);
  183. $imUtil = new ImUtil();
  184. $imUtil->sendMessage("system_event", $receiveUser->im_account, [$message->toArray()]);
  185. }
  186. public function agreePairFriendApply(AgreePairFriendApplyMessage $message)
  187. {
  188. $uid = Auth::auth();
  189. $contactUid = $message->getContactUid();
  190. $activityManager = new PairActivityManager(new ActivityModel());
  191. $enrollingActivity = $activityManager->getEnrollingActivity();
  192. if (!$enrollingActivity) {
  193. throw new AlertException("活动邀请已过期");
  194. }
  195. if (!($pair = PairModel::where([array('uid', $uid), array('stage_id', $enrollingActivity->stage_id)])->first())) {
  196. throw new AlertException("你还没有参加本期活动");
  197. }
  198. if ($pair->assoc_id) {
  199. throw new AlertException("你已经有CP了");
  200. }
  201. if ($pair = PairModel::where([array('uid', $contactUid), array('stage_id', $enrollingActivity->stage_id)])->first() && $pair->assoc_id) {
  202. throw new AlertException("TA已经有CP了");
  203. }
  204. if (!($pairFriendApply = PairContactApply::where([array('activity_id', $enrollingActivity->stage_id), array('send_uid', $contactUid), array('receive_uid', $uid)])->first())) {
  205. throw new AlertException("没有收到邀请");
  206. }
  207. DB::transaction(function () use ($pairFriendApply, $enrollingActivity) {
  208. $pairFriendApply->handle = 1;
  209. $pairFriendApply->save();
  210. $pairManager = new PairManager();
  211. $pairManager->matchUserAndEnrollAndSign($enrollingActivity->stage_id, $pairFriendApply->send_uid, $pairFriendApply->receive_uid);
  212. $friendManager = new FriendManager();
  213. $friendManager->beFriend($pairFriendApply->send_uid, $pairFriendApply->receive_uid);
  214. });
  215. }
  216. public function sendPairFriendApply(SendPairFriendApplyMessage $message)
  217. {
  218. $uid = Auth::auth();
  219. $contactUid = $message->getContactUid();
  220. $activityManager = new PairActivityManager(new ActivityModel());
  221. $enrollingActivity = $activityManager->getEnrollingActivity();
  222. if (!$enrollingActivity) {
  223. throw new AlertException("不在活动邀请时间");
  224. }
  225. if (PairContactApply::where([array('activity_id', $enrollingActivity->stage_id), array('send_uid', $uid), array('handle', '!=', '-1')])->first()) {
  226. throw new AlertException("每期活动只能发起一个邀请");
  227. }
  228. if (PairContactApply::where([array('activity_id', $enrollingActivity->stage_id), array('send_uid', $uid), array('receive_uid', $contactUid)])->first()) {
  229. throw new AlertException("你已经对TA发起过邀请");
  230. }
  231. if (!($pair = PairModel::where([array('uid', $uid), array('stage_id', $enrollingActivity->stage_id)])->first())) {
  232. throw new AlertException("你还没有参加本期活动");
  233. }
  234. if ($pair->assoc_id) {
  235. throw new AlertException("你已经有CP了");
  236. }
  237. if ($pair = PairModel::where([array('uid', $contactUid), array('stage_id', $enrollingActivity->stage_id)])->first() && $pair->assoc_id) {
  238. throw new AlertException("TA已经有CP了");
  239. }
  240. PairContactApply::create(['created_at' => time(), 'send_uid' => $uid, 'receive_uid' => $contactUid, 'activity_id' => $enrollingActivity->stage_id]);
  241. }
  242. /**
  243. * 已读的打招呼联系人列表
  244. * @param GetReadSayHelloContactListMessage $message
  245. */
  246. public function getReadSayHelloContactList(GetReadSayHelloContactListMessage $message)
  247. {
  248. $page = $message->getPage() ?? 1;
  249. $take = 20;
  250. $uid = Auth::auth();
  251. $lists = FriendContact::where([array('uid', $uid), array('friend_level', 0), array('unread_cnt', 0)])->with('user')->orderByDesc('last_at')->take($take)->skip(($page - 1) * $take)->get();
  252. $lists = array_values($lists->toArray());
  253. FriendContact::where('uid', $uid)->where('friend_level', 0)->update(['list_unread_msg_cnt' => 0]);
  254. $pages = array('total' => FriendContact::where([array('uid', $uid), array('friend_level', 0), array('unread_cnt', 0)])->count(), 'page' => $page, 'limit' => $take);
  255. $message->setResponse(PageDTO::initFromModel($pages), ListFriendDTO::initFromModels($lists));
  256. }
  257. /**
  258. * 未读的打招呼联系人列表
  259. * @param GetUnReadSayHelloContactListMessage $message
  260. */
  261. public function getUnReadSayHelloContactList(GetUnReadSayHelloContactListMessage $message)
  262. {
  263. $page = $message->getPage() ?? 1;
  264. $take = 20;
  265. $uid = Auth::auth();
  266. $lists = FriendContact::where([array('uid', $uid), array('friend_level', 0), array('unread_cnt', '>', 0)])->with('user')->orderByDesc('last_at')->take($take)->skip(($page - 1) * $take)->get();
  267. $lists = array_values($lists->toArray());
  268. FriendContact::where('uid', $uid)->where('friend_level', 0)->update(['list_unread_msg_cnt' => 0]);
  269. $pages = array('total' => FriendContact::where([array('uid', $uid), array('friend_level', 0), array('unread_cnt', '>', 0)])->count(), 'page' => $page, 'limit' => $take);
  270. $message->setResponse(PageDTO::initFromModel($pages), ListFriendDTO::initFromModels($lists));
  271. }
  272. /**
  273. * 获取联系人信息
  274. * @param GetContactMessage $message
  275. */
  276. public function getContact(GetContactMessage $message)
  277. {
  278. $uid = Auth::auth();
  279. $contactUid = $message->getContactUid();
  280. if ($list = FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->first()) {
  281. $friendLevel = $list->friend_level;
  282. $isBlackList = $list->is_blacklist;
  283. $starAt = $list->star_at;
  284. $user = $list->user->toArray();
  285. } else {
  286. $friendLevel = 0;
  287. $isBlackList = 0;
  288. $starAt = 0;
  289. $user = UserModel::find($contactUid)->toArray();
  290. $user['note'] = null;
  291. }
  292. if (FriendApply::where([array('uid', $contactUid), array('apply_uid', $uid), array('handle', 0)])->first()) {
  293. $isUnHandlerFriendApply = true;
  294. } else {
  295. $isUnHandlerFriendApply = false;
  296. }
  297. $message->setResponse($isUnHandlerFriendApply, $friendLevel, $isBlackList, $starAt, ContactUserDTO::initFromEloquent($user));
  298. }
  299. /**
  300. * 全部好友申请列表
  301. * @param GetAllFriendAppliesMessage $message
  302. */
  303. public function getAllFriendApplies(GetAllFriendAppliesMessage $message)
  304. {
  305. $page = $message->getPage() ?? 1;
  306. $take = 20;
  307. $uid = Auth::auth();
  308. $friendApplies = FriendApply::where('apply_uid', $uid)->orWhere('uid', $uid)->with('sendUser')->with('receiveUser')->orderByDesc('last_at')->take($take)->skip(($page - 1) * $take)->get()->toArray();
  309. array_walk($friendApplies, function (&$item) use ($uid) {
  310. if ($uid == Arr::get($item, 'uid')) {
  311. Arr::set($item, 'is_unread', Arr::get($item, 'send_unread'));
  312. Arr::set($item, 'user', Arr::get($item, 'receive_user'));
  313. } else {
  314. Arr::set($item, 'is_unread', Arr::get($item, 'receive_unread'));
  315. Arr::set($item, 'user', Arr::get($item, 'send_user'));
  316. }
  317. return $item;
  318. });
  319. $pages = array('total' => FriendApply::where('apply_uid', $uid)->orWhere('uid', $uid)->count(), 'page' => $page, 'limit' => $take);
  320. $message->setResponse(PageDTO::initFromModel($pages), ListFriendApplyDTO::initFromModels($friendApplies));
  321. }
  322. /**
  323. * 发出的好友申请列表
  324. * @param GetSendFriendApplyListMessage $message
  325. */
  326. public function getSendFriendApplyList(GetSendFriendApplyListMessage $message)
  327. {
  328. $page = $message->getPage() ?? 1;
  329. $take = 20;
  330. $uid = Auth::auth();
  331. $friendApplies = FriendApply::where('uid', $uid)->with('receiveUser')->orderByDesc('last_at')->take($take)->skip(($page - 1) * $take)->get()->toArray();
  332. array_walk($friendApplies, function (&$item) use ($uid) {
  333. Arr::set($item, 'is_unread', Arr::get($item, 'send_unread'));
  334. Arr::set($item, 'user', Arr::get($item, 'receive_user'));
  335. return $item;
  336. });
  337. $pages = array('total' => FriendApply::where('apply_uid', $uid)->orWhere('uid', $uid)->count(), 'page' => $page, 'limit' => $take);
  338. $message->setResponse(PageDTO::initFromModel($pages), ListFriendApplyDTO::initFromModels($friendApplies));
  339. }
  340. /**
  341. * 收到的好友申请列表
  342. * @param GetReceiveFriendApplyListMessage $message
  343. */
  344. public function getReceiveFriendApplyList(GetReceiveFriendApplyListMessage $message)
  345. {
  346. $page = $message->getPage() ?? 1;
  347. $take = 20;
  348. $uid = Auth::auth();
  349. $friendApplies = FriendApply::where([array('apply_uid', $uid)])->with('sendUser')->orderByDesc('last_at')->take($take)->skip(($page - 1) * $take)->get()->toArray();
  350. array_walk($friendApplies, function (&$item) use ($uid) {
  351. Arr::set($item, 'is_unread', Arr::get($item, 'receive_unread'));
  352. Arr::set($item, 'user', Arr::get($item, 'send_user'));
  353. return $item;
  354. });
  355. $pages = array('total' => FriendApply::where('apply_uid', $uid)->orWhere('uid', $uid)->count(), 'page' => $page, 'limit' => $take);
  356. $message->setResponse(PageDTO::initFromModel($pages), ListFriendApplyDTO::initFromModels($friendApplies));
  357. }
  358. /**
  359. * 好友申请消息概览
  360. * @param FriendApplyMsgOverviewMessage $message
  361. */
  362. public function friendApplyMsgOverview(FriendApplyMsgOverviewMessage $message)
  363. {
  364. $uid = Auth::auth();
  365. $sendUnRead = FriendApply::where([array('send_unread', 1), array('uid', $uid)])->count();
  366. $receiveUnRead = FriendApply::where([array('receive_unread', 1), array('apply_uid', $uid)])->count();
  367. $message->setResponse($sendUnRead + $receiveUnRead, $sendUnRead, $receiveUnRead);
  368. }
  369. public function changeContactNote(ChangeContactNoteMessage $message)
  370. {
  371. $uid = Auth::auth();
  372. $contactUid = $message->getContactUid();
  373. $note = $message->getNote();
  374. FriendContact::where([array('uid', $uid), array('friend_uid', $contactUid)])->update(['note' => $note]);
  375. }
  376. /**
  377. * 相互心动联系人列表
  378. * @param GetEachLikeContactsMessage $message
  379. */
  380. public function getEachLikeContacts(GetEachLikeContactsMessage $message)
  381. {
  382. $uid = Auth::auth();
  383. $lists = FriendContact::where([array('uid', $uid), array('each_like', 1)])->with('user')->orderByDesc('last_at')->get();
  384. $lists = array_values($lists->toArray());
  385. FriendContact::where('uid', $uid)->where('friend_level', 0)->update(['list_unread_msg_cnt' => 0]);
  386. $message->setResponse(ListFriendDTO::initFromEloquents($lists));
  387. }
  388. }