MyController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\BaseDictionaryOptionModel;
  4. use App\Models\BaseSettingsModel;
  5. use App\Models\DreamInfoModel;
  6. use App\Models\SearchInfoModel;
  7. use App\Models\SystemInfoModel;
  8. use App\Models\UserCareDream;
  9. use App\Models\UserInfoModel;
  10. use Illuminate\Http\Request;
  11. use App\Services\Base\ErrorCode;
  12. class MyController extends Controller
  13. {
  14. /**
  15. * @api {get} /api/my/show 个人首页
  16. * @apiDescription 个人首页
  17. * @apiGroup My
  18. * @apiPermission Passport
  19. * @apiSuccessExample {json} Success-Response:
  20. * HTTP/1.1 200 O* "status": true,
  21. *{
  22. * "status": true,
  23. * "status_code": 0,
  24. * "message": "",
  25. * "data": {
  26. * "id": 1,
  27. * "phone": "13880642880",
  28. * "nickname": "name1",
  29. * "avatar": "https://tims%2Fitem%225_kzrcM.thumb.224_0.jpeg",
  30. * "birthday": "2000-06-21",
  31. * "sign": 0,
  32. * "money": 0,
  33. * "coin": 1300,
  34. * "sex": 1,
  35. * "signture": "",
  36. * "height": 170,
  37. * "work": "",
  38. * "emotion": 1,
  39. * "address": "",
  40. * "city": "",
  41. * "detail_address": "",
  42. * "status": 1,
  43. * "wechat": "",
  44. * "weibo": "",
  45. * "remember_token": "",
  46. * "created_at": "2017-06-25 10:27:08",
  47. * "updated_at": "2017-06-25 15:20:11",
  48. * "deleted_at": null,
  49. * "care_dreams_number": 2,
  50. * "fans_number": 0,
  51. * "collection_number": 0,
  52. * "interaction_number": 0,
  53. * }
  54. *}
  55. * @apiErrorExample {json} Error-Response:
  56. * HTTP/1.1 400 Bad Request
  57. *{
  58. * "status": true,
  59. * "status_code": 0,
  60. * "message": "",
  61. * "data": {
  62. * "user": null
  63. * }
  64. *}
  65. */
  66. public function show()
  67. {
  68. $user = $this->getUser();
  69. /*
  70. * 我的关注人数,我的粉丝,收藏
  71. * 点赞?徽章?
  72. * */
  73. $care_num = $user->careDreams;
  74. $user->care_dreams_number = count($care_num);
  75. $fens = UserCareDream::where('dream_user_id',$user->id)->get();
  76. $user->fans_number = count($fens);
  77. $collection = $user->myCollection;
  78. $user->collection_number = count($collection);
  79. $interaction_infos = $user->allInteraction;
  80. $user->interaction_number = count($interaction_infos);
  81. return $this->api($user);
  82. }
  83. /**
  84. * @api {get} /api/my/edit 修改个人信息
  85. * @apiDescription 修改个人信息
  86. * @apiGroup My
  87. * @apiPermission Passport
  88. * @apiVersion 0.1.0
  89. * @apiSuccessExample {json} Success-Response:
  90. * HTTP/1.1 200 OK
  91. *{
  92. * "status": true,
  93. * "status_code": 0,
  94. * "message": "",
  95. "data": {
  96. "emotion": [
  97. {
  98. "value": "1",
  99. "name": "未婚"
  100. },
  101. {
  102. "value": "2",
  103. "name": "已婚"
  104. },
  105. {
  106. "value": "3",
  107. "name": "离异"
  108. }
  109. ],
  110. "sex": [
  111. {
  112. "value": "0",
  113. "name": "男"
  114. },
  115. {
  116. "value": "1",
  117. "name": "女"
  118. }
  119. ]
  120. }
  121. * }
  122. * @apiErrorExample {json} Error-Response:
  123. * HTTP/1.1 400 Bad Request
  124. * {
  125. * "status": false,
  126. * "status_code": 1500,
  127. * "message": "会员不存在",
  128. * "data": null
  129. * }
  130. */
  131. public function edit()
  132. {
  133. $sex = BaseDictionaryOptionModel::where('dictionary_table_code','user_info')->
  134. where('dictionary_code','sex')->get();
  135. $emotion = BaseDictionaryOptionModel::where('dictionary_table_code','user_info')->
  136. where('dictionary_code','emotion')->get();
  137. return $this->api(compact('emotion','sex'));
  138. }
  139. /**
  140. * @api {post} /api/my/update 保存个人信息
  141. * @apiDescription 保存个人信息
  142. * @apiParam {string} pic 头像
  143. * @apiParam {int} sex 性别
  144. * @apiParam {string} signture 个性签名
  145. * @apiParam {int} emotion 情感状态
  146. * @apiParam {string} work 职业
  147. * @apiParam {int} height 身高
  148. * @apiGroup My
  149. * @apiPermission Passport
  150. * @apiVersion 0.1.0
  151. * @apiSuccessExample {json} Success-Response:
  152. * HTTP/1.1 200 OK
  153. *{
  154. * "status": true,
  155. * "status_code": 0,
  156. * "message": "",
  157. * "data": ""
  158. *}
  159. * @apiErrorExample {json} Error-Response:
  160. * HTTP/1.1 400 Bad Request
  161. *{
  162. * "status": false,
  163. * "status_code": 600,
  164. * "message": "保存用户数据失败",
  165. * "data": null
  166. * }
  167. */
  168. public function update(Request $request)
  169. {
  170. $user = $this->getUser();
  171. $data = $request->except('_token');
  172. $ok = $user->update($data);
  173. if ($ok == true) {
  174. return $this->api('');
  175. }else{
  176. return $this->error(ErrorCode::SAVE_USER_FAILED);
  177. }
  178. }
  179. /**
  180. * @api {post} /api/my/recharge 充值
  181. * @apiDescription 充值
  182. * @apiGroup My
  183. * @apiParam {int} coin 充值金额
  184. * @apiPermission Passport
  185. * @apiVersion 0.1.0
  186. * @apiSuccessExample {json} Success-Response:
  187. * HTTP/1.1 200 OK
  188. * @apiErrorExample {json} Error-Response:
  189. * HTTP/1.1 400 Bad Request
  190. */
  191. public function recharge(Request $request)
  192. {
  193. $validator = \Validator::make($request->all(),
  194. [
  195. 'coin' => 'required|integer',
  196. ],
  197. [
  198. 'coin.required' => '请填写金额',
  199. 'coin.integer' => '请输入整数',
  200. ]
  201. );
  202. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  203. $user = $this->getUser();
  204. $user->coin += $request->coin;
  205. $user->save();
  206. return $this->api('');
  207. }
  208. /**
  209. * @api {get} /api/my/system_info 系统消息
  210. * @apiDescription 系统消息
  211. * @apiGroup My
  212. * @apiPermission Passport
  213. * @apiVersion 0.1.0
  214. * @apiSuccessExample {json} Success-Response:
  215. * HTTP/1.1 200 OK
  216. *{
  217. * "status": true,
  218. * "status_code": 0,
  219. * "message": "",
  220. * "data": {
  221. * "data": [
  222. * ...
  223. * ]
  224. *}
  225. * @apiErrorExample {json} Error-Response:
  226. * HTTP/1.1 400 Bad Request
  227. */
  228. public function systemInfo()
  229. {
  230. $user = $this->getUser();
  231. $data = SystemInfoModel::where('user_id',$user->id)->orderBy('id','desc')->get();
  232. return $this->api(compact('data'));
  233. }
  234. // 回复我的
  235. // public function replyMy()
  236. // {
  237. //
  238. // $user = $this->getUser();
  239. //// 梦想
  240. // $dreams = $user->UserDream;
  241. //
  242. // $data = $user->allInteraction;
  243. // foreach ($data as $item) {
  244. // $item->get_money = $item->dream->get_money;
  245. // $item->money = $item->dream->money;
  246. // }
  247. // dd($data) ;
  248. // if (count($dreams) == 0)
  249. // return $this->error(ErrorCode::DREAM_NOT_EXIST);
  250. // $comments_infos = [];
  251. // foreach ($dreams as $dream){
  252. // $comments_info = $dream->DreamInfo;
  253. // if (count($comments_info) > 0) {
  254. // foreach ($comments_info as $k => $value) {
  255. // $value->dream_name = $dream->dream;
  256. // $value->dream_about = $dream->about;
  257. // $value->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  258. // $value->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  259. // $value->reviewer = $value->CommentUser->nickname;
  260. // $value->reviewer_pic = $value->CommentUser->pic;
  261. // }
  262. // $comments_infos[] = $comments_info;
  263. // }
  264. // }
  265. //
  266. // return $this->api(compact('comments_infos'));
  267. // }
  268. /**
  269. * @api {get} /api/my/dream 我的梦想
  270. * @apiDescription 我的梦想
  271. * @apiGroup My
  272. * @apiPermission Passport
  273. * @apiVersion 0.1.0
  274. * @apiSuccessExample {json} Success-Response:
  275. * HTTP/1.1 200 OK
  276. *{
  277. * "status": true,
  278. * "status_code": 0,
  279. * "message": "",
  280. * "data": [
  281. * {
  282. * "id": 5,
  283. * "user_id": 1,
  284. * "name": "梦想标题1",
  285. * "about": "梦想介绍",
  286. * "coin": 2500,
  287. * "time": 21,
  288. * "get_coin": 0,
  289. * "status": 0,
  290. * "video": null,
  291. * "sign": "",
  292. * "created_at": "2017-06-25 12:45:22",
  293. * "updated_at": "2017-06-25 12:45:22",
  294. * "pic": "https://timgsa.baidu.com/timg?image7b14f12f.jpg",
  295. * },
  296. * ]
  297. *}
  298. * @apiErrorExample {json} Error-Response:
  299. * HTTP/1.1 400 Bad Request
  300. */
  301. public function dream()
  302. {
  303. $user = $this->getUser();
  304. $dreams = $user->dreams;
  305. if (count($dreams) == 0)
  306. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  307. foreach ($dreams as $dream){
  308. $dream->pic = count($dream->img) > 0 ? $dream->img->pic : '';
  309. }
  310. return $this->api($dreams);
  311. }
  312. /**
  313. * @api {get} /api/my/collection 我的收藏
  314. * @apiDescription 我的收藏
  315. * @apiGroup My
  316. * @apiPermission Passport
  317. * @apiVersion 0.1.0
  318. * @apiSuccessExample {json} Success-Response:
  319. * HTTP/1.1 200 OK
  320. *{
  321. * "status": true,
  322. * "status_code": 0,
  323. * "message": "",
  324. * "data": {
  325. * "data": [
  326. * { 梦想详情
  327. * "id": 12,
  328. * "user_id": 2,
  329. * "name": "用户2梦想标题166",
  330. * "about": "用户2梦想介绍666",
  331. * "coin": 2500,
  332. * "time": 21,
  333. * "get_coin": 0,
  334. * "status": 0,
  335. * "video": null,
  336. * "sign": "",
  337. * },
  338. * "img": {
  339. * "title": "",
  340. * "pic": "https://f12f.jpg" 梦想封面图片
  341. * }
  342. * ],
  343. * "users": {
  344. * "2": "https://xxx.jpeg" ID号和头像
  345. * }
  346. * }
  347. *}
  348. * @apiErrorExample {json} Error-Response:
  349. * HTTP/1.1 400 Bad Request
  350. */
  351. public function collection()
  352. {
  353. $user = $this->getUser();
  354. $dreams = $user->collection;
  355. $users = [];
  356. foreach ($dreams as $item) {
  357. if ($item->pivot->interaction_number > 0) {
  358. $user_info = UserInfoModel::find($item->pivot->dream_user_id);
  359. $avatar = $user_info ? $user_info->avatar : '';
  360. if (!array_key_exists($item->pivot->dream_user_id,$users)) {
  361. $users[$item->pivot->dream_user_id] = $avatar;
  362. }
  363. }
  364. $item->img;
  365. }
  366. return $this->api(compact('dreams','users'));
  367. }
  368. /**
  369. * @api {get} /api/my/setting 设置
  370. * @apiDescription 设置
  371. * @apiGroup My
  372. * @apiPermission Passport
  373. * @apiVersion 0.1.0
  374. * @apiSuccessExample {json} Success-Response:
  375. * HTTP/1.1 200 OK
  376. *{
  377. * "status": true,
  378. * "status_code": 0,
  379. * "message": "",
  380. * "data": {
  381. * "key": "2511789", 电话
  382. * "value": "关于喵喵介绍" 关于喵喵
  383. * }
  384. *}
  385. * @apiErrorExample {json} Error-Response:
  386. * HTTP/1.1 400 Bad Request
  387. */
  388. public function setting()
  389. {
  390. $data = BaseSettingsModel::where('category','miaomiao')->select('key','value')->first();
  391. return $this->api($data);
  392. }
  393. /**
  394. * @api {get} /api/my/search_collection 梦想搜索
  395. * @apiDescription 梦想搜索
  396. * @apiGroup My
  397. * @apiPermission none
  398. * @apiVersion 0.1.0
  399. * @apiParam {string} keyword 关键字
  400. * @apiSuccessExample {json} Success-Response:
  401. * HTTP/1.1 200 OK
  402. {
  403. "status": true,
  404. "status_code": 0,
  405. "message": "",
  406. "data": [
  407. {
  408. "id": 5,
  409. "user_id": 1,
  410. "name": "梦想标题1",
  411. "about": "梦想介绍",
  412. "coin": 2500,
  413. "time": 72000,
  414. "get_coin": 0,
  415. "mark": 0,
  416. "status": 0,
  417. "video": "url",
  418. "score": 100079365,
  419. "sign": "梦想达人",
  420. "created_at": "2017-06-25 12:45:22",
  421. "updated_at": "2017-06-28 15:50:41",
  422. "user": {},
  423. "imgs": []
  424. },
  425. ]
  426. }
  427. * @apiErrorExample {json} Error-Response:
  428. * HTTP/1.1 400 Bad Request
  429. */
  430. public function searchCollection(Request $request)
  431. {
  432. $user = $this->getUser();
  433. if (empty($request->keyword)) {
  434. return $this->api('');
  435. }
  436. $keyword ='%'.$request->keyword.'%';
  437. $data = DreamInfoModel::where('name','like',$keyword)->
  438. orWhere('sign','like',$keyword)->with('user','imgs')->get();
  439. $this->insertSearchTable($user->id,$request->keyword);
  440. $data = $user->careDreams()->whereHas('careDreams', function ($query) use ($keyword) {
  441. $query-> where('name','like',$keyword)->
  442. orWhere('sign','like',$keyword)->with('user','imgs');
  443. });
  444. return $data;
  445. return $this->api(compact('data'));
  446. }
  447. public function insertSearchTable($id,$keyword)
  448. {
  449. $info = SearchInfoModel::where('user_id',$id)->
  450. where('search',trim($keyword))->first();
  451. if (count($info) == 0) {
  452. SearchInfoModel::create(['user_id'=>$id,'search'=>trim($keyword),'times'=>1]);
  453. }else{
  454. $info->times += 1;
  455. $info->save();
  456. }
  457. }
  458. }