DreamController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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)->whereNotIn('to_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)->whereNotIn('to_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. //收藏梦想
  435. if (empty($user_care_dream)) {
  436. $data = [
  437. 'user_id' =>$user->id,
  438. 'dream_id' =>$dream_id,
  439. 'dream_user_id' =>$dream->user_id,
  440. ];
  441. UserCareDream::create($data);
  442. // 关注成功发送私信
  443. $message = BaseSettingsModel::where('category','message')->first();
  444. $message = empty($message) ? ""."<span style='color: #00c3da'>[$user->nickname]</span> 收藏了你的梦想《".$dream->name.'》' : $message->value;
  445. // $this->jPush($message,'',$dream->user_id,1,$dream->id);
  446. $info2 = [
  447. 'to_user_id' => $dream->user_id,
  448. 'message' => $message,
  449. 'dream_id' => $request->id,
  450. 'user_id' => $user->id,
  451. 'is_reply' => 1,
  452. 'type_id' => 1,
  453. 'attr_id' => 2,
  454. ];
  455. SystemInfoModel::firstOrCreate($info2);
  456. // 长连接
  457. $mess = $user->nickname."收藏了你的梦想《".$dream->name.">";
  458. // $this->jPush($mess,'',$dream->user_id,1,$dream->id);
  459. }
  460. $setting = BaseSettingsModel::where('category','score')->select('key','value','sort')->first();
  461. $a = count($setting) > 0 ? $setting->key : 0;
  462. $b = ceil((time()-strtotime($dream->created_at))/60); //梦想发布分钟数
  463. $c = count($setting) > 0 ? $setting->value : 0;
  464. if(empty($c)) $c = 3;
  465. $n = count($setting) > 0 ? $setting->sort : 0;
  466. //Todo 支持乘数目公式 pow(x,y)
  467. // exp(x) $number = N*exp(-a*(pow(t,1/b)))+1
  468. $number =round( $n*exp(-$a*(pow($b,1/$c)))+1,2);
  469. if ($number <= 1) $number = 1;
  470. if ($user->coin < $coin) {
  471. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  472. }else{
  473. //支持成功之前
  474. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  475. $top = [] ;
  476. foreach ($support_dream as $item) {
  477. if (!array_key_exists($item->user_id,$top)) {
  478. $top[$item->user_id] = $item->coin;
  479. }else{
  480. $top[$item->user_id] += $item->coin;
  481. }
  482. }
  483. arsort($top);
  484. $new_arr = array_values($top);
  485. if (!empty($new_arr)) {
  486. $old_top_user_id = array_search($new_arr[0],$top);
  487. }else{
  488. $old_top_user_id = 0;
  489. }
  490. /* dd($user->id);
  491. dd($old_top_user_id);*/
  492. $user->coin -= $coin;
  493. $user->save();
  494. $dream->get_coin += $coin;
  495. $dream->mark += $coin*$number;
  496. $dream->save();
  497. $to_user = $dream->user;
  498. $user_coin = $user->coin;
  499. // 支持成功记录交易记录
  500. if (!empty($to_user)) {
  501. $arr = [
  502. 'from_type'=>'梦想币',
  503. 'from_id'=>$user->id,
  504. 'from_name'=>$user->nickname,
  505. 'op'=>'支持-'.$dream->name,
  506. 'from_amount'=>$coin,
  507. 'to_type'=>'梦想币',
  508. // 'to_id'=>$dream->user_id,
  509. 'to_id'=>'',
  510. 'to_name'=>$to_user->nickname,
  511. 'to_amount'=>$coin,
  512. 'channel'=>'平台内',
  513. 'transaction_id'=>date('YmdHis') . mt_rand(1000, 9999),
  514. 'avatar'=>empty($dream->img) ? '':$dream->img->pic,
  515. ];
  516. AccountLog::create($arr);
  517. }
  518. // 梦想实现创建二维码
  519. if ($dream->coin >= $dream->get_coin && empty($dream->code)) {
  520. // 生成二维码
  521. $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  522. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  523. $code_url = env('APP_URL').'/api/user/meet?dream_id='.$dream_id;
  524. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  525. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  526. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  527. DreamInfoModel::where('id',$dream_id)->update(compact('code'));
  528. $message = '你的梦想《'.$dream->name.'》已结束,点击了解接下来的步骤 ';
  529. $info = [
  530. 'user_id' => $dream->user_id,
  531. 'message' => $message,
  532. ];
  533. SystemInfoModel::create($info);
  534. // 长连接
  535. // $this->jPush($message,'',$dream->user_id,1,$dream->id);
  536. }
  537. $data = [
  538. 'user_id'=>$user->id,
  539. 'dream_id'=>$dream_id,
  540. 'to_user_id'=>$user_id,
  541. 'coin'=>$coin,
  542. 'score'=>$coin*$number,
  543. ];
  544. $ok = SupportDreamModel::create($data);
  545. // 在支持梦想时 如果梦主排名配取代时发送系统消息
  546. $this->systemInfo($dream_id,$user->id,$old_top_user_id);
  547. if (!$ok) {
  548. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  549. }
  550. // ""."<span style='color: #00c3da'>[UserInfoModel::find($user->id)->nickname]</span> 为你《"
  551. $nickname = UserInfoModel::find($user->id)->nickname;
  552. $message =""."<span style='color: #00c3da'>[$nickname]</span> 为你《".$dream->name.'》支持了'.$coin.'梦想币';
  553. $info = [
  554. 'to_user_id' => $user_id,
  555. 'user_id'=>$user->id,
  556. 'message' => $message,
  557. 'is_reply' => 1,
  558. 'type_id' => 1,
  559. 'attr_id' => 1,
  560. ];
  561. SystemInfoModel::create($info);
  562. // 长连接
  563. $mess ="{$nickname}为你《".$dream->name.'》支持了'.$coin.'梦想币';
  564. $this->jPush($mess,'',$user_id,1,$dream->id);
  565. // 支持成功返回数据到当前页面 top3user
  566. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  567. $top = [] ;
  568. $top3user = []; // 支持用户排行前三
  569. foreach ($support_dream as $item) {
  570. if (!array_key_exists($item->user_id,$top)) {
  571. $top[$item->user_id] = $item->score;
  572. }else{
  573. $top[$item->user_id] += $item->score;
  574. }
  575. }
  576. arsort($top);
  577. foreach ($top as $user_id => $score){
  578. $user = UserInfoModel::find($user_id);
  579. if (!empty($user)) {
  580. $user->score = $score;
  581. if(count($top3user) <= 2) $top3user[] = $user ;
  582. }
  583. }
  584. // 梦想分 mark
  585. $dream->top3user = $top3user;
  586. $dream->is_collection = $is_collection;
  587. return $this->api(compact('dream','user_coin','number'));
  588. }
  589. }
  590. public function systemInfo($dream_id,$user_id,$old_top_user_id)
  591. {
  592. $user = UserInfoModel::find($user_id);
  593. $dream = DreamInfoModel::find($dream_id);
  594. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  595. $top = [] ;
  596. foreach ($support_dream as $item) {
  597. if (!array_key_exists($item->user_id,$top)) {
  598. $top[$item->user_id] = $item->coin;
  599. }else{
  600. $top[$item->user_id] += $item->coin;
  601. }
  602. }
  603. arsort($top);
  604. $new_arr = array_values($top);
  605. if (!empty($new_arr)) {
  606. $top_user_id = array_search($new_arr[0],$top);
  607. }else{
  608. $top_user_id = $old_top_user_id;
  609. }
  610. if ($top_user_id != $old_top_user_id) { //新的最大支持者不是以前的最大支持者时
  611. $arr = [
  612. 'user_id'=>0,
  613. 'message'=>"你已不是《".$dream->name."》的梦主啦!"."<span style='color: #00c3da'>[$user->nickname]</span> "."以$new_arr[0]分刚取代你的位置",
  614. 'to_user_id'=>$old_top_user_id,
  615. 'dream_id'=>$dream_id,
  616. 'is_url'=>1,
  617. 'type_id'=>2,
  618. 'attr_id'=>10,
  619. ];
  620. SystemInfoModel::create($arr);
  621. $mess = "你已不是《".$dream->name."》的梦主啦!"."[$user->nickname] "."以$new_arr[0]分刚取代你的位置";
  622. $this->jPush($mess,'',$old_top_user_id,4,$dream->id);
  623. }
  624. }
  625. // 收藏关注梦想
  626. /**
  627. * @api {get} /api/dream/collection 收藏梦想
  628. * @apiDescription 收藏梦想
  629. * @apiGroup Dream
  630. * @apiPermission Passport
  631. * @apiVersion 0.1.0
  632. * @apiParam {int} id 梦想ID
  633. * @apiParam {int} is_collection 0取消关注 1关注
  634. * @apiSuccessExample {json} Success-Response:
  635. * HTTP/1.1 200 OK
  636. *{
  637. * "status": true,
  638. * "status_code": 0,
  639. * "message": "",
  640. * "data": {
  641. * "is_collection": 1 1已关注 0 未关注
  642. * }
  643. *}
  644. * @apiErrorExample {json} Error-Response:
  645. *HTTP/1.1 400 Bad Request
  646. * {
  647. * "state": false,
  648. * "code": 1000,
  649. * "message": "传入参数不正确",
  650. * "data": null or []
  651. * }
  652. *
  653. */
  654. public function collection(Request $request)
  655. {
  656. $validator = \Validator::make($request->all(),
  657. [
  658. 'id' => 'required',
  659. 'is_collection' => 'required',
  660. ],
  661. [
  662. 'id.required' => '收藏梦想id不能为空',
  663. 'is_collection.required' => '关注值不存在',
  664. ]
  665. );
  666. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  667. $user = $this->getUser();
  668. if(!$dream = DreamInfoModel::find($request->id)){
  669. $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  670. }
  671. $is_collection = $request->is_collection;
  672. if ($is_collection) {
  673. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  674. where('dream_id',$request->id)->first();
  675. if (empty($user_care_dream)) {
  676. $data = [
  677. 'user_id' =>$user->id,
  678. 'dream_id' =>$request->id,
  679. 'dream_user_id' =>$dream->user_id,
  680. ];
  681. UserCareDream::create($data);
  682. // 关注成功发送私信
  683. $message = BaseSettingsModel::where('category','message')->first();
  684. $message = empty($message) ? ""."<span style='color: #00c3da'>[$user->nickname]</span> 收藏了你的梦想《".$dream->name.'》' : $message->value;
  685. // $this->jPush($message,'',$dream->user_id,4,$dream->id);/**/
  686. $info2 = [
  687. 'to_user_id' => $dream->user_id,
  688. 'message' => $message,
  689. 'dream_id' => $request->id,
  690. 'user_id' => $user->id,
  691. 'is_reply' => 1,
  692. 'type_id' => 1,
  693. 'attr_id' => 2,
  694. ];
  695. SystemInfoModel::firstOrCreate($info2);
  696. // 长连接
  697. $mess = $user->nickname."收藏了你的梦想《".$dream->name.">";
  698. // $this->jPush($mess,'',$dream->user_id);
  699. }
  700. }else{
  701. UserCareDream::where('user_id',$user->id)->
  702. where('dream_id',$request->id)->delete();
  703. // 取消收藏删除系统消息
  704. SystemInfoModel::where('to_user_id',$dream->user_id)->where('dream_id',$request->id)->delete();
  705. }
  706. // 当有人收藏/取消收藏你的梦想时 梦想分发生变化
  707. $setting = BaseSettingsModel::where('category','paihang')->first();
  708. $a = $setting?$setting->key:1;
  709. $b = $dream->parameter;
  710. $x = $setting?$setting->sort:10;
  711. if($x==0) $x=10;
  712. $t = 21*24*60; //梦想发布分钟数
  713. $care_num = UserCareDream::where('dream_id',$dream->id)->get()->count();
  714. if ($care_num==0) $care_num=1;
  715. $score = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ;
  716. $dream->score = $score;
  717. $dream->save();
  718. // 并且同时会关注这个梦想者
  719. $array = [
  720. 'user_id' =>$user->id,
  721. 'other_user_id' =>$dream->user_id,
  722. ];
  723. $user_care_user = UserCareUser::where('user_id',$user->id)->
  724. where('other_user_id',$dream->user_id)->first();
  725. if (empty($user_care_user)) {
  726. UserCareUser::create($array);
  727. }
  728. return $this->api($is_collection);
  729. }
  730. /**
  731. * @api {get} /api/dream/search 梦想搜索
  732. * @apiDescription 梦想搜索)
  733. * @apiGroup Dream
  734. * @apiPermission none
  735. * @apiVersion 0.1.0
  736. * @apiParam {string} keyword 关键字
  737. * @apiSuccessExample {json} Success-Response:
  738. * HTTP/1.1 200 OK
  739. {
  740. "status": true,
  741. "status_code": 0,
  742. "message": "",
  743. "data": [
  744. {
  745. "id": 5,
  746. "user_id": 1,
  747. "name": "梦想标题1",
  748. "about": "梦想介绍",
  749. "coin": 2500,
  750. "time": 72000,
  751. "get_coin": 0,
  752. "mark": 0,
  753. "status": 0,
  754. "video": "url",
  755. "score": 100079365,
  756. "sign": "梦想达人",
  757. "created_at": "2017-06-25 12:45:22",
  758. "updated_at": "2017-06-28 15:50:41",
  759. "user": {},
  760. "imgs": []
  761. },
  762. ]
  763. }
  764. * @apiErrorExample {json} Error-Response:
  765. * HTTP/1.1 400 Bad Request
  766. */
  767. public function search(Request $request)
  768. {
  769. $user = $this->getUser();
  770. if (empty($request->keyword)) {
  771. return $this->api('');
  772. }
  773. $keyword ='%'.$request->keyword.'%';
  774. $data = DreamInfoModel::where('name','like',$keyword)->
  775. orWhere('sign','like',$keyword)->with(['user','img'])->get();
  776. foreach ($data as $k => $dream) {
  777. $info = UserCareDream::where('dream_id',$dream->id)->get();
  778. $dream->care_num = count($info);
  779. $dream->img = $dream->img?$dream->img->pic:'';
  780. }
  781. $this->insertSearchTable($user->id,$request->keyword);
  782. return $this->api($data);
  783. }
  784. public function insertSearchTable($id,$keyword)
  785. {
  786. $info = SearchInfoModel::where('user_id',$id)->
  787. where('search',trim($keyword))->first();
  788. if (count($info) == 0) {
  789. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  790. }else{
  791. $info->times += 1;
  792. $info->save();
  793. }
  794. }
  795. /**
  796. * @api {post} /api/dream/store 发布梦想
  797. * @apiDescription 发布梦想
  798. * @apiParam {string} pics 梦想图片 数组
  799. * @apiParam {string} video 梦想视频
  800. * @apiParam {string} name 梦想标题
  801. * @apiParam {string} about 梦想介绍
  802. * @apiParam {int} coin 梦想币
  803. * @apiParam {int} time 实现时间默认21*3600
  804. * @apiGroup Dream
  805. * @apiPermission Passport
  806. * @apiVersion 0.1.0
  807. * @apiSuccessExample {json} Success-Response:
  808. * {
  809. * "status": true,
  810. * "status_code": 0,
  811. * "message": "",
  812. * "data": ""
  813. *}
  814. * HTTP/1.1 200 OK
  815. * @apiErrorExample {json} Error-Response:
  816. * {
  817. * "status": false,
  818. * "status_code": 1203,
  819. * "message": "附件不存在",
  820. * "data": null
  821. * }
  822. * HTTP/1.1 400 Bad Request
  823. */
  824. public function store(Request $request)
  825. {
  826. $user = $this->getUser();
  827. $validator = \Validator::make($request->all(),
  828. [
  829. 'name' => 'required',
  830. 'about' => 'required',
  831. 'coin' => 'required|integer',
  832. 'days' => 'required|integer',
  833. 'pics' => 'required|array',
  834. ],
  835. [
  836. 'name.required' => '梦想标题必填',
  837. 'about.required' => '梦想介绍必填',
  838. 'coin.required' => '梦想币必填',
  839. 'coin.integer' => '梦想币必须是正整数',
  840. 'days.required' => '实现时间必填',
  841. 'days.integer' => '实现时间必须是正整数',
  842. 'pics.required' => '至少上传一张图片',
  843. 'pics.array' => '至少上传一张图片数组',
  844. ]
  845. );
  846. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  847. \Log::debug($user);
  848. $care_num = 1; //梦想收藏人数 新建梦想时默认为1
  849. $setting = BaseSettingsModel::where('category','paihang')->first();
  850. $a = $setting?$setting->key:1;
  851. $b = $setting?$setting->value:0;
  852. $x = $setting?$setting->sort:10;
  853. $t = 1; //创建梦想时梦想发布时间最小为一分钟
  854. $days = $request->days;
  855. $data = $request->except('_token','pics','days');
  856. $data['user_id'] = $user->id;
  857. $data['end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' days'));
  858. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  859. if ($care_num == 0) {
  860. $data['score'] = (($a/$t) + $b)*100000000000000 ;
  861. }else{
  862. $data['score'] = (log($care_num,$x) + ($a/$t) + $b)*100000000000000 ;
  863. }
  864. $data['created_at'] = date('Y-m-d H:i:s');
  865. $data['updated_at'] = date('Y-m-d H:i:s');
  866. $data['status'] = 1; //默认审核通过
  867. $video = $request->video;
  868. if (!empty($video)) $data['video'] =env('APP_URL').'/attachment/'. $request->video;
  869. $dream_id = DreamInfoModel::insertGetId($data);
  870. if ($dream_id) {
  871. // 梦想创建成功 关联关系中最新动态+1
  872. // UserCareUser::where('other_user_id',$user->id)->increment('dream_number',1);
  873. $pics = $request->pics;
  874. if (!is_array($pics) && empty($pics) && empty($video)) {
  875. DreamInfoModel::destroy($dream_id);
  876. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  877. }
  878. $data = [];
  879. if (is_array($pics)) {
  880. foreach ($pics as $v){
  881. $data[] = [
  882. 'dream_id'=>$dream_id,
  883. 'pic' => $v,
  884. 'created_at' =>date('Y-m-d H:i:s'),
  885. 'updated_at' =>date('Y-m-d H:i:s'),
  886. ];
  887. }
  888. DreamImages::insert($data);
  889. }
  890. // 梦想创建成功后所有关注我的梦想的人也会收到我最新梦想发布的消息
  891. $ids = UserCareDream::where('dream_user_id',$user->id)->select('user_id')->get()->toArray();
  892. $id_arrs = array_column($ids,'user_id');
  893. if (!empty($id_arrs)) {
  894. $arr = [] ;
  895. foreach ($id_arrs as $id){
  896. $arr[] = [
  897. 'to_user_id' => $id,
  898. 'user_id' => $user->id,
  899. 'message' => '关注的梦想有新的梦想动态'.$dream_id,
  900. ];
  901. // 长连接
  902. // $this->jPush('关注的梦想有新的梦想动态','',$id);
  903. }
  904. SystemInfoModel::insert($arr);
  905. }
  906. return $this->api('');
  907. }
  908. }
  909. /**
  910. * @api {get} /api/dream/share 分享
  911. * @apiDescription 分享
  912. * @apiParam {int} id 梦想id
  913. * @apiGroup Dream
  914. * @apiPermission Passport
  915. * @apiVersion 0.1.0
  916. */
  917. // 分享梦想
  918. public function share(Request $request)
  919. {
  920. $validator = \Validator::make($request->all(),
  921. [
  922. 'id' => 'required',
  923. ],
  924. [
  925. 'id.required' => '梦想id不存在',
  926. ]
  927. );
  928. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  929. $url = env('APP_URL').'/api/dream/show?id='.$request->id;
  930. $dream = DreamInfoModel::with('img')->find($request->id);
  931. $img = !empty($dream->img) ? $dream->img->pic : '';
  932. $html =
  933. "<div style='text-align: center'>
  934. <a href='$url'>
  935. <h1>$dream->name</h1>
  936. <img src=\"$img\" style='width: 20px;height: 20px;' alt=''>
  937. </a>
  938. </div>";
  939. return $html;
  940. }
  941. }