DreamController.php 32 KB

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