DreamController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 默认home
  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. "name": "梦想标题1",
  37. "about": "梦想介绍",
  38. "coin": 2500,
  39. "time": 21,
  40. "get_coin": 600,
  41. "status": 0,
  42. "video": null,
  43. "score": 100079365,
  44. "sign": "",
  45. "created_at": "2017-06-25 11:47:45",
  46. "updated_at": "2017-06-25 15:10:50",
  47. "imgs": [ 梦想图片
  48. {
  49. "title": "",
  50. "pic": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498372396639&di=43f031aa703d741f2adbd0e6a501aaaf&imgtype=0&src=http%3A%2F%2Fimg2.touxiang.cn%2Ffile%2F20170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  51. },
  52. {
  53. "title": "",
  54. "pic": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498372396639&di=43f031aa703d741f2adbd0e6a501aaaf&imgtype=0&src=http%3A%2F%2Fimg2.touxiang.cn%2Ffile%2F20170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  55. },
  56. {
  57. "title": "",
  58. "pic": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498372396639&di=43f031aa703d741f2adbd0e6a501aaaf&imgtype=0&src=http%3A%2F%2Fimg2.touxiang.cn%2Ffile%2F20170524%2Feaca9092ca852e6e9c6ec46b7b14f12f.jpg"
  59. }
  60. ],
  61. "user": { 梦想用户
  62. "id": 1,
  63. "phone": "13880642880",
  64. "nickname": "name1",
  65. "avatar": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498379352960&di=0e62b422eb24482043701a568fb290d8&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201507%2F14%2F20150714130925_kzrcM.thumb.224_0.jpeg",
  66. "birthday": "2000-06-21",
  67. "sign": 0,
  68. "money": 0,
  69. "coin": 1300,
  70. "sex": 1,
  71. "signture": "",
  72. "height": 170,
  73. "work": "",
  74. "emotion": 1,
  75. "address": "",
  76. "city": "",
  77. "detail_address": "",
  78. "status": 1,
  79. "wechat": "",
  80. "weibo": "",
  81. "remember_token": "",
  82. "created_at": "2017-06-25 10:27:08",
  83. "updated_at": "2017-06-25 15:20:11",
  84. "deleted_at": null
  85. }
  86. },
  87. "top3user": [ 支持者前三
  88. {
  89. "id": 2,
  90. "phone": "13880642881",
  91. "nickname": "name2",
  92. "avatar": "https://timgsa.baidu.com/timg?imamb.224_0.jpeg",
  93. "score": 2399
  94. }
  95. ],
  96. "number": 1 支持乘数
  97. }
  98. }
  99. * @apiErrorExample {json} Error-Response:
  100. *HTTP/1.1 400 Bad Request
  101. *{
  102. * "status": false,
  103. * "status_code": 1105,
  104. * "message": "用户不存在",
  105. * "data": null
  106. * }
  107. */
  108. public function show(Request $request)
  109. {
  110. $type = $request->type; //类型
  111. $id = $request->id; //梦想ID
  112. if (empty($id)) return $this->error(ErrorCode::KEY_ERROR);
  113. $support_dream = SupportDreamModel::where('dream_id',$id)->get();
  114. $top = [] ;
  115. $topuser = []; //所有支持用户排行
  116. $top3user = []; // 支持用户排行前三
  117. foreach ($support_dream as $item) {
  118. if (!array_key_exists($item->user_id,$top)) {
  119. $top[$item->user_id] = $item->score;
  120. }else{
  121. $top[$item->user_id] += $item->score;
  122. }
  123. }
  124. arsort($top);
  125. foreach ($top as $user_id => $score){
  126. $user = UserInfoModel::find($user_id);
  127. $user->score = $score;
  128. $topuser[] = $user;
  129. if(count($top3user) <= 2) $top3user[] = $user ;
  130. }
  131. if ($type == 'paihang') return $this->api($topuser);
  132. $interactios = InteractionInfo::where('dream_id',$id)->orderBy('id','desc')->get();
  133. foreach ($interactios as $item) {
  134. $item->comments;
  135. foreach ($item->comments as $comment) {
  136. $comment->pic = UserInfoModel::find($comment->user_id)->pic;
  137. $comment->replay;
  138. foreach ($comment->replay as $k1 => $v1){
  139. $v1->pic = UserInfoModel::find($v1->user_id)->pic;
  140. }
  141. }
  142. }
  143. if ($type == 'interaction') return $this->api(compact('interactios'));
  144. // 梦想 图片 支持者前三 支持乘数参数 用户余额 梦想分数
  145. // $money = $user->money;
  146. $dream_info = DreamInfoModel::find($id);
  147. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first()->moey;
  148. $a = count($setting) > 0 ? $setting->key : '';
  149. $created_at = $dream_info ? $dream_info->created_at : 0;
  150. $b = intval((time()-strtotime($created_at))/60) ;
  151. $c = count($setting) > 0 ? $setting->value : '';
  152. //Todo 支持乘数目公式
  153. $number =round( -$a * $b + $c,1);
  154. if ($number <= 1) $number = 1;
  155. $dream = DreamInfoModel::with(['imgs','user'])->find($id);
  156. return $this->api(compact('dream','top3user','number'));
  157. }
  158. /**
  159. * @api {post} /api/dream/support 支持梦想
  160. * @apiDescription 支持梦想
  161. * @apiGroup Dream
  162. * @apiPermission Passport
  163. * @apiVersion 0.1.0
  164. * @apiParam {int} coin 支持梦想币数量
  165. * @apiParam {int} id 梦想ID
  166. * @apiSuccessExample {json} Success-Response:
  167. * HTTP/1.1 200 OK
  168. *{
  169. * "status": true,
  170. * "status_code": 0,
  171. * "message": "",
  172. * "data": {
  173. * "top3user": [
  174. * {
  175. * "id": 1,
  176. * "phone": "13880642880",
  177. * "nickname": "name1",
  178. * "avatar": "https://timgsa.baidu.cb9999_1.224_0.jpeg",
  179. * "score": 4640 支持总分数
  180. * }
  181. * ],
  182. * "mark": 4640, 梦想的分数
  183. * "get_coin": 616, 梦想获得的梦想币
  184. * "user_coin": 684, 当前用户剩余梦想币
  185. * "number": 5 支持乘数
  186. * }
  187. *}
  188. * @apiErrorExample {json} Error-Response:
  189. *HTTP/1.1 400 Bad Request
  190. * {
  191. * "state": false,
  192. * "code": 1000,
  193. * "message": "传入参数不正确",
  194. * "data": null or []
  195. * }
  196. * 可能出现的代码
  197. * {
  198. * "status": false,
  199. * "status_code": 1303,
  200. * "message": "余额不足",
  201. * "data": null
  202. * }
  203. *
  204. */
  205. public function support(Request $request)
  206. {
  207. $validator = \Validator::make($request->all(),
  208. [
  209. 'coin' => 'required',
  210. 'id' => 'required',
  211. ],
  212. [
  213. 'coin.required' => '梦想币不能为空',
  214. 'id.required' => '支持梦想id不能为空',
  215. ]
  216. );
  217. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  218. $user = $this->getUser();
  219. $dream_id = $request->id;
  220. $dream_info = DreamInfoModel::find($dream_id);
  221. $user_id = $dream_info->user_id;
  222. $coin = $request->coin;
  223. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  224. $a = count($setting) > 0 ? $setting->key : '';
  225. $b = intval((time()-strtotime($dream_info->created_at))/60) ;
  226. $c = count($setting) > 0 ? $setting->value : '';
  227. //Todo 支持乘数目公式
  228. $number =round( -$a * $b + $c,1);
  229. if ($number <= 1) $number = 1;
  230. if ($user->coin < $coin) {
  231. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  232. }else{
  233. $user->coin -= $coin;
  234. $user->save();
  235. $dream_info->get_coin += $coin;
  236. $dream_info->mark += $coin*$number;
  237. $dream_info->save();
  238. $data = [
  239. 'user_id'=>$user->id,
  240. 'dream_id'=>$dream_id,
  241. 'to_user_id'=>$user_id,
  242. 'coin'=>$coin,
  243. 'score'=>$coin*$number,
  244. ];
  245. $ok = SupportDreamModel::create($data);
  246. if (!$ok) {
  247. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  248. }
  249. // 记录充值记录 新增系统消息
  250. $data = [
  251. 'from_id' =>$user->id,
  252. 'to_id' => $user_id,
  253. 'from_amount' => $coin,
  254. 'to_amount' => $coin,
  255. 'from_type' => AccountLog::TYPE_COIN,
  256. 'to_type' => AccountLog::TYPE_COIN,
  257. 'op' => AccountLog::OP_SUPPORT,
  258. ];
  259. AccountLog::create($data);
  260. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  261. $info = [
  262. 'user_id' => $user_id,
  263. 'message' => $message,
  264. ];
  265. SystemInfoModel::create($info);
  266. // 支持成功返回数据到当前页面 top3user
  267. $support_dream = SupportDreamModel::where('dream_id',$dream_id)->get();
  268. $top = [] ;
  269. $top3user = []; // 支持用户排行前三
  270. foreach ($support_dream as $item) {
  271. if (!array_key_exists($item->user_id,$top)) {
  272. $top[$item->user_id] = $item->score;
  273. }else{
  274. $top[$item->user_id] += $item->score;
  275. }
  276. }
  277. arsort($top);
  278. foreach ($top as $user_id => $score){
  279. $user = UserInfoModel::find($user_id);
  280. $user->score = $score;
  281. if(count($top3user) <= 2) $top3user[] = $user ;
  282. }
  283. // 梦想分 mark
  284. $mark = $dream_info->mark;
  285. $get_coin = $dream_info->get_coin;
  286. $user_coin = $user->coin;
  287. return $this->api(compact('top3user','mark','get_coin','user_coin','number'));
  288. }
  289. }
  290. // 收藏关注梦想
  291. /**
  292. * @api {get} /api/dream/collection 收藏梦想
  293. * @apiDescription 收藏梦想
  294. * @apiGroup Dream
  295. * @apiPermission Passport
  296. * @apiVersion 0.1.0
  297. * @apiParam {int} id 梦想ID
  298. * @apiSuccessExample {json} Success-Response:
  299. * HTTP/1.1 200 OK
  300. *{
  301. * "status": true,
  302. * "status_code": 0,
  303. * "message": "",
  304. * "data": ""
  305. *}
  306. * @apiErrorExample {json} Error-Response:
  307. *HTTP/1.1 400 Bad Request
  308. * {
  309. * "state": false,
  310. * "code": 1000,
  311. * "message": "传入参数不正确",
  312. * "data": null or []
  313. * }
  314. *
  315. */
  316. public function collection(Request $request)
  317. {
  318. $validator = \Validator::make($request->all(),
  319. [
  320. 'id' => 'required',
  321. ],
  322. [
  323. 'id.required' => '收藏梦想id不能为空',
  324. ]
  325. );
  326. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  327. $user = $this->getUser();
  328. $data = [
  329. 'user_id' =>$user->id,
  330. 'dream_id' =>$request->id,
  331. 'dream_user_id' =>DreamInfoModel::find($request->id)->user_id,
  332. ];
  333. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  334. where('dream_id',$request->id)->first();
  335. if (!$user_care_dream) {
  336. UserCareDream::create($data);
  337. }
  338. return $this->api('');
  339. }
  340. /**
  341. * @api {get} /api/dream/search 梦想搜索
  342. * @apiDescription 梦想搜索)
  343. * @apiGroup Dream
  344. * @apiPermission none
  345. * @apiVersion 0.1.0
  346. * @apiParam {string} keyword 关键字
  347. * @apiSuccessExample {json} Success-Response:
  348. * HTTP/1.1 200 OK
  349. *{
  350. * "status": true,
  351. * "status_code": 0,
  352. * "message": "",
  353. * "data": {
  354. * "data": [
  355. * {
  356. * "dream": "haha", 梦想名
  357. * "user_pic": [
  358. * {
  359. * "pic": "", 用户头像
  360. * }
  361. * ],
  362. * "dream_img": "", 梦想图片
  363. * }
  364. * ]
  365. * }
  366. *}
  367. * @apiErrorExample {json} Error-Response:
  368. * HTTP/1.1 400 Bad Request
  369. */
  370. public function search(Request $request)
  371. {
  372. $user = $this->getUser();
  373. if (empty($request->keyword)) {
  374. return $this->api('');
  375. }
  376. $keyword ='%'.$request->keyword.'%';
  377. $data = DreamInfoModel::where('dream','like',$keyword)->
  378. orWhere('sign','like',$keyword)->get();
  379. foreach ($data as $k => $value) {
  380. $value->user_pic = $value->dreamFindUser;
  381. $value->dream_img = $value->dreamImgsFirst->pic;
  382. }
  383. $this->insertSearchTable($user->id,$request->keyword);
  384. return $this->api(compact('data'));
  385. }
  386. public function insertSearchTable($id,$keyword)
  387. {
  388. $info = SearchInfoModel::where('user_id',$id)->
  389. where('search',trim($keyword))->first();
  390. if (count($info) == 0) {
  391. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  392. }else{
  393. $info->times += 1;
  394. $info->save();
  395. }
  396. }
  397. /**
  398. * @api {post} /api/dream/store 发布梦想
  399. * @apiDescription 发布梦想
  400. * @apiParam {string} pics 梦想图片 数组
  401. * @apiParam {string} video 梦想视频
  402. * @apiParam {string} name 梦想标题
  403. * @apiParam {string} about 梦想介绍
  404. * @apiParam {int} coin 梦想币
  405. * @apiParam {int} time 实现时间默认21*3600
  406. * @apiGroup Dream
  407. * @apiPermission Passport
  408. * @apiVersion 0.1.0
  409. * @apiSuccessExample {json} Success-Response:
  410. * {
  411. * "status": true,
  412. * "status_code": 0,
  413. * "message": "",
  414. * "data": ""
  415. *}
  416. * HTTP/1.1 200 OK
  417. * @apiErrorExample {json} Error-Response:
  418. * {
  419. * "status": false,
  420. * "status_code": 1203,
  421. * "message": "附件不存在",
  422. * "data": null
  423. * }
  424. * HTTP/1.1 400 Bad Request
  425. */
  426. public function store(Request $request)
  427. {
  428. $user = $this->getUser();
  429. $validator = \Validator::make($request->all(),
  430. [
  431. 'name' => 'required',
  432. 'about' => 'required',
  433. 'coin' => 'required|integer',
  434. 'time' => 'required|integer',
  435. ],
  436. [
  437. 'name.required' => '梦想标题必填',
  438. 'about.required' => '梦想介绍必填',
  439. 'coin.required' => '梦想币必填',
  440. 'coin.integer' => '梦想币必须是正整数',
  441. 'time.required' => '实现时间必填',
  442. 'time.integer' => '实现时间必须是正整数',
  443. ]
  444. );
  445. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  446. \Log::debug($user);
  447. $data2 = $user->myCareNum;
  448. $care_num = count($data2);
  449. $setting = BaseSettingsModel::where('category','paihang')->first();
  450. $a = $setting?$setting->key:1;
  451. $b = $setting?$setting->value:1;
  452. $t = 21*3600 / 60;
  453. $data = $request->except('_token','pics');
  454. $data['user_id'] = $user->id;
  455. \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  456. if ($care_num == 0) {
  457. $data['score'] = (($a/$t) + $b)*100000000 ;
  458. }else{
  459. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  460. }
  461. $data['created_at'] = date('Y-m-d H:i:s');
  462. $data['updated_at'] = date('Y-m-d H:i:s');
  463. $dream_id = DreamInfoModel::insertGetId($data);
  464. if ($dream_id) {
  465. // 梦想创建成功 关联关系中最新动态+1
  466. /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  467. foreach ($info as $item) {
  468. $item->dream_num += 1;
  469. $item->updated_at = date('Y-m-d H:i:s');
  470. $item->save();
  471. }*/
  472. $pics = $request->pics;
  473. $video = $request->video;
  474. if (!is_array($pics) && empty($pics) && empty($video)) {
  475. DreamInfoModel::destroy($dream_id);
  476. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  477. }
  478. $data = [];
  479. if (is_array($pics)) {
  480. foreach ($pics as $v){
  481. $data[] = [
  482. 'dream_id'=>$dream_id,
  483. 'pic' => $v,
  484. 'created_at' =>date('Y-m-d H:i:s'),
  485. 'updated_at' =>date('Y-m-d H:i:s'),
  486. ];
  487. }
  488. DreamImages::insert($data);
  489. }
  490. return $this->api('');
  491. }
  492. }
  493. }