HomeController.php 9.4 KB

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