HomeController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Api\V1\Controller;
  4. use App\Models\BaseSettingsModel;
  5. use App\Models\DreamImages;
  6. use App\Models\DreamInfoModel;
  7. use App\Models\SystemInfoModel;
  8. use App\Models\UserCareUser;
  9. use App\Models\UserDream;
  10. use App\Models\UserInfoModel;
  11. use Illuminate\Http\Request;
  12. use App\Services\Base\ErrorCode;
  13. class HomeController extends Controller
  14. {
  15. /**
  16. * @api {get} /api/user/index/ 用户信息(index)
  17. * @apiDescription 用户信息(index)
  18. * @apiGroup Home
  19. * @apiPermission Passport
  20. * @apiVersion 0.1.0
  21. * @apiParam {int} user_id 用户ID
  22. * @apiSuccessExample {json} Success-Response:
  23. * HTTP/1.1 200 OK
  24. * {
  25. * "nickname": "", 名字
  26. * "pic": "", 头像
  27. * "tall": "", 身高
  28. * "job": "", 职业
  29. * "emotion": "1", 情感
  30. * "address": "",地址
  31. * "score": 1000, 支持分
  32. * "care": 4, 关注
  33. * "fens": 3, 粉丝
  34. * "user_dream": [
  35. * "id": 3,
  36. * "dream": "3去旅游",
  37. * "about": "",
  38. * "money": 0,
  39. * "time": 0,
  40. * "get_money": 0,
  41. * "mark": 0,
  42. * "status": 0,
  43. * "pic": [],
  44. * "dream_imgs": []
  45. * "near_dream_pic": [
  46. * {
  47. * "pic": "234"
  48. * },
  49. * ]
  50. * }
  51. * @apiErrorExample {json} Error-Response:
  52. *HTTP/1.1 400 Bad Request
  53. *{
  54. *"status": false,
  55. *"status_code": 1105,
  56. * "message": "用户不存在",
  57. *"data": null
  58. * }
  59. */
  60. public function index(Request $request)
  61. {
  62. $user_id = $request->user_id;
  63. $care = UserCareUser::where('user_id',$user_id)->get();
  64. $fens = UserCareUser::where('other_user_id',$user_id)->get();
  65. $user = UserInfoModel::find($user_id);
  66. if (count($user == 0)) return $this->error(ErrorCode::USER_DOES_NOT_EXIST);
  67. $job = BaseSettingsModel::where(['category' => 'job'])->where(['key' => $user->job])->first();
  68. $job = count($job) > 0 ? $job->value : '';
  69. $emotion = BaseSettingsModel::where(['category' => 'emotion'])->where(['key' => $user->emotion])->first();
  70. $emotion = count($emotion) > 0 ? $emotion->value : '';
  71. // 当前梦想
  72. $near_dream_id = UserDream::where('user_id',$user_id)->orderBy('id','desc')->first()->dream_id;
  73. $near_dream =DreamInfoModel::find($near_dream_id);
  74. // 封面图片
  75. $near_dream_pic = DreamImages::where('dream_id',$near_dream_id)->select('pic')->get();
  76. // 曾经的梦想
  77. $dreams = $user->UserDream;
  78. foreach ($dreams as $dream){
  79. $dream->pic = $dream->dreamImgs;
  80. }
  81. $user->score = 1000;//自定义 算法
  82. $user->care = count($care);
  83. $user->fens = count($fens);
  84. $user->job = $job;
  85. $user->emotion = $emotion;
  86. // 支持的梦想
  87. $sup_dreams = $user->supDream;
  88. foreach ($sup_dreams as $sup_dream){
  89. $sup_dream->pic = $sup_dream->dreamImgs;
  90. }
  91. return $this->api(compact('user','near_dream','sup_dreams','near_dream_pic'));
  92. }
  93. /**
  94. * @api {post} /api/user/support 支持梦想
  95. * @apiDescription 支持梦想
  96. * @apiGroup Home
  97. * @apiPermission Passport
  98. * @apiVersion 0.1.0
  99. * @apiParam {int} coin 支持梦想币数量
  100. * @apiParam {int} dream_id 梦想ID
  101. * @apiSuccessExample {json} Success-Response:
  102. * HTTP/1.1 200 OK
  103. *{
  104. * "status": true,
  105. * "status_code": 0,
  106. * "message": "",
  107. * "data": ""
  108. *}
  109. * @apiErrorExample {json} Error-Response:
  110. *HTTP/1.1 400 Bad Request
  111. * {
  112. * "state": false,
  113. * "code": 1000,
  114. * "message": "传入参数不正确",
  115. * "data": null or []
  116. * }
  117. * 可能出现的代码
  118. * {
  119. * "status": false,
  120. * "status_code": 1303,
  121. * "message": "商户余额不足",
  122. * "data": null
  123. * }
  124. *
  125. */
  126. public function support(Request $request)
  127. {
  128. $user = $this->getUser();
  129. $dream_id = $request->dream_id;
  130. $dream_info = DreamInfoModel::find($dream_id);
  131. $validator = \Validator::make($request->all(),
  132. [
  133. 'coin' => 'required',
  134. 'dream_id' => 'required',
  135. ],
  136. [
  137. 'coin.required' => '梦想币不能为空',
  138. 'dream_id.required' => '支持对象不能为空',
  139. ]
  140. );
  141. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  142. $coin = $request->coin;
  143. if ($user->money < $coin) {
  144. return $this->error(ErrorCode::MERCHANT_BALANCE_NOT_ENOUGH);
  145. }else{
  146. $user->money = $user->money - $coin;
  147. $user->save();
  148. $dream_info->get_money += $coin;
  149. $dream_info->save();
  150. $data = [
  151. 'user_id'=>$user->id,
  152. 'other_id'=>$dream_id,
  153. 'coin'=>$coin,
  154. ];
  155. $ok = SystemInfoModel::create($data);
  156. if (!$ok) {
  157. return $this->error(ErrorCode::MERCHANT_SERVICE_STATUS_INVALID);
  158. }
  159. return $this->api('');
  160. }
  161. }
  162. /**
  163. * @api {post} /api/user/interaction 互动
  164. * @apiDescription 互动
  165. * @apiGroup Home
  166. * @apiPermission Passport
  167. * @apiVersion 0.1.0
  168. * @apiParam {int} user_id 用户ID
  169. * @apiSuccessExample {json} Success-Response:
  170. * HTTP/1.1 200 OK
  171. * {
  172. * "status": true,
  173. * "status_code": 0,
  174. * "message": "",
  175. * "data": {
  176. * 评论的梦想
  177. * "dream1": [
  178. * {
  179. * "dream": "1的梦想",
  180. * "about": "介绍",
  181. * "pic": [
  182. * {
  183. * "pic": "111",
  184. * }
  185. * ],
  186. * "user": [
  187. * {
  188. * "pic": "",
  189. * ],
  190. * "comments": [
  191. * {
  192. * "level": 0,
  193. * "content": "EST",
  194. * "created_at": "2017-04-27 12:17:20",
  195. * }
  196. * ],
  197. * 回复的梦想
  198. * "dream2": [
  199. * {
  200. * "level": 0,
  201. * "content": "EST",
  202. * "created_at": "2017-04-27 12:17:20",
  203. * "updated_at": null,
  204. * "deleted_at": null,
  205. * "reply_dream": {
  206. * "dream": "1的梦想",
  207. * "about": "介绍",
  208. * "time": 0,
  209. * "dream_imgs": [
  210. * {
  211. * "pic": "111",
  212. * },
  213. * ]
  214. * },
  215. * "reply_dream_pic": [
  216. * {
  217. * "pic": "111",
  218. * }
  219. * ],
  220. * "reply_created_at": {
  221. * "date": "2017-06-13 02:26:31.000000",
  222. * "timezone_type": 3,
  223. * "timezone": "UTC"
  224. * },
  225. * "reply_content": "haha",
  226. * "reply_level": 0,
  227. * "dream": {
  228. * "dream": "1的梦想",
  229. * "about": "介绍",
  230. * "money": 5000,
  231. * "time": 0,
  232. * "dream_imgs": [
  233. * {
  234. * "pic": "111",
  235. * },
  236. * ]
  237. * }
  238. * }
  239. * ]
  240. * }
  241. * }
  242. * @apiErrorExample {json} Error-Response:
  243. *HTTP/1.1 400 Bad Request
  244. * {
  245. * "status": false,
  246. * "status_code": 1500,
  247. * "message": "会员不存在",
  248. * "data": null
  249. * }
  250. */
  251. public function interaction(Request $request)
  252. {
  253. $user_id = $request->user_id;
  254. $user = UserInfoModel::find($user_id);
  255. if (count($user) == 0) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  256. // 参与的评论与回复 梦想
  257. $dream1 = $user->comDream;
  258. foreach ($dream1 as $item){
  259. $item->pic = $item->dreamImgs;
  260. $item->user = $item->dreamUser;
  261. $item->comments = $item->DreamInfo;
  262. $item->created_at = $item->pivot->created_at;
  263. $item->content = $item->pivot->content;
  264. $item->level = $item->pivot->level;
  265. }
  266. $dream2 = $user->replyDream;
  267. foreach ($dream2 as $comment) {
  268. $comment->reply_dream = $comment->dream;
  269. $comment->reply_dream_pic = $comment->dream->dreamImgs;
  270. $comment->reply_created_at = $comment->pivot->created_at;
  271. $comment->reply_content = $comment->pivot->content;
  272. $comment->reply_level = $comment->pivot->level;
  273. }
  274. return $this->api(compact('dream1','dream2'));
  275. }
  276. /**
  277. * @api {post} /api/Home/paihang 排行
  278. * @apiDescription 排行
  279. * @apiGroup Home
  280. * @apiPermission Passport
  281. * @apiVersion 0.1.0
  282. * @apiParam {int} user_id 用户ID
  283. * @apiSuccessExample {json} Success-Response:
  284. * HTTP/1.1 200 OK
  285. *{
  286. * "status": true,
  287. * "status_code": 0,
  288. * "message": "",
  289. * "data": {
  290. * "arr3": [
  291. * {
  292. * "nickname": "", 昵称
  293. * "pic": "", 头像
  294. * "coin": 23 总支持梦想币
  295. * }
  296. * ]
  297. * }
  298. *}
  299. *
  300. * @apiErrorExample {json} Error-Response:
  301. *HTTP/1.1 400 Bad Request
  302. *{
  303. * "status": false,
  304. * "status_code": 1105,
  305. * "message": "用户不存在",
  306. * "data": null
  307. * }
  308. */
  309. public function paihang(Request $request)
  310. {
  311. // 获取支持过用户的人
  312. $user_id = $request->user_id;
  313. $user = UserInfoModel::find($user_id);
  314. if (count($user) == 0) return $this->error(ErrorCode::MEMBER_NOT_EXIST);
  315. $dreams = $user->UserDream;
  316. $arr1 = [];
  317. foreach ($dreams as $dream) {
  318. $arr1[] = $dream->systemInfo;
  319. }
  320. // 用户总的支持梦想币
  321. $arr2 = [];
  322. foreach ($arr1 as $v) {
  323. foreach ($v as $item){
  324. if (!array_key_exists($item->user_id,$arr2)) {
  325. $arr2[$item->user_id] = $item->coin;
  326. }else{
  327. $arr2[$item->user_id] += $item->coin;
  328. }
  329. }
  330. }
  331. $arr3 = [] ;
  332. foreach ($arr2 as $k => $v){
  333. $user = UserInfoModel::find($k);
  334. $user->coin = $v;
  335. $arr3[] = $user;
  336. }
  337. return $this->api(compact('arr3'));
  338. }
  339. }