CircleRepository.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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['circle_id'])
  62. ->first();
  63. if ($row) {
  64. return jsonError('您已经加入该圈子了');
  65. }
  66. $circle = $this->interestCircle->where('circle_id',$request['id'])->first();
  67. if($circle->join_limit){
  68. $checkRow = $this->checkQuestion($circle->join_question,$request['answer']);
  69. if(!$checkRow){
  70. return jsonError('学习学习,明天再来吧~');
  71. }
  72. }
  73. DB::beginTransaction();
  74. try {
  75. $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['circle_id']]);
  76. DB::commit();
  77. //加入圈子成功后,清除错误回答次数
  78. $key = 'circle_error_count_' . $userInfo['uid'];
  79. Redis::del($key);
  80. return jsonSuccess();
  81. } catch (QueryException $exception) {
  82. DB::rollBack();
  83. return jsonError('加入圈子失败,再试试吧');
  84. }
  85. }
  86. private function checkQuestion($question,$answer){
  87. $result = true;
  88. $rightAnswer = [];
  89. foreach($question as $key=>$value){
  90. //$answer['question_id'] = $key+1;
  91. foreach ($value['answer'] as $k=>$v){
  92. if($v['right']){
  93. $answer['answer_id'][] = $k+1;
  94. }
  95. }
  96. $rightAnswer[$key+1] = $answer;
  97. }
  98. var_dump($rightAnswer);
  99. foreach ($answer as $ak=>$av){
  100. $right = $rightAnswer[$av['question']];
  101. if(array_intersect($right,$av['answer'])){
  102. $result = false;
  103. break;
  104. }
  105. }
  106. return $result;
  107. }
  108. /**
  109. * 退出圈子
  110. * @param $request
  111. * @param $userInfo
  112. * @return array
  113. */
  114. public function exitCircle($request, $userInfo)
  115. {
  116. $info = $this->interestCircleUser
  117. ->where('uid', $userInfo['uid'])
  118. ->where('circle_id', $request['circle_id'])
  119. ->first();
  120. if (!$info) {
  121. return jsonError('您未加入该圈子');
  122. }
  123. DB::beginTransaction();
  124. try {
  125. $info->delete();
  126. DB::commit();
  127. //退出圈子成功后,清除错误回答次数
  128. $key = 'circle_error_count_' . $userInfo['uid'];
  129. Redis::del($key);
  130. return jsonSuccess();
  131. } catch (QueryException $exception) {
  132. DB::rollBack();
  133. return jsonError('退出圈子失败,再试试吧');
  134. }
  135. }
  136. /**
  137. * 相册列表
  138. */
  139. public function pictureLists($request)
  140. {
  141. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  142. $where = [];
  143. if (isset($request['circle_id'])) {
  144. $where[] = ['circle_id', $request['circle_id']];
  145. }
  146. if (isset($request['uid'])) {
  147. $where[] = ['uid', $request['uid']];
  148. }
  149. return $this->interestCirclePicture
  150. ->where($where)
  151. ->orderBy('created_at', 'desc')
  152. ->paginate($perPage);
  153. }
  154. /**
  155. * 删除相册图片
  156. */
  157. public function deletePicture($request)
  158. {
  159. $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
  160. if (!$circle) {
  161. return Response::create([
  162. 'message' => '获取图片信息失败',
  163. 'status_code' => 500
  164. ]);
  165. }
  166. DB::beginTransaction();
  167. try {
  168. $circle->delete();
  169. DB::commit();
  170. return Response::create();
  171. } catch (QueryException $exception) {
  172. DB::rollBack();
  173. Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
  174. return Response::create([
  175. 'message' => '操作失败,请重试',
  176. 'error' => $exception->getMessage(),
  177. 'status_code' => 500
  178. ]);
  179. }
  180. }
  181. /**
  182. * 圈子用户列表
  183. */
  184. public function memberLists($request)
  185. {
  186. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  187. $where[] = ['is_black', 0];
  188. $where[] = ['circle_id', $request['circle_id']];
  189. $userModel = $this->interestCircleUser;
  190. return $userModel
  191. ->where($where)
  192. ->orderBy('created_at', 'desc')
  193. ->paginate($perPage);
  194. }
  195. }