DreamController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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->user_coin = $user->coin;
  161. $dream->topuser = $topuser;
  162. $dream->top3user = $top3user;
  163. $dream->interactions = $interactions;
  164. $dream->number = $number;
  165. $dream->is_collection = $is_collection;
  166. $dream->a = $a;
  167. $dream->c = $c;
  168. return $this->api($dream);
  169. }
  170. /**
  171. * @api {post} /api/dream/support 支持梦想
  172. * @apiDescription 支持梦想
  173. * @apiGroup Dream
  174. * @apiPermission Passport
  175. * @apiVersion 0.1.0
  176. * @apiParam {int} coin 支持梦想币数量
  177. * @apiParam {int} id 梦想ID
  178. * @apiSuccessExample {json} Success-Response:
  179. * HTTP/1.1 200 OK
  180. {
  181. "status": true,
  182. "status_code": 0,
  183. "message": "",
  184. "data": {
  185. "id": 12,
  186. "user_id": 2,
  187. "name": "用户2梦想标题166",
  188. "about": "用户2梦想介绍666",
  189. "coin": 2500,
  190. "time": 21,
  191. "get_coin": 704,
  192. "status": 0,
  193. "video": null,
  194. "score": 158730,
  195. "mark": 158730,
  196. "sign": "",
  197. "created_at": "2017-06-25 13:10:56",
  198. "updated_at": "2017-06-27 18:16:02",
  199. "top3user": [ 支持者前三
  200. {
  201. "id": 1,
  202. "phone": "13880642880",
  203. "nickname": "name1",
  204. "avatar": "https://timgsa.bumb.224_0.jpeg",
  205. "birthday": "2000-06-21",
  206. "sign": 0,
  207. "money": 0,
  208. "coin": 596,
  209. "sex": 1,
  210. "signture": "我的个性签名1",
  211. "height": 170,
  212. "work": "网红",
  213. "emotion": 1,
  214. "address": "",
  215. "city": "上海",
  216. "detail_address": "",
  217. "status": 1,
  218. "wechat": "",
  219. "weibo": "",
  220. "remember_token": "",
  221. "created_at": "2017-06-25 10:27:08",
  222. "updated_at": "2017-06-27 18:16:02",
  223. "deleted_at": null,
  224. "score": 5062
  225. }
  226. ]
  227. "number": 4.8
  228. "is_collection" : 1 1已收藏 0未收藏
  229. }
  230. }
  231. * @apiErrorExample {json} Error-Response:
  232. *HTTP/1.1 400 Bad Request
  233. * {
  234. * "state": false,
  235. * "code": 1000,
  236. * "message": "传入参数不正确",
  237. * "data": null or []
  238. * }
  239. * 可能出现的代码
  240. * {
  241. * "status": false,
  242. * "status_code": 1303,
  243. * "message": "余额不足",
  244. * "data": null
  245. * }
  246. *
  247. */
  248. public function support(Request $request)
  249. {
  250. $validator = \Validator::make($request->all(),
  251. [
  252. 'coin' => 'required',
  253. 'id' => 'required',
  254. ],
  255. [
  256. 'coin.required' => '梦想币不能为空',
  257. 'id.required' => '支持梦想id不能为空',
  258. ]
  259. );
  260. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  261. $user = $this->getUser();
  262. $dream_id = $request->id;
  263. $dream = DreamInfoModel::find($dream_id);
  264. $user_id = $dream->user_id;
  265. $coin = $request->coin;
  266. if ($dream->get_coin == $dream->coin) {
  267. return $this->error(ErrorCode::SUP_TOP);
  268. } elseif($coin + $dream->get_coin > $dream->coin){
  269. $coin = $coin + $dream->get_coin - $dream->coin ;
  270. }
  271. $user_care_dream = UserCareDream::where('user_id', $user->id)->where('dream_id', $dream_id)->first();
  272. $is_collection = empty($user_care_dream) ? 0 :1 ;
  273. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  274. $a = count($setting) > 0 ? $setting->key : '';
  275. $b = intval((time()-strtotime($dream->created_at))/60) ;
  276. $c = count($setting) > 0 ? $setting->value : '';
  277. //Todo 支持乘数目公式
  278. $number =round( -$a * $b + $c,1);
  279. if ($number <= 1) $number = 1;
  280. if ($user->coin < $coin) {
  281. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  282. }else{
  283. $user->coin -= $coin;
  284. $user->save();
  285. $dream->get_coin += $coin;
  286. $dream->mark += $coin*$number;
  287. $dream->save();
  288. $data = [
  289. 'user_id'=>$user->id,
  290. 'dream_id'=>$dream_id,
  291. 'to_user_id'=>$user_id,
  292. 'coin'=>$coin,
  293. 'score'=>$coin*$number,
  294. ];
  295. $ok = SupportDreamModel::create($data);
  296. if (!$ok) {
  297. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  298. }
  299. // 记录充值记录 新增系统消息
  300. $data = [
  301. 'from_id' =>$user->id,
  302. 'to_id' => $user_id,
  303. 'from_amount' => $coin,
  304. 'to_amount' => $coin,
  305. 'from_type' => AccountLog::TYPE_COIN,
  306. 'to_type' => AccountLog::TYPE_COIN,
  307. 'op' => AccountLog::OP_SUPPORT,
  308. ];
  309. AccountLog::create($data);
  310. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  311. $info = [
  312. 'user_id' => $user_id,
  313. 'message' => $message,
  314. ];
  315. SystemInfoModel::create($info);
  316. // 支持成功返回数据到当前页面 top3user
  317. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  318. $top = [] ;
  319. $top3user = []; // 支持用户排行前三
  320. foreach ($support_dream as $item) {
  321. if (!array_key_exists($item->user_id,$top)) {
  322. $top[$item->user_id] = $item->score;
  323. }else{
  324. $top[$item->user_id] += $item->score;
  325. }
  326. }
  327. arsort($top);
  328. foreach ($top as $user_id => $score){
  329. $user = UserInfoModel::find($user_id);
  330. $user->score = $score;
  331. if(count($top3user) <= 2) $top3user[] = $user ;
  332. }
  333. // 梦想分 mark
  334. $dream->top3user = $top3user;
  335. $dream->is_collection = $is_collection;
  336. $user_coin = $user->coin;
  337. return $this->api(compact('dream','user_coin'));
  338. }
  339. }
  340. // 收藏关注梦想
  341. /**
  342. * @api {get} /api/dream/collection 收藏梦想
  343. * @apiDescription 收藏梦想
  344. * @apiGroup Dream
  345. * @apiPermission Passport
  346. * @apiVersion 0.1.0
  347. * @apiParam {int} id 梦想ID
  348. * @apiParam {int} is_collection 0取消关注 1关注
  349. * @apiSuccessExample {json} Success-Response:
  350. * HTTP/1.1 200 OK
  351. *{
  352. * "status": true,
  353. * "status_code": 0,
  354. * "message": "",
  355. * "data": {
  356. * "is_collection": 1 1已关注 0 未关注
  357. * }
  358. *}
  359. * @apiErrorExample {json} Error-Response:
  360. *HTTP/1.1 400 Bad Request
  361. * {
  362. * "state": false,
  363. * "code": 1000,
  364. * "message": "传入参数不正确",
  365. * "data": null or []
  366. * }
  367. *
  368. */
  369. public function collection(Request $request)
  370. {
  371. $validator = \Validator::make($request->all(),
  372. [
  373. 'id' => 'required',
  374. 'is_collection' => 'required',
  375. ],
  376. [
  377. 'id.required' => '收藏梦想id不能为空',
  378. 'is_collection.required' => '关注值不存在',
  379. ]
  380. );
  381. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  382. $user = $this->getUser();
  383. if(!$dream = DreamInfoModel::find($request->id)){
  384. $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  385. }
  386. $data = [
  387. 'user_id' =>$user->id,
  388. 'dream_id' =>$request->id,
  389. 'dream_user_id' =>$dream->user_id,
  390. ];
  391. $is_collection = $request->is_collection;
  392. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  393. where('dream_id',$request->id)->first();
  394. if ($is_collection) {
  395. if (empty($user_care_dream)) {
  396. UserCareDream::create($data);
  397. }
  398. }else{
  399. UserCareDream::where('user_id',$user->id)->
  400. where('dream_id',$request->id)->delete();
  401. }
  402. return $this->api($is_collection);
  403. }
  404. /**
  405. * @api {get} /api/dream/search 梦想搜索
  406. * @apiDescription 梦想搜索)
  407. * @apiGroup Dream
  408. * @apiPermission none
  409. * @apiVersion 0.1.0
  410. * @apiParam {string} keyword 关键字
  411. * @apiSuccessExample {json} Success-Response:
  412. * HTTP/1.1 200 OK
  413. {
  414. "status": true,
  415. "status_code": 0,
  416. "message": "",
  417. "data": [
  418. {
  419. "id": 5,
  420. "user_id": 1,
  421. "name": "梦想标题1",
  422. "about": "梦想介绍",
  423. "coin": 2500,
  424. "time": 72000,
  425. "get_coin": 0,
  426. "mark": 0,
  427. "status": 0,
  428. "video": "url",
  429. "score": 100079365,
  430. "sign": "梦想达人",
  431. "created_at": "2017-06-25 12:45:22",
  432. "updated_at": "2017-06-28 15:50:41",
  433. "user": {},
  434. "imgs": []
  435. },
  436. ]
  437. }
  438. * @apiErrorExample {json} Error-Response:
  439. * HTTP/1.1 400 Bad Request
  440. */
  441. public function search(Request $request)
  442. {
  443. $user = $this->getUser();
  444. if (empty($request->keyword)) {
  445. return $this->api('');
  446. }
  447. $keyword ='%'.$request->keyword.'%';
  448. $data = DreamInfoModel::where('name','like',$keyword)->
  449. orWhere('sign','like',$keyword)->with(['user','img'])->get();
  450. $this->insertSearchTable($user->id,$request->keyword);
  451. return $this->api($data);
  452. }
  453. public function insertSearchTable($id,$keyword)
  454. {
  455. $info = SearchInfoModel::where('user_id',$id)->
  456. where('search',trim($keyword))->first();
  457. if (count($info) == 0) {
  458. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  459. }else{
  460. $info->times += 1;
  461. $info->save();
  462. }
  463. }
  464. /**
  465. * @api {post} /api/dream/store 发布梦想
  466. * @apiDescription 发布梦想
  467. * @apiParam {string} pics 梦想图片 数组
  468. * @apiParam {string} video 梦想视频
  469. * @apiParam {string} name 梦想标题
  470. * @apiParam {string} about 梦想介绍
  471. * @apiParam {int} coin 梦想币
  472. * @apiParam {int} time 实现时间默认21*3600
  473. * @apiGroup Dream
  474. * @apiPermission Passport
  475. * @apiVersion 0.1.0
  476. * @apiSuccessExample {json} Success-Response:
  477. * {
  478. * "status": true,
  479. * "status_code": 0,
  480. * "message": "",
  481. * "data": ""
  482. *}
  483. * HTTP/1.1 200 OK
  484. * @apiErrorExample {json} Error-Response:
  485. * {
  486. * "status": false,
  487. * "status_code": 1203,
  488. * "message": "附件不存在",
  489. * "data": null
  490. * }
  491. * HTTP/1.1 400 Bad Request
  492. */
  493. public function store(Request $request)
  494. {
  495. $user = $this->getUser();
  496. $validator = \Validator::make($request->all(),
  497. [
  498. 'name' => 'required',
  499. 'about' => 'required',
  500. 'coin' => 'required|integer',
  501. 'days' => 'required|integer',
  502. ],
  503. [
  504. 'name.required' => '梦想标题必填',
  505. 'about.required' => '梦想介绍必填',
  506. 'coin.required' => '梦想币必填',
  507. 'coin.integer' => '梦想币必须是正整数',
  508. 'days.required' => '实现时间必填',
  509. 'days.integer' => '实现时间必须是正整数',
  510. ]
  511. );
  512. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  513. \Log::debug($user);
  514. $data2 = $user->myCareNum;
  515. $care_num = count($data2);
  516. $setting = BaseSettingsModel::where('category','paihang')->first();
  517. $a = $setting?$setting->key:1;
  518. $b = $setting?$setting->value:1;
  519. $t = 21*3600 / 60;
  520. $days = $request->days;
  521. $data = $request->except('_token','pics','days');
  522. $data['user_id'] = $user->id;
  523. $data['end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' days'));
  524. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  525. if ($care_num == 0) {
  526. $data['score'] = (($a/$t) + $b)*100000000 ;
  527. }else{
  528. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  529. }
  530. $data['created_at'] = date('Y-m-d H:i:s');
  531. $data['updated_at'] = date('Y-m-d H:i:s');
  532. $dream_id = DreamInfoModel::insertGetId($data);
  533. if ($dream_id) {
  534. // 梦想创建成功 关联关系中最新动态+1
  535. /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  536. foreach ($info as $item) {
  537. $item->dream_num += 1;
  538. $item->updated_at = date('Y-m-d H:i:s');
  539. $item->save();
  540. }*/
  541. $pics = $request->pics;
  542. $video = $request->video;
  543. if (!is_array($pics) && empty($pics) && empty($video)) {
  544. DreamInfoModel::destroy($dream_id);
  545. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  546. }
  547. $data = [];
  548. if (is_array($pics)) {
  549. foreach ($pics as $v){
  550. $data[] = [
  551. 'dream_id'=>$dream_id,
  552. 'pic' => $v,
  553. 'created_at' =>date('Y-m-d H:i:s'),
  554. 'updated_at' =>date('Y-m-d H:i:s'),
  555. ];
  556. }
  557. DreamImages::insert($data);
  558. }
  559. return $this->api('');
  560. }
  561. }
  562. }