LikeManager.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Managers;
  3. use App\Exceptions\AlertException;
  4. use App\Models\PartnerModel;
  5. use App\Models\PraiseModel;
  6. use App\Models\Share\LikemeCheckModel;
  7. use App\Models\User\UserModel;
  8. use App\Services\Share\NoticeService;
  9. use Illuminate\Support\Facades\DB;
  10. class LikeManager
  11. {
  12. /**
  13. * 核销解锁助力
  14. * @param int $uid
  15. * @param int $invite_id
  16. * @return array
  17. * @throws \Exception
  18. */
  19. public function check(int $uid, int $invite_id): array
  20. {
  21. $invite = PraiseModel::findOrFail($invite_id);
  22. if ($invite->lock >= 100) {
  23. throw new AlertException("该助力已结束,卡片成功解锁啦", 104);
  24. }
  25. if (LikemeCheckModel::where([['praise_id', $invite_id], ['invite_uid', $uid]])->exists()) {
  26. throw new AlertException("你已经帮ta助力过了哦", 102);
  27. }
  28. $user = UserModel::findOrFail($uid);
  29. if (
  30. $user->partner_id != $invite->partner_id
  31. && LikemeCheckModel::where([['invite_uid', $uid], ['created_at', '>', mktime(0, 0, 0)]])->count() > 2
  32. ) {
  33. throw new AlertException("你今日的助力机会用完啦,明天再帮ta助力吧", 103);
  34. }
  35. DB::beginTransaction();
  36. try {
  37. $likememodel = new LikemeCheckModel();
  38. $likeme = $likememodel->fill([
  39. 'praise_id' => $invite->id,
  40. 'partner_id' => $invite->partner_id,
  41. 'uid' => $invite->uid,
  42. 'invite_uid' => $uid
  43. ]);
  44. $dis_score = min(ceil(max((100 - $invite->lock) * max(
  45. sprintf("%.2f", 0 + mt_rand() / mt_getrandmax() * 1),
  46. 0.2
  47. ), 15)), 100 - $invite->lock);
  48. $likeme->dis_score = $dis_score;
  49. $likeme->save();
  50. PraiseModel::where('id', $invite->id)->increment('lock', $likeme->dis_score);
  51. DB::commit();
  52. $islike = UserModel::where('partner_id', $invite->partner_id)->first();
  53. if ($user->partner_id != $invite->partner_id) {
  54. $ns = new NoticeService();
  55. $ns->inviteToLock($islike->uid, $uid, $invite->id);
  56. }
  57. return array(
  58. 'dis_score' => $likeme->dis_score,
  59. 'user' => [
  60. 'nickname' => $islike->nickname
  61. ]
  62. );
  63. } catch (\Exception $exception) {
  64. DB::rollBack();
  65. throw $exception;
  66. }
  67. }
  68. /**
  69. * 获取某个邀请的助力历史
  70. * @param int $invite_id
  71. * @return array
  72. */
  73. public function history(int $invite_id): array
  74. {
  75. $invites = LikemeCheckModel::where('praise_id', $invite_id)->get();
  76. foreach ($invites as $k => &$invite) {
  77. try {
  78. $invite->user = UserModel::findOrFail($invite->invite_uid, ['uid', 'headimgurl', 'nickname', 'sex']);
  79. } catch (\Exception $e) {
  80. unset($invites->$k);
  81. continue;
  82. }
  83. }
  84. return $invites->toArray();
  85. }
  86. /**
  87. * 获取分享信息
  88. * @param int $invite_id
  89. * @return array
  90. */
  91. public function get(int $invite_id): array
  92. {
  93. $praise = PraiseModel::findOrFail($invite_id);
  94. $like = UserModel::findOrFail($praise->uid);
  95. $partner = PartnerModel::findOrFail($like->partner_id);
  96. $partner->photo_src = empty($partner->photo_src) ? null : "http://oss.pocketuniversity.cn{$partner->photo_src}";
  97. $partner->photo_1 = empty($partner->photo_1) ? null : "http://oss.pocketuniversity.cn{$partner->photo_1}";
  98. $partner->photo_2 = empty($partner->photo_2) ? null : "http://oss.pocketuniversity.cn{$partner->photo_2}";
  99. $partner->photo_3 = empty($partner->photo_3) ? null : "http://oss.pocketuniversity.cn{$partner->photo_3}";
  100. $partner->photo_4 = empty($partner->photo_4) ? null : "http://oss.pocketuniversity.cn{$partner->photo_4}";
  101. $islike = UserModel::where('partner_id', $praise->partner_id)->first();
  102. $lock_cnt = LikemeCheckModel::where('praise_id', $praise->id)->count();
  103. return array(
  104. 'praise_id' => $praise->id,
  105. 'updated_at' => $praise->updated_at,
  106. 'lock' => $praise->lock,
  107. 'lock_cnt' => $lock_cnt,
  108. 'partner' => array(
  109. 'id' => $partner->id,
  110. 'partner_id' => $partner->id,
  111. 'photo_src' => $partner->photo_src,
  112. 'photo_1' => $partner->photo_1,
  113. 'photo_2' => $partner->photo_2,
  114. 'photo_3' => $partner->photo_3,
  115. 'photo_4' => $partner->photo_4,
  116. 'voice' => $partner->voice,
  117. 'praises' => $partner->praises,
  118. ),
  119. 'user' => array(
  120. 'uid' => $like->uid,
  121. 'nickname' => $like->nickname,
  122. 'sex' => $like->sex
  123. )
  124. );
  125. }
  126. }