DreamController.php 30 KB

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