HomeController.php 11 KB

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