CircleRepository.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. $limitCount = 0;
  69. if ($circle->join_limit) {
  70. if($circle->limit_condition){
  71. $key = 'circle_error_count_' . $userInfo['uid'];
  72. $errorCount = Redis::get($key);
  73. if ($errorCount >= $circle->limit_condition) {
  74. return jsonError('今日次数已用完,明天再来吧', ['answer_count' => 0]);
  75. }
  76. if (empty($request['answer'])) {
  77. return jsonError('请输入答案~', ['answer_count' => intval($circle->limit_condition)-$errorCount]);
  78. }
  79. }else{
  80. $limitCount = -1;
  81. if (empty($request['answer'])) {
  82. return jsonError('请输入答案~', ['answer_count' => $limitCount]);
  83. }
  84. }
  85. $question = json_decode($circle->join_question, true);
  86. $answer = json_decode($request['answer'], true);
  87. $checkRow = $this->checkQuestion($question, $answer);
  88. if (!$checkRow) {
  89. if($circle->limit_condition){
  90. if (!$errorCount) {
  91. Redis::set($key, 1);
  92. $expire = Carbon::today()->endOfDay()->timestamp - Carbon::now()->timestamp;
  93. Redis::expire($key, $expire);
  94. } else {
  95. Redis::incr($key);
  96. }
  97. $answerCount = intval($circle->limit_condition - Redis::get($key));
  98. }else{
  99. $answerCount = -1;
  100. }
  101. if ($answerCount == 0) {
  102. return jsonError('学习学习,明天再来吧~', ['answer_count' => $answerCount]);
  103. } else {
  104. return jsonError('真遗憾,没有答对哦~', ['answer_count' => $answerCount]);
  105. }
  106. }
  107. }
  108. DB::beginTransaction();
  109. try {
  110. $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['id']]);
  111. $this->interestCircle->where('id', $request['id'])->increment('join_count');
  112. DB::commit();
  113. //加入圈子成功后,清除错误回答次数
  114. $key = 'circle_error_count_' . $userInfo['uid'];
  115. Redis::del($key);
  116. return jsonSuccess();
  117. } catch (QueryException $exception) {
  118. DB::rollBack();
  119. return jsonError('加入圈子失败,再试试吧', ['answer_count' => $limitCount]);
  120. }
  121. }
  122. /**
  123. * 检测答案是否正确
  124. * @param $question 问题
  125. * @param $answer 答案
  126. * @return bool
  127. */
  128. private function checkQuestion($question, $answer)
  129. {
  130. $result = true;
  131. $rightAnswer = [];
  132. foreach ($question as $key => $value) {
  133. $temp = [];
  134. foreach ($value['answer'] as $k => $v) {
  135. if ($v['right']) {
  136. $temp['answer_id'][] = $k + 1;
  137. }
  138. }
  139. $rightAnswer[$key + 1] = $temp;
  140. }
  141. // var_dump(json_encode($rightAnswer));
  142. foreach ($answer as $ak => $av) {
  143. $right = $rightAnswer[$av['question']]['answer_id'];
  144. // var_dump($right);
  145. // var_dump($av['answer']);
  146. // var_dump($right != $av['answer']);
  147. if ($right != $av['answer']) {
  148. $result = false;
  149. break;
  150. }
  151. }
  152. return $result;
  153. }
  154. /**
  155. * 退出圈子
  156. * @param $request
  157. * @param $userInfo
  158. * @return array
  159. */
  160. public function exitCircle($request, $userInfo)
  161. {
  162. $info = $this->interestCircleUser
  163. ->where('uid', $userInfo['uid'])
  164. ->where('circle_id', $request['id'])
  165. ->first();
  166. if (!$info) {
  167. return jsonError('您未加入该圈子');
  168. }
  169. if($info->is_black){
  170. return jsonError('您在本圈子内的权限受限');
  171. }
  172. DB::beginTransaction();
  173. try {
  174. $info->delete();
  175. $this->interestCircle->where('id', $request['id'])->decrement('join_count');
  176. DB::commit();
  177. //退出圈子成功后,清除错误回答次数
  178. $key = 'circle_error_count_' . $userInfo['uid'];
  179. Redis::del($key);
  180. return jsonSuccess();
  181. } catch (QueryException $exception) {
  182. DB::rollBack();
  183. return jsonError('退出圈子失败,再试试吧');
  184. }
  185. }
  186. }