DreamController.php 29 KB

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