123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\BaseSettingsModel;
- use App\Models\CommentInfoModel;
- use App\Models\DreamInfoModel;
- use App\Models\SearchInfoModel;
- use App\Models\SupportDreamModel;
- use App\Models\SystemInfoModel;
- use App\Models\UserCareDream;
- use App\Models\UserCareUser;
- use App\Models\UserInfoModel;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class IndexController extends Controller
- {
- /**
- * @api {get} /api/index/home 首页
- * @apiDescription 首页
- * @apiGroup Index
- * @apiParam {string} [type=hot] (热门)hot/(潮流)trend/(最新)news
- * @apiParam {int} [page=1] 页码(分页参数)
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "banners": [
- * {
- * "id": 32,
- * "key": "2",
- * "value": "http://w17.9026.com/img/banner/banner_2.png",
- * "sort": 2,
- * "category": "banner",
- * "pid": 0,
- * "status": 1,
- * "created_at": null,
- * "updated_at": "2017-06-26 09:21:36",
- * "deleted_at": null
- * }
- * ],
- * "users": [ 关注梦想有新的动态显示用户
- * {
- * "id": 2,
- * "dream_id": 2, 最近动态的梦想id
- * "phone": "13880642881",
- * "nickname": "name2",
- * "avatar": "http://www.miao.com/upload/user/20170630/08e235d7211944e9b6482965b4d7c42e.jpg",
- * },
- * ],
- * "dreams": {
- * "current_page": 1,
- * "data": [
- * {
- * "id": 5,
- * "user_id": 1,
- * "name": "梦想标题1",
- * "about": "梦想介绍",
- * "coin": 2500,
- * "end_time": "2017-05-11 17:00:00",
- * "get_coin": 0,
- * "mark": 0,
- * "status": 2,
- * "video": "url",
- * "score": 100079365,
- * "sign": "梦想达人",
- * "signs": "[]", //梦想标签
- * "code": "",
- * "parameter": 100,
- * "created_at": "2017-06-25 12:45:22",
- * "updated_at": "2017-07-03 12:23:37",
- * "care_num": 1,
- * "img": {
- * "title": "",
- * "pic": "http://www.miao.com/upload/dream/20170629/a18cadd1560f21453a08fdd81fb8fb2d.jpg"
- * },
- * "user": {
- * "id": 1,
- * "avatar": "/upload/user/20170629/e77068cc14c82086fb6b16e5bb7c3dd1.jpg",
- * }
- * },
- * }
- * ],
- * "from": 1,
- * "last_page": 1,
- * "next_page_url": null,
- * "path": "http://www.miao.com/api/index/home",
- * "per_page": 20,
- * "prev_page_url": null,
- * "to": 11,
- * "total": 11
- * }
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- */
- public function home(Request $request)
- {
- $login_user = $this->getUser();
- $id = $login_user->id;
- $arr1 =DreamInfoModel::orderBy('score','desc')->limit(30)->select('id')->get()->toArray();
- $id_arr1 = array_column($arr1,'id');
- $arr2 =DreamInfoModel::orderBy('score','desc')->offset(15)->limit(120)->select('id')->get()->toArray();
- $id_arr2 = array_column($arr2,'id');
- // 首页显示用户条件 关注的梦想有新的动态 2动态下有新的评论
- $users_care_dream = UserCareDream::where('user_id',$id)->where('interaction_number','>',0)->get()->toArray();
- $user_ids = array_unique(array_column($users_care_dream,'dream_user_id','dream_id'));
- Log::info($user_ids);
- $users = [] ;
- if (!empty($user_ids)) {
- foreach ($user_ids as $dream_id => $user_id) {
- $user = UserInfoModel::find($user_id);
- if($user){
- $user->dream_id = $dream_id;
- $users[] = $user;
- }
- }
- }
- $type = $request->type;
- if ($type == 'trend') {
- // 潮流 发布三天之内的梦想
- $banners = $this->getBanner();
- $dreams = DreamInfoModel::where('status',1)->where('end_time','>',date('Y-m-d H:i:s'))->where('created_at','<',date('Y-m-d H:i:s'))->where('created_at','>',date('Y-m-d H:i:s',time()-3*24*3600))->orderBy('score','desc')->with('user')->paginate(20);
- $this->dreams($dreams);
- } elseif ($type == 'news') {
- // 最新 发布不到一天前的梦想 ->whereNotIn('id', $id_arr2)
- $banners = $this->getBanner();
- $dreams = DreamInfoModel::where('status',1)->where('end_time','>',date('Y-m-d H:i:s'))->where('created_at','<',date('Y-m-d H:i:s'))->where('created_at','>',date('Y-m-d H:i:s',time()-1*24*3600))->orderBy('created_at','desc')->with('user')->paginate(20);
- $this->dreams($dreams);
- } else{
- // 热门
- $banners = $this->getBanner();
- $dreams = DreamInfoModel::where('status',1)->where('end_time','>',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->paginate(15);
- $this->dreams($dreams);
- }
- foreach ($dreams as $dream) {
- $dream->signs = explode(',',$dream->sign);
- }
- return $this->api(compact('banners','users','dreams'));
- }
- /**
- * @api {get} /api/index/search 搜索
- * @apiDescription 搜索
- * @apiGroup Index
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} [keyword] 关键字可选 (ha/name)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * get
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "hot_searches": {
- * "name": 1,
- * "梦想": 1,
- * "ha": 1
- * },
- * "history_searches": [
- * {
- * "id": 3,
- * "user_id": 1,
- * "search": "ha",
- * "times": 1,
- * "created_at": "2017-06-25 16:21:47",
- * "updated_at": "2017-06-25 16:21:47"
- * },
- * ]
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function search(Request $request)
- {
- $user = $this->getUser();
- $keyword ='%'.$request->keyword.'%';
- $user_infos = UserInfoModel::where('nickname','like',$keyword)->get();
- $dream_infos = DreamInfoModel::where(function ($query) use($keyword) {
- // ->where('end_time','>=',time())
- $query->where('name','like',$keyword);
- })
- ->orWhere(function ($query) use($keyword){
- $query->where('sign','like',$keyword);
- })->with(['user','img'])->get();
- $this->dreams($dream_infos);
- $signs = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
- if (empty($request->keyword)) {
- // 历史搜索
- $history_searches = $user->search()->orderBy('id','desc')->limit(10)->get();
- // 热门搜索
- $data2 = SearchInfoModel::get();
- $hot_searches = [];
- foreach ($data2 as $k => $v) {
- if (count($hot_searches) == 6) {
- break;
- }
- if (!array_key_exists($v->search,$hot_searches)) {
- $hot_searches[$v->search] = $v->times;
- }else{
- $hot_searches[$v->search] += $v->times;
- }
- }
- arsort($hot_searches);
- return $this->api(compact('hot_searches','history_searches'));
- }
- // 写入搜索记录
- $this->insertSearchTable($user->id,$request->keyword);
- return $this->api(compact('user_infos','dream_infos','signs'));
- }
- /**
- * @api {get} /api/index/user_search 用户搜索
- * @apiDescription 用户搜索
- * @apiGroup Index
- * @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":[
- * {
- * "nickname": "ha", 昵称
- * "pic": "", 头像
- * ...
- * },
- * ]
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function userSearch(Request $request)
- {
- $user = $this->getUser();
- if (empty($request->keyword)) {
- return $this->api('');
- }
- $keyword ='%'.$request->keyword.'%';
- $users = UserInfoModel::where('nickname','like',$keyword)->get();
- $this->insertSearchTable($user->id,$request->keyword);
- return $this->api($users);
- }
- //获取轮播图
- public function getBanner()
- {
- $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
- ->orderBy('sort')->get()->toArray();
- return $banner;
- }
- // 完善梦想信息
- public function dreams($dreams)
- {
- foreach ($dreams as $k => $dream) {
- $data = UserCareDream::where('dream_id',$dream->id)->get();
- $t = ceil((time()-strtotime($dream->created_at))/60); //梦想发布分钟数
- $care_num = count($data) + $dream->add_care_number;
- if (count($data)==0) $care_num=1;
- $setting = BaseSettingsModel::where('category','paihang')->first();
- $a = $setting?$setting->key:1;
- $x = $setting?$setting->sort:10;
- if($x==0) $x = 10;
- $dream->score = (log($care_num,$x) + ($a/$t) + $dream->parameter)*100000000000000 ;
- $dream->save();
- $dream->care_num = count($data);
- $dream->img = $dream->img?$dream->img->pic:'';
- }
- }
- // 查看关注用户的最新动态
- // public function newsInfo(Request $request)
- // {
- //// 查看后user_care_user dream_info 更新为零 首页不再显示
- // $user = $this->getUser();
- // $other_id = $request->id;
- // if (empty($other_id)) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
- // UserCareDream::where('user_id',$user->id)->where('dream_user_id',$other_id)->update(['interaction_number'=>0]);
- // $data = UserInfoModel::where('id',$other_id)->with(['allInteraction'=>function ($query){
- // $query->orderBy('id','desc');
- // }])->first();
- //// $data = UserInfoModel::find($other_id)->allInteraction;
- //// dd($data) ;
- //// dd($data->allInteraction);
- // foreach ($data->allInteraction as $item) {
- // $item->time = DreamInfoModel::find($item->dream_id)->time;
- // $item->comments = $item->comments;
- // foreach ($item->comments as $k => $v) {
- // $v->pic = UserInfoModel::find($v->user_id)->pic;
- // $v->replay = $v->replay;
- // foreach ($v->replay as $k1 => $v1) {
- // $v1->pic = UserInfoModel::find($v1->user_id)->pic;
- // }
- // }
- // }
- // 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 {get} /api/index/filter 筛选
- * @apiDescription 筛选
- * @apiGroup Index
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} [sex] 性别 //1男,2女,0:全部,
- * @apiParam {string} [age] 年龄段 0:全部,1:19-25,2:26-35,3:>36,7:<18, 接收1,2,3,4...
- * @apiParam {string} [area] 地区 例:成都
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data":[
- * {
- * "nickname": "ha", 昵称
- * "pic": "", 头像
- * ...
- * },
- * ]
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function filter(Request $request)
- {
- $sex = $request->sex;
- $age = $request->age;
- $area = $request->area;
- $login_user = $this->getUser();
- $id = $login_user->id;
- $query = new DreamInfoModel();
- if (!empty($age)) {
- $age_arr = explode(',',$age); // 被选中的年龄数组
- foreach ($age_arr as $value) {
- switch ($value) {
- case 1:
- $query = $query->orWhereHas('user', function ($select) use ($age) {
- $select->whereYear('birthday','>=',(date('Y')-25))->whereYear('birthday','<=',(date('Y')-19));
- });
- break;
- case 2:
- $query = $query->orWhereHas('user', function ($select) use ($age) {
- $select->whereYear('birthday','>=',(date('Y')-26))->whereYear('birthday','<=',(date('Y')-35));
- });
- break;
- // case 3:
- // $query = $query->orWhereHas('user', function ($select) use ($age) {
- // $select->whereYear('birthday','>=',(date('Y')-29))->whereYear('birthday','<=',(date('Y')-26));
- // });
- // break;
- // case 4:
- // $query = $query->orWhereHas('user', function ($select) use ($age) {
- // $select->whereYear('birthday','>=',(date('Y')-33))->whereYear('birthday','<=',(date('Y')-30));
- // });
- // break;
- // case 5:
- // $query = $query->orWhereHas('user', function ($select) use ($age) {
- // $select->whereYear('birthday','>=',(date('Y')-37))->whereYear('birthday','<=',(date('Y')-34));
- // });
- // break;
- case 3:
- $query = $query->orWhereHas('user', function ($select) use ($age) {
- $select->whereYear('birthday','<=',(date('Y')-36));
- });
- break;
- case 7:
- $query = $query->orWhereHas('user', function ($select) use ($age) {
- $select->whereYear('birthday','>=',(date('Y')-18));
- });
- break;
- }
- }
- if (($sex) == 1) {
- $query = $query->whereHas('user', function ($select) use ($sex) {
- $select->where('sex',0);
- });
- }
- if (($sex) == 2) {
- $query = $query->whereHas('user', function ($select) use ($sex) {
- $select->where('sex',1);
- });
- }
- }
- /* if ($age == 7) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
- });
- }
- if ($age == 1) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-21)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-18)));
- });
- }
- if ($age == 2) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-25)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-22)));
- });
- }
- if ($age == 3) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-29)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-26)));
- });
- }
- if ($age == 4) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-33)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-30)));
- });
- }
- if ($age == 5) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-37)))->where('birthday','<=',date('Y-m-d',strtotime(date('Y')-34)));
- });
- }
- if ($age == 6) {
- $query = $query->whereHas('user', function ($select) use ($age) {
- $select->where('birthday','>=',date('Y-m-d',strtotime(date('Y')-38)));
- });
- }*/
- if (!empty($area)) {
- $query = $query->whereHas('user', function ($select) use ($area) {
- $select->where('city','like','%'.$area.'%');
- });
- }
- \Log::info($query->toSql());
- $arr1 =DreamInfoModel::orderBy('id')->limit(20)->select('id')->get()->toArray();
- $id_arr1 = array_column($arr1,'id');
- $arr2 =DreamInfoModel::orderBy('id','desc')->limit(120)->select('id')->get()->toArray();
- $id_arr2 = array_column($arr2,'id');
- $dtusers =UserCareUser::where('user_id',$id)->with('other_user')->
- where('dream_number','>',0)->orderBy('created_at')->get()->toArray();
- $hdusers = CommentInfoModel::where(function ($query) use ($id) {
- $query->where('user_id',$id)->orWhere('to_user_id',$id);
- })->where('is_read',0)->with('to_user')->orderBy('created_at')->get()->toArray();
- $users = [] ;
- foreach ($dtusers as $k => $v){
- $users[] = $v['other_user'];
- }
- foreach ($hdusers as $k => $v){
- $users[] = $v['to_user'];
- }
- $type = $request->type;
- if ($type == 'trend') {
- $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->whereNotIn('id', $id_arr1)->limit(100)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('users','dreams'));
- } elseif ($type == 'news') {
- $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->orderBy('created_at','desc')->with('user')->whereNotIn('id', $id_arr2)->limit(500)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('users','dreams'));
- } else{
- $banners = $this->getBanner();
- $dreams = $query->where('end_time','<',date('Y-m-d H:i:s'))->orderBy('score','desc')->with('user')->limit(20)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('banners','users','dreams'));
- }
- }
- }
|