DreamController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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\UserCareUser;
  13. use App\Models\UserInfoModel;
  14. use Illuminate\Http\Request;
  15. use App\Services\Base\ErrorCode;
  16. class DreamController extends Controller
  17. {
  18. /**
  19. * @api {get} /api/dream/show 梦想详情
  20. * @apiDescription 梦想详情
  21. * @apiGroup Dream
  22. * @apiPermission Passport
  23. * @apiVersion 0.1.0
  24. * @apiParam {int} [type=home] home interaction paihang 默认all
  25. * @apiParam {int} id 梦想ID
  26. * @apiSuccessExample {json} Success-Response:
  27. * HTTP/1.1 200 OK
  28. * 主页
  29. {
  30. "status": true,
  31. "status_code": 0,
  32. "message": "",
  33. "data": {
  34. "id": 4,
  35. "user_id": 1,
  36. "a": 1, //支持乘数系数 后台配置
  37. "c": 1, //支持乘数系数 后台配置
  38. "name": "梦想标题1",
  39. "about": "梦想介绍",
  40. "coin": 2500,
  41. "time": 21,
  42. "get_coin": 600,
  43. "status": 0,
  44. "video": null,
  45. "score": 100079365,
  46. "sign": "",
  47. "created_at": "2017-06-25 11:47:45",
  48. "updated_at": "2017-06-25 15:10:50",
  49. "is_collection" : 1 1已收藏 0未收藏
  50. "show_button" : 1 1关注按钮显示 0不显示
  51. "top3user": [ 支持者前三
  52. {
  53. "id": 2,
  54. "phone": "13880642881",
  55. "nickname": "name2",
  56. "avatar": "https://timgsa.baidu.com/timg?imamb.224_0.jpeg",
  57. "score": 2399
  58. }
  59. ],
  60. "interactions": [ 动态
  61. {
  62. "id": 1,
  63. "dream_id": 11,
  64. "title": "用户2 梦想ID11的动态1",
  65. "pic1": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498974141&di=114258609665a88d6aed7c38fe65c1a2&imgtype=jpg&er=1&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201507%2F14%2F20150714130925_kzrcM.thumb.224_0.jpeg",
  66. "pic2": "",
  67. "pic3": "",
  68. "pic4": "",
  69. "pic5": "",
  70. "pic6": "",
  71. "pic7": "",
  72. "pic8": "",
  73. "pic9": "",
  74. "status": 1,
  75. "created_at": "2017-06-25 13:41:18",
  76. "updated_at": "2017-06-25 13:41:18",
  77. "deleted_at": null,
  78. "comments": [ 评论
  79. {
  80. "interaction_id": 1,
  81. "user_id": 2,
  82. "user_avatar": "",
  83. "user_nickname": "",
  84. "to_user_id": 0,
  85. "to_user_avatar": "",
  86. "to_user_nickname": "",
  87. "level": 0,
  88. "content": "09",
  89. "is_read": 0,
  90. "updated_at": "2017-06-30 18:15:57",
  91. "reply_time": null
  92. },
  93. {
  94. "interaction_id": 1,
  95. "user_id": 1,
  96. "user_avatar": "",
  97. "user_nickname": "",
  98. "to_user_id": 2,
  99. "to_user_avatar": "",
  100. "to_user_nickname": "",
  101. "level": 0,
  102. "content": "08",
  103. "is_read": 1,
  104. "updated_at": "2017-08-16 16:08:04",
  105. "reply_time": null
  106. },
  107. {
  108. "interaction_id": 1,
  109. "user_id": 1,
  110. "user_avatar": "",
  111. "user_nickname": "",
  112. "to_user_id": 3,
  113. "to_user_avatar": "",
  114. "to_user_nickname": "",
  115. "level": 0,
  116. "content": "07",
  117. "is_read": 1,
  118. "updated_at": "2017-08-16 16:08:04",
  119. "reply_time": null
  120. },
  121. {
  122. "interaction_id": 1,
  123. "user_id": 1,
  124. "user_avatar": "",
  125. "user_nickname": "",
  126. "to_user_id": 0,
  127. "to_user_avatar": "",
  128. "to_user_nickname": "",
  129. "level": 0,
  130. "content": "1118888",
  131. "is_read": 1,
  132. "updated_at": "2017-08-16 16:08:04",
  133. "reply_time": null
  134. }
  135. ]
  136. }
  137. ],
  138. "number": 1 支持乘数
  139. "imgs": [ 梦想图片
  140. {
  141. "title": "",
  142. "pic": "https://timgsa.ba0170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  143. },
  144. {
  145. "title": "",
  146. "pic": "https://timgsa.baid524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  147. },
  148. {
  149. "title": "",
  150. "pic": "https://timgsa20170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  151. }
  152. ],
  153. "user": { 梦想用户
  154. "id": 1,
  155. "phone": "13880642880",
  156. "nickname": "name1",
  157. "avatar": "https://timgsumb.224_0.jpeg",
  158. "birthday": "2000-06-21",
  159. "sign": 0,
  160. "money": 0,
  161. "coin": 1300,
  162. "sex": 1,
  163. "signture": "",
  164. "height": 170,
  165. "work": "",
  166. "emotion": 1,
  167. "address": "",
  168. "city": "",
  169. "detail_address": "",
  170. "status": 1,
  171. "wechat": "",
  172. "weibo": "",
  173. "remember_token": "",
  174. "created_at": "2017-06-25 10:27:08",
  175. "updated_at": "2017-06-25 15:20:11",
  176. "deleted_at": null
  177. }
  178. }
  179. }
  180. * @apiErrorExample {json} Error-Response:
  181. *HTTP/1.1 400 Bad Request
  182. *{
  183. * "status": false,
  184. * "status_code": 1105,
  185. * "message": "用户不存在",
  186. * "data": null
  187. * }
  188. */
  189. public function show(Request $request)
  190. {
  191. $user = $this->getUser();
  192. $type = $request->type; //类型
  193. $id = $request->id; //梦想ID
  194. $user_care_dream = UserCareDream::where('user_id',$user->id)->where('dream_id',$id)->first();
  195. $is_collection = empty($user_care_dream) ? 0 :1 ;
  196. if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
  197. $support_dream = SupportDreamModel::where('dream_id',$id)->get();
  198. $top = [] ;
  199. $topuser = []; //所有支持用户排行
  200. $top3user = []; // 支持用户排行前三
  201. foreach ($support_dream as $item) {
  202. if (!array_key_exists($item->user_id,$top)) {
  203. $top[$item->user_id] = $item->score;
  204. }else{
  205. $top[$item->user_id] += $item->score;
  206. }
  207. }
  208. arsort($top);
  209. foreach ($top as $user_id => $score){
  210. $user = UserInfoModel::find($user_id);
  211. $user->score = $score;
  212. $topuser[] = $user;
  213. if(count($top3user) <= 2) $top3user[] = $user ;
  214. }
  215. if ($type == 'paihang') return $this->api($topuser);
  216. $interactions = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get();
  217. foreach ($interactions as $item) {
  218. $item->comments;
  219. foreach ($item->comments as $comment) {
  220. $comment->pic = UserInfoModel::find($comment->user_id)->pic;
  221. $comment->replay;
  222. foreach ($comment->replay as $k1 => $v1){
  223. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  224. }
  225. }
  226. }
  227. if ($type == 'interaction') return $this->api($interactions);
  228. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  229. // $money = $user->money;
  230. $dream_info = DreamInfoModel::find($id);
  231. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  232. $a = count($setting) > 0 ? $setting->key : 0;
  233. $created_at = $dream_info ? $dream_info->created_at : 0;
  234. $b = intval((time()-strtotime($created_at))/60) ;
  235. $c = count($setting) > 0 ? $setting->value : 0;
  236. $number =round( -$a * $b + $c,1);
  237. if ($number <= 1) $number = 1;
  238. $dream = DreamInfoModel::with(['imgs','user'])->find($id);
  239. if (empty($dream)) return $this->error(ErrorCode::DREAM_NOT_EXIST);
  240. $dream->user_coin = $user->coin;
  241. $dream->topuser = $topuser;
  242. $dream->top3user = $top3user;
  243. $dream->interactions = $interactions;
  244. $dream->number = $number;
  245. $dream->is_collection = $is_collection;
  246. $dream->a = $a;
  247. $dream->c = $c;
  248. // 如果当前梦想用户就是当前登录用户就不显示关注按钮
  249. $show_button = $dream->user_id == $user->id ? '0' : '1';
  250. $dream->show_button = $show_button;
  251. // 梦想互动
  252. $interactions = InteractionInfo::where('dream_id',$id)->with(['comments' => function ($query) {
  253. $query->orderBy('created_at','desc');
  254. }])->orderBy('created_at','desc')->get();
  255. $dream->interactions = $interactions;
  256. // dd($interactions);
  257. return $this->api($dream);
  258. }
  259. /**
  260. * @api {post} /api/dream/support 支持梦想
  261. * @apiDescription 支持梦想
  262. * @apiGroup Dream
  263. * @apiPermission Passport
  264. * @apiVersion 0.1.0
  265. * @apiParam {int} coin 支持梦想币数量
  266. * @apiParam {int} id 梦想ID
  267. * @apiSuccessExample {json} Success-Response:
  268. * HTTP/1.1 200 OK
  269. {
  270. "status": true,
  271. "status_code": 0,
  272. "message": "",
  273. "data": {
  274. "id": 12,
  275. "user_id": 2,
  276. "name": "用户2梦想标题166",
  277. "about": "用户2梦想介绍666",
  278. "coin": 2500,
  279. "time": 21,
  280. "get_coin": 704,
  281. "status": 0,
  282. "video": null,
  283. "score": 158730,
  284. "mark": 158730,
  285. "sign": "",
  286. "created_at": "2017-06-25 13:10:56",
  287. "updated_at": "2017-06-27 18:16:02",
  288. "top3user": [ 支持者前三
  289. {
  290. "id": 1,
  291. "phone": "13880642880",
  292. "nickname": "name1",
  293. "avatar": "https://timgsa.bumb.224_0.jpeg",
  294. "birthday": "2000-06-21",
  295. "sign": 0,
  296. "money": 0,
  297. "coin": 596,
  298. "sex": 1,
  299. "signture": "我的个性签名1",
  300. "height": 170,
  301. "work": "网红",
  302. "emotion": 1,
  303. "address": "",
  304. "city": "上海",
  305. "detail_address": "",
  306. "status": 1,
  307. "wechat": "",
  308. "weibo": "",
  309. "remember_token": "",
  310. "created_at": "2017-06-25 10:27:08",
  311. "updated_at": "2017-06-27 18:16:02",
  312. "deleted_at": null,
  313. "score": 5062
  314. }
  315. ]
  316. "number": 4.8
  317. "is_collection" : 1 1已收藏 0未收藏
  318. }
  319. }
  320. * @apiErrorExample {json} Error-Response:
  321. *HTTP/1.1 400 Bad Request
  322. * {
  323. * "state": false,
  324. * "code": 1000,
  325. * "message": "传入参数不正确",
  326. * "data": null or []
  327. * }
  328. * 可能出现的代码
  329. * {
  330. * "status": false,
  331. * "status_code": 1303,
  332. * "message": "余额不足",
  333. * "data": null
  334. * }
  335. * {
  336. * "status": false,
  337. * "status_code": 1410,
  338. * "message": "支持已达上限",
  339. * "data": null
  340. * }
  341. *
  342. */
  343. public function support(Request $request)
  344. {
  345. $validator = \Validator::make($request->all(),
  346. [
  347. 'coin' => 'required',
  348. 'id' => 'required',
  349. ],
  350. [
  351. 'coin.required' => '梦想币不能为空',
  352. 'id.required' => '支持梦想id不能为空',
  353. ]
  354. );
  355. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  356. $user = $this->getUser();
  357. $dream_id = $request->id;
  358. $dream = DreamInfoModel::find($dream_id);
  359. $user_id = $dream->user_id;
  360. $coin = $request->coin;
  361. if ($dream->get_coin >= $dream->coin) {
  362. return $this->error(ErrorCode::SUP_TOP);
  363. } elseif($coin + $dream->get_coin > $dream->coin){
  364. $coin = $dream->coin - $dream->get_coin;
  365. }
  366. $user_care_dream = UserCareDream::where('user_id', $user->id)->where('dream_id', $dream_id)->first();
  367. $is_collection = empty($user_care_dream) ? 0 :1 ;
  368. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  369. $a = count($setting) > 0 ? $setting->key : '';
  370. $b = intval((time()-strtotime($dream->created_at))/60) ;
  371. $c = count($setting) > 0 ? $setting->value : '';
  372. //Todo 支持乘数目公式
  373. $number =round( -$a * $b + $c,1);
  374. if ($number <= 1) $number = 1;
  375. if ($user->coin < $coin) {
  376. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  377. }else{
  378. $user->coin -= $coin;
  379. $user->save();
  380. $dream->get_coin += $coin;
  381. $dream->mark += $coin*$number;
  382. $dream->save();
  383. // 梦想实现创建二维码
  384. if ($dream->coin == $dream->get_coin) {
  385. // 生成二维码
  386. $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  387. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  388. $code_url = env('APP_URL').'/user/meet?dream_id='.$dream_id;
  389. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  390. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  391. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  392. DreamInfoModel::where('id',$dream_id)->update(compact('code'));
  393. }
  394. $data = [
  395. 'user_id'=>$user->id,
  396. 'dream_id'=>$dream_id,
  397. 'to_user_id'=>$user_id,
  398. 'coin'=>$coin,
  399. 'score'=>$coin*$number,
  400. ];
  401. $ok = SupportDreamModel::create($data);
  402. if (!$ok) {
  403. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  404. }
  405. // 记录充值记录 新增系统消息
  406. $data = [
  407. 'from_id' =>$user->id,
  408. 'to_id' => $user_id,
  409. 'from_amount' => $coin,
  410. 'to_amount' => $coin,
  411. 'from_type' => AccountLog::TYPE_COIN,
  412. 'to_type' => AccountLog::TYPE_COIN,
  413. 'op' => AccountLog::OP_SUPPORT,
  414. ];
  415. AccountLog::create($data);
  416. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  417. $info = [
  418. 'user_id' => $user_id,
  419. 'message' => $message,
  420. ];
  421. SystemInfoModel::create($info);
  422. // 支持成功返回数据到当前页面 top3user
  423. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  424. $top = [] ;
  425. $top3user = []; // 支持用户排行前三
  426. foreach ($support_dream as $item) {
  427. if (!array_key_exists($item->user_id,$top)) {
  428. $top[$item->user_id] = $item->score;
  429. }else{
  430. $top[$item->user_id] += $item->score;
  431. }
  432. }
  433. arsort($top);
  434. foreach ($top as $user_id => $score){
  435. $user = UserInfoModel::find($user_id);
  436. $user->score = $score;
  437. if(count($top3user) <= 2) $top3user[] = $user ;
  438. }
  439. // 梦想分 mark
  440. $dream->top3user = $top3user;
  441. $dream->is_collection = $is_collection;
  442. $user_coin = $user->coin;
  443. return $this->api(compact('dream','user_coin'));
  444. }
  445. }
  446. // 收藏关注梦想
  447. /**
  448. * @api {get} /api/dream/collection 收藏梦想
  449. * @apiDescription 收藏梦想
  450. * @apiGroup Dream
  451. * @apiPermission Passport
  452. * @apiVersion 0.1.0
  453. * @apiParam {int} id 梦想ID
  454. * @apiParam {int} is_collection 0取消关注 1关注
  455. * @apiSuccessExample {json} Success-Response:
  456. * HTTP/1.1 200 OK
  457. *{
  458. * "status": true,
  459. * "status_code": 0,
  460. * "message": "",
  461. * "data": {
  462. * "is_collection": 1 1已关注 0 未关注
  463. * }
  464. *}
  465. * @apiErrorExample {json} Error-Response:
  466. *HTTP/1.1 400 Bad Request
  467. * {
  468. * "state": false,
  469. * "code": 1000,
  470. * "message": "传入参数不正确",
  471. * "data": null or []
  472. * }
  473. *
  474. */
  475. public function collection(Request $request)
  476. {
  477. $validator = \Validator::make($request->all(),
  478. [
  479. 'id' => 'required',
  480. 'is_collection' => 'required',
  481. ],
  482. [
  483. 'id.required' => '收藏梦想id不能为空',
  484. 'is_collection.required' => '关注值不存在',
  485. ]
  486. );
  487. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  488. $user = $this->getUser();
  489. if(!$dream = DreamInfoModel::find($request->id)){
  490. $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  491. }
  492. $data = [
  493. 'user_id' =>$user->id,
  494. 'dream_id' =>$request->id,
  495. 'dream_user_id' =>$dream->user_id,
  496. ];
  497. $is_collection = $request->is_collection;
  498. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  499. where('dream_id',$request->id)->first();
  500. if ($is_collection) {
  501. if (empty($user_care_dream)) {
  502. UserCareDream::create($data);
  503. }
  504. }else{
  505. UserCareDream::where('user_id',$user->id)->
  506. where('dream_id',$request->id)->delete();
  507. }
  508. // 并且同时会关注这个梦想者
  509. $array = [
  510. 'user_id' =>$user->id,
  511. 'other_user_id' =>$dream->user_id,
  512. ];
  513. $user_care_user = UserCareUser::where('user_id',$user->id)->
  514. where('other_user_id',$dream->user_id)->first();
  515. if (empty($user_care_user)) {
  516. UserCareUser::create($array);
  517. // 关注成功发送私信
  518. $message = BaseSettingsModel::where('category','message')->first();
  519. $message = empty($message) ? '' : $message->value;
  520. $info2 = [
  521. 'user_id'=>$request->id,
  522. 'to_user_id' => $dream->user_id,
  523. 'message' =>$message
  524. ];
  525. SystemInfoModel::create($info2);
  526. }
  527. return $this->api($is_collection);
  528. }
  529. /**
  530. * @api {get} /api/dream/search 梦想搜索
  531. * @apiDescription 梦想搜索)
  532. * @apiGroup Dream
  533. * @apiPermission none
  534. * @apiVersion 0.1.0
  535. * @apiParam {string} keyword 关键字
  536. * @apiSuccessExample {json} Success-Response:
  537. * HTTP/1.1 200 OK
  538. {
  539. "status": true,
  540. "status_code": 0,
  541. "message": "",
  542. "data": [
  543. {
  544. "id": 5,
  545. "user_id": 1,
  546. "name": "梦想标题1",
  547. "about": "梦想介绍",
  548. "coin": 2500,
  549. "time": 72000,
  550. "get_coin": 0,
  551. "mark": 0,
  552. "status": 0,
  553. "video": "url",
  554. "score": 100079365,
  555. "sign": "梦想达人",
  556. "created_at": "2017-06-25 12:45:22",
  557. "updated_at": "2017-06-28 15:50:41",
  558. "user": {},
  559. "imgs": []
  560. },
  561. ]
  562. }
  563. * @apiErrorExample {json} Error-Response:
  564. * HTTP/1.1 400 Bad Request
  565. */
  566. public function search(Request $request)
  567. {
  568. $user = $this->getUser();
  569. if (empty($request->keyword)) {
  570. return $this->api('');
  571. }
  572. $keyword ='%'.$request->keyword.'%';
  573. $data = DreamInfoModel::where('name','like',$keyword)->
  574. orWhere('sign','like',$keyword)->with(['user','img'])->get();
  575. foreach ($data as $k => $dream) {
  576. $info = UserCareDream::where('dream_id',$dream->id)->get();
  577. $dream->care_num = count($info);
  578. $dream->img = $dream->img?$dream->img->pic:'';
  579. }
  580. $this->insertSearchTable($user->id,$request->keyword);
  581. return $this->api($data);
  582. }
  583. public function insertSearchTable($id,$keyword)
  584. {
  585. $info = SearchInfoModel::where('user_id',$id)->
  586. where('search',trim($keyword))->first();
  587. if (count($info) == 0) {
  588. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  589. }else{
  590. $info->times += 1;
  591. $info->save();
  592. }
  593. }
  594. /**
  595. * @api {post} /api/dream/store 发布梦想
  596. * @apiDescription 发布梦想
  597. * @apiParam {string} pics 梦想图片 数组
  598. * @apiParam {string} video 梦想视频
  599. * @apiParam {string} name 梦想标题
  600. * @apiParam {string} about 梦想介绍
  601. * @apiParam {int} coin 梦想币
  602. * @apiParam {int} time 实现时间默认21*3600
  603. * @apiGroup Dream
  604. * @apiPermission Passport
  605. * @apiVersion 0.1.0
  606. * @apiSuccessExample {json} Success-Response:
  607. * {
  608. * "status": true,
  609. * "status_code": 0,
  610. * "message": "",
  611. * "data": ""
  612. *}
  613. * HTTP/1.1 200 OK
  614. * @apiErrorExample {json} Error-Response:
  615. * {
  616. * "status": false,
  617. * "status_code": 1203,
  618. * "message": "附件不存在",
  619. * "data": null
  620. * }
  621. * HTTP/1.1 400 Bad Request
  622. */
  623. public function store(Request $request)
  624. {
  625. $user = $this->getUser();
  626. $validator = \Validator::make($request->all(),
  627. [
  628. 'name' => 'required',
  629. 'about' => 'required',
  630. 'coin' => 'required|integer',
  631. 'days' => 'required|integer',
  632. 'pics' => 'required|array',
  633. ],
  634. [
  635. 'name.required' => '梦想标题必填',
  636. 'about.required' => '梦想介绍必填',
  637. 'coin.required' => '梦想币必填',
  638. 'coin.integer' => '梦想币必须是正整数',
  639. 'days.required' => '实现时间必填',
  640. 'days.integer' => '实现时间必须是正整数',
  641. 'pics.required' => '至少上传一张图片',
  642. 'pics.array' => '至少上传一张图片数组',
  643. ]
  644. );
  645. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  646. \Log::debug($user);
  647. $data2 = $user->myCareNum;
  648. $care_num = count($data2);
  649. $setting = BaseSettingsModel::where('category','paihang')->first();
  650. $a = $setting?$setting->key:1;
  651. $b = $setting?$setting->value:1;
  652. $t = 21*3600 / 60;
  653. $days = $request->days;
  654. $data = $request->except('_token','pics','days');
  655. $data['user_id'] = $user->id;
  656. $data['end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' days'));
  657. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  658. if ($care_num == 0) {
  659. $data['score'] = (($a/$t) + $b)*100000000 ;
  660. }else{
  661. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  662. }
  663. $data['created_at'] = date('Y-m-d H:i:s');
  664. $data['updated_at'] = date('Y-m-d H:i:s');
  665. $dream_id = DreamInfoModel::insertGetId($data);
  666. if ($dream_id) {
  667. // 梦想创建成功 关联关系中最新动态+1
  668. /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  669. foreach ($info as $item) {
  670. $item->dream_num += 1;
  671. $item->updated_at = date('Y-m-d H:i:s');
  672. $item->save();
  673. }*/
  674. $pics = $request->pics;
  675. $video = $request->video;
  676. if (!is_array($pics) && empty($pics) && empty($video)) {
  677. DreamInfoModel::destroy($dream_id);
  678. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  679. }
  680. $data = [];
  681. if (is_array($pics)) {
  682. foreach ($pics as $v){
  683. $data[] = [
  684. 'dream_id'=>$dream_id,
  685. 'pic' => $v,
  686. 'created_at' =>date('Y-m-d H:i:s'),
  687. 'updated_at' =>date('Y-m-d H:i:s'),
  688. ];
  689. }
  690. DreamImages::insert($data);
  691. }
  692. // 梦想创建成功后所有关注我的梦想的人也会收到我最新梦想发布的消息
  693. $ids = UserCareDream::where('dream_user_id',$user->id)->select('user_id')->get()->toArray();
  694. $id_arrs = array_column($ids,'user_id');
  695. if (!empty($id_arrs)) {
  696. $arr = [] ;
  697. foreach ($id_arrs as $id){
  698. $arr[] = [
  699. 'to_user_id' => $id,
  700. 'user_id' => $user->id,
  701. 'message' => '关注的梦想有新的梦想动态'.$dream_id,
  702. ];
  703. }
  704. SystemInfoModel::insert($arr);
  705. }
  706. return $this->api('');
  707. }
  708. }
  709. }