123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Circle;
- use App\Models\InterestCircle;
- use App\Models\InterestCircleUser;
- use Illuminate\Database\QueryException;
- use Dingo\Api\Http\Response;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class CircleRepository
- {
- public function __construct(InterestCircle $interestCircle,
- InterestCircleUser $interestCircleUser)
- {
- $this->interestCircle = $interestCircle;
- $this->interestCircleUser = $interestCircleUser;
- }
- /**
- * 圈子列表
- */
- public function circleLists($request, $limit = 3)
- {
- $where[] = ['is_open', 1];
- if (isset($request['is_recommend'])) {
- $where[] = ['is_recommend', 1];
- }
- return $this->interestCircle
- ->where($where)
- ->orderBy('id', 'desc')
- ->take($limit)
- ->get();
- }
- /**
- * 圈子详情
- */
- public function detail($request, $isTrashed = false)
- {
- $model = $this->interestCircle;
- if ($isTrashed) {
- $model->withTrashed();
- }
- return $model->find($request['id']);
- }
- /**
- * 加入圈子
- * @param $request
- * @param $userInfo
- * @return array
- */
- public function joinCircle($request, $userInfo)
- {
- $row = $this->interestCircleUser
- ->where('uid', $userInfo['uid'])
- ->where('circle_id', $request['circle_id'])
- ->first();
- if ($row) {
- return jsonError('您已经加入该圈子了');
- }
- $circle = $this->interestCircle->where('circle_id',$request['id'])->first();
- if($circle->join_limit){
- $checkRow = $this->checkQuestion($circle->join_question,$request['answer']);
- if(!$checkRow){
- return jsonError('学习学习,明天再来吧~');
- }
- }
- DB::beginTransaction();
- try {
- $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['circle_id']]);
- DB::commit();
- //加入圈子成功后,清除错误回答次数
- $key = 'circle_error_count_' . $userInfo['uid'];
- Redis::del($key);
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- return jsonError('加入圈子失败,再试试吧');
- }
- }
- private function checkQuestion($question,$answer){
- $result = true;
- $rightAnswer = [];
- foreach($question as $key=>$value){
- //$answer['question_id'] = $key+1;
- foreach ($value['answer'] as $k=>$v){
- if($v['right']){
- $answer['answer_id'][] = $k+1;
- }
- }
- $rightAnswer[$key+1] = $answer;
- }
- var_dump($rightAnswer);
- foreach ($answer as $ak=>$av){
- $right = $rightAnswer[$av['question']];
- if(array_intersect($right,$av['answer'])){
- $result = false;
- break;
- }
- }
- return $result;
- }
- /**
- * 退出圈子
- * @param $request
- * @param $userInfo
- * @return array
- */
- public function exitCircle($request, $userInfo)
- {
- $info = $this->interestCircleUser
- ->where('uid', $userInfo['uid'])
- ->where('circle_id', $request['circle_id'])
- ->first();
- if (!$info) {
- return jsonError('您未加入该圈子');
- }
- DB::beginTransaction();
- try {
- $info->delete();
- DB::commit();
- //退出圈子成功后,清除错误回答次数
- $key = 'circle_error_count_' . $userInfo['uid'];
- Redis::del($key);
- return jsonSuccess();
- } catch (QueryException $exception) {
- DB::rollBack();
- return jsonError('退出圈子失败,再试试吧');
- }
- }
- /**
- * 相册列表
- */
- public function pictureLists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['circle_id'])) {
- $where[] = ['circle_id', $request['circle_id']];
- }
- if (isset($request['uid'])) {
- $where[] = ['uid', $request['uid']];
- }
- return $this->interestCirclePicture
- ->where($where)
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- /**
- * 删除相册图片
- */
- public function deletePicture($request)
- {
- $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
- if (!$circle) {
- return Response::create([
- 'message' => '获取图片信息失败',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $circle->delete();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 圈子用户列表
- */
- public function memberLists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where[] = ['is_black', 0];
- $where[] = ['circle_id', $request['circle_id']];
- $userModel = $this->interestCircleUser;
- return $userModel
- ->where($where)
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- }
|