LockInvite.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Http\Controllers\Gnight;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Controllers\Miniprogram\Auth;
  6. use App\Models\Gnight\GetVoiceModel;
  7. use App\Models\Gnight\LockInviteModel;
  8. use App\Models\Gnight\UserModel;
  9. use Illuminate\Support\Facades\DB;
  10. class LockInvite extends Controller
  11. {
  12. /**
  13. * @param int $get_voice_id
  14. * @return array
  15. * @throws AlertException
  16. * @api {get} /api/gnight/lockinvite/check 助力解锁
  17. * @apiName check
  18. * @apiGroup lockinvite
  19. *
  20. * @apiParam {int} $get_voice_id 助力链接id
  21. *
  22. * @apiSuccess {int} lock 当前解锁进度
  23. */
  24. public function check(int $get_voice_id)
  25. {
  26. try {
  27. $uid = Auth::auth();
  28. if (
  29. LockInviteModel::where([
  30. ['invite_uid', $uid],
  31. ['get_voice_id', $get_voice_id]
  32. ])->exists()
  33. ) {
  34. throw new AlertException("你已为TA助力", 103);
  35. }
  36. $getVoice = GetVoiceModel::findOrFail($get_voice_id);
  37. if ($getVoice->lock >= 3) {
  38. throw new AlertException("已经被解锁", 101);
  39. }
  40. if ($uid == $getVoice->uid) {
  41. throw new AlertException("不能给自己助力", 105);
  42. }
  43. if (
  44. LockInviteModel::where([
  45. ['invite_uid', $uid],
  46. ['created_at', '>', mktime(0, 0, 0)]
  47. ])->exists()
  48. ) {
  49. throw new AlertException("你今日助力次数已用完", 102);
  50. }
  51. DB::beginTransaction();
  52. try {
  53. DB::table('kdgx_gnight_lock_invite')->insert([
  54. 'created_at' => time(),
  55. 'get_voice_id' => $get_voice_id,
  56. 'invite_uid' => $uid
  57. ]);
  58. DB::table('kdgx_gnight_get_voice')->where('id', $get_voice_id)->increment('lock', 1);
  59. if ($getVoice->lock == 2) {
  60. // 发送小程序通知
  61. try {
  62. $notive = new Notive();
  63. $notive->lock($getVoice->uid);
  64. } catch (\Exception $e) {
  65. }
  66. }
  67. DB::commit();
  68. return array(
  69. 'code' => 200,
  70. 'message' => 'success',
  71. 'data' => [
  72. 'lock' => $getVoice->lock + 1
  73. ],
  74. );
  75. } catch (\Exception $e) {
  76. DB::rollBack();
  77. throw $e;
  78. }
  79. } catch (\Exception $e) {
  80. throw new AlertException($e->getMessage(), intval($e->getCode()));
  81. }
  82. }
  83. /**
  84. * @param int $get_voice_id
  85. * @return array
  86. * @api {get} /api/gnight/lockinvite/invites 邀请页信息
  87. * @apiName invites
  88. * @apiGroup lockinvite
  89. *
  90. * @apiParam {int} get_voice_id 助力链接id
  91. *
  92. * @apiSuccess {int} get_voice_id 助力链接id
  93. * @apiSuccess {int} lock 解锁进度
  94. * @apiSuccess {string} headimgurl 解锁用户头像
  95. * @apiSuccess {array} invites 助力用户列表
  96. * @apiSuccess {int} invites.uid 助力用户id
  97. * @apiSuccess {int} invites.headimgurl 助力用户头像
  98. *
  99. */
  100. public function invites(int $get_voice_id)
  101. {
  102. $result = array(
  103. 'uid' => 0,
  104. 'get_voice_id' => $get_voice_id,
  105. 'lock' => 0,
  106. 'headimgurl' => '',
  107. 'invites' => []
  108. );
  109. $getVoice = GetVoiceModel::findOrFail($get_voice_id);
  110. $result['uid'] = $getVoice->uid;
  111. $user = UserModel::findOrFail($getVoice->uid);
  112. $result['headimgurl'] = $user->headimgurl;
  113. $result['lock'] = $getVoice->lock;
  114. $invites = LockInviteModel::where('get_voice_id', $get_voice_id)->get();
  115. foreach ($invites as &$invite) {
  116. try {
  117. $user = UserModel::findOrFail($invite->invite_uid);
  118. array_push($result['invites'], [
  119. 'uid' => $user->uid,
  120. 'headimgurl' => $user->headimgurl
  121. ]);
  122. } catch (\Exception $e) {
  123. array_push($result['invites'], [
  124. 'uid' => 0,
  125. 'headimgurl' => ''
  126. ]);
  127. continue;
  128. }
  129. }
  130. return array(
  131. 'code' => 200,
  132. 'message' => 'success',
  133. 'data' => $result
  134. );
  135. }
  136. }