123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\BaseSettingsModel;
- use App\Models\DreamInfoModel;
- use App\Models\SearchInfoModel;
- use App\Models\UserCareDream;
- use App\Models\UserCareUser;
- use App\Models\UserDream;
- use App\Models\UserInfoModel;
- use Illuminate\Http\Request;
- use App\Services\Base\ErrorCode;
- class IndexController extends Controller
- {
- /**
- * @api {get} /api/index/index 首页
- * @apiDescription 首页
- * @apiGroup Index
- * @apiParam {string} type (热门)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": {
- * "banner": [ 轮播图
- * {
- * "value": "http://w17.9026.com/img/banner/banner_2.png",
- * },
- * {
- * "value": "http://w17.9026.com/img/banner/banner_3.png",
- * },
- * ],
- * "users": {
- * "1": [ 1用户ID
- * [
- * { 用户梦想互动
- * "id": 8,
- * "dream_id": 1,
- * "title": "2",
- * "pic1": "www.buniang.com",
- * "pic2": "",
- * "pic3": "",
- * "pic4": "",
- * "pic5": "",
- * "pic6": "",
- * "pic7": "",
- * "pic8": "",
- * "pic9": "",
- * "status": 1,
- * "created_at": "2017-06-23 08:13:39",
- * "updated_at": "2017-06-23 08:13:39",
- * "deleted_at": null,
- * "user_id": 1
- * },
- * {
- * "id": 9,
- * "dream_id": 1,
- * "title": "2haha",
- * "pic1": "www.buniang.com",
- * "pic2": "",
- * "pic3": "",
- * "pic4": "",
- * "pic5": "",
- * "pic6": "",
- * "pic7": "",
- * "pic8": "",
- * "pic9": "",
- * "status": 1,
- * "created_at": "2017-06-23 08:15:01",
- * "updated_at": "2017-06-23 08:15:01",
- * "deleted_at": null,
- * "user_id": 1
- * },
- * ],
- * "http://www.wsfjq.com/photos/bd119684755.jpg" 用户头像
- * 3 新消息条数
- * ],
- *}
- * "dreams": { 梦想列表
- * "current_page": 1,
- * "data": [
- * {
- * "id": 1,
- * "user_id": 1,
- * "dream": "梦想标题",
- * "about": "梦想烧",
- * "video": "",
- * "money": 10000,
- * "time": 720000,
- * "get_money": 0,
- * "status": 0,
- * "updated_at": "2017-06-22 09:42:41",
- * "dream_user": { 梦想所有者
- * "nickname": "ha",
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg",
- * "money": 123568,
- * "sex": 1,
- * "signture": "signture",
- * "city": "",
- * "birthday": "2017-06-21",
- * "status": 1,
- * },
- * "care_num": 1, 关注人数
- * "dream_imgs_first": { 梦想封面图片
- * "title": "",
- * "pic": "http://w17.9026.com/img/banner/banner_3.png"
- * },
- * "dream_detailed": { 点击进入梦想详情
- * "dream_imgs": [
- * {
- * "title": "",
- * "pic": "http://w17.9026.com/img/banner/banner_3.png"
- * },
- * {
- * "title": "",
- * "pic": "http://w17.9026.com/img/banner/banner_3.png"
- * }
- * ],
- * "comments": [
- * {
- * "user_id": 2,
- * "level": 0,
- * "content": "EST评论",
- * "updated_at": null,
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg", 评论者头像
- * "replay": [
- * {
- * "user_id": 2,
- * "level": 0,
- * "content": "拉了",
- * "updated_at": null,
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg" 回复者头像
- * },
- * {
- * "user_id": 3,
- * "level": 0,
- * "content": "userid3回复了你的消息",
- * "updated_at": null,
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
- * }
- * ]
- * },
- * }
- * ]
- * },
- * "from": 1,
- * "last_page": 1,
- * "next_page_url": null,
- * "path": "http://www.miao.com/api/index/index",
- * "per_page": 20,
- * "prev_page_url": null,
- * "to": 8,
- * "total": 8
- * }
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- * {
- * "state": false,
- * "code": 1000,
- * "message": "传入参数不正确",
- * "data": null or []
- * }
- */
- public function index(Request $request)
- {
- $user = $this->getUser();
- // 关注的梦想
- $users = $user->UserCareDream;
- // $arr = UserInfoModel::where('id',$user->id)->whereHas('UserCareDream', function ($query) {
- // $query->where('interaction_number', '>', 0);
- // })->get();
- // dd($arr->toArray());
- // $users = [] ;
- // $arr1 = [];
- // foreach ($arr as $k => $v){
- // if ($v->pivot->interaction_number > 0) {
- // if (!array_key_exists($v->pivot->dream_user_id,$arr1)) {
- // $arr1[$v->pivot->dream_user_id] = $v->pivot->interaction_number;
- // }else{
- // $arr1[$v->pivot->dream_user_id] += $v->pivot->interaction_number;
- // }
- // }
- // }
- // dd($arr1);
- // foreach ($arr1 as $k => $v){
- // $a = UserInfoModel::find($k);
- // $users[$k] =[$a->allInteraction,$a->pic,$v] ;
- // }
- $type = $request->type;
- if ($type == 'trend') {
- $dreams = DreamInfoModel::orderBy('score','desc')->offset(20)->limit(100)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('users','dreams'));
- } elseif ($type == 'news') {
- $dreams = DreamInfoModel::orderBy('score','desc')->offset(100)->limit(500)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('users','dreams'));
- } else{
- $banners = $this->getBanner();
- // 获取其他用户信息 及梦想
- $dreams = DreamInfoModel::orderBy('score','desc')->limit(20)->paginate(20);
- $this->dreams($dreams);
- return $this->api(compact('banners','users','dreams'));
- }
- }
- /**
- * @api {get} /api/index/search 搜索(search)
- * @apiDescription 搜索(search)
- * @apiGroup Index
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {string} keyword 关键字可选 (ha)
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * get
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "arr": { // 热门搜索
- * ...
- * },
- * "data1": [
- * {
- * "search": "ha", // 历史搜索
- * }
- * ]
- * }
- *}
- * post
- * {
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "data1": [
- * ... //用户
- * ],
- * "data2": [
- * ... //梦想
- * ],
- * "data3": [
- * ... //标签
- * ]
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function search(Request $request)
- {
- $user = $this->getUser();
- $keyword ='%'.$request->keyword.'%';
- $data1 = UserInfoModel::where('nickname','like',$keyword)->get();
- $data2 = DreamInfoModel::where('dream','like',$keyword)->
- orWhere('sign','like',$keyword)->get();
- $data3 = BaseSettingsModel::where('category','sign')->where('value','like',$keyword)->get();
- if (empty($request->keyword)) {
- // 历史搜索
- $data1 = $user->search()->orderBy('id','desc')->limit(10)->get();
- // 热门搜索
- $data2 = SearchInfoModel::get();
- $arr = [];
- foreach ($data2 as $k => $v) {
- if (count($arr) == 8) {
- break;
- }
- if (!array_key_exists($v->search,$arr)) {
- $arr[$v->search] = $v->times;
- }else{
- $arr[$v->search] += $v->times;
- }
- }
- arsort($arr);
- return $this->api(compact('arr','data1'));
- }
- // 写入搜索记录
- $this->insertSearchTable($user->id,$request->keyword);
- return $this->api(compact('data1','data2','data3'));
- }
- /**
- * @api {get} /api/index/user_search 用户搜索(search)
- * @apiDescription 用户搜索(search)
- * @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": {
- * "data1": [
- * "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.'%';
- $data1 = UserInfoModel::where('nickname','like',$keyword)->get();
- $this->insertSearchTable($user->id,$request->keyword);
- return $this->api(compact('data1'));
- }
- /**
- * @api {get} /api/index/dream_search 梦想搜索(search)
- * @apiDescription 梦想搜索(search)
- * @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": {
- * "data": [
- * {
- * "dream": "haha", 梦想名
- * "user_pic": [
- * {
- * "pic": "", 用户头像
- * }
- * ],
- * "dream_img": "", 梦想图片
- * }
- * ]
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function dreamSearch(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 getBanner()
- {
- $banner = BaseSettingsModel::where(['category' => 'banner'])->where(['status' => '1'])
- ->orderBy('sort')->get()->toArray();
- return $banner;
- }
- // 完善梦想信息
- public function dreams($dreams)
- {
- foreach ($dreams as $k => $dream) {
- $dream->dream_user = $dream->user;
- // 计算被关注总人数
- $user_id = $dream->user_id;
- $data = UserCareDream::where('dream_id',$dream->id)->get();
- $dream->care_num = count($data);
- $dream->dream_imgs_first = $dream->dreamImgsFirst?$dream->dreamImgsFirst->pic:'';
- // 梦想详情 detailed 图片 ,评论 ,回复
- $arr = [] ;
- $arr['dream_imgs'] = $dream->imgs;
- $dream->dream_detailed = $arr;
- }
- }
- // 查看关注用户的最新动态
- /**
- * @api {get} /api/index/news_info 关注用户动态详情
- * @apiDescription 关注用户动态详情
- * @apiGroup Index
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiParam {int} id 被点击用户ID
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "all_interaction": [ 互动
- * {
- * "id": 7,
- * "dream_id": 2,
- * "title": "梦想动态有力来了",
- * "pic1": "http\"//www.baidu.com",
- * "pic2": "",
- * "pic3": "",
- * "pic4": "",
- * "pic5": "",
- * "pic6": "",
- * "pic7": "",
- * "pic8": "",
- * "pic9": "",
- * "status": 1,
- * "created_at": "2017-06-22 21:09:18",
- * "updated_at": "2017-06-22 21:09:18",
- * "deleted_at": null,
- * "user_id": 2,
- * "time": 720000, 倒计时
- * "comments": [ 评论
- * {
- * "interaction_id": 7,
- * "user_id": 1,
- * "level": 0,
- * "content": "不错的梦想",
- * "updated_at": null,
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg",
- * "replay": [ 回复
- * {
- * "user_id": 1,
- * "level": 0,
- * "content": "我也觉得不错",
- * "updated_at": null,
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
- * },
- * {
- * "user_id": 6,
- * "level": 0,
- * "content": "祝你梦想成真",
- * "updated_at": null,
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
- * }
- * ]
- * }
- * ]
- * },
- * ]
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- 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();
- }
- }
- }
|