DreamController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 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": 7,
  35. * "user_id": 2,
  36. * "dream": "梦想标1123",
  37. * "about": "梦想烧1111213",
  38. * "video": "",
  39. * "sign": null,
  40. * "money": 10000,
  41. * "time": 720000,
  42. * "get_money": 0,
  43. * "status": 0,
  44. * "updated_at": "2017-06-22 09:47:03"
  45. * },
  46. * "imgs": [ 梦想图片
  47. * {
  48. * "title": "",
  49. * "pic": "http://w17.9026.com/img/banner/banner_3.png"
  50. * },
  51. * {
  52. * "title": "",
  53. * "pic": "http://w17.9026.com/img/banner/banner_3.png"
  54. * }
  55. * ],
  56. * "arr": { 支持者前三(user_id和头像)
  57. * "6": "http://www.wsfjq.com/photos/bd119684755.jpg",
  58. * "1": "http://www.wsfjq.com/photos/bd119684755.jpg"
  59. * }
  60. * "money" : "1000" 用户余额
  61. * "score" : "1000" 梦想分数
  62. * "a": "0.001", 支持乘数计算 y = -a * b +c b = updated_at-当前时间 (分钟) y>=1
  63. * "c": "8"
  64. * }
  65. *}
  66. * 排行
  67. *{
  68. * "status": true,
  69. * "status_code": 0,
  70. * "message": "",
  71. * "data": [
  72. * {
  73. * "nickname": "ha", 昵称
  74. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg", 头像
  75. * "score": 112000 支持分
  76. * },
  77. * ]
  78. *}
  79. * 互动
  80. *{
  81. * "status": true,
  82. * "status_code": 0,
  83. * "message": "",
  84. * "data": {
  85. * "data": [
  86. * {
  87. * "id": 7,
  88. * "dream_id": 2,
  89. * "title": "梦想动态有力来了",
  90. * "pic1": "http\"//www.baidu.com",
  91. * "created_at": "2017-06-22 21:09:18",
  92. * "updated_at": "2017-06-22 21:09:18",
  93. * "deleted_at": null,
  94. * "comments": [ 评论
  95. * {
  96. * "interaction_id": 7,
  97. * "user_id": 1,
  98. * "level": 0,
  99. * "content": "不错的梦想",
  100. * "updated_at": null,
  101. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg", 评论者头像
  102. * "replay": [ 回复
  103. * {
  104. * "user_id": 1,
  105. * "level": 0,
  106. * "content": "我也觉得不错",
  107. * "updated_at": null,
  108. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg" 回复者头像
  109. * },
  110. * {
  111. * "user_id": 6,
  112. * "level": 0,
  113. * "content": "祝你梦想成真",
  114. * "updated_at": null,
  115. * "pic": "http://www.wsfjq.com/photos/bd119684755.jpg"
  116. * }
  117. * }
  118. * ]
  119. * ]
  120. * },
  121. * "times": 720000 梦想实现倒计时(s)
  122. * }
  123. *}
  124. * @apiErrorExample {json} Error-Response:
  125. *HTTP/1.1 400 Bad Request
  126. *{
  127. * "status": false,
  128. * "status_code": 1105,
  129. * "message": "用户不存在",
  130. * "data": null
  131. * }
  132. */
  133. public function show(Request $request)
  134. {
  135. $user = $this->getUser();
  136. $type = $request->type; //类型
  137. $id = $request->id; //梦想ID
  138. if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
  139. $support_dream = SupportDreamModel::where('dream_id',$id)->get();
  140. $top = [] ;
  141. $topuser = []; //所有支持用户排行
  142. $top3user = []; // 支持用户排行前三
  143. foreach ($support_dream as $item) {
  144. if (!array_key_exists($item->user_id,$top)) {
  145. $top[$item->user_id] = $item->score;
  146. }else{
  147. $top[$item->user_id] += $item->score;
  148. }
  149. }
  150. arsort($top);
  151. foreach ($top as $user_id => $score){
  152. $user = UserInfoModel::find($user_id);
  153. $user->score = $score;
  154. $topuser[] = $user;
  155. if(count($top3user)<=2) $top3user[] = $user;
  156. }
  157. if ($type == 'paihang') return $this->api($topuser);
  158. $interactios = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get();
  159. foreach ($interactios as $item) {
  160. dd($item->comments);
  161. // $item->with(['comments']);
  162. foreach ($item->comments as $comment) {
  163. $comment->pic = UserInfoModel::find($comment->user_id)->pic;
  164. $comment->replay = $comment->replay;
  165. foreach ($comment->replay as $k1 => $v1){
  166. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  167. }
  168. }
  169. }
  170. if ($type == 'interaction') return $this->api(compact('data'));
  171. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  172. // $money = $user->money;
  173. $number = BaseSettingsModel::where('category','score')->select('key','value')->first()->moey;
  174. $a = count($number) > 0 ? $number->key : '';
  175. $c = count($number) > 0 ? $number->value : '';
  176. $dream = DreamInfoModel::with(['dreamImgs','dreamUser'])->find($id);
  177. // $imgs = DreamImages::where('dream_id',$id)->orderBy('id','desc')->get();
  178. // $info = SupportDreamModel::where('dream_id',$id)->get();
  179. $top3 = [] ;
  180. $score = 0 ;
  181. foreach ($info as $item) {
  182. $score += $item->score;
  183. if (!array_key_exists($item->user_id,$top3)) {
  184. $top3[$item->user_id] = $item->score;
  185. }else{
  186. $top3[$item->user_id] += $item->score;
  187. }
  188. }
  189. arsort($top3);
  190. foreach ($top3 as $k => $v){
  191. $user = UserInfoModel::find($k);
  192. if($user)
  193. $top3[$k] = $user->pic;
  194. }
  195. return $this->api(compact('dream','top3user','a','c','score','topuser'));
  196. }
  197. /**
  198. * @api {post} /api/dream/support 支持梦想
  199. * @apiDescription 支持梦想
  200. * @apiGroup Dream
  201. * @apiPermission Passport
  202. * @apiVersion 0.1.0
  203. * @apiParam {int} coin 支持梦想币数量
  204. * @apiParam {int} id 梦想ID
  205. * @apiParam {int} number 支持乘数
  206. * @apiSuccessExample {json} Success-Response:
  207. * HTTP/1.1 200 OK
  208. *{
  209. * "status": true,
  210. * "status_code": 0,
  211. * "message": "",
  212. * "data": ""
  213. *}
  214. * @apiErrorExample {json} Error-Response:
  215. *HTTP/1.1 400 Bad Request
  216. * {
  217. * "state": false,
  218. * "code": 1000,
  219. * "message": "传入参数不正确",
  220. * "data": null or []
  221. * }
  222. * 可能出现的代码
  223. * {
  224. * "status": false,
  225. * "status_code": 1303,
  226. * "message": "商户余额不足",
  227. * "data": null
  228. * }
  229. *
  230. */
  231. public function support(Request $request)
  232. {
  233. $validator = \Validator::make($request->all(),
  234. [
  235. 'coin' => 'required',
  236. 'id' => 'required',
  237. ],
  238. [
  239. 'coin.required' => '梦想币不能为空',
  240. 'id.required' => '支持梦想不能为空',
  241. ]
  242. );
  243. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  244. $user = $this->getUser();
  245. $dream_id = $request->id;
  246. $dream_info = DreamInfoModel::find($dream_id);
  247. $user_id = $dream_info->user_id;
  248. $coin = $request->coin;
  249. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  250. $a = count($setting) > 0 ? $setting->key : '';
  251. $c = count($setting) > 0 ? $setting->value : '';
  252. //Todo 支持乘数目公式
  253. $number = 2;
  254. if ($user->money < $coin) {
  255. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  256. }else{
  257. $user->money = $user->money - $coin;
  258. $user->save();
  259. $dream_info->get_money += $coin;
  260. $dream_info->save();
  261. $data = [
  262. 'user_id'=>$user->id,
  263. 'dream_id'=>$dream_id,
  264. 'dream_user_id'=>$user_id,
  265. 'coin'=>$coin,
  266. 'score'=>$coin*$number,
  267. ];
  268. $ok = SupportDreamModel::create($data);
  269. if (!$ok) {
  270. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  271. }
  272. // 记录充值记录 新增系统消息
  273. $data = [
  274. 'from_id' =>$user->id,
  275. 'to_id' => $user_id,
  276. 'from_amount' => $coin,
  277. 'to_amount' => $coin,
  278. 'from_type' => AccountLog::TYPE_COIN,
  279. 'to_type' => AccountLog::TYPE_COIN,
  280. 'op' => AccountLog::OP_SUPPORT,
  281. ];
  282. AccountLog::create($data);
  283. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  284. $info = [
  285. 'user_id' => $user_id,
  286. 'message' => $message,
  287. ];
  288. SystemInfoModel::create($info);
  289. return $this->api($number);
  290. }
  291. }
  292. // 收藏关注梦想
  293. /**
  294. * @api {get} /api/dream/collection 收藏梦想
  295. * @apiDescription 收藏梦想
  296. * @apiGroup Dream
  297. * @apiPermission Passport
  298. * @apiVersion 0.1.0
  299. * @apiParam {int} id 梦想ID
  300. * @apiSuccessExample {json} Success-Response:
  301. * HTTP/1.1 200 OK
  302. *{
  303. * "status": true,
  304. * "status_code": 0,
  305. * "message": "",
  306. * "data": ""
  307. *}
  308. * @apiErrorExample {json} Error-Response:
  309. *HTTP/1.1 400 Bad Request
  310. * {
  311. * "state": false,
  312. * "code": 1000,
  313. * "message": "传入参数不正确",
  314. * "data": null or []
  315. * }
  316. *
  317. */
  318. public function collection(Request $request)
  319. {
  320. $validator = \Validator::make($request->all(),
  321. [
  322. 'id' => 'required',
  323. ],
  324. [
  325. 'id.required' => '支持梦想不能为空',
  326. ]
  327. );
  328. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  329. $user = $this->getUser();
  330. $data = [
  331. 'user_id' =>$user->id,
  332. 'dream_id' =>$request->id,
  333. 'dream_user_id' =>DreamInfoModel::find($request->id)->user_id,
  334. ];
  335. $info = UserCareDream::where('user_id',$user->id)->
  336. where('dream_id',$request->id)->first();
  337. if (count($info) == 0) {
  338. UserCareDream::create($data);
  339. }
  340. return $this->api('');
  341. }
  342. // 发布关于梦想的动态
  343. /**
  344. * @api {post} /api/dream/add_info 新增动态
  345. * @apiDescription 新增动态
  346. * @apiGroup Dream
  347. * @apiPermission Passport
  348. * @apiVersion 0.1.0
  349. * @apiParam {int} id 梦想ID
  350. * @apiParam {string} title 梦想标题
  351. * @apiParam {array} pics[] 图片数组
  352. * @apiSuccessExample {json} Success-Response:
  353. * HTTP/1.1 200 OK
  354. *{
  355. * "status": true,
  356. * "status_code": 0,
  357. * "message": "",
  358. * "data": ""
  359. *}
  360. * @apiErrorExample {json} Error-Response:
  361. *HTTP/1.1 400 Bad Request
  362. * {
  363. * "state": false,
  364. * "code": 1000,
  365. * "message": "传入参数不正确",
  366. * "data": null or []
  367. * }
  368. *
  369. */
  370. public function addInfo(Request $request)
  371. {
  372. $validator = \Validator::make($request->all(),
  373. [
  374. 'id' => 'required',
  375. 'title' => 'required',
  376. ],
  377. [
  378. 'dream_id.required' => '梦想ID不能为空',
  379. 'title.required' => '动态标题不能为空',
  380. ]
  381. );
  382. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  383. $pics = $request->pics;
  384. if (empty($pics) || !is_array($pics)) {
  385. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  386. }
  387. $data = [];
  388. foreach ($pics as $k => $pic) {
  389. $data['pic'.($k+1)] = $pic;
  390. }
  391. $dream_id = $request->id;
  392. $title = $request->title;
  393. $data['dream_id'] = $dream_id;
  394. $data['title'] = $title;
  395. $ok = InteractionInfo::create($data);
  396. if ($ok) {
  397. // 收藏梦想最新动态加一
  398. UserCareDream::where('dream_id',$dream_id)->increment('interaction_number',1);
  399. return $this->api('');
  400. }else{
  401. return $this->error(ErrorCode::SAVE_USER_FAILED);
  402. }
  403. }
  404. /**
  405. * @api {get} /api/dream/search 梦想搜索
  406. * @apiDescription 梦想搜索)
  407. * @apiGroup Dream
  408. * @apiPermission none
  409. * @apiVersion 0.1.0
  410. * @apiParam {string} keyword 关键字
  411. * @apiSuccessExample {json} Success-Response:
  412. * HTTP/1.1 200 OK
  413. *{
  414. * "status": true,
  415. * "status_code": 0,
  416. * "message": "",
  417. * "data": {
  418. * "data": [
  419. * {
  420. * "dream": "haha", 梦想名
  421. * "user_pic": [
  422. * {
  423. * "pic": "", 用户头像
  424. * }
  425. * ],
  426. * "dream_img": "", 梦想图片
  427. * }
  428. * ]
  429. * }
  430. *}
  431. * @apiErrorExample {json} Error-Response:
  432. * HTTP/1.1 400 Bad Request
  433. */
  434. public function search(Request $request)
  435. {
  436. $user = $this->getUser();
  437. if (empty($request->keyword)) {
  438. return $this->api('');
  439. }
  440. $keyword ='%'.$request->keyword.'%';
  441. $data = DreamInfoModel::where('dream','like',$keyword)->
  442. orWhere('sign','like',$keyword)->get();
  443. foreach ($data as $k => $value) {
  444. $value->user_pic = $value->dreamFindUser;
  445. $value->dream_img = $value->dreamImgsFirst->pic;
  446. }
  447. $this->insertSearchTable($user->id,$request->keyword);
  448. return $this->api(compact('data'));
  449. }
  450. public function insertSearchTable($id,$keyword)
  451. {
  452. $info = SearchInfoModel::where('user_id',$id)->
  453. where('search',trim($keyword))->first();
  454. if (count($info) == 0) {
  455. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  456. }else{
  457. $info->times += 1;
  458. $info->save();
  459. }
  460. }
  461. }