DreamController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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))/3600) ;
  151. $c = count($setting) > 0 ? $setting->value : '';
  152. //Todo 支持乘数目公式
  153. $number = -$a * $b + $c;
  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. * @apiParam {int} number 支持乘数
  167. * @apiSuccessExample {json} Success-Response:
  168. * HTTP/1.1 200 OK
  169. *{
  170. * "status": true,
  171. * "status_code": 0,
  172. * "message": "",
  173. * "data": ""
  174. *}
  175. * @apiErrorExample {json} Error-Response:
  176. *HTTP/1.1 400 Bad Request
  177. * {
  178. * "state": false,
  179. * "code": 1000,
  180. * "message": "传入参数不正确",
  181. * "data": null or []
  182. * }
  183. * 可能出现的代码
  184. * {
  185. * "status": false,
  186. * "status_code": 1303,
  187. * "message": "商户余额不足",
  188. * "data": null
  189. * }
  190. *
  191. */
  192. public function support(Request $request)
  193. {
  194. $validator = \Validator::make($request->all(),
  195. [
  196. 'coin' => 'required',
  197. 'id' => 'required',
  198. ],
  199. [
  200. 'coin.required' => '梦想币不能为空',
  201. 'id.required' => '支持梦想id不能为空',
  202. ]
  203. );
  204. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  205. $user = $this->getUser();
  206. $dream_id = $request->id;
  207. $dream_info = DreamInfoModel::find($dream_id);
  208. $user_id = $dream_info->user_id;
  209. $coin = $request->coin;
  210. $setting = BaseSettingsModel::where('category','score')->select('key','value')->first();
  211. $a = count($setting) > 0 ? $setting->key : '';
  212. $b = intval((time()-strtotime($dream_info->created_at))/3600) ;
  213. $c = count($setting) > 0 ? $setting->value : '';
  214. //Todo 支持乘数目公式
  215. $number = -$a * $b + $c;
  216. if ($number <= 1) $number = 1;
  217. if ($user->coin < $coin) {
  218. return $this->error(ErrorCode::COIN_NOT_ENOUGH);
  219. }else{
  220. $user->coin -= $coin;
  221. $user->save();
  222. $dream_info->get_coin += $coin;
  223. $dream_info->save();
  224. $data = [
  225. 'user_id'=>$user->id,
  226. 'dream_id'=>$dream_id,
  227. 'to_user_id'=>$user_id,
  228. 'coin'=>$coin,
  229. 'score'=>$coin*$number,
  230. ];
  231. $ok = SupportDreamModel::create($data);
  232. if (!$ok) {
  233. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  234. }
  235. // 记录充值记录 新增系统消息
  236. $data = [
  237. 'from_id' =>$user->id,
  238. 'to_id' => $user_id,
  239. 'from_amount' => $coin,
  240. 'to_amount' => $coin,
  241. 'from_type' => AccountLog::TYPE_COIN,
  242. 'to_type' => AccountLog::TYPE_COIN,
  243. 'op' => AccountLog::OP_SUPPORT,
  244. ];
  245. AccountLog::create($data);
  246. $message = UserInfoModel::find($user->id)->nickname.'为你的梦想捐赠'.$coin.'梦想币';
  247. $info = [
  248. 'user_id' => $user_id,
  249. 'message' => $message,
  250. ];
  251. SystemInfoModel::create($info);
  252. return $this->api($number);
  253. }
  254. }
  255. // 收藏关注梦想
  256. /**
  257. * @api {get} /api/dream/collection 收藏梦想
  258. * @apiDescription 收藏梦想
  259. * @apiGroup Dream
  260. * @apiPermission Passport
  261. * @apiVersion 0.1.0
  262. * @apiParam {int} id 梦想ID
  263. * @apiSuccessExample {json} Success-Response:
  264. * HTTP/1.1 200 OK
  265. *{
  266. * "status": true,
  267. * "status_code": 0,
  268. * "message": "",
  269. * "data": ""
  270. *}
  271. * @apiErrorExample {json} Error-Response:
  272. *HTTP/1.1 400 Bad Request
  273. * {
  274. * "state": false,
  275. * "code": 1000,
  276. * "message": "传入参数不正确",
  277. * "data": null or []
  278. * }
  279. *
  280. */
  281. public function collection(Request $request)
  282. {
  283. $validator = \Validator::make($request->all(),
  284. [
  285. 'id' => 'required',
  286. ],
  287. [
  288. 'id.required' => '收藏梦想id不能为空',
  289. ]
  290. );
  291. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  292. $user = $this->getUser();
  293. $data = [
  294. 'user_id' =>$user->id,
  295. 'dream_id' =>$request->id,
  296. 'dream_user_id' =>DreamInfoModel::find($request->id)->user_id,
  297. ];
  298. $user_care_dream = UserCareDream::where('user_id',$user->id)->
  299. where('dream_id',$request->id)->first();
  300. if (!$user_care_dream) {
  301. UserCareDream::create($data);
  302. }
  303. return $this->api('');
  304. }
  305. /**
  306. * @api {get} /api/dream/search 梦想搜索
  307. * @apiDescription 梦想搜索)
  308. * @apiGroup Dream
  309. * @apiPermission none
  310. * @apiVersion 0.1.0
  311. * @apiParam {string} keyword 关键字
  312. * @apiSuccessExample {json} Success-Response:
  313. * HTTP/1.1 200 OK
  314. *{
  315. * "status": true,
  316. * "status_code": 0,
  317. * "message": "",
  318. * "data": {
  319. * "data": [
  320. * {
  321. * "dream": "haha", 梦想名
  322. * "user_pic": [
  323. * {
  324. * "pic": "", 用户头像
  325. * }
  326. * ],
  327. * "dream_img": "", 梦想图片
  328. * }
  329. * ]
  330. * }
  331. *}
  332. * @apiErrorExample {json} Error-Response:
  333. * HTTP/1.1 400 Bad Request
  334. */
  335. public function search(Request $request)
  336. {
  337. $user = $this->getUser();
  338. if (empty($request->keyword)) {
  339. return $this->api('');
  340. }
  341. $keyword ='%'.$request->keyword.'%';
  342. $data = DreamInfoModel::where('dream','like',$keyword)->
  343. orWhere('sign','like',$keyword)->get();
  344. foreach ($data as $k => $value) {
  345. $value->user_pic = $value->dreamFindUser;
  346. $value->dream_img = $value->dreamImgsFirst->pic;
  347. }
  348. $this->insertSearchTable($user->id,$request->keyword);
  349. return $this->api(compact('data'));
  350. }
  351. public function insertSearchTable($id,$keyword)
  352. {
  353. $info = SearchInfoModel::where('user_id',$id)->
  354. where('search',trim($keyword))->first();
  355. if (count($info) == 0) {
  356. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  357. }else{
  358. $info->times += 1;
  359. $info->save();
  360. }
  361. }
  362. /**
  363. * @api {post} /api/dream/store 发布梦想
  364. * @apiDescription 发布梦想
  365. * @apiParam {string} pics 梦想图片 数组
  366. * @apiParam {string} video 梦想视频
  367. * @apiParam {string} name 梦想标题
  368. * @apiParam {string} about 梦想介绍
  369. * @apiParam {int} coin 梦想币
  370. * @apiParam {int} time 实现时间默认21*3600
  371. * @apiGroup Dream
  372. * @apiPermission Passport
  373. * @apiVersion 0.1.0
  374. * @apiSuccessExample {json} Success-Response:
  375. * {
  376. * "status": true,
  377. * "status_code": 0,
  378. * "message": "",
  379. * "data": ""
  380. *}
  381. * HTTP/1.1 200 OK
  382. * @apiErrorExample {json} Error-Response:
  383. * {
  384. * "status": false,
  385. * "status_code": 1203,
  386. * "message": "附件不存在",
  387. * "data": null
  388. * }
  389. * HTTP/1.1 400 Bad Request
  390. */
  391. public function store(Request $request)
  392. {
  393. $user = $this->getUser();
  394. $validator = \Validator::make($request->all(),
  395. [
  396. 'name' => 'required',
  397. 'about' => 'required',
  398. 'coin' => 'required|integer',
  399. 'time' => 'required|integer',
  400. ],
  401. [
  402. 'name.required' => '梦想标题必填',
  403. 'about.required' => '梦想介绍必填',
  404. 'coin.required' => '梦想币必填',
  405. 'coin.integer' => '梦想币必须是正整数',
  406. 'time.required' => '实现时间必填',
  407. 'time.integer' => '实现时间必须是正整数',
  408. ]
  409. );
  410. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  411. \Log::debug($user);
  412. $data2 = $user->myCareNum;
  413. $care_num = count($data2);
  414. $setting = BaseSettingsModel::where('category','paihang')->first();
  415. $a = $setting?$setting->key:1;
  416. $b = $setting?$setting->value:1;
  417. $t = 21*3600 / 60;
  418. $data = $request->except('_token','pics');
  419. $data['user_id'] = $user->id;
  420. \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  421. if ($care_num == 0) {
  422. $data['score'] = (($a/$t) + $b)*100000000 ;
  423. }else{
  424. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  425. }
  426. $data['created_at'] = date('Y-m-d H:i:s');
  427. $data['updated_at'] = date('Y-m-d H:i:s');
  428. $dream_id = DreamInfoModel::insertGetId($data);
  429. if ($dream_id) {
  430. // 梦想创建成功 关联关系中最新动态+1
  431. /* $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  432. foreach ($info as $item) {
  433. $item->dream_num += 1;
  434. $item->updated_at = date('Y-m-d H:i:s');
  435. $item->save();
  436. }*/
  437. $pics = $request->pics;
  438. $video = $request->video;
  439. if (!is_array($pics) && empty($pics) && empty($video)) {
  440. DreamInfoModel::destroy($dream_id);
  441. return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  442. }
  443. $data = [];
  444. if (is_array($pics)) {
  445. foreach ($pics as $v){
  446. $data[] = [
  447. 'dream_id'=>$dream_id,
  448. 'pic' => $v,
  449. 'created_at' =>date('Y-m-d H:i:s'),
  450. 'updated_at' =>date('Y-m-d H:i:s'),
  451. ];
  452. }
  453. DreamImages::insert($data);
  454. }
  455. return $this->api('');
  456. }
  457. }
  458. }