123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\AccountLog;
- use App\Models\BaseSettingsModel;
- use App\Models\DreamImages;
- use App\Models\DreamInfoModel;
- use App\Models\InteractionInfo;
- use App\Models\SupportDreamModel;
- use App\Models\SystemInfoModel;
- use App\Models\UserCareDream;
- use App\Models\UserInfoModel;
- use Illuminate\Http\Request;
- use App\Services\Base\ErrorCode;
- class DreamController extends Controller
- {
- /**
- * @api {get} /api/dream/show 梦想
- * @apiDescription 梦想
- * @apiGroup Dream
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiParam {int} type index interaction paihang 默认all
- * @apiParam {int} id 梦想ID
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * 主页
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "dream": { 梦想
- * "id": 7,
- * "user_id": 2,
- * "dream": "梦想标1123",
- * "about": "梦想烧1111213",
- * "video": "",
- * "sign": null,
- * "money": 10000,
- * "time": 720000,
- * "get_money": 0,
- * "status": 0,
- * "updated_at": "2017-06-22 09:47:03"
- * },
- * "imgs": [ 梦想图片
- * {
- * "title": "",
- * "pic": "http://w17.9026.com/img/banner/banner_3.png"
- * },
- * {
- * "title": "",
- * "pic": "http://w17.9026.com/img/banner/banner_3.png"
- * }
- * ],
- * "arr": { 支持者前三(user_id和头像)
- * "6": "http://www.wsfjq.com/photos/bd119684755.jpg",
- * "1": "http://www.wsfjq.com/photos/bd119684755.jpg"
- * }
- * "money" : "1000" 用户余额
- * "score" : "1000" 梦想分数
- * "a": "0.001", 支持乘数计算 y = -a * b +c b = updated_at-当前时间 (分钟) y>=1
- * "c": "8"
- * }
- *}
- * 排行
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": [
- * {
- * "nickname": "ha", 昵称
- * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg", 头像
- * "score": 112000 支持分
- * },
- * ]
- *}
- * 互动
- *{
- * "status": true,
- * "status_code": 0,
- * "message": "",
- * "data": {
- * "data": [
- * {
- * "id": 7,
- * "dream_id": 2,
- * "title": "梦想动态有力来了",
- * "pic1": "http\"//www.baidu.com",
- * "created_at": "2017-06-22 21:09:18",
- * "updated_at": "2017-06-22 21:09:18",
- * "deleted_at": null,
- * "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"
- * }
- * }
- * ]
- * ]
- * },
- * "times": 720000 梦想实现倒计时(s)
- * }
- *}
- * @apiErrorExample {json} Error-Response:
- *HTTP/1.1 400 Bad Request
- *{
- * "status": false,
- * "status_code": 1105,
- * "message": "用户不存在",
- * "data": null
- * }
- */
- public function show(Request $request)
- {
- $user = $this->getUser();
- $type = $request->type; //类型
- $id = $request->id; //梦想ID
- if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
- $info = SupportDreamModel::where('dream_id',$id)->get();
- $top = [] ;
- $topuser = [];
- $top3user = [];
- foreach ($info 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) {
- dd($item->comments);
- $item->with(['comments']);
- foreach ($item->comments as $comment) {
- $comment->pic = UserInfoModel::find($comment->user_id)->pic;
- $comment->replay = $comment->replay;
- foreach ($comment->replay as $k1 => $v1){
- $v1->pic = UserInfoModel::find($v1->user_id)->pic;
- }
- }
- }
- if ($type == 'interaction') return $this->api(compact('data'));
- // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
- // $money = $user->money;
- $number = BaseSettingsModel::where('category','score')->select('key','value')->first()->moey;
- $a = count($number) > 0 ? $number->key : '';
- $c = count($number) > 0 ? $number->value : '';
- $dream = DreamInfoModel::with(['imgs','user'])->find($id);
- // $imgs = DreamImages::where('dream_id',$id)->orderBy('id','desc')->get();
- // $info = SupportDreamModel::where('dream_id',$id)->get();
- $top3 = [] ;
- $score = 0 ;
- foreach ($info as $item) {
- $score += $item->score;
- if (!array_key_exists($item->user_id,$top3)) {
- $top3[$item->user_id] = $item->score;
- }else{
- $top3[$item->user_id] += $item->score;
- }
- }
- arsort($top3);
- foreach ($top3 as $k => $v){
- $user = UserInfoModel::find($k);
- if($user)
- $top3[$k] = $user->pic;
- }
- return $this->api(compact('dream','top3user','a','c','score','topuser'));
- }
- /**
- * @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' => '支持梦想不能为空',
- ]
- );
- 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 : '';
- $c = count($setting) > 0 ? $setting->value : '';
- //Todo 支持乘数目公式
- $number = 2;
- if ($user->money < $coin) {
- return $this->error(ErrorCode::COIN_NOT_ENOUGH);
- }else{
- $user->money = $user->money - $coin;
- $user->save();
- $dream_info->get_money += $coin;
- $dream_info->save();
- $data = [
- 'user_id'=>$user->id,
- 'dream_id'=>$dream_id,
- 'dream_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' => '支持梦想不能为空',
- ]
- );
- 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,
- ];
- $info = UserCareDream::where('user_id',$user->id)->
- where('dream_id',$request->id)->first();
- if (count($info) == 0) {
- UserCareDream::create($data);
- }
- return $this->api('');
- }
- // 发布关于梦想的动态
- /**
- * @api {post} /api/dream/add_info 新增动态
- * @apiDescription 新增动态
- * @apiGroup Dream
- * @apiPermission Passport
- * @apiVersion 0.1.0
- * @apiParam {int} id 梦想ID
- * @apiParam {string} title 梦想标题
- * @apiParam {array} pics[] 图片数组
- * @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 addInfo(Request $request)
- {
- $validator = \Validator::make($request->all(),
- [
- 'id' => 'required',
- 'title' => 'required',
- ],
- [
- 'dream_id.required' => '梦想ID不能为空',
- 'title.required' => '动态标题不能为空',
- ]
- );
- if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- $pics = $request->pics;
- if (empty($pics) || !is_array($pics)) {
- return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
- }
- $data = [];
- foreach ($pics as $k => $pic) {
- $data['pic'.($k+1)] = $pic;
- }
- $dream_id = $request->id;
- $title = $request->title;
- $data['dream_id'] = $dream_id;
- $data['title'] = $title;
- $ok = InteractionInfo::create($data);
- if ($ok) {
- // 收藏梦想最新动态加一
- UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
- return $this->api('');
- }else{
- return $this->error(ErrorCode::SAVE_USER_FAILED);
- }
- }
- }
|