DreamController.php 21 KB

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