HomeController.php 11 KB

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