DreamController.php 29 KB

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