DreamController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\AccountLog;
  4. use App\Models\BaseSettingsModel;
  5. use App\Models\DreamImages;
  6. use App\Models\DreamInfoModel;
  7. use App\Models\InteractionInfo;
  8. use App\Models\SearchInfoModel;
  9. use App\Models\SupportDreamModel;
  10. use App\Models\SystemInfoModel;
  11. use App\Models\UserCareDream;
  12. use App\Models\UserInfoModel;
  13. use Illuminate\Http\Request;
  14. use App\Services\Base\ErrorCode;
  15. class DreamController extends Controller
  16. {
  17. /**
  18. * @api {get} /api/dream/show 梦想详情
  19. * @apiDescription 梦想详情
  20. * @apiGroup Dream
  21. * @apiPermission Passport
  22. * @apiVersion 0.1.0
  23. * @apiParam {int} [type=home] home interaction paihang 默认all
  24. * @apiParam {int} id 梦想ID
  25. * @apiSuccessExample {json} Success-Response:
  26. * HTTP/1.1 200 OK
  27. * 主页
  28. {
  29. "status": true,
  30. "status_code": 0,
  31. "message": "",
  32. "data": {
  33. "dream": { 梦想
  34. "id": 4,
  35. "user_id": 1,
  36. "a": 1, //支持乘数系数 后台配置
  37. "c": 1, //支持乘数系数 后台配置
  38. "name": "梦想标题1",
  39. "about": "梦想介绍",
  40. "coin": 2500,
  41. "time": 21,
  42. "get_coin": 600,
  43. "status": 0,
  44. "video": null,
  45. "score": 100079365,
  46. "sign": "",
  47. "created_at": "2017-06-25 11:47:45",
  48. "updated_at": "2017-06-25 15:10:50",
  49. "is_collection" : 1 1已收藏 0未收藏
  50. "top3user": [ 支持者前三
  51. {
  52. "id": 2,
  53. "phone": "13880642881",
  54. "nickname": "name2",
  55. "avatar": "https://timgsa.baidu.com/timg?imamb.224_0.jpeg",
  56. "score": 2399
  57. }
  58. ],
  59. "number": 1 支持乘数
  60. "imgs": [ 梦想图片
  61. {
  62. "title": "",
  63. "pic": "https://timgsa.ba0170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  64. },
  65. {
  66. "title": "",
  67. "pic": "https://timgsa.baid524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  68. },
  69. {
  70. "title": "",
  71. "pic": "https://timgsa20170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  72. }
  73. ],
  74. "user": { 梦想用户
  75. "id": 1,
  76. "phone": "13880642880",
  77. "nickname": "name1",
  78. "avatar": "https://timgsumb.224_0.jpeg",
  79. "birthday": "2000-06-21",
  80. "sign": 0,
  81. "money": 0,
  82. "coin": 1300,
  83. "sex": 1,
  84. "signture": "",
  85. "height": 170,
  86. "work": "",
  87. "emotion": 1,
  88. "address": "",
  89. "city": "",
  90. "detail_address": "",
  91. "status": 1,
  92. "wechat": "",
  93. "weibo": "",
  94. "remember_token": "",
  95. "created_at": "2017-06-25 10:27:08",
  96. "updated_at": "2017-06-25 15:20:11",
  97. "deleted_at": null
  98. }
  99. }
  100. }
  101. }
  102. * @apiErrorExample {json} Error-Response:
  103. *HTTP/1.1 400 Bad Request
  104. *{
  105. * "status": false,
  106. * "status_code": 1105,
  107. * "message": "用户不存在",
  108. * "data": null
  109. * }
  110. */
  111. public function show(Request $request)
  112. {
  113. $user = $this->getUser();
  114. $type = $request->type; //类型
  115. $id = $request->id; //梦想ID
  116. $user_care_dream = UserCareDream::where('user_id',$user->id)->where('dream_id',$id)->first();
  117. $is_collection = empty($user_care_dream) ? 0 :1 ;
  118. if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
  119. $support_dream = SupportDreamModel::where('dream_id',$id)->get();
  120. $top = [] ;
  121. $topuser = []; //所有支持用户排行
  122. $top3user = []; // 支持用户排行前三
  123. foreach ($support_dream as $item) {
  124. if (!array_key_exists($item->user_id,$top)) {
  125. $top[$item->user_id] = $item->score;
  126. }else{
  127. $top[$item->user_id] += $item->score;
  128. }
  129. }
  130. arsort($top);
  131. foreach ($top as $user_id => $score){
  132. $user = UserInfoModel::find($user_id);
  133. $user->score = $score;
  134. $topuser[] = $user;
  135. if(count($top3user) <= 2) $top3user[] = $user ;
  136. }
  137. if ($type == 'paihang') return $this->api($topuser);
  138. $interactions = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get();
  139. foreach ($interactions as $item) {
  140. $item->comments;
  141. foreach ($item->comments as $comment) {
  142. $comment->pic = UserInfoModel::find($comment->user_id)->pic;
  143. $comment->replay;
  144. foreach ($comment->replay as $k1 => $v1){
  145. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  146. }
  147. }
  148. }
  149. if ($type == 'interaction') return $this->api($interactions);
  150. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  151. // $money = $user->money;
  152. $dream_info = DreamInfoModel::find($id);
  153. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  154. $a = count($setting) > 0 ? $setting->key : 0;
  155. $created_at = $dream_info ? $dream_info->created_at : 0;
  156. $b = intval((time()-strtotime($created_at))/60) ;
  157. $c = count($setting) > 0 ? $setting->value : 0;
  158. $number =round( -$a * $b + $c,1);
  159. if ($number <= 1) $number = 1;
  160. $dream = DreamInfoModel::with(['imgs','user'])->find($id);
  161. $dream->user_coin = $user->coin;
  162. $dream->topuser = $topuser;
  163. $dream->top3user = $top3user;
  164. $dream->interactions = $interactions;
  165. $dream->number = $number;
  166. $dream->is_collection = $is_collection;
  167. $dream->a = $a;
  168. $dream->c = $c;
  169. return $this->api($dream);
  170. }
  171. /**
  172. * @api {post} /api/dream/support 支持梦想
  173. * @apiDescription 支持梦想
  174. * @apiGroup Dream
  175. * @apiPermission Passport
  176. * @apiVersion 0.1.0
  177. * @apiParam {int} coin 支持梦想币数量
  178. * @apiParam {int} id 梦想ID
  179. * @apiSuccessExample {json} Success-Response:
  180. * HTTP/1.1 200 OK
  181. {
  182. "status": true,
  183. "status_code": 0,
  184. "message": "",
  185. "data": {
  186. "id": 12,
  187. "user_id": 2,
  188. "name": "用户2梦想标题166",
  189. "about": "用户2梦想介绍666",
  190. "coin": 2500,
  191. "time": 21,
  192. "get_coin": 704,
  193. "status": 0,
  194. "video": null,
  195. "score": 158730,
  196. "mark": 158730,
  197. "sign": "",
  198. "created_at": "2017-06-25 13:10:56",
  199. "updated_at": "2017-06-27 18:16:02",
  200. "top3user": [ 支持者前三
  201. {
  202. "id": 1,
  203. "phone": "13880642880",
  204. "nickname": "name1",
  205. "avatar": "https://timgsa.bumb.224_0.jpeg",
  206. "birthday": "2000-06-21",
  207. "sign": 0,
  208. "money": 0,
  209. "coin": 596,
  210. "sex": 1,
  211. "signture": "我的个性签名1",
  212. "height": 170,
  213. "work": "网红",
  214. "emotion": 1,
  215. "address": "",
  216. "city": "上海",
  217. "detail_address": "",
  218. "status": 1,
  219. "wechat": "",
  220. "weibo": "",
  221. "remember_token": "",
  222. "created_at": "2017-06-25 10:27:08",
  223. "updated_at": "2017-06-27 18:16:02",
  224. "deleted_at": null,
  225. "score": 5062
  226. }
  227. ]
  228. "number": 4.8
  229. "is_collection" : 1 1已收藏 0未收藏
  230. }
  231. }
  232. * @apiErrorExample {json} Error-Response:
  233. *HTTP/1.1 400 Bad Request
  234. * {
  235. * "state": false,
  236. * "code": 1000,
  237. * "message": "传入参数不正确",
  238. * "data": null or []
  239. * }
  240. * 可能出现的代码
  241. * {
  242. * "status": false,
  243. * "status_code": 1303,
  244. * "message": "余额不足",
  245. * "data": null
  246. * }
  247. * {
  248. * "status": false,
  249. * "status_code": 1410,
  250. * "message": "支持已达上限",
  251. * "data": null
  252. * }
  253. *
  254. */
  255. public function support(Request $request)
  256. {
  257. $validator = \Validator::make($request->all(),
  258. [
  259. 'coin' => 'required',
  260. 'id' => 'required',
  261. ],
  262. [
  263. 'coin.required' => '梦想币不能为空',
  264. 'id.required' => '支持梦想id不能为空',
  265. ]
  266. );
  267. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  268. $user = $this->getUser();
  269. $dream_id = $request->id;
  270. $dream = DreamInfoModel::find($dream_id);
  271. $user_id = $dream->user_id;
  272. $coin = $request->coin;
  273. if ($dream->get_coin == $dream->coin) {
  274. return $this->error(ErrorCode::SUP_TOP);
  275. } elseif($coin + $dream->get_coin > $dream->coin){
  276. $coin = $coin + $dream->get_coin - $dream->coin ;
  277. }
  278. $user_care_dream = UserCareDream::where('user_id', $user->id)->where('dream_id', $dream_id)->first();
  279. $is_collection = empty($user_care_dream) ? 0 :1 ;
  280. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  281. $a = count($setting) > 0 ? $setting->key : '';
  282. $b = intval((time()-strtotime($dream->created_at))/60) ;
  283. $c = count($setting) > 0 ? $setting->value : '';
  284. //Todo 支持乘数目公式
  285. $number =round( -$a * $b + $c,1);
  286. if ($number <= 1) $number = 1;
  287. if ($user->coin < $coin) {
  288. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  289. }else{
  290. $user->coin -= $coin;
  291. $user->save();
  292. $dream->get_coin += $coin;
  293. $dream->mark += $coin*$number;
  294. $dream->save();
  295. $data = [
  296. 'user_id'=>$user->id,
  297. 'dream_id'=>$dream_id,
  298. 'to_user_id'=>$user_id,
  299. 'coin'=>$coin,
  300. 'score'=>$coin*$number,
  301. ];
  302. $ok = SupportDreamModel::create($data);
  303. if (!$ok) {
  304. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  305. }
  306. // 记录充值记录 新增系统消息
  307. $data = [
  308. 'from_id' =>$user->id,
  309. 'to_id' => $user_id,
  310. 'from_amount' => $coin,
  311. 'to_amount' => $coin,
  312. 'from_type' => AccountLog::TYPE_COIN,
  313. 'to_type' => AccountLog::TYPE_COIN,
  314. 'op' => AccountLog::OP_SUPPORT,
  315. ];
  316. AccountLog::create($data);
  317. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  318. $info = [
  319. 'user_id' => $user_id,
  320. 'message' => $message,
  321. ];
  322. SystemInfoModel::create($info);
  323. // 支持成功返回数据到当前页面 top3user
  324. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  325. $top = [] ;
  326. $top3user = []; // 支持用户排行前三
  327. foreach ($support_dream as $item) {
  328. if (!array_key_exists($item->user_id,$top)) {
  329. $top[$item->user_id] = $item->score;
  330. }else{
  331. $top[$item->user_id] += $item->score;
  332. }
  333. }
  334. arsort($top);
  335. foreach ($top as $user_id => $score){
  336. $user = UserInfoModel::find($user_id);
  337. $user->score = $score;
  338. if(count($top3user) <= 2) $top3user[] = $user ;
  339. }
  340. // 梦想分 mark
  341. $dream->top3user = $top3user;
  342. $dream->is_collection = $is_collection;
  343. $user_coin = $user->coin;
  344. return $this->api(compact('dream','user_coin'));
  345. }
  346. }
  347. // 收藏关注梦想
  348. /**
  349. * @api {get} /api/dream/collection 收藏梦想
  350. * @apiDescription 收藏梦想
  351. * @apiGroup Dream
  352. * @apiPermission Passport
  353. * @apiVersion 0.1.0
  354. * @apiParam {int} id 梦想ID
  355. * @apiParam {int} is_collection 0取消关注 1关注
  356. * @apiSuccessExample {json} Success-Response:
  357. * HTTP/1.1 200 OK
  358. *{
  359. * "status": true,
  360. * "status_code": 0,
  361. * "message": "",
  362. * "data": {
  363. * "is_collection": 1 1已关注 0 未关注
  364. * }
  365. *}
  366. * @apiErrorExample {json} Error-Response:
  367. *HTTP/1.1 400 Bad Request
  368. * {
  369. * "state": false,
  370. * "code": 1000,
  371. * "message": "传入参数不正确",
  372. * "data": null or []
  373. * }
  374. *
  375. */
  376. public function collection(Request $request)
  377. {
  378. $validator = \Validator::make($request->all(),
  379. [
  380. 'id' => 'required',
  381. 'is_collection' => 'required',
  382. ],
  383. [
  384. 'id.required' => '收藏梦想id不能为空',
  385. 'is_collection.required' => '关注值不存在',
  386. ]
  387. );
  388. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  389. $user = $this->getUser();
  390. if(!$dream = DreamInfoModel::find($request->id)){
  391. $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  392. }
  393. $data = [
  394. 'user_id' =>$user->id,
  395. 'dream_id' =>$request->id,
  396. 'dream_user_id' =>$dream->user_id,
  397. ];
  398. $is_collection = $request->is_collection;
  399. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  400. where('dream_id',$request->id)->first();
  401. if ($is_collection) {
  402. if (empty($user_care_dream)) {
  403. UserCareDream::create($data);
  404. }
  405. }else{
  406. UserCareDream::where('user_id',$user->id)->
  407. where('dream_id',$request->id)->delete();
  408. }
  409. return $this->api($is_collection);
  410. }
  411. /**
  412. * @api {get} /api/dream/search 梦想搜索
  413. * @apiDescription 梦想搜索)
  414. * @apiGroup Dream
  415. * @apiPermission none
  416. * @apiVersion 0.1.0
  417. * @apiParam {string} keyword 关键字
  418. * @apiSuccessExample {json} Success-Response:
  419. * HTTP/1.1 200 OK
  420. {
  421. "status": true,
  422. "status_code": 0,
  423. "message": "",
  424. "data": [
  425. {
  426. "id": 5,
  427. "user_id": 1,
  428. "name": "梦想标题1",
  429. "about": "梦想介绍",
  430. "coin": 2500,
  431. "time": 72000,
  432. "get_coin": 0,
  433. "mark": 0,
  434. "status": 0,
  435. "video": "url",
  436. "score": 100079365,
  437. "sign": "梦想达人",
  438. "created_at": "2017-06-25 12:45:22",
  439. "updated_at": "2017-06-28 15:50:41",
  440. "user": {},
  441. "imgs": []
  442. },
  443. ]
  444. }
  445. * @apiErrorExample {json} Error-Response:
  446. * HTTP/1.1 400 Bad Request
  447. */
  448. public function search(Request $request)
  449. {
  450. $user = $this->getUser();
  451. if (empty($request->keyword)) {
  452. return $this->api('');
  453. }
  454. $keyword ='%'.$request->keyword.'%';
  455. $data = DreamInfoModel::where('name','like',$keyword)->
  456. orWhere('sign','like',$keyword)->with(['user','img'])->get();
  457. $this->insertSearchTable($user->id,$request->keyword);
  458. return $this->api($data);
  459. }
  460. public function insertSearchTable($id,$keyword)
  461. {
  462. $info = SearchInfoModel::where('user_id',$id)->
  463. where('search',trim($keyword))->first();
  464. if (count($info) == 0) {
  465. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  466. }else{
  467. $info->times += 1;
  468. $info->save();
  469. }
  470. }
  471. /**
  472. * @api {post} /api/dream/store 发布梦想
  473. * @apiDescription 发布梦想
  474. * @apiParam {string} pics 梦想图片 数组
  475. * @apiParam {string} video 梦想视频
  476. * @apiParam {string} name 梦想标题
  477. * @apiParam {string} about 梦想介绍
  478. * @apiParam {int} coin 梦想币
  479. * @apiParam {int} time 实现时间默认21*3600
  480. * @apiGroup Dream
  481. * @apiPermission Passport
  482. * @apiVersion 0.1.0
  483. * @apiSuccessExample {json} Success-Response:
  484. * {
  485. * "status": true,
  486. * "status_code": 0,
  487. * "message": "",
  488. * "data": ""
  489. *}
  490. * HTTP/1.1 200 OK
  491. * @apiErrorExample {json} Error-Response:
  492. * {
  493. * "status": false,
  494. * "status_code": 1203,
  495. * "message": "附件不存在",
  496. * "data": null
  497. * }
  498. * HTTP/1.1 400 Bad Request
  499. */
  500. public function store(Request $request)
  501. {
  502. $user = $this->getUser();
  503. $validator = \Validator::make($request->all(),
  504. [
  505. 'name' => 'required',
  506. 'about' => 'required',
  507. 'coin' => 'required|integer',
  508. 'days' => 'required|integer',
  509. ],
  510. [
  511. 'name.required' => '梦想标题必填',
  512. 'about.required' => '梦想介绍必填',
  513. 'coin.required' => '梦想币必填',
  514. 'coin.integer' => '梦想币必须是正整数',
  515. 'days.required' => '实现时间必填',
  516. 'days.integer' => '实现时间必须是正整数',
  517. ]
  518. );
  519. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  520. \Log::debug($user);
  521. $data2 = $user->myCareNum;
  522. $care_num = count($data2);
  523. $setting = BaseSettingsModel::where('category','paihang')->first();
  524. $a = $setting?$setting->key:1;
  525. $b = $setting?$setting->value:1;
  526. $t = 21*3600 / 60;
  527. $days = $request->days;
  528. $data = $request->except('_token','pics','days');
  529. $data['user_id'] = $user->id;
  530. $data['end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' days'));
  531. \Log::debug($data['end_time'].' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  532. if ($care_num == 0) {
  533. $data['score'] = (($a/$t) + $b)*100000000 ;
  534. }else{
  535. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  536. }
  537. $data['created_at'] = date('Y-m-d H:i:s');
  538. $data['updated_at'] = date('Y-m-d H:i:s');
  539. $dream_id = DreamInfoModel::insertGetId($data);
  540. // 生成二维码
  541. $info['transaction_id'] = date('YmdHis') . mt_rand(1000, 9999);
  542. $info['code'] = 'WECHATPAY_' . $info['transaction_id'];
  543. $code_url = env('APP_URL').'/user/meet?dream_id='.$dream_id;
  544. $code_path = public_path('qrcodes/'.$info['code'].'.png');
  545. \QrCode::format('png')->size(500)->generate($code_url,$code_path);
  546. $code = env('APP_URL').'/qrcodes/'.$info['code'].'.png';
  547. DreamInfoModel::where('id',$dream_id)->update(compact('code'));
  548. if ($dream_id) {
  549. // 梦想创建成功 关联关系中最新动态+1
  550. /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  551. foreach ($info as $item) {
  552. $item->dream_num += 1;
  553. $item->updated_at = date('Y-m-d H:i:s');
  554. $item->save();
  555. }*/
  556. $pics = $request->pics;
  557. $video = $request->video;
  558. if (!is_array($pics) && empty($pics) && empty($video)) {
  559. DreamInfoModel::destroy($dream_id);
  560. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  561. }
  562. $data = [];
  563. if (is_array($pics)) {
  564. foreach ($pics as $v){
  565. $data[] = [
  566. 'dream_id'=>$dream_id,
  567. 'pic' => $v,
  568. 'created_at' =>date('Y-m-d H:i:s'),
  569. 'updated_at' =>date('Y-m-d H:i:s'),
  570. ];
  571. }
  572. DreamImages::insert($data);
  573. }
  574. return $this->api('');
  575. }
  576. }
  577. }