DreamController.php 35 KB

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