DreamController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\AccountLog;
  4. use App\Models\BaseSettingsModel;
  5. use App\Models\DreamImages;
  6. use App\Models\DreamInfoModel;
  7. use App\Models\InteractionInfo;
  8. use App\Models\SearchInfoModel;
  9. use App\Models\SupportDreamModel;
  10. use App\Models\SystemInfoModel;
  11. use App\Models\UserCareDream;
  12. use App\Models\UserInfoModel;
  13. use Illuminate\Http\Request;
  14. use App\Services\Base\ErrorCode;
  15. class DreamController extends Controller
  16. {
  17. /**
  18. * @api {get} /api/dream/show 梦想详情
  19. * @apiDescription 梦想详情
  20. * @apiGroup Dream
  21. * @apiPermission Passport
  22. * @apiVersion 0.1.0
  23. * @apiParam {int} type home interaction paihang 默认all
  24. * @apiParam {int} id 梦想ID
  25. * @apiSuccessExample {json} Success-Response:
  26. * HTTP/1.1 200 OK
  27. * 主页
  28. {
  29. "status": true,
  30. "status_code": 0,
  31. "message": "",
  32. "data": {
  33. "dream": { 梦想
  34. "id": 4,
  35. "user_id": 1,
  36. "name": "梦想标题1",
  37. "about": "梦想介绍",
  38. "coin": 2500,
  39. "time": 21,
  40. "get_coin": 600,
  41. "status": 0,
  42. "video": null,
  43. "score": 100079365,
  44. "sign": "",
  45. "created_at": "2017-06-25 11:47:45",
  46. "updated_at": "2017-06-25 15:10:50",
  47. "is_collection" : 1 1已收藏 0未收藏
  48. "top3user": [ 支持者前三
  49. {
  50. "id": 2,
  51. "phone": "13880642881",
  52. "nickname": "name2",
  53. "avatar": "https://timgsa.baidu.com/timg?imamb.224_0.jpeg",
  54. "score": 2399
  55. }
  56. ],
  57. "number": 1 支持乘数
  58. "imgs": [ 梦想图片
  59. {
  60. "title": "",
  61. "pic": "https://timgsa.ba0170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  62. },
  63. {
  64. "title": "",
  65. "pic": "https://timgsa.baid524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  66. },
  67. {
  68. "title": "",
  69. "pic": "https://timgsa20170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  70. }
  71. ],
  72. "user": { 梦想用户
  73. "id": 1,
  74. "phone": "13880642880",
  75. "nickname": "name1",
  76. "avatar": "https://timgsumb.224_0.jpeg",
  77. "birthday": "2000-06-21",
  78. "sign": 0,
  79. "money": 0,
  80. "coin": 1300,
  81. "sex": 1,
  82. "signture": "",
  83. "height": 170,
  84. "work": "",
  85. "emotion": 1,
  86. "address": "",
  87. "city": "",
  88. "detail_address": "",
  89. "status": 1,
  90. "wechat": "",
  91. "weibo": "",
  92. "remember_token": "",
  93. "created_at": "2017-06-25 10:27:08",
  94. "updated_at": "2017-06-25 15:20:11",
  95. "deleted_at": null
  96. }
  97. }
  98. }
  99. }
  100. * @apiErrorExample {json} Error-Response:
  101. *HTTP/1.1 400 Bad Request
  102. *{
  103. * "status": false,
  104. * "status_code": 1105,
  105. * "message": "用户不存在",
  106. * "data": null
  107. * }
  108. */
  109. public function show(Request $request)
  110. {
  111. $user = $this->getUser();
  112. $type = $request->type; //类型
  113. $id = $request->id; //梦想ID
  114. $user_care_dream = UserCareDream::where('user_id',$user->id)->where('dream_id',$id)->first();
  115. $is_collection = empty($user_care_dream) ? 0 :1 ;
  116. if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
  117. $support_dream = SupportDreamModel::where('dream_id',$id)->get();
  118. $top = [] ;
  119. $topuser = []; //所有支持用户排行
  120. $top3user = []; // 支持用户排行前三
  121. foreach ($support_dream as $item) {
  122. if (!array_key_exists($item->user_id,$top)) {
  123. $top[$item->user_id] = $item->score;
  124. }else{
  125. $top[$item->user_id] += $item->score;
  126. }
  127. }
  128. arsort($top);
  129. foreach ($top as $user_id => $score){
  130. $user = UserInfoModel::find($user_id);
  131. $user->score = $score;
  132. $topuser[] = $user;
  133. if(count($top3user) <= 2) $top3user[] = $user ;
  134. }
  135. if ($type == 'paihang') return $this->api($topuser);
  136. $interactions = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get();
  137. foreach ($interactions as $item) {
  138. $item->comments;
  139. foreach ($item->comments as $comment) {
  140. $comment->pic = UserInfoModel::find($comment->user_id)->pic;
  141. $comment->replay;
  142. foreach ($comment->replay as $k1 => $v1){
  143. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  144. }
  145. }
  146. }
  147. if ($type == 'interaction') return $this->api($interactions);
  148. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  149. // $money = $user->money;
  150. $dream_info = DreamInfoModel::find($id);
  151. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  152. $a = count($setting) > 0 ? $setting->key : 0;
  153. $created_at = $dream_info ? $dream_info->created_at : 0;
  154. $b = intval((time()-strtotime($created_at))/60) ;
  155. $c = count($setting) > 0 ? $setting->value : 0;
  156. //Todo 支持乘数目公式
  157. $number =round( -$a * $b + $c,1);
  158. if ($number <= 1) $number = 1;
  159. $dream = DreamInfoModel::with(['imgs','user'])->find($id);
  160. $dream->topuser = $topuser;
  161. $dream->top3user = $top3user;
  162. $dream->interactions = $interactions;
  163. $dream->number = $number;
  164. $dream->is_collection = $is_collection;
  165. $dream->a = $a;
  166. $dream->c = $c;
  167. return $this->api($dream);
  168. }
  169. /**
  170. * @api {post} /api/dream/support 支持梦想
  171. * @apiDescription 支持梦想
  172. * @apiGroup Dream
  173. * @apiPermission Passport
  174. * @apiVersion 0.1.0
  175. * @apiParam {int} coin 支持梦想币数量
  176. * @apiParam {int} id 梦想ID
  177. * @apiSuccessExample {json} Success-Response:
  178. * HTTP/1.1 200 OK
  179. {
  180. "status": true,
  181. "status_code": 0,
  182. "message": "",
  183. "data": {
  184. "id": 12,
  185. "user_id": 2,
  186. "name": "用户2梦想标题166",
  187. "about": "用户2梦想介绍666",
  188. "coin": 2500,
  189. "time": 21,
  190. "get_coin": 704,
  191. "status": 0,
  192. "video": null,
  193. "score": 158730,
  194. "mark": 158730,
  195. "sign": "",
  196. "created_at": "2017-06-25 13:10:56",
  197. "updated_at": "2017-06-27 18:16:02",
  198. "top3user": [ 支持者前三
  199. {
  200. "id": 1,
  201. "phone": "13880642880",
  202. "nickname": "name1",
  203. "avatar": "https://timgsa.bumb.224_0.jpeg",
  204. "birthday": "2000-06-21",
  205. "sign": 0,
  206. "money": 0,
  207. "coin": 596,
  208. "sex": 1,
  209. "signture": "我的个性签名1",
  210. "height": 170,
  211. "work": "网红",
  212. "emotion": 1,
  213. "address": "",
  214. "city": "上海",
  215. "detail_address": "",
  216. "status": 1,
  217. "wechat": "",
  218. "weibo": "",
  219. "remember_token": "",
  220. "created_at": "2017-06-25 10:27:08",
  221. "updated_at": "2017-06-27 18:16:02",
  222. "deleted_at": null,
  223. "score": 5062
  224. }
  225. ]
  226. "number": 4.8
  227. "is_collection" : 1 1已收藏 0未收藏
  228. }
  229. }
  230. * @apiErrorExample {json} Error-Response:
  231. *HTTP/1.1 400 Bad Request
  232. * {
  233. * "state": false,
  234. * "code": 1000,
  235. * "message": "传入参数不正确",
  236. * "data": null or []
  237. * }
  238. * 可能出现的代码
  239. * {
  240. * "status": false,
  241. * "status_code": 1303,
  242. * "message": "余额不足",
  243. * "data": null
  244. * }
  245. *
  246. */
  247. public function support(Request $request)
  248. {
  249. $validator = \Validator::make($request->all(),
  250. [
  251. 'coin' => 'required',
  252. 'id' => 'required',
  253. ],
  254. [
  255. 'coin.required' => '梦想币不能为空',
  256. 'id.required' => '支持梦想id不能为空',
  257. ]
  258. );
  259. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  260. $user = $this->getUser();
  261. $dream_id = $request->id;
  262. $dream = DreamInfoModel::find($dream_id);
  263. $user_id = $dream->user_id;
  264. $coin = $request->coin;
  265. $user_care_dream = UserCareDream::where('user_id',$user->id)->where('dream_id',$dream_id)->first();
  266. $is_collection = empty($user_care_dream) ? 0 :1 ;
  267. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  268. $a = count($setting) > 0 ? $setting->key : '';
  269. $b = intval((time()-strtotime($dream->created_at))/60) ;
  270. $c = count($setting) > 0 ? $setting->value : '';
  271. //Todo 支持乘数目公式
  272. $number =round( -$a * $b + $c,1);
  273. if ($number <= 1) $number = 1;
  274. if ($user->coin < $coin) {
  275. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  276. }else{
  277. $user->coin -= $coin;
  278. $user->save();
  279. $dream->get_coin += $coin;
  280. $dream->mark += $coin*$number;
  281. $dream->save();
  282. $data = [
  283. 'user_id'=>$user->id,
  284. 'dream_id'=>$dream_id,
  285. 'to_user_id'=>$user_id,
  286. 'coin'=>$coin,
  287. 'score'=>$coin*$number,
  288. ];
  289. $ok = SupportDreamModel::create($data);
  290. if (!$ok) {
  291. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  292. }
  293. // 记录充值记录 新增系统消息
  294. $data = [
  295. 'from_id' =>$user->id,
  296. 'to_id' => $user_id,
  297. 'from_amount' => $coin,
  298. 'to_amount' => $coin,
  299. 'from_type' => AccountLog::TYPE_COIN,
  300. 'to_type' => AccountLog::TYPE_COIN,
  301. 'op' => AccountLog::OP_SUPPORT,
  302. ];
  303. AccountLog::create($data);
  304. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  305. $info = [
  306. 'user_id' => $user_id,
  307. 'message' => $message,
  308. ];
  309. SystemInfoModel::create($info);
  310. // 支持成功返回数据到当前页面 top3user
  311. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  312. $top = [] ;
  313. $top3user = []; // 支持用户排行前三
  314. foreach ($support_dream as $item) {
  315. if (!array_key_exists($item->user_id,$top)) {
  316. $top[$item->user_id] = $item->score;
  317. }else{
  318. $top[$item->user_id] += $item->score;
  319. }
  320. }
  321. arsort($top);
  322. foreach ($top as $user_id => $score){
  323. $user = UserInfoModel::find($user_id);
  324. $user->score = $score;
  325. if(count($top3user) <= 2) $top3user[] = $user ;
  326. }
  327. // 梦想分 mark
  328. $dream->top3user = $top3user;
  329. $dream->is_collection = $is_collection;
  330. $user_coin = $user->coin;
  331. return $this->api(compact('dream','user_coin'));
  332. }
  333. }
  334. // 收藏关注梦想
  335. /**
  336. * @api {get} /api/dream/collection 收藏梦想
  337. * @apiDescription 收藏梦想
  338. * @apiGroup Dream
  339. * @apiPermission Passport
  340. * @apiVersion 0.1.0
  341. * @apiParam {int} id 梦想ID
  342. * @apiParam {int} is_collection 0取消关注 1关注
  343. * @apiSuccessExample {json} Success-Response:
  344. * HTTP/1.1 200 OK
  345. *{
  346. * "status": true,
  347. * "status_code": 0,
  348. * "message": "",
  349. * "data": {
  350. * "is_collection": 1 1已关注 0 未关注
  351. * }
  352. *}
  353. * @apiErrorExample {json} Error-Response:
  354. *HTTP/1.1 400 Bad Request
  355. * {
  356. * "state": false,
  357. * "code": 1000,
  358. * "message": "传入参数不正确",
  359. * "data": null or []
  360. * }
  361. *
  362. */
  363. public function collection(Request $request)
  364. {
  365. $validator = \Validator::make($request->all(),
  366. [
  367. 'id' => 'required',
  368. 'is_collection' => 'required',
  369. ],
  370. [
  371. 'id.required' => '收藏梦想id不能为空',
  372. 'is_collection.required' => '关注值不存在',
  373. ]
  374. );
  375. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  376. $user = $this->getUser();
  377. if(!$dream = DreamInfoModel::find($request->id)){
  378. $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  379. }
  380. $data = [
  381. 'user_id' =>$user->id,
  382. 'dream_id' =>$request->id,
  383. 'dream_user_id' =>$dream->user_id,
  384. ];
  385. $is_collection = $request->is_collection;
  386. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  387. where('dream_id',$request->id)->first();
  388. if ($is_collection) {
  389. if (empty($user_care_dream)) {
  390. UserCareDream::create($data);
  391. }
  392. }else{
  393. UserCareDream::where('user_id',$user->id)->
  394. where('dream_id',$request->id)->delete();
  395. }
  396. return $this->api($is_collection);
  397. }
  398. /**
  399. * @api {get} /api/dream/search 梦想搜索
  400. * @apiDescription 梦想搜索)
  401. * @apiGroup Dream
  402. * @apiPermission none
  403. * @apiVersion 0.1.0
  404. * @apiParam {string} keyword 关键字
  405. * @apiSuccessExample {json} Success-Response:
  406. * HTTP/1.1 200 OK
  407. {
  408. "status": true,
  409. "status_code": 0,
  410. "message": "",
  411. "data": [
  412. {
  413. "id": 5,
  414. "user_id": 1,
  415. "name": "梦想标题1",
  416. "about": "梦想介绍",
  417. "coin": 2500,
  418. "time": 72000,
  419. "get_coin": 0,
  420. "mark": 0,
  421. "status": 0,
  422. "video": "url",
  423. "score": 100079365,
  424. "sign": "梦想达人",
  425. "created_at": "2017-06-25 12:45:22",
  426. "updated_at": "2017-06-28 15:50:41",
  427. "user": {},
  428. "imgs": []
  429. },
  430. ]
  431. }
  432. * @apiErrorExample {json} Error-Response:
  433. * HTTP/1.1 400 Bad Request
  434. */
  435. public function search(Request $request)
  436. {
  437. $user = $this->getUser();
  438. if (empty($request->keyword)) {
  439. return $this->api('');
  440. }
  441. $keyword ='%'.$request->keyword.'%';
  442. $data = DreamInfoModel::where('name','like',$keyword)->
  443. orWhere('sign','like',$keyword)->with(['user','img'])->get();
  444. $this->insertSearchTable($user->id,$request->keyword);
  445. return $this->api($data);
  446. }
  447. public function insertSearchTable($id,$keyword)
  448. {
  449. $info = SearchInfoModel::where('user_id',$id)->
  450. where('search',trim($keyword))->first();
  451. if (count($info) == 0) {
  452. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  453. }else{
  454. $info->times += 1;
  455. $info->save();
  456. }
  457. }
  458. /**
  459. * @api {post} /api/dream/store 发布梦想
  460. * @apiDescription 发布梦想
  461. * @apiParam {string} pics 梦想图片 数组
  462. * @apiParam {string} video 梦想视频
  463. * @apiParam {string} name 梦想标题
  464. * @apiParam {string} about 梦想介绍
  465. * @apiParam {int} coin 梦想币
  466. * @apiParam {int} time 实现时间默认21*3600
  467. * @apiGroup Dream
  468. * @apiPermission Passport
  469. * @apiVersion 0.1.0
  470. * @apiSuccessExample {json} Success-Response:
  471. * {
  472. * "status": true,
  473. * "status_code": 0,
  474. * "message": "",
  475. * "data": ""
  476. *}
  477. * HTTP/1.1 200 OK
  478. * @apiErrorExample {json} Error-Response:
  479. * {
  480. * "status": false,
  481. * "status_code": 1203,
  482. * "message": "附件不存在",
  483. * "data": null
  484. * }
  485. * HTTP/1.1 400 Bad Request
  486. */
  487. public function store(Request $request)
  488. {
  489. $user = $this->getUser();
  490. $validator = \Validator::make($request->all(),
  491. [
  492. 'name' => 'required',
  493. 'about' => 'required',
  494. 'coin' => 'required|integer',
  495. 'days' => 'required|integer',
  496. ],
  497. [
  498. 'name.required' => '梦想标题必填',
  499. 'about.required' => '梦想介绍必填',
  500. 'coin.required' => '梦想币必填',
  501. 'coin.integer' => '梦想币必须是正整数',
  502. 'days.required' => '实现时间必填',
  503. 'days.integer' => '实现时间必须是正整数',
  504. ]
  505. );
  506. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  507. \Log::debug($user);
  508. $data2 = $user->myCareNum;
  509. $care_num = count($data2);
  510. $setting = BaseSettingsModel::where('category','paihang')->first();
  511. $a = $setting?$setting->key:1;
  512. $b = $setting?$setting->value:1;
  513. $t = 21*3600 / 60;
  514. $days = $request->days;
  515. $data = $request->except('_token','pics','days');
  516. $data['user_id'] = $user->id;
  517. $data['end_time'] = strtotime('+'+$days+' days');
  518. \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  519. if ($care_num == 0) {
  520. $data['score'] = (($a/$t) + $b)*100000000 ;
  521. }else{
  522. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  523. }
  524. $data['created_at'] = date('Y-m-d H:i:s');
  525. $data['updated_at'] = date('Y-m-d H:i:s');
  526. $dream_id = DreamInfoModel::insertGetId($data);
  527. if ($dream_id) {
  528. // 梦想创建成功 关联关系中最新动态+1
  529. /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  530. foreach ($info as $item) {
  531. $item->dream_num += 1;
  532. $item->updated_at = date('Y-m-d H:i:s');
  533. $item->save();
  534. }*/
  535. $pics = $request->pics;
  536. $video = $request->video;
  537. if (!is_array($pics) && empty($pics) && empty($video)) {
  538. DreamInfoModel::destroy($dream_id);
  539. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  540. }
  541. $data = [];
  542. if (is_array($pics)) {
  543. foreach ($pics as $v){
  544. $data[] = [
  545. 'dream_id'=>$dream_id,
  546. 'pic' => $v,
  547. 'created_at' =>date('Y-m-d H:i:s'),
  548. 'updated_at' =>date('Y-m-d H:i:s'),
  549. ];
  550. }
  551. DreamImages::insert($data);
  552. }
  553. return $this->api('');
  554. }
  555. }
  556. }