DreamController.php 20 KB

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