getUser(); if (!empty($request->input('dream_user_id'))) { // 查看梦想后 最新最新梦想为0 // UserCareUser::where('user_id',$user->id)->where('other_user_id',$request->input('dream_user_id'))->update(['dream_id'=>0,'dream_number'=>0]); UserCareDream::where('user_id',$user->id)->where('dream_user_id',$request->input('dream_user_id'))->update(['interaction_number'=>0]); } $type = $request->type; //类型 $id = $request->id; //梦想ID $user_care_dream = UserCareDream::where('user_id',$user->id)->where('dream_id',$id)->first(); $is_collection = empty($user_care_dream) ? 0 :1 ; $dream = DreamInfoModel::find($id); if (empty($id)) return $this->error(ErrorCode::KEY_ERROR); if (empty($dream)) return $this->error(ErrorCode::DREAM_NOT_EXIST); $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){ $user1 = UserInfoModel::find($user_id); if (!empty($user1)) { $user1->score = $score; $topuser[] = $user1; if(count($top3user) <= 2) $top3user[] = $user1 ; } } if ($type == 'paihang') return $this->api($topuser); $interactions = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get(); foreach ($interactions as $interaction) { // 被屏蔽的人 $arr = array_filter(explode(',',$interaction->black_list)); if(!empty($arr)){ $interaction->comments = CommentInfoModel::where('interaction_id',$interaction->id)->where(function ($query) use ($arr) { $query->whereNotIn('user_id',$arr); })->orderBy('created_at')->get()->toArray(); }else{ $interaction->comments = CommentInfoModel::where('interaction_id',$interaction->id)->orderBy('created_at')->get()->toArray(); } } /*dd($interactions); foreach ($interactions 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($interactions); // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数 // $money = $user->money; $dream_info = DreamInfoModel::find($id); // $setting = BaseSettingsModel::where('category','score')->select('key','value')->first(); // $a = count($setting) > 0 ? $setting->key : 0; // $created_at = $dream_info ? $dream_info->created_at : 0; // $b = intval((time()-strtotime($created_at))/60) ; // $c = count($setting) > 0 ? $setting->value : 0; // $number =round( -$a * $b + $c,1); $setting = BaseSettingsModel::where('category','score')->select('key','value','sort')->first(); $a = count($setting) > 0 ? $setting->key : 0; $b = ceil((time()-strtotime($dream_info->created_at))/60); //梦想发布分钟数 $c = count($setting) > 0 ? $setting->value : 3; if(empty($c)) $c = 3; $n = count($setting) > 0 ? $setting->sort : 0; //Todo 支持乘数目公式 pow(x,y) // exp(x) $number = N*exp(-a*(pow(t,1/b)))+1 180 $number =round( $n*exp(-$a*(pow($b,1/$c)))+1,2); $test =round( $n*exp(-$a*(pow(180,1/$c)))+1,2); Log::info("发布的时间(分钟):$b"); Log::info("支持乘数:$number"); Log::info("支持乘数测试(180分钟):$test"); Log::info("--------"); if ($number <= 1) $number = 1; $dream = DreamInfoModel::with(['imgs','user'])->find($id); if (empty($dream)) return $this->error(ErrorCode::DREAM_NOT_EXIST); $dream->signs = explode(',',$dream->sign); $dream->user_coin = $user->coin; $dream->topuser = $topuser; $dream->top3user = $top3user; $dream->interactions = $interactions; $dream->number = $number; $dream->is_collection = $is_collection; $dream->a = $a; $dream->c = $c; // 如果当前梦想用户就是当前登录用户就不显示关注按钮 $show_button = $dream->user_id == $user->id ? '0' : '1'; $dream->show_button = $show_button; // 梦想互动 $interaction_id = $request->input('interaction_id'); $interactions = InteractionInfo::where('dream_id',$id)->orderBy('created_at','desc')->get(); foreach ($interactions as $interaction) { // 被屏蔽的人 $arr = array_filter(explode(',',$interaction->black_list)); if(!empty($arr)){ $interaction->comments = CommentInfoModel::where('interaction_id',$interaction->id)->where(function ($query) use ($arr) { $query->whereNotIn('user_id',$arr); })->orderBy('created_at')->get()->toArray(); }else{ $interaction->comments = CommentInfoModel::where('interaction_id',$interaction->id)->orderBy('created_at')->get()->toArray(); } } if (!empty($interaction_id)) { $data = InteractionInfo::with(['comments' => function ($query) { $query->orderBy('created_at'); }])->find($interaction_id); if (count($data) > 0) { $interactions = $interactions->toArray(); foreach ($interactions as $key => $value) { if ($value['id']==$interaction_id) { unset($interactions[$key]); } } array_unshift($interactions,$data); } } $dream->interactions = $interactions; return $this->api($dream); } /** * @api {post} /api/dream/support 支持梦想 * @apiDescription 支持梦想 * @apiGroup Dream * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} coin 支持梦想币数量 * @apiParam {int} id 梦想ID * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK { "status": true, "status_code": 0, "message": "", "data": { "id": 12, "user_id": 2, "name": "用户2梦想标题166", "about": "用户2梦想介绍666", "coin": 2500, "time": 21, "get_coin": 704, "status": 0, "video": null, "score": 158730, "mark": 158730, "sign": "", "created_at": "2017-06-25 13:10:56", "updated_at": "2017-06-27 18:16:02", "top3user": [ 支持者前三 { "id": 1, "phone": "13880642880", "nickname": "name1", "avatar": "https://timgsa.bumb.224_0.jpeg", "birthday": "2000-06-21", "sign": 0, "money": 0, "coin": 596, "sex": 1, "signture": "我的个性签名1", "height": 170, "work": "网红", "emotion": 1, "address": "", "city": "上海", "detail_address": "", "status": 1, "wechat": "", "weibo": "", "remember_token": "", "created_at": "2017-06-25 10:27:08", "updated_at": "2017-06-27 18:16:02", "deleted_at": null, "score": 5062 } ] "number": 4.8 支持乘数 "is_collection" : 1 1已收藏 0未收藏 } } * @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 * } * { * "status": false, * "status_code": 1410, * "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 = DreamInfoModel::find($dream_id); $user_id = $dream->user_id; $coin = $request->coin; /* if ($dream->get_coin >= $dream->coin) {$number return $this->error(ErrorCode::SUP_TOP); } elseif($coin + $dream->get_coin > $dream->coin){ $coin = $dream->coin - $dream->get_coin; }*/ $user_care_dream = UserCareDream::where('user_id', $user->id)->where('dream_id', $dream_id)->first(); $is_collection = empty($user_care_dream) ? 0 :1 ; $setting = BaseSettingsModel::where('category','score')->select('key','value','sort')->first(); $a = count($setting) > 0 ? $setting->key : 0; $b = ceil((time()-strtotime($dream->created_at))/60); //梦想发布分钟数 $c = count($setting) > 0 ? $setting->value : 0; if(empty($c)) $c = 3; $n = count($setting) > 0 ? $setting->sort : 0; //Todo 支持乘数目公式 pow(x,y) // exp(x) $number = N*exp(-a*(pow(t,1/b)))+1 $number =round( $n*exp(-$a*(pow($b,1/$c)))+1,2); if ($number <= 1) $number = 1; if ($user->coin < $coin) { return $this->error(ErrorCode::COIN_NOT_ENOUGH); }else{ //支持成功之前 $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get(); $top = [] ; foreach ($support_dream as $item) { if (!array_key_exists($item->user_id,$top)) { $top[$item->user_id] = $item->coin; }else{ $top[$item->user_id] += $item->coin; } } arsort($top); $new_arr = array_values($top); if (!empty($new_arr)) { $old_top_user_id = array_search($new_arr[0],$top); }else{ $old_top_user_id = 0; } /* dd($user->id); dd($old_top_user_id);*/ $user->coin -= $coin; $user->save(); $dream->get_coin += $coin; $dream->mark += $coin*$number; $dream->save(); $to_user = $dream->user; $user_coin = $user->coin; // 支持成功记录交易记录 if (!empty($to_user)) { $arr = [ 'from_type'=>'梦想币', 'from_id'=>$user->id, 'from_name'=>$user->nickname, 'op'=>'支持-'.$dream->name, 'from_amount'=>$coin, 'to_type'=>'梦想币', // 'to_id'=>$dream->user_id, 'to_id'=>'', 'to_name'=>$to_user->nickname, 'to_amount'=>$coin, 'channel'=>'平台内', 'transaction_id'=>date('YmdHis') . mt_rand(1000, 9999), 'avatar'=>empty($dream->img) ? '':$dream->img->pic, ]; AccountLog::create($arr); } // 梦想实现创建二维码 if ($dream->coin >= $dream->get_coin && empty($dream->code)) { // 生成二维码 $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999); $info['code'] = 'WECHATPAY_' . $info['transaction_id']; $code_url = env('APP_URL').'/api/user/meet?dream_id='.$dream_id; $code_path = public_path('qrcodes/'.$info['code'].'.png'); \QrCode::format('png')->size(500)->generate($code_url,$code_path); $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png'; DreamInfoModel::where('id',$dream_id)->update(compact('code')); $message = '你的梦想《'.$dream->name.'》已结束,点击了解接下来的步骤 '; $info = [ 'user_id' => $dream->user_id, 'message' => $message, ]; SystemInfoModel::create($info); // 长连接 $this->jPush($message,'',$dream->user_id); } $data = [ 'user_id'=>$user->id, 'dream_id'=>$dream_id, 'to_user_id'=>$user_id, 'coin'=>$coin, 'score'=>$coin*$number, ]; $ok = SupportDreamModel::create($data); // 在支持梦想时 如果梦主排名配取代时发送系统消息 $this->systemInfo($dream_id,$user->id,$old_top_user_id); if (!$ok) { return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID); } // ""."[UserInfoModel::find($user->id)->nickname] 为你《" $nickname = UserInfoModel::find($user->id)->nickname; $message =""."[$nickname] 为你《".$dream->name.'》支持了'.$coin.'梦想币'; $info = [ 'to_user_id' => $user_id, 'user_id'=>$user->id, 'message' => $message, 'is_reply' => 1, 'type_id' => 1, 'attr_id' => 1, ]; SystemInfoModel::create($info); // 长连接 $mess ="{$nickname}为你《".$dream->name.'》支持了'.$coin.'梦想币'; $this->jPush($mess,'',$user_id); // 支持成功返回数据到当前页面 top3user $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get(); $top = [] ; $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); if (!empty($user)) { $user->score = $score; if(count($top3user) <= 2) $top3user[] = $user ; } } // 梦想分 mark $dream->top3user = $top3user; $dream->is_collection = $is_collection; return $this->api(compact('dream','user_coin','number')); } } public function systemInfo($dream_id,$user_id,$old_top_user_id) { $user = UserInfoModel::find($user_id); $dream = DreamInfoModel::find($dream_id); $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get(); $top = [] ; foreach ($support_dream as $item) { if (!array_key_exists($item->user_id,$top)) { $top[$item->user_id] = $item->coin; }else{ $top[$item->user_id] += $item->coin; } } arsort($top); $new_arr = array_values($top); if (!empty($new_arr)) { $top_user_id = array_search($new_arr[0],$top); }else{ $top_user_id = $old_top_user_id; } if ($top_user_id != $old_top_user_id) { //新的最大支持者不是以前的最大支持者时 $arr = [ 'user_id'=>0, 'message'=>"你已不是《".$dream->name."》的《梦主》啦!"."[$user->nickname] "."以$new_arr[0]分刚取代你的位置", 'to_user_id'=>$old_top_user_id, 'dream_id'=>$dream_id, 'is_url'=>1, 'type_id'=>2, 'attr_id'=>10, ]; SystemInfoModel::create($arr); $mess = "你已不是《".$dream->name."》的《梦主》啦!"."[$user->nickname] "."以$new_arr[0]分刚取代你的位置"; $this->jPush($mess,'',$old_top_user_id); } } // 收藏关注梦想 /** * @api {get} /api/dream/collection 收藏梦想 * @apiDescription 收藏梦想 * @apiGroup Dream * @apiPermission Passport * @apiVersion 0.1.0 * @apiParam {int} id 梦想ID * @apiParam {int} is_collection 0取消关注 1关注 * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK *{ * "status": true, * "status_code": 0, * "message": "", * "data": { * "is_collection": 1 1已关注 0 未关注 * } *} * @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', 'is_collection' => 'required', ], [ 'id.required' => '收藏梦想id不能为空', 'is_collection.required' => '关注值不存在', ] ); if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS); $user = $this->getUser(); if(!$dream = DreamInfoModel::find($request->id)){ $this->error(ErrorCode::CLIENT_WRONG_PARAMS); } $data = [ 'user_id' =>$user->id, 'dream_id' =>$request->id, 'dream_user_id' =>$dream->user_id, ]; $is_collection = $request->is_collection; $user_care_dream = UserCareDream::where('user_id',$user->id)-> where('dream_id',$request->id)->first(); if ($is_collection) { if (empty($user_care_dream)) { UserCareDream::create($data); } // 关注成功发送私信 $message = BaseSettingsModel::where('category','message')->first(); $message = empty($message) ? ""."[$user->nickname] 收藏了你的梦想《".$dream->name.'》' : $message->value; $this->jPush($message,'',$dream->user_id); $info2 = [ 'to_user_id' => $dream->user_id, 'message' => $message, 'dream_id' => $request->id, 'user_id' => $user->id, 'is_reply' => 1, 'type_id' => 1, 'attr_id' => 2, ]; SystemInfoModel::firstOrCreate($info2); // 长连接 $mess = $user->nickname."收藏了你的梦想《".$dream->name.">"; $this->jPush($mess,'',$dream->user_id); }else{ UserCareDream::where('user_id',$user->id)-> where('dream_id',$request->id)->delete(); // 取消收藏删除系统消息 SystemInfoModel::where('to_user_id',$dream->user_id)->where('dream_id',$request->id)->delete(); } // 当有人收藏/取消收藏你的梦想时 梦想分发生变化 $setting = BaseSettingsModel::where('category','paihang')->first(); $a = $setting?$setting->key:1; $b = $dream->parameter; $x = $setting?$setting->sort:10; if($x==0) $x=10; $t = 21*24*60; //梦想发布分钟数 $care_num = UserCareDream::where('dream_id',$dream->id)->get()->count(); if ($care_num==0) $care_num=1; $score = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ; $dream->score = $score; $dream->save(); // 并且同时会关注这个梦想者 $array = [ 'user_id' =>$user->id, 'other_user_id' =>$dream->user_id, ]; $user_care_user = UserCareUser::where('user_id',$user->id)-> where('other_user_id',$dream->user_id)->first(); if (empty($user_care_user)) { UserCareUser::create($array); } return $this->api($is_collection); } /** * @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": [ { "id": 5, "user_id": 1, "name": "梦想标题1", "about": "梦想介绍", "coin": 2500, "time": 72000, "get_coin": 0, "mark": 0, "status": 0, "video": "url", "score": 100079365, "sign": "梦想达人", "created_at": "2017-06-25 12:45:22", "updated_at": "2017-06-28 15:50:41", "user": {}, "imgs": [] }, ] } * @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('name','like',$keyword)-> orWhere('sign','like',$keyword)->with(['user','img'])->get(); foreach ($data as $k => $dream) { $info = UserCareDream::where('dream_id',$dream->id)->get(); $dream->care_num = count($info); $dream->img = $dream->img?$dream->img->pic:''; } $this->insertSearchTable($user->id,$request->keyword); return $this->api($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', 'days' => 'required|integer', 'pics' => 'required|array', ], [ 'name.required' => '梦想标题必填', 'about.required' => '梦想介绍必填', 'coin.required' => '梦想币必填', 'coin.integer' => '梦想币必须是正整数', 'days.required' => '实现时间必填', 'days.integer' => '实现时间必须是正整数', 'pics.required' => '至少上传一张图片', 'pics.array' => '至少上传一张图片数组', ] ); if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS); \Log::debug($user); $care_num = 1; //梦想收藏人数 新建梦想时默认为1 $setting = BaseSettingsModel::where('category','paihang')->first(); $a = $setting?$setting->key:1; $b = $setting?$setting->value:0; $x = $setting?$setting->sort:10; $t = 1; //创建梦想时梦想发布时间最小为一分钟 $days = $request->days; $data = $request->except('_token','pics','days'); $data['user_id'] = $user->id; $data['end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' days')); \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t); if ($care_num == 0) { $data['score'] = (($a/$t) + $b)*100000000000000 ; }else{ $data['score'] = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ; } $data['created_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s'); $data['status'] = 1; //默认审核通过 $video = $request->video; if (!empty($video)) $data['video'] =env('APP_URL').'/attachment/'. $request->video; $dream_id = DreamInfoModel::insertGetId($data); if ($dream_id) { // 梦想创建成功 关联关系中最新动态+1 // UserCareUser::where('other_user_id',$user->id)->increment('dream_number',1); $pics = $request->pics; 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); } // 梦想创建成功后所有关注我的梦想的人也会收到我最新梦想发布的消息 $ids = UserCareDream::where('dream_user_id',$user->id)->select('user_id')->get()->toArray(); $id_arrs = array_column($ids,'user_id'); if (!empty($id_arrs)) { $arr = [] ; foreach ($id_arrs as $id){ $arr[] = [ 'to_user_id' => $id, 'user_id' => $user->id, 'message' => '关注的梦想有新的梦想动态'.$dream_id, ]; // 长连接 $this->jPush('关注的梦想有新的梦想动态','',$id); } SystemInfoModel::insert($arr); } return $this->api(''); } } /** * @api {get} /api/dream/share 分享 * @apiDescription 分享 * @apiParam {int} id 梦想id * @apiGroup Dream * @apiPermission Passport * @apiVersion 0.1.0 */ // 分享梦想 public function share(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); $url = env('APP_URL').'/api/dream/show?id='.$request->id; $dream = DreamInfoModel::with('img')->find($request->id); $img = !empty($dream->img) ? $dream->img->pic : ''; $html = "

$dream->name

"; return $html; } }