DreamController.php 20 KB

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