CircleRepository.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleUser;
  11. use Illuminate\Database\QueryException;
  12. use Dingo\Api\Http\Response;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class CircleRepository
  18. {
  19. public function __construct(InterestCircle $interestCircle,
  20. InterestCircleUser $interestCircleUser)
  21. {
  22. $this->interestCircle = $interestCircle;
  23. $this->interestCircleUser = $interestCircleUser;
  24. }
  25. /**
  26. * 圈子列表
  27. */
  28. public function circleLists($request, $limit = 3)
  29. {
  30. $where[] = ['is_open', 1];
  31. if (isset($request['is_recommend'])) {
  32. $where[] = ['is_recommend', 1];
  33. }
  34. return $this->interestCircle
  35. ->where($where)
  36. ->orderBy('id', 'desc')
  37. ->take($limit)
  38. ->get();
  39. }
  40. /**
  41. * 圈子详情
  42. */
  43. public function detail($request, $isTrashed = false)
  44. {
  45. $model = $this->interestCircle;
  46. if ($isTrashed) {
  47. $model->withTrashed();
  48. }
  49. $this->interestCircle->where('id', $request['id'])->increment('view_count');
  50. return $model->find($request['id']);
  51. }
  52. /**
  53. * 加入圈子
  54. * @param $request
  55. * @param $userInfo
  56. * @return array
  57. */
  58. public function joinCircle($request, $userInfo)
  59. {
  60. $row = $this->interestCircleUser
  61. ->where('uid', $userInfo['uid'])
  62. ->where('circle_id', $request['id'])
  63. ->first();
  64. if ($row) {
  65. return jsonError('您已经加入该圈子了', ['answer_count' => 0]);
  66. }
  67. $circle = $this->interestCircle->where('id', $request['id'])->first();
  68. if ($circle->join_limit) {
  69. $key = 'circle_error_count_' . $userInfo['uid'];
  70. $errorCount = Redis::get($key);
  71. if ($errorCount >= $circle->limit_condition) {
  72. return jsonError('今日次数已用完,明天再来吧', ['answer_count' => 0]);
  73. }
  74. if (empty($request['answer'])) {
  75. return jsonError('请输入答案~', ['answer_count' => intval($circle->limit_condition)]);
  76. }
  77. $question = json_decode($circle->join_question, true);
  78. $answer = json_decode($request['answer'], true);
  79. $checkRow = $this->checkQuestion($question, $answer);
  80. if (!$checkRow) {
  81. if (!$errorCount) {
  82. Redis::set($key, 1);
  83. $expire = Carbon::today()->endOfDay()->timestamp - Carbon::now()->timestamp;
  84. Redis::expire($key, $expire);
  85. } else {
  86. Redis::incr($key);
  87. }
  88. $answerCount = intval($circle->limit_condition - Redis::get($key));
  89. if ($answerCount <= 0) {
  90. return jsonError('学习学习,明天再来吧~', ['answer_count' => $answerCount]);
  91. } else {
  92. return jsonError('真遗憾,没有答对哦~', ['answer_count' => $answerCount]);
  93. }
  94. }
  95. }
  96. DB::beginTransaction();
  97. try {
  98. $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['id']]);
  99. $this->interestCircle->where('id', $request['id'])->increment('join_count');
  100. DB::commit();
  101. //加入圈子成功后,清除错误回答次数
  102. $key = 'circle_error_count_' . $userInfo['uid'];
  103. Redis::del($key);
  104. return jsonSuccess();
  105. } catch (QueryException $exception) {
  106. DB::rollBack();
  107. return jsonError('加入圈子失败,再试试吧', ['answer_count' => 0]);
  108. }
  109. }
  110. /**
  111. * 检测答案是否正确
  112. * @param $question 问题
  113. * @param $answer 答案
  114. * @return bool
  115. */
  116. private function checkQuestion($question, $answer)
  117. {
  118. $result = true;
  119. $rightAnswer = [];
  120. foreach ($question as $key => $value) {
  121. $temp = [];
  122. foreach ($value['answer'] as $k => $v) {
  123. if ($v['right']) {
  124. $temp['answer_id'][] = $k + 1;
  125. }
  126. }
  127. $rightAnswer[$key + 1] = $temp;
  128. }
  129. // var_dump(json_encode($rightAnswer));
  130. foreach ($answer as $ak => $av) {
  131. $right = $rightAnswer[$av['question']]['answer_id'];
  132. // var_dump($right);
  133. // var_dump($av['answer']);
  134. // var_dump($right != $av['answer']);
  135. if ($right != $av['answer']) {
  136. $result = false;
  137. break;
  138. }
  139. }
  140. return $result;
  141. }
  142. /**
  143. * 退出圈子
  144. * @param $request
  145. * @param $userInfo
  146. * @return array
  147. */
  148. public function exitCircle($request, $userInfo)
  149. {
  150. $info = $this->interestCircleUser
  151. ->where('uid', $userInfo['uid'])
  152. ->where('circle_id', $request['id'])
  153. ->first();
  154. if (!$info) {
  155. return jsonError('您未加入该圈子');
  156. }
  157. DB::beginTransaction();
  158. try {
  159. $info->delete();
  160. $this->interestCircle->where('id', $request['id'])->decrement('join_count');
  161. DB::commit();
  162. //退出圈子成功后,清除错误回答次数
  163. $key = 'circle_error_count_' . $userInfo['uid'];
  164. Redis::del($key);
  165. return jsonSuccess();
  166. } catch (QueryException $exception) {
  167. DB::rollBack();
  168. return jsonError('退出圈子失败,再试试吧');
  169. }
  170. }
  171. }