MyController.php 18 KB

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