DreamController.php 32 KB

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