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