CircleRepository.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. return $model->find($request['id']);
  50. }
  51. /**
  52. * 加入圈子
  53. * @param $request
  54. * @param $userInfo
  55. * @return array
  56. */
  57. public function joinCircle($request, $userInfo)
  58. {
  59. $row = $this->interestCircleUser
  60. ->where('uid', $userInfo['uid'])
  61. ->where('circle_id', $request['id'])
  62. ->first();
  63. if ($row) {
  64. return jsonError('您已经加入该圈子了');
  65. }
  66. $circle = $this->interestCircle->where('id', $request['id'])->first();
  67. $key = 'circle_error_count_' . $userInfo['uid'];
  68. $errorCount = Redis::get($key);
  69. if ($errorCount >= $circle->limit_condition) {
  70. return jsonError('今日次数已用完,明天再来吧');
  71. }
  72. if ($circle->join_limit) {
  73. if (empty($request['answer'])) {
  74. return jsonError('请输入答案~');
  75. }
  76. $question = json_decode($circle->join_question, true);
  77. $answer = json_decode($request['answer'], true);
  78. $checkRow = $this->checkQuestion($question, $answer);
  79. if (!$checkRow) {
  80. if (!$errorCount) {
  81. Redis::set($key, 1);
  82. $expire = Carbon::today()->endOfDay()->timestamp - Carbon::now()->timestamp;
  83. Redis::expire($key, $expire);
  84. } else {
  85. Redis::incr($key);
  86. }
  87. return jsonError('学习学习,明天再来吧~');
  88. }
  89. }
  90. DB::beginTransaction();
  91. try {
  92. $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['id']]);
  93. DB::commit();
  94. //加入圈子成功后,清除错误回答次数
  95. $key = 'circle_error_count_' . $userInfo['uid'];
  96. Redis::del($key);
  97. return jsonSuccess();
  98. } catch (QueryException $exception) {
  99. DB::rollBack();
  100. return jsonError('加入圈子失败,再试试吧');
  101. }
  102. }
  103. /**
  104. * 检测答案是否正确
  105. * @param $question 问题
  106. * @param $answer 答案
  107. * @return bool
  108. */
  109. private function checkQuestion($question, $answer)
  110. {
  111. $result = true;
  112. $rightAnswer = [];
  113. foreach ($question as $key => $value) {
  114. $temp = [];
  115. foreach ($value['answer'] as $k => $v) {
  116. if ($v['right']) {
  117. $temp['answer_id'][] = $k + 1;
  118. }
  119. }
  120. $rightAnswer[$key + 1] = $temp;
  121. }
  122. // var_dump(json_encode($rightAnswer));
  123. foreach ($answer as $ak => $av) {
  124. $right = $rightAnswer[$av['question']]['answer_id'];
  125. // var_dump($right);
  126. // var_dump($av['answer']);
  127. // var_dump($right != $av['answer']);
  128. if ($right != $av['answer']) {
  129. $result = false;
  130. break;
  131. }
  132. }
  133. return $result;
  134. }
  135. /**
  136. * 退出圈子
  137. * @param $request
  138. * @param $userInfo
  139. * @return array
  140. */
  141. public function exitCircle($request, $userInfo)
  142. {
  143. $info = $this->interestCircleUser
  144. ->where('uid', $userInfo['uid'])
  145. ->where('circle_id', $request['id'])
  146. ->first();
  147. if (!$info) {
  148. return jsonError('您未加入该圈子');
  149. }
  150. DB::beginTransaction();
  151. try {
  152. $info->delete();
  153. DB::commit();
  154. //退出圈子成功后,清除错误回答次数
  155. $key = 'circle_error_count_' . $userInfo['uid'];
  156. Redis::del($key);
  157. return jsonSuccess();
  158. } catch (QueryException $exception) {
  159. DB::rollBack();
  160. return jsonError('退出圈子失败,再试试吧');
  161. }
  162. }
  163. /**
  164. * 相册列表
  165. */
  166. public function pictureLists($request)
  167. {
  168. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  169. $where = [];
  170. if (isset($request['circle_id'])) {
  171. $where[] = ['circle_id', $request['circle_id']];
  172. }
  173. if (isset($request['uid'])) {
  174. $where[] = ['uid', $request['uid']];
  175. }
  176. return $this->interestCirclePicture
  177. ->where($where)
  178. ->orderBy('created_at', 'desc')
  179. ->paginate($perPage);
  180. }
  181. /**
  182. * 删除相册图片
  183. */
  184. public function deletePicture($request)
  185. {
  186. $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
  187. if (!$circle) {
  188. return Response::create([
  189. 'message' => '获取图片信息失败',
  190. 'status_code' => 500
  191. ]);
  192. }
  193. DB::beginTransaction();
  194. try {
  195. $circle->delete();
  196. DB::commit();
  197. return Response::create();
  198. } catch (QueryException $exception) {
  199. DB::rollBack();
  200. Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
  201. return Response::create([
  202. 'message' => '操作失败,请重试',
  203. 'error' => $exception->getMessage(),
  204. 'status_code' => 500
  205. ]);
  206. }
  207. }
  208. /**
  209. * 圈子用户列表
  210. */
  211. public function memberLists($request)
  212. {
  213. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  214. $where[] = ['is_black', 0];
  215. $where[] = ['circle_id', $request['circle_id']];
  216. $userModel = $this->interestCircleUser;
  217. return $userModel
  218. ->where($where)
  219. ->orderBy('created_at', 'desc')
  220. ->paginate($perPage);
  221. }
  222. }