DreamController.php 36 KB

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