DreamController.php 14 KB

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