type; //类型 $id = $request->id; //梦想ID if (empty($id)) return $this->error(ErrorCode::KEY_ERROR); $support_dream = SupportDreamModel::where('dream_id',$id)->get(); $top = [] ; $topuser = []; //所有支持用户排行 $top3user = []; // 支持用户排行前三 foreach ($support_dream as $item) { if (!array_key_exists($item->user_id,$top)) { $top[$item->user_id] = $item->score; }else{ $top[$item->user_id] += $item->score; } } arsort($top); foreach ($top as $user_id => $score){ $user = UserInfoModel::find($user_id); $user->score = $score; $topuser[] = $user; if(count($top3user) <= 2) $top3user[] = $user ; } if ($type == 'paihang') return $this->api($topuser); $interactios = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get(); foreach ($interactios as $item) { $item->comments; foreach ($item->comments as $comment) { $comment->pic = UserInfoModel::find($comment->user_id)->pic; $comment->replay; foreach ($comment->replay as $k1 => $v1){ $v1->pic = UserInfoModel::find($v1->user_id)->pic; } } } if ($type == 'interaction') return $this->api(compact('interactios')); // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数 // $money = $user->money; $dream_info = DreamInfoModel::find($id); $setting = BaseSettingsModel::where('category','score')->select('key','value')->first()->moey; $a = count($setting) > 0 ? $setting->key : ''; $created_at = $dream_info ? $dream_info->created_at : 0; $b = intval((time()-strtotime($created_at))/3600) ; $c = count($setting) > 0 ? $setting->value : ''; //Todo 支持乘数目公式 $number = -$a * $b + $c; if ($number <= 1) $number = 1; $dream = DreamInfoModel::with(['imgs','user'])->find($id); return $this->api(compact('dream','top3user','number')); } /** * @api {post} /api/dream/support 支持梦想 * @apiDescription 支持梦想 * @apiGroup Dream * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} coin 支持梦想币数量 * @apiParam {int} id 梦想ID * @apiParam {int} number 支持乘数 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK *{ * "status": true, * "status_code": 0, * "message": "", * "data": "" *} * @apiErrorExample {json} Error-Response: *HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * 可能出现的代码 * { * "status": false, * "status_code": 1303, * "message": "商户余额不足", * "data": null * } * */ public function support(Request $request) { $validator = \Validator::make($request->all(), [ 'coin' => 'required', 'id' => 'required', ], [ 'coin.required' => '梦想币不能为空', 'id.required' => '支持梦想id不能为空', ] ); if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS); $user = $this->getUser(); $dream_id = $request->id; $dream_info = DreamInfoModel::find($dream_id); $user_id = $dream_info->user_id; $coin = $request->coin; $setting = BaseSettingsModel::where('category','score')->select('key','value')->first(); $a = count($setting) > 0 ? $setting->key : ''; $b = intval((time()-strtotime($dream_info->created_at))/3600) ; $c = count($setting) > 0 ? $setting->value : ''; //Todo 支持乘数目公式 $number = -$a * $b + $c; if ($number <= 1) $number = 1; if ($user->coin < $coin) { return $this->error(ErrorCode::COIN_NOT_ENOUGH); }else{ $user->coin -= $coin; $user->save(); $dream_info->get_coin += $coin; $dream_info->save(); $data = [ 'user_id'=>$user->id, 'dream_id'=>$dream_id, 'to_user_id'=>$user_id, 'coin'=>$coin, 'score'=>$coin*$number, ]; $ok = SupportDreamModel::create($data); if (!$ok) { return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID); } // 记录充值记录 新增系统消息 $data = [ 'from_id' =>$user->id, 'to_id' => $user_id, 'from_amount' => $coin, 'to_amount' => $coin, 'from_type' => AccountLog::TYPE_COIN, 'to_type' => AccountLog::TYPE_COIN, 'op' => AccountLog::OP_SUPPORT, ]; AccountLog::create($data); $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币'; $info = [ 'user_id' => $user_id, 'message' => $message, ]; SystemInfoModel::create($info); return $this->api($number); } } // 收藏关注梦想 /** * @api {get} /api/dream/collection 收藏梦想 * @apiDescription 收藏梦想 * @apiGroup Dream * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} id 梦想ID * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK *{ * "status": true, * "status_code": 0, * "message": "", * "data": "" *} * @apiErrorExample {json} Error-Response: *HTTP/1.1 400 Bad Request * { * "state": false, * "code": 1000, * "message": "传入参数不正确", * "data": null or [] * } * */ public function collection(Request $request) { $validator = \Validator::make($request->all(), [ 'id' => 'required', ], [ 'id.required' => '收藏梦想id不能为空', ] ); if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS); $user = $this->getUser(); $data = [ 'user_id' =>$user->id, 'dream_id' =>$request->id, 'dream_user_id' =>DreamInfoModel::find($request->id)->user_id, ]; $user_care_dream = UserCareDream::where('user_id',$user->id)-> where('dream_id',$request->id)->first(); if (!$user_care_dream) { UserCareDream::create($data); } return $this->api(''); } /** * @api {get} /api/dream/search 梦想搜索 * @apiDescription 梦想搜索) * @apiGroup Dream * @apiPermission none * @apiVersion 0.1.0 * @apiParam {string} keyword 关键字 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK *{ * "status": true, * "status_code": 0, * "message": "", * "data": { * "data": [ * { * "dream": "haha", 梦想名 * "user_pic": [ * { * "pic": "", 用户头像 * } * ], * "dream_img": "", 梦想图片 * } * ] * } *} * @apiErrorExample {json} Error-Response: * HTTP/1.1 400 Bad Request */ public function search(Request $request) { $user = $this->getUser(); if (empty($request->keyword)) { return $this->api(''); } $keyword ='%'.$request->keyword.'%'; $data = DreamInfoModel::where('dream','like',$keyword)-> orWhere('sign','like',$keyword)->get(); foreach ($data as $k => $value) { $value->user_pic = $value->dreamFindUser; $value->dream_img = $value->dreamImgsFirst->pic; } $this->insertSearchTable($user->id,$request->keyword); return $this->api(compact('data')); } public function insertSearchTable($id,$keyword) { $info = SearchInfoModel::where('user_id',$id)-> where('search',trim($keyword))->first(); if (count($info) == 0) { SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]); }else{ $info->times += 1; $info->save(); } } /** * @api {post} /api/dream/store 发布梦想 * @apiDescription 发布梦想 * @apiParam {string} pics 梦想图片 数组 * @apiParam {string} video 梦想视频 * @apiParam {string} name 梦想标题 * @apiParam {string} about 梦想介绍 * @apiParam {int} coin 梦想币 * @apiParam {int} time 实现时间默认21*3600 * @apiGroup Dream * @apiPermission Passport * @apiVersion 0.1.0 * @apiSuccessExample {json} Success-Response: * { * "status": true, * "status_code": 0, * "message": "", * "data": "" *} * HTTP/1.1 200 OK * @apiErrorExample {json} Error-Response: * { * "status": false, * "status_code": 1203, * "message": "附件不存在", * "data": null * } * HTTP/1.1 400 Bad Request */ public function store(Request $request) { $user = $this->getUser(); $validator = \Validator::make($request->all(), [ 'name' => 'required', 'about' => 'required', 'coin' => 'required|integer', 'time' => 'required|integer', ], [ 'name.required' => '梦想标题必填', 'about.required' => '梦想介绍必填', 'coin.required' => '梦想币必填', 'coin.integer' => '梦想币必须是正整数', 'time.required' => '实现时间必填', 'time.integer' => '实现时间必须是正整数', ] ); if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS); \Log::debug($user); $data2 = $user->myCareNum; $care_num = count($data2); $setting = BaseSettingsModel::where('category','paihang')->first(); $a = $setting?$setting->key:1; $b = $setting?$setting->value:1; $t = 21*3600 / 60; $data = $request->except('_token','pics'); $data['user_id'] = $user->id; \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t); if ($care_num == 0) { $data['score'] = (($a/$t) + $b)*100000000 ; }else{ $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ; } $data['created_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s'); $dream_id = DreamInfoModel::insertGetId($data); if ($dream_id) { // 梦想创建成功 关联关系中最新动态+1 /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20); foreach ($info as $item) { $item->dream_num += 1; $item->updated_at = date('Y-m-d H:i:s'); $item->save(); }*/ $pics = $request->pics; $video = $request->video; if (!is_array($pics) && empty($pics) && empty($video)) { DreamInfoModel::destroy($dream_id); return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST); } $data = []; if (is_array($pics)) { foreach ($pics as $v){ $data[] = [ 'dream_id'=>$dream_id, 'pic' => $v, 'created_at' =>date('Y-m-d H:i:s'), 'updated_at' =>date('Y-m-d H:i:s'), ]; } DreamImages::insert($data); } return $this->api(''); } } }