DreamController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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)->with('comments')->orderBy('id','desc')->get();
  217. /*dd($interactions);
  218. foreach ($interactions as $item) {
  219. $item->comments;
  220. foreach ($item->comments as $comment) {
  221. $comment->pic = UserInfoModel::find($comment->user_id)->pic;
  222. $comment->replay;
  223. foreach ($comment->replay as $k1 => $v1){
  224. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  225. }
  226. }
  227. }*/
  228. if ($type == 'interaction') return $this->api($interactions);
  229. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  230. // $money = $user->money;
  231. $dream_info = DreamInfoModel::find($id);
  232. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  233. $a = count($setting) > 0 ? $setting->key : 0;
  234. $created_at = $dream_info ? $dream_info->created_at : 0;
  235. $b = intval((time()-strtotime($created_at))/60) ;
  236. $c = count($setting) > 0 ? $setting->value : 0;
  237. $number =round( -$a * $b + $c,1);
  238. if ($number <= 1) $number = 1;
  239. $dream = DreamInfoModel::with(['imgs','user'])->find($id);
  240. if (empty($dream)) return $this->error(ErrorCode::DREAM_NOT_EXIST);
  241. $dream->user_coin = $user->coin;
  242. $dream->topuser = $topuser;
  243. $dream->top3user = $top3user;
  244. $dream->interactions = $interactions;
  245. $dream->number = $number;
  246. $dream->is_collection = $is_collection;
  247. $dream->a = $a;
  248. $dream->c = $c;
  249. // 如果当前梦想用户就是当前登录用户就不显示关注按钮
  250. $show_button = $dream->user_id == $user->id ? '0' : '1';
  251. $dream->show_button = $show_button;
  252. // 梦想互动
  253. $interactions = InteractionInfo::where('dream_id',$id)->with(['comments' => function ($query) {
  254. $query->orderBy('created_at','desc');
  255. }])->orderBy('created_at','desc')->get();
  256. $dream->interactions = $interactions;
  257. // dd($interactions);
  258. return $this->api($dream);
  259. }
  260. /**
  261. * @api {post} /api/dream/support 支持梦想
  262. * @apiDescription 支持梦想
  263. * @apiGroup Dream
  264. * @apiPermission Passport
  265. * @apiVersion 0.1.0
  266. * @apiParam {int} coin 支持梦想币数量
  267. * @apiParam {int} id 梦想ID
  268. * @apiSuccessExample {json} Success-Response:
  269. * HTTP/1.1 200 OK
  270. {
  271. "status": true,
  272. "status_code": 0,
  273. "message": "",
  274. "data": {
  275. "id": 12,
  276. "user_id": 2,
  277. "name": "用户2梦想标题166",
  278. "about": "用户2梦想介绍666",
  279. "coin": 2500,
  280. "time": 21,
  281. "get_coin": 704,
  282. "status": 0,
  283. "video": null,
  284. "score": 158730,
  285. "mark": 158730,
  286. "sign": "",
  287. "created_at": "2017-06-25 13:10:56",
  288. "updated_at": "2017-06-27 18:16:02",
  289. "top3user": [ 支持者前三
  290. {
  291. "id": 1,
  292. "phone": "13880642880",
  293. "nickname": "name1",
  294. "avatar": "https://timgsa.bumb.224_0.jpeg",
  295. "birthday": "2000-06-21",
  296. "sign": 0,
  297. "money": 0,
  298. "coin": 596,
  299. "sex": 1,
  300. "signture": "我的个性签名1",
  301. "height": 170,
  302. "work": "网红",
  303. "emotion": 1,
  304. "address": "",
  305. "city": "上海",
  306. "detail_address": "",
  307. "status": 1,
  308. "wechat": "",
  309. "weibo": "",
  310. "remember_token": "",
  311. "created_at": "2017-06-25 10:27:08",
  312. "updated_at": "2017-06-27 18:16:02",
  313. "deleted_at": null,
  314. "score": 5062
  315. }
  316. ]
  317. "number": 4.8
  318. "is_collection" : 1 1已收藏 0未收藏
  319. }
  320. }
  321. * @apiErrorExample {json} Error-Response:
  322. *HTTP/1.1 400 Bad Request
  323. * {
  324. * "state": false,
  325. * "code": 1000,
  326. * "message": "传入参数不正确",
  327. * "data": null or []
  328. * }
  329. * 可能出现的代码
  330. * {
  331. * "status": false,
  332. * "status_code": 1303,
  333. * "message": "余额不足",
  334. * "data": null
  335. * }
  336. * {
  337. * "status": false,
  338. * "status_code": 1410,
  339. * "message": "支持已达上限",
  340. * "data": null
  341. * }
  342. *
  343. */
  344. public function support(Request $request)
  345. {
  346. $validator = \Validator::make($request->all(),
  347. [
  348. 'coin' => 'required',
  349. 'id' => 'required',
  350. ],
  351. [
  352. 'coin.required' => '梦想币不能为空',
  353. 'id.required' => '支持梦想id不能为空',
  354. ]
  355. );
  356. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  357. $user = $this->getUser();
  358. $dream_id = $request->id;
  359. $dream = DreamInfoModel::find($dream_id);
  360. $user_id = $dream->user_id;
  361. $coin = $request->coin;
  362. if ($dream->get_coin >= $dream->coin) {
  363. return $this->error(ErrorCode::SUP_TOP);
  364. } elseif($coin + $dream->get_coin > $dream->coin){
  365. $coin = $dream->coin - $dream->get_coin;
  366. }
  367. $user_care_dream = UserCareDream::where('user_id', $user->id)->where('dream_id', $dream_id)->first();
  368. $is_collection = empty($user_care_dream) ? 0 :1 ;
  369. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  370. $a = count($setting) > 0 ? $setting->key : '';
  371. $b = intval((time()-strtotime($dream->created_at))/60) ;
  372. $c = count($setting) > 0 ? $setting->value : '';
  373. //Todo 支持乘数目公式
  374. $number =round( -$a * $b + $c,1);
  375. if ($number <= 1) $number = 1;
  376. if ($user->coin < $coin) {
  377. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  378. }else{
  379. $user->coin -= $coin;
  380. $user->save();
  381. $dream->get_coin += $coin;
  382. $dream->mark += $coin*$number;
  383. $dream->save();
  384. // 梦想实现创建二维码
  385. if ($dream->coin == $dream->get_coin) {
  386. // 生成二维码
  387. $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  388. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  389. $code_url = env('APP_URL').'/user/meet?dream_id='.$dream_id;
  390. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  391. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  392. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  393. DreamInfoModel::where('id',$dream_id)->update(compact('code'));
  394. }
  395. $data = [
  396. 'user_id'=>$user->id,
  397. 'dream_id'=>$dream_id,
  398. 'to_user_id'=>$user_id,
  399. 'coin'=>$coin,
  400. 'score'=>$coin*$number,
  401. ];
  402. $ok = SupportDreamModel::create($data);
  403. if (!$ok) {
  404. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  405. }
  406. // 记录充值记录 新增系统消息
  407. $data = [
  408. 'from_id' =>$user->id,
  409. 'to_id' => $user_id,
  410. 'from_amount' => $coin,
  411. 'to_amount' => $coin,
  412. 'from_type' => AccountLog::TYPE_COIN,
  413. 'to_type' => AccountLog::TYPE_COIN,
  414. 'op' => AccountLog::OP_SUPPORT,
  415. ];
  416. AccountLog::create($data);
  417. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  418. $info = [
  419. 'user_id' => $user_id,
  420. 'message' => $message,
  421. ];
  422. SystemInfoModel::create($info);
  423. // 支持成功返回数据到当前页面 top3user
  424. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  425. $top = [] ;
  426. $top3user = []; // 支持用户排行前三
  427. foreach ($support_dream as $item) {
  428. if (!array_key_exists($item->user_id,$top)) {
  429. $top[$item->user_id] = $item->score;
  430. }else{
  431. $top[$item->user_id] += $item->score;
  432. }
  433. }
  434. arsort($top);
  435. foreach ($top as $user_id => $score){
  436. $user = UserInfoModel::find($user_id);
  437. $user->score = $score;
  438. if(count($top3user) <= 2) $top3user[] = $user ;
  439. }
  440. // 梦想分 mark
  441. $dream->top3user = $top3user;
  442. $dream->is_collection = $is_collection;
  443. $user_coin = $user->coin;
  444. return $this->api(compact('dream','user_coin'));
  445. }
  446. }
  447. // 收藏关注梦想
  448. /**
  449. * @api {get} /api/dream/collection 收藏梦想
  450. * @apiDescription 收藏梦想
  451. * @apiGroup Dream
  452. * @apiPermission Passport
  453. * @apiVersion 0.1.0
  454. * @apiParam {int} id 梦想ID
  455. * @apiParam {int} is_collection 0取消关注 1关注
  456. * @apiSuccessExample {json} Success-Response:
  457. * HTTP/1.1 200 OK
  458. *{
  459. * "status": true,
  460. * "status_code": 0,
  461. * "message": "",
  462. * "data": {
  463. * "is_collection": 1 1已关注 0 未关注
  464. * }
  465. *}
  466. * @apiErrorExample {json} Error-Response:
  467. *HTTP/1.1 400 Bad Request
  468. * {
  469. * "state": false,
  470. * "code": 1000,
  471. * "message": "传入参数不正确",
  472. * "data": null or []
  473. * }
  474. *
  475. */
  476. public function collection(Request $request)
  477. {
  478. $validator = \Validator::make($request->all(),
  479. [
  480. 'id' => 'required',
  481. 'is_collection' => 'required',
  482. ],
  483. [
  484. 'id.required' => '收藏梦想id不能为空',
  485. 'is_collection.required' => '关注值不存在',
  486. ]
  487. );
  488. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  489. $user = $this->getUser();
  490. if(!$dream = DreamInfoModel::find($request->id)){
  491. $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  492. }
  493. $data = [
  494. 'user_id' =>$user->id,
  495. 'dream_id' =>$request->id,
  496. 'dream_user_id' =>$dream->user_id,
  497. ];
  498. $is_collection = $request->is_collection;
  499. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  500. where('dream_id',$request->id)->first();
  501. if ($is_collection) {
  502. if (empty($user_care_dream)) {
  503. UserCareDream::create($data);
  504. }
  505. }else{
  506. UserCareDream::where('user_id',$user->id)->
  507. where('dream_id',$request->id)->delete();
  508. }
  509. // 并且同时会关注这个梦想者
  510. $array = [
  511. 'user_id' =>$user->id,
  512. 'other_user_id' =>$dream->user_id,
  513. ];
  514. $user_care_user = UserCareUser::where('user_id',$user->id)->
  515. where('other_user_id',$dream->user_id)->first();
  516. if (empty($user_care_user)) {
  517. UserCareUser::create($array);
  518. // 关注成功发送私信
  519. $message = BaseSettingsModel::where('category','message')->first();
  520. $message = empty($message) ? '' : $message->value;
  521. $info2 = [
  522. 'user_id'=>$request->id,
  523. 'to_user_id' => $dream->user_id,
  524. 'message' =>$message
  525. ];
  526. SystemInfoModel::create($info2);
  527. }
  528. return $this->api($is_collection);
  529. }
  530. /**
  531. * @api {get} /api/dream/search 梦想搜索
  532. * @apiDescription 梦想搜索)
  533. * @apiGroup Dream
  534. * @apiPermission none
  535. * @apiVersion 0.1.0
  536. * @apiParam {string} keyword 关键字
  537. * @apiSuccessExample {json} Success-Response:
  538. * HTTP/1.1 200 OK
  539. {
  540. "status": true,
  541. "status_code": 0,
  542. "message": "",
  543. "data": [
  544. {
  545. "id": 5,
  546. "user_id": 1,
  547. "name": "梦想标题1",
  548. "about": "梦想介绍",
  549. "coin": 2500,
  550. "time": 72000,
  551. "get_coin": 0,
  552. "mark": 0,
  553. "status": 0,
  554. "video": "url",
  555. "score": 100079365,
  556. "sign": "梦想达人",
  557. "created_at": "2017-06-25 12:45:22",
  558. "updated_at": "2017-06-28 15:50:41",
  559. "user": {},
  560. "imgs": []
  561. },
  562. ]
  563. }
  564. * @apiErrorExample {json} Error-Response:
  565. * HTTP/1.1 400 Bad Request
  566. */
  567. public function search(Request $request)
  568. {
  569. $user = $this->getUser();
  570. if (empty($request->keyword)) {
  571. return $this->api('');
  572. }
  573. $keyword ='%'.$request->keyword.'%';
  574. $data = DreamInfoModel::where('name','like',$keyword)->
  575. orWhere('sign','like',$keyword)->with(['user','img'])->get();
  576. foreach ($data as $k => $dream) {
  577. $info = UserCareDream::where('dream_id',$dream->id)->get();
  578. $dream->care_num = count($info);
  579. $dream->img = $dream->img?$dream->img->pic:'';
  580. }
  581. $this->insertSearchTable($user->id,$request->keyword);
  582. return $this->api($data);
  583. }
  584. public function insertSearchTable($id,$keyword)
  585. {
  586. $info = SearchInfoModel::where('user_id',$id)->
  587. where('search',trim($keyword))->first();
  588. if (count($info) == 0) {
  589. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  590. }else{
  591. $info->times += 1;
  592. $info->save();
  593. }
  594. }
  595. /**
  596. * @api {post} /api/dream/store 发布梦想
  597. * @apiDescription 发布梦想
  598. * @apiParam {string} pics 梦想图片 数组
  599. * @apiParam {string} video 梦想视频
  600. * @apiParam {string} name 梦想标题
  601. * @apiParam {string} about 梦想介绍
  602. * @apiParam {int} coin 梦想币
  603. * @apiParam {int} time 实现时间默认21*3600
  604. * @apiGroup Dream
  605. * @apiPermission Passport
  606. * @apiVersion 0.1.0
  607. * @apiSuccessExample {json} Success-Response:
  608. * {
  609. * "status": true,
  610. * "status_code": 0,
  611. * "message": "",
  612. * "data": ""
  613. *}
  614. * HTTP/1.1 200 OK
  615. * @apiErrorExample {json} Error-Response:
  616. * {
  617. * "status": false,
  618. * "status_code": 1203,
  619. * "message": "附件不存在",
  620. * "data": null
  621. * }
  622. * HTTP/1.1 400 Bad Request
  623. */
  624. public function store(Request $request)
  625. {
  626. $user = $this->getUser();
  627. $validator = \Validator::make($request->all(),
  628. [
  629. 'name' => 'required',
  630. 'about' => 'required',
  631. 'coin' => 'required|integer',
  632. 'days' => 'required|integer',
  633. 'pics' => 'required|array',
  634. ],
  635. [
  636. 'name.required' => '梦想标题必填',
  637. 'about.required' => '梦想介绍必填',
  638. 'coin.required' => '梦想币必填',
  639. 'coin.integer' => '梦想币必须是正整数',
  640. 'days.required' => '实现时间必填',
  641. 'days.integer' => '实现时间必须是正整数',
  642. 'pics.required' => '至少上传一张图片',
  643. 'pics.array' => '至少上传一张图片数组',
  644. ]
  645. );
  646. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  647. \Log::debug($user);
  648. $data2 = $user->myCareNum;
  649. $care_num = count($data2);
  650. $setting = BaseSettingsModel::where('category','paihang')->first();
  651. $a = $setting?$setting->key:1;
  652. $b = $setting?$setting->value:1;
  653. $t = 21*3600 / 60;
  654. $days = $request->days;
  655. $data = $request->except('_token','pics','days');
  656. $data['user_id'] = $user->id;
  657. $data['end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' days'));
  658. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  659. if ($care_num == 0) {
  660. $data['score'] = (($a/$t) + $b)*100000000 ;
  661. }else{
  662. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  663. }
  664. $data['created_at'] = date('Y-m-d H:i:s');
  665. $data['updated_at'] = date('Y-m-d H:i:s');
  666. $dream_id = DreamInfoModel::insertGetId($data);
  667. if ($dream_id) {
  668. // 梦想创建成功 关联关系中最新动态+1
  669. UserCareUser::where('other_user_id',$user->id)->increment('dream_number',1);
  670. $pics = $request->pics;
  671. $video = $request->video;
  672. if (!is_array($pics) && empty($pics) && empty($video)) {
  673. DreamInfoModel::destroy($dream_id);
  674. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  675. }
  676. $data = [];
  677. if (is_array($pics)) {
  678. foreach ($pics as $v){
  679. $data[] = [
  680. 'dream_id'=>$dream_id,
  681. 'pic' => $v,
  682. 'created_at' =>date('Y-m-d H:i:s'),
  683. 'updated_at' =>date('Y-m-d H:i:s'),
  684. ];
  685. }
  686. DreamImages::insert($data);
  687. }
  688. // 梦想创建成功后所有关注我的梦想的人也会收到我最新梦想发布的消息
  689. $ids = UserCareDream::where('dream_user_id',$user->id)->select('user_id')->get()->toArray();
  690. $id_arrs = array_column($ids,'user_id');
  691. if (!empty($id_arrs)) {
  692. $arr = [] ;
  693. foreach ($id_arrs as $id){
  694. $arr[] = [
  695. 'to_user_id' => $id,
  696. 'user_id' => $user->id,
  697. 'message' => '关注的梦想有新的梦想动态'.$dream_id,
  698. ];
  699. }
  700. SystemInfoModel::insert($arr);
  701. }
  702. return $this->api('');
  703. }
  704. }
  705. /**
  706. * @api {get} /api/dream/share 分享
  707. * @apiDescription 分享
  708. * @apiParam {int} id 梦想id
  709. * @apiGroup Dream
  710. * @apiPermission Passport
  711. * @apiVersion 0.1.0
  712. */
  713. // 分享梦想
  714. public function share(Request $request)
  715. {
  716. $validator = \Validator::make($request->all(),
  717. [
  718. 'id' => 'required',
  719. ],
  720. [
  721. 'id.required' => '梦想id不存在',
  722. ]
  723. );
  724. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  725. $url = env('APP_URL').'/api/dream/show?id='.$request->id;
  726. $dream = DreamInfoModel::with('img')->find($request->id);
  727. $img = !empty($dream->img) ? $dream->img->pic : '';
  728. $html =
  729. "<div style='text-align: center'>
  730. <a href='$url'>
  731. <h1>$dream->name</h1>
  732. <img src=\"$img\" style='width: 20px;height: 20px;' alt=''>
  733. </a>
  734. </div>";
  735. return $html;
  736. }
  737. }