FriendService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. namespace App\Services\Deed;
  3. use App\Events\EachLike;
  4. use App\Events\UnEachLike;
  5. use App\Models\Deed\EachLikeModel;
  6. use App\Models\Deed\FriendsLogModel;
  7. use App\Models\Deed\FriendsModel;
  8. use App\Models\Deed\InvitationCardModel;
  9. use App\Models\Fpdx\PairModel;
  10. use App\Models\Fpdx\RoomModel as PairRoomModel;
  11. use App\Models\Goodnight\RoomMemberModel as goodnightMemberModel;
  12. use App\Models\Goodnight\RoomModel as goodnightRoomModel;
  13. use App\Models\Gteam\RoomMemberModel as gameMemberModel;
  14. use App\Models\Gteam\RoomModel as gameRoomModel;
  15. use App\Models\Log\ClickUserHomeLogModel;
  16. use App\Models\NoticeModel;
  17. use App\Models\PraiseModel;
  18. use App\Models\User\LikeInviteQuestionModel;
  19. use App\Models\User\UserModel;
  20. use App\Services\Service;
  21. use Illuminate\Support\Facades\DB;
  22. class FriendService extends Service
  23. {
  24. public function overview(int $uid)
  25. {
  26. /** @var UserModel $user */
  27. $user = UserModel::findOrFail($uid);
  28. $friendCnt = FriendsModel::where([['uid', $uid], ['is_hide', 0]])->count();
  29. $clickMeCount = ClickUserHomeLogModel::where([
  30. ['home_uid', $uid],
  31. ['created_at', '>', time() - 86400 * 7],
  32. ])->count() + 3;
  33. $likeMeCount = 0;
  34. if ($user->partner_id > 0) {
  35. $likeMeCount = DB::table('kdgx_partner_charge_praise as praise')
  36. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  37. ->where(array(['praise.partner_id', $user->partner_id], ['praise.type', 1]))
  38. ->count();
  39. }
  40. $myLikeCount = DB::table('kdgx_partner_charge_praise as praise')
  41. ->join('kdgx_partner_charge_partner as partner', 'praise.partner_id', '=', 'partner.id')
  42. ->where([['praise.uid', $uid], ['type', 1], ['is_sell', 1]])->count();
  43. return array(
  44. 'clickme' => $clickMeCount,
  45. 'friend' => $friendCnt,
  46. 'likeme' => $likeMeCount,
  47. 'ilike' => $myLikeCount,
  48. );
  49. }
  50. /**
  51. * 好友列表数字 全部:未读 | 相互喜欢:未读 | 收到邀请:未读 | 发出邀请:未读
  52. * @param int $uid
  53. * @return array
  54. */
  55. public function getListCnt(int $uid)
  56. {
  57. $user = UserModel::findOrFail($uid);
  58. $like_me = PraiseModel::where([['read', 0], ['partner_id', $user->partner_id], ['type', 1]])->count();
  59. $eachlike = EachLikeModel::where([['tuid', $uid], ['read_at', 0], ['state', 1]])->count();
  60. $invite_me = InvitationCardModel::where([
  61. ['read', 0],
  62. ['state', 0],
  63. ['invite_uid', $uid],
  64. ['expired_at', '>', time()],
  65. ])->count();
  66. $send_invite = InvitationCardModel::where([['send_read', 0], ['uid', $uid]])->count();
  67. $sys_notice = NoticeModel::where([['uid', $uid], ['is_read', 0]])->count();
  68. return array(
  69. 'all' => $eachlike + $invite_me + $send_invite,
  70. 'each_like' => $eachlike,
  71. 'invite_me' => $invite_me,
  72. 'send_invite' => $send_invite,
  73. 'like_me' => $like_me,
  74. 'sys_notice' => $sys_notice,
  75. );
  76. }
  77. /**
  78. * 全部好友列表
  79. * @param int $uid
  80. * @param array $pages
  81. * @return array
  82. */
  83. public function getAllList(int $uid, array $pages)
  84. {
  85. $total = FriendsModel::where([['uid', $uid], ['is_hide', 0]])->count();
  86. $datas = FriendsModel::where([['uid', $uid], ['is_hide', 0]])->orderBy(
  87. 'last_at',
  88. 'desc'
  89. )->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  90. foreach ($datas as &$data) {
  91. $data->read_at = 1;
  92. /** @var FriendsModel $data */
  93. try {
  94. $data->friend = UserModel::find($data->friend_uid, [
  95. 'uid',
  96. 'headimgurl',
  97. 'nickname',
  98. 'sex',
  99. 'weixin',
  100. 'qq',
  101. 'identity_auth',
  102. 'wx_auth',
  103. 'be_vip_at',
  104. 'supvip_endat',
  105. ]);
  106. switch ($data->friend_type) {
  107. case 0: // 相互喜欢
  108. $each = EachLikeModel::find($data->friend_type_data);
  109. if (collect($each)->isNotEmpty() && $uid == $each->tuid) {
  110. $data->read_at = $each->read_at;
  111. } else {
  112. $data->read_at = 1;
  113. }
  114. break;
  115. case 1: // 心动邀请
  116. case 6: // 被心动邀请
  117. $invite = InvitationCardModel::find($data->friend_type_data);
  118. if (collect($invite)->isNotEmpty() && $uid == $invite->uid) {
  119. $data->read_at = $invite->send_read;
  120. } else {
  121. $data->read_at = $invite->read;
  122. }
  123. break;
  124. case 8: // 被取消心动
  125. $data->read_at = $data->unread_cnt > 0 ? 0 : 2;
  126. break;
  127. case 2: // 活动72h匹配
  128. case 3: // 晚安匹配
  129. case 4: // 开黑匹配
  130. case 5: // 开黑wz匹配
  131. case 7: // 取消心动
  132. default:
  133. $data->read_at = 1;
  134. break;
  135. }
  136. } catch (\Exception $exception) {
  137. }
  138. }
  139. return array(
  140. 'total' => $total,
  141. 'page' => $pages['page'],
  142. 'limit' => $pages['limit'],
  143. 'list' => $datas,
  144. );
  145. }
  146. /**
  147. * 相互喜欢列表
  148. * @param int $uid
  149. * @param array $pages
  150. * @return array
  151. */
  152. public function getEachLikeList(int $uid, array $pages)
  153. {
  154. $total = EachLikeModel::where([['uid', $uid], ['state', 1]])->orWhere([['tuid', $uid], ['state', 1]])->count();
  155. $datas = EachLikeModel::where([['uid', $uid], ['state', 1]])->orWhere([
  156. ['tuid', $uid],
  157. ['state', 1],
  158. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  159. foreach ($datas as &$data) {
  160. try {
  161. $tuid = $data->tuid;
  162. if ($data->tuid == $uid) {
  163. $tuid = $data->uid;
  164. } else {
  165. $data->read_at = 1;
  166. }
  167. $data->friend = UserModel::findOrFail($tuid, [
  168. 'uid',
  169. 'headimgurl',
  170. 'nickname',
  171. 'sex',
  172. 'weixin',
  173. 'identity_auth',
  174. 'wx_auth',
  175. 'be_vip_at',
  176. 'supvip_endat',
  177. ]);
  178. } catch (\Exception $exception) {
  179. app('sentry')->captureException($exception);
  180. $data->friend = null;
  181. }
  182. }
  183. return array(
  184. 'total' => $total,
  185. 'page' => $pages['page'],
  186. 'limit' => $pages['limit'],
  187. 'list' => $datas,
  188. );
  189. }
  190. /**
  191. * 星标好友列表
  192. * @param int $uid
  193. * @param array $pages
  194. * @return array
  195. */
  196. public function getStarList(int $uid, array $pages)
  197. {
  198. $total = FriendsModel::where([['uid', $uid], ['star_at', '>', 0], ['is_hide', 0]])->count();
  199. $datas = FriendsModel::where([['uid', $uid], ['star_at', '>', 0], ['is_hide', 0]])->orderBy(
  200. 'last_at',
  201. 'desc'
  202. )->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  203. foreach ($datas as &$data) {
  204. $data->read_at = 1;
  205. /** @var FriendsModel $data */
  206. try {
  207. $data->friend = UserModel::findOrFail($data->friend_uid, [
  208. 'uid',
  209. 'headimgurl',
  210. 'nickname',
  211. 'sex',
  212. 'weixin',
  213. 'identity_auth',
  214. 'wx_auth',
  215. 'be_vip_at',
  216. 'supvip_endat',
  217. ]);
  218. switch ($data->friend_type) {
  219. case 0: // 相互喜欢
  220. $each = EachLikeModel::find($data->friend_type_data);
  221. if ($each->isNotEmpty() && $uid == $each->tuid) {
  222. $data->read_at = $each->read_at;
  223. } else {
  224. $data->read_at = 1;
  225. }
  226. break;
  227. case 1: // 心动邀请
  228. case 6: // 被心动邀请
  229. $invite = InvitationCardModel::findOrFail($data->friend_type_data);
  230. if ($uid == $invite->uid) {
  231. $data->read_at = $invite->send_read;
  232. } else {
  233. $data->read_at = $invite->read;
  234. }
  235. break;
  236. case 8:
  237. $data->read_at = $data->unread_cnt > 0 ? 0 : 2;
  238. break;
  239. case 7:
  240. case 2: // 活动72h匹配
  241. case 3: // 晚安匹配
  242. case 4: // 开黑匹配
  243. case 5: // 开黑wz匹配
  244. default:
  245. $data->read_at = 1;
  246. break;
  247. }
  248. } catch (\Exception $exception) {
  249. $data->friend = null;
  250. }
  251. }
  252. return array(
  253. 'total' => $total,
  254. 'page' => $pages['page'],
  255. 'limit' => $pages['limit'],
  256. 'list' => $datas,
  257. );
  258. }
  259. /**
  260. * 标记相互喜欢
  261. * @param int $uid
  262. * @param int $partner_id
  263. * @param int $thumb
  264. * @return UserModel|bool
  265. */
  266. public function eachlike(int $uid, int $partner_id, int $thumb)
  267. {
  268. /** @var UserModel $user */
  269. $user = UserModel::findOrFail($uid);
  270. /** @var UserModel $puser */
  271. $puser = UserModel::where('partner_id', $partner_id)->firstOrFail([
  272. 'uid',
  273. 'nickname',
  274. 'headimgurl',
  275. 'sex',
  276. 'weixin',
  277. 'qq',
  278. 'identity_auth',
  279. 'wx_auth',
  280. 'be_vip_at',
  281. 'supvip_endat',
  282. ]);
  283. $eachlike = EachLikeModel::where(array(['uid', $user->uid], ['tuid', $puser->uid]))
  284. ->orWhere(array(['uid', $puser->uid], ['tuid', $user->uid]))->first();
  285. if (collect($eachlike)->isEmpty()) {
  286. if (0 == $thumb) {
  287. return false;
  288. }
  289. if (PraiseModel::where([['uid', $puser->uid], ['partner_id', $user->partner_id], ['type', 1]])->exists()) {
  290. EachLikeModel::firstOrCreate(
  291. ['uid' => $user->uid, 'tuid' => $puser->uid],
  292. [
  293. 'created_at' => time(),
  294. 'updated_at' => time(),
  295. 'read_at' => 0,
  296. 'state' => 1,
  297. 'tuid' => $puser->uid,
  298. ]
  299. );
  300. // log=你和xxx相互心动
  301. event(new EachLike($uid, $puser->uid));
  302. $ns = new NoticeService();
  303. $ns->eachLike($puser->uid, $uid);
  304. return $puser;
  305. }
  306. } else {
  307. if (0 == $thumb && 1 == $eachlike->state) {
  308. $eachlike->state = 0;
  309. $eachlike->read_at = 0;
  310. $eachlike->updated_at = time();
  311. $eachlike->save();
  312. // log=xxx取消了对你的心动
  313. event(new UnEachLike($uid, $puser->uid));
  314. return false;
  315. }
  316. if (PraiseModel::where([['uid', $puser->uid], ['partner_id', $user->partner_id], ['type', 1]])->exists()) {
  317. $eachlike->state = 1;
  318. $eachlike->read_at = 0;
  319. $eachlike->updated_at = time();
  320. $eachlike->save();
  321. // log=你和xxx相互心动
  322. event(new EachLike($uid, $puser->uid));
  323. $ns = new NoticeService();
  324. $ns->eachLike($puser->uid, $uid);
  325. return $puser;
  326. }
  327. }
  328. return false;
  329. }
  330. /**
  331. * 好友关系维护-取消心动
  332. * @param int $uid
  333. * @param int $friend_uid
  334. */
  335. public function unEachLike(int $uid, int $friend_uid)
  336. {
  337. if ($uid == $friend_uid) {
  338. return;
  339. }
  340. FriendsModel::where([['uid', $uid], ['friend_uid', $friend_uid]])->update([
  341. 'friend_type' => 7,
  342. 'last_at' => time(),
  343. ]);
  344. FriendsModel::where([['uid', $friend_uid], ['friend_uid', $uid]])->update([
  345. 'friend_type' => 8,
  346. 'last_at' => time(),
  347. 'unread_cnt' => 1,
  348. ]);
  349. // 邀请
  350. $blInvite = InvitationCardModel::where([['uid', $uid], ['invite_uid', $friend_uid], ['state', 1]])
  351. ->orWhere([['uid', $friend_uid], ['invite_uid', $uid], ['state', 1]])->exists();
  352. // 开黑
  353. $blGteam = !empty((new gameMemberModel())->friendRooms($uid, $friend_uid));
  354. // 晚安
  355. $blGoodnight = !empty((new goodnightMemberModel())->friendRooms($uid, $friend_uid));
  356. // 72h
  357. $blPair = PairRoomModel::where('type', 1)->where('member', "{$uid},{$friend_uid}")->orWhere(
  358. 'member',
  359. "{$friend_uid},{$uid}"
  360. )->exists();
  361. if (!($blInvite || $blGteam || $blGoodnight || $blPair)) {
  362. FriendsModel::where([['uid', $uid], ['friend_uid', $friend_uid]])->update(['is_friend' => 0]);
  363. FriendsModel::where([['uid', $friend_uid], ['friend_uid', $uid]])->update(['is_friend' => 0]);
  364. }
  365. return;
  366. }
  367. /**
  368. * 成为好友
  369. * @param int $uid
  370. * @param int $friend_uid
  371. * @param int $friend_type
  372. * @param $friend_type_data
  373. * @return bool
  374. */
  375. public function befriend(int $uid, int $friend_uid, int $friend_type, $friend_type_data)
  376. {
  377. if ($uid == $friend_uid) {
  378. return false;
  379. }
  380. $type1 = $type2 = $friend_type;
  381. if ($friend_type == 1) {
  382. $type2 = 6;
  383. }
  384. if ($friend_type == 6) {
  385. $type2 = 1;
  386. }
  387. FriendsModel::updateOrCreate([
  388. 'uid' => $uid,
  389. 'friend_uid' => $friend_uid,
  390. ], [
  391. 'created_at' => time(),
  392. 'uid' => $uid,
  393. 'friend_uid' => $friend_uid,
  394. 'last_at' => time(),
  395. 'friend_type' => $type1,
  396. 'friend_type_data' => $friend_type_data,
  397. ]);
  398. FriendsModel::updateOrCreate([
  399. 'uid' => $friend_uid,
  400. 'friend_uid' => $uid,
  401. ], [
  402. 'created_at' => time(),
  403. 'uid' => $friend_uid,
  404. 'friend_uid' => $uid,
  405. 'last_at' => time(),
  406. 'friend_type' => $type2,
  407. 'friend_type_data' => $friend_type_data,
  408. ]);
  409. $user = UserModel::findOrFail($uid);
  410. $friend = UserModel::findOrFail($friend_uid);
  411. PraiseModel::where([['uid', $user->uid], ['partner_id', $friend->partner_id]])->update(['lock' => 100]);
  412. PraiseModel::where([['uid', $friend->uid], ['partner_id', $user->partner_id]])->update(['lock' => 100]);
  413. return true;
  414. }
  415. /**
  416. * 获取好友交互历史
  417. * @param int $uid
  418. * @param int $friend_uid
  419. * @return \Illuminate\Support\Collection
  420. */
  421. public function history(int $uid, int $friend_uid)
  422. {
  423. if ($uid == $friend_uid) {
  424. return collect();
  425. }
  426. FriendsModel::where('uid', $uid)->update(['unread_cnt' => 0]);
  427. $my_invites = InvitationCardModel::where([['uid', $uid], ['invite_uid', $friend_uid]])
  428. ->select(
  429. 'id',
  430. 'created_at',
  431. 'state',
  432. 'expired_at',
  433. 'receive_at',
  434. 'read',
  435. 'question_id',
  436. 'question_type',
  437. 'question_answer',
  438. 'say_hello',
  439. 'kk',
  440. 'paint_id',
  441. 'request_content',
  442. 'response_content'
  443. )
  444. ->get()->map(function ($value) use ($uid) {
  445. /** @var InvitationCardModel $value */
  446. if ($value->state == 1 && $value->send_read == 0) {
  447. $value->send_read = 1;
  448. $value->save();
  449. }
  450. $question = LikeInviteQuestionModel::withTrashed()->find($value->question_id);
  451. $value->question_uid = $question ? $question->uid : 1;
  452. $value->voice_src = $question ? $question->voice_src : '';
  453. $value->thumbs = $question ? $question->thumbs : 0;
  454. $value->question = $question ? json_decode($question->question, true) : null;
  455. switch ($value->question_type) {
  456. case '4':
  457. case '3':
  458. break;
  459. }
  460. return collect($value)->put('origin', 'my_invite');
  461. });
  462. $friend_invites = InvitationCardModel::where([['uid', $friend_uid], ['invite_uid', $uid]])
  463. ->where('state', '>=', 0)
  464. ->select(
  465. 'id',
  466. 'created_at',
  467. 'state',
  468. 'expired_at',
  469. 'receive_at',
  470. 'read',
  471. 'question_id',
  472. 'question_type',
  473. 'question_answer',
  474. 'say_hello',
  475. 'kk',
  476. 'paint_id',
  477. 'request_content',
  478. 'response_content'
  479. )
  480. ->get()->map(function ($value) use ($uid) {
  481. /** @var InvitationCardModel $value */
  482. if ($value->state == 0 && $value->read == 0) {
  483. $value->read = 1;
  484. $value->save();
  485. }
  486. $question = LikeInviteQuestionModel::withTrashed()->find($value->question_id);
  487. $value->question_uid = $question ? $question->uid : 1;
  488. $value->voice_src = $question ? $question->voice_src : '';
  489. $value->thumbs = $question ? $question->thumbs : 0;
  490. $value->question = $question ? json_decode($question->question, true) : null;
  491. switch ($value->question_type) {
  492. case '4':
  493. case '3':
  494. break;
  495. }
  496. return collect($value)->put('origin', 'friend_invite');
  497. });
  498. $game_rooms = gameRoomModel::find(
  499. (new gameMemberModel())->friendRooms($uid, $friend_uid),
  500. ['id', 'created_at', 'type']
  501. )
  502. ->map(function ($room) {
  503. if (in_array($room->type, ['cj_wx', 'cj_qq'])) {
  504. return collect($room)->put('origin', 'game_cj');
  505. } else {
  506. return collect($room)->put('origin', 'game_wz');
  507. }
  508. });
  509. $night_rooms = goodnightRoomModel::find(
  510. (new goodnightMemberModel())->friendRooms($uid, $friend_uid),
  511. ['id', 'created_at']
  512. )
  513. ->map(function ($room) {
  514. return collect($room)->put('origin', 'goodnight');
  515. });
  516. $pair_rooms = PairRoomModel::select(['created_at', 'room_id as id', 'stage_id'])->where(
  517. 'type',
  518. 1
  519. )->where('member', "{$uid},{$friend_uid}")->orWhere('member', "{$friend_uid},{$uid}")->get()
  520. ->map(function ($room) {
  521. /** @var PairRoomModel $room */
  522. if (PairModel::where(['room_id' => $room->id])->value('activity_type') == 'qbj') {
  523. return collect($room)->put('origin', 'qbj');
  524. } else {
  525. return collect($room)->put('origin', 'pair');
  526. }
  527. });
  528. EachLikeModel::where([['uid', $friend_uid], ['tuid', $uid], ['read_at', 0]])->update(['read_at' => time()]);
  529. $each_likes = FriendsLogModel::select([
  530. 'id',
  531. 'created_at',
  532. 'uid',
  533. 'tuid',
  534. 'do',
  535. 'log',
  536. 'read_at',
  537. ])->where([['uid', $uid], ['tuid', $friend_uid]])
  538. ->orWhere(array(['uid', $friend_uid], ['tuid', $uid]))->get()->map(function ($value) use ($uid) {
  539. /** @var FriendsLogModel $value */
  540. if ($uid == $value->tuid && $value->read_at == 0) {
  541. $value->read_at = time();
  542. $value->save();
  543. }
  544. return collect($value)->put('origin', 'eachlike');
  545. });
  546. return collect()->merge($my_invites)->merge($friend_invites)->merge($game_rooms)->merge($night_rooms)
  547. ->merge($pair_rooms)
  548. ->merge($each_likes)
  549. ->sortByDesc('created_at')
  550. ->values()->all();
  551. }
  552. }