DreamController.php 33 KB

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