MyController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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\ReplyCommentsInfo;
  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 MyController extends Controller
  15. {
  16. /**
  17. * @api {get} /api/my/index 个人首页(index)
  18. * @apiDescription 个人首页(index)
  19. * @apiGroup My
  20. * @apiPermission Passport
  21. * @apiSuccessExample {json} Success-Response:
  22. * HTTP/1.1 200 O* "status": true,
  23. * "status_code": 0,
  24. * "message": "",
  25. * "data": {
  26. * "my_care_num": 4,关注
  27. * "my_fens_num": 3,粉丝
  28. * "my_collection_num": 2,收藏
  29. * "my_dream_num": 1,发帖
  30. * "user": {
  31. * "id": 1,
  32. * "tel": "13880642880",
  33. * "password": "",
  34. * "nickname": "",
  35. * "pic": "",
  36. * "sign": 0,
  37. * "money": 0,余额
  38. * "sex": 1,
  39. * "signture": "",个性签名
  40. * "tall": "",
  41. * "job": "",
  42. * "emotion": 1,
  43. * "address": "",
  44. * "detail_address": "",
  45. * "status": 1,
  46. * "remember_token": "",
  47. * }
  48. * }
  49. * @apiErrorExample {json} Error-Response:
  50. * HTTP/1.1 400 Bad Request
  51. *{
  52. * "status": true,
  53. * "status_code": 0,
  54. * "message": "",
  55. * "data": {
  56. * "user": null
  57. * }
  58. *}
  59. */
  60. public function index()
  61. {
  62. $user = $this->getUser();
  63. /*
  64. * 我的关注人数,我的粉丝,收藏
  65. * 点赞?徽章?
  66. * */
  67. $data1 = $user->myCareNum;
  68. $data2 = $user->myFens;
  69. $data3 = $user->myCollection;
  70. $data4 = $user->UserDream;
  71. $my_care_num = count($data1);
  72. $my_fens_num = count($data2);
  73. $my_collection_num = count($data3);
  74. $my_dream_num = count($data4);//发帖数
  75. return $this->api(compact('my_care_num','my_fens_num','my_collection_num','my_dream_num','user'));
  76. }
  77. /**
  78. * @api {get} /api/my/persona 个人信息(persona)
  79. * @apiDescription 个人信息(persona)
  80. * @apiGroup My
  81. * @apiPermission Passport
  82. * @apiVersion 0.1.0
  83. * @apiSuccessExample {json} Success-Response:
  84. * HTTP/1.1 200 OK
  85. *{
  86. * "status": true,
  87. * "status_code": 0,
  88. * "message": "",
  89. * "data": {
  90. * "user": {
  91. * "id": 1,
  92. * "tel": "13880642880",手机
  93. * "nickname": "",昵称
  94. * "sex": 1,性别
  95. * "job": "", 职业
  96. * "emotion": 1,情感状态
  97. * "address": "",
  98. * "detail_address": "",
  99. * "status": 1,
  100. * "tall": 1, 身高
  101. * }
  102. * }
  103. *}
  104. * @apiErrorExample {json} Error-Response:
  105. * HTTP/1.1 400 Bad Request
  106. *{
  107. * "status": true,
  108. * "status_code": 0,
  109. * "message": "",
  110. * "data": {
  111. * "user": null
  112. * }
  113. *}
  114. */
  115. public function persona()
  116. {
  117. $user = $this->getUser();
  118. return $this->api(compact('user'));
  119. }
  120. /**
  121. * @api {get} /api/my/edit_user_info 修改个人信息
  122. * @apiDescription 修改个人信息
  123. * @apiGroup My
  124. * @apiPermission Passport
  125. * @apiVersion 0.1.0
  126. * @apiSuccessExample {json} Success-Response:
  127. * HTTP/1.1 200 OK
  128. *{
  129. * "status": true,
  130. * "status_code": 0,
  131. * "message": "",
  132. "data": {
  133. "emotion": [
  134. {
  135. "value": "1",
  136. "name": "未婚"
  137. },
  138. {
  139. "value": "2",
  140. "name": "已婚"
  141. },
  142. {
  143. "value": "3",
  144. "name": "离异"
  145. }
  146. ],
  147. "sex": [
  148. {
  149. "value": "0",
  150. "name": "男"
  151. },
  152. {
  153. "value": "1",
  154. "name": "女"
  155. }
  156. ]
  157. }
  158. * }
  159. * @apiErrorExample {json} Error-Response:
  160. * HTTP/1.1 400 Bad Request
  161. * {
  162. * "status": false,
  163. * "status_code": 1500,
  164. * "message": "会员不存在",
  165. * "data": null
  166. * }
  167. */
  168. public function editUserInfo (Request $request)
  169. {
  170. $sex = BaseDictionaryOptionModel::where('dictionary_table_code','user_info')->
  171. where('dictionary_code','sex')->paginate(20);
  172. $emotion = BaseDictionaryOptionModel::where('dictionary_table_code','user_info')->
  173. where('dictionary_code','emotion')->paginate(20);
  174. return $this->api(compact('emotion','sex'));
  175. }
  176. /**
  177. * @api {post} /api/my/update_user_info 保存个人信息
  178. * @apiDescription 保存个人信息
  179. * @apiParam {string} pic 头像
  180. * @apiParam {int} sex 性别
  181. * @apiParam {string} signture 个性签名
  182. * @apiParam {int} emotion 情感状态
  183. * @apiParam {string} job 职业
  184. * @apiParam {int} tall 身高
  185. * @apiGroup My
  186. * @apiPermission Passport
  187. * @apiVersion 0.1.0
  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. * @apiErrorExample {json} Error-Response:
  197. * HTTP/1.1 400 Bad Request
  198. *{
  199. * "status": false,
  200. * "status_code": 600,
  201. * "message": "保存用户数据失败",
  202. * "data": null
  203. * }
  204. */
  205. public function updateUserInfo(Request $request)
  206. {
  207. $user = $this->getUser();
  208. $data = $request->except('_token');
  209. $ok = $user->update($data);
  210. if ($ok == true) {
  211. return $this->api('');
  212. }else{
  213. return $this->error(ErrorCode::SAVE_USER_FAILED);
  214. }
  215. }
  216. /**
  217. * @api {get} /api/my/recharge 充值(recharge)
  218. * @apiDescription 充值(recharge)
  219. * @apiGroup My
  220. * @apiPermission Passport
  221. * @apiVersion 0.1.0
  222. * @apiSuccessExample {json} Success-Response:
  223. * HTTP/1.1 200 OK
  224. * @apiErrorExample {json} Error-Response:
  225. * HTTP/1.1 400 Bad Request
  226. */
  227. public function recharge()
  228. {
  229. $user = $this->getUser();
  230. return $this->api(compact('user'));
  231. }
  232. /**
  233. * @api {get} /api/my/system_info 系统消息(systemInfo)
  234. * @apiDescription 系统消息(systemInfo)
  235. * @apiGroup My
  236. * @apiPermission Passport
  237. * @apiVersion 0.1.0
  238. * @apiSuccessExample {json} Success-Response:
  239. * HTTP/1.1 200 OK
  240. *{
  241. * "status": true,
  242. * "status_code": 0,
  243. * "message": "",
  244. * "data": {
  245. * "data": [
  246. * ...
  247. * ]
  248. *}
  249. * @apiErrorExample {json} Error-Response:
  250. * HTTP/1.1 400 Bad Request
  251. * {
  252. * "status": true,
  253. * "status_code": 0,
  254. * "message": "",
  255. * "data": {
  256. * "data": []
  257. * }
  258. *}
  259. */
  260. public function systemInfo()
  261. {
  262. $user = $this->getUser();
  263. $data = SystemInfoModel::where('user_id',$user->id)->orderBy('id','desc')->get();
  264. return $this->api(compact('data'));
  265. }
  266. /**
  267. * @api {get} /api/my/reply_my 回复我的(replyMy)
  268. * @apiDescription 回复我的(replyMy)
  269. * @apiGroup My
  270. * @apiPermission Passport
  271. * @apiVersion 0.1.0
  272. * @apiSuccessExample {json} Success-Response:
  273. * HTTP/1.1 200 OK
  274. *
  275. * {
  276. * "status": true,
  277. * "status_code": 0,
  278. * "message": "",
  279. * "data": {
  280. * "comments_infos": [
  281. * {
  282. * "id": 1,
  283. * "dream_id": 2,
  284. * "user_id": 2,
  285. * "level": 2,
  286. * "content": "啊哈", 评论内容
  287. * "created_at": null, 评论时间
  288. * "updated_at": null,
  289. * "deleted_at": null,
  290. * "dream_name": "去旅游去旅游2", 梦想介绍
  291. * "dream_pic": "aaaaa", 梦想图片
  292. * "progress": 0, 进度
  293. * "reviewer": "22222", 评论者
  294. * "reviewer_pic": "22222pic", 评论者头像
  295. * }
  296. * ]
  297. * }
  298. * }
  299. * @apiErrorExample {json} Error-Response:
  300. * HTTP/1.1 400 Bad Request
  301. */
  302. public function replyMy()
  303. {
  304. $user = $this->getUser();
  305. // 梦想
  306. $dreams = $user->UserDream;
  307. $data = $user->allInteraction;
  308. foreach ($data as $item) {
  309. $item->get_money = $item->dream->get_money;
  310. $item->money = $item->dream->money;
  311. }
  312. dd($data) ;
  313. if (count($dreams) == 0)
  314. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  315. $comments_infos = [];
  316. foreach ($dreams as $dream){
  317. $comments_info = $dream->DreamInfo;
  318. if (count($comments_info) > 0) {
  319. foreach ($comments_info as $k => $value) {
  320. $value->dream_name = $dream->dream;
  321. $value->dream_about = $dream->about;
  322. $value->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  323. $value->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  324. $value->reviewer = $value->CommentUser->nickname;
  325. $value->reviewer_pic = $value->CommentUser->pic;
  326. }
  327. $comments_infos[] = $comments_info;
  328. }
  329. }
  330. return $this->api(compact('comments_infos'));
  331. }
  332. /**
  333. * @api {post} /api/my/my_reply 我的回复(myReply)
  334. * @apiDescription 我的回复(recharge)
  335. * @apiGroup My
  336. * @apiParam {text} content 回复内容
  337. * @apiParam {int} comment_id 评论ID
  338. * @apiPermission Passport
  339. * @apiVersion 0.1.0
  340. * @apiSuccessExample {json} Success-Response:
  341. * {
  342. * "status": true,
  343. * "status_code": 0,
  344. * "message": "",
  345. * "data": ""
  346. *}
  347. * HTTP/1.1 200 OK
  348. * @apiErrorExample {json} Error-Response:
  349. * {
  350. * "status": false,
  351. * "status_code": 1000,
  352. * "message": "输入不正确",
  353. * "data": null
  354. *}
  355. *{
  356. * "status": false,
  357. * "status_code": 600,
  358. * "message": "保存用户数据失败",
  359. * "data": null
  360. *}
  361. * HTTP/1.1 400 Bad Request
  362. */
  363. public function myReply(Request $request)
  364. {
  365. $user = $this->getUser();
  366. $data = $request->except('_token');
  367. $data['user_id'] = $user->id;
  368. if (!$request->content)
  369. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  370. $ok = ReplyCommentsInfo::create($data);
  371. if ($ok) {
  372. return $this->api('');
  373. }else{
  374. return $this->error(ErrorCode::SAVE_USER_FAILED);
  375. }
  376. }
  377. /**
  378. * @api {get} /api/my/dream 我的梦想(dream)
  379. * @apiDescription 我的梦想(recharge)
  380. * @apiGroup My
  381. * @apiPermission Passport
  382. * @apiVersion 0.1.0
  383. * @apiSuccessExample {json} Success-Response:
  384. * {
  385. * "status": true,
  386. * "status_code": 0,
  387. * "message": "",
  388. * "data": {
  389. * "dreams": [
  390. * {
  391. * "id": 2,
  392. * "dream": "去旅游去旅游2",
  393. * "about": "欧冠胡234",
  394. * "dream_pic": "aaaaa", 封面图片
  395. * "progress": 0, 进度
  396. * ]
  397. * }
  398. * }
  399. * HTTP/1.1 200 OK
  400. * @apiErrorExample {json} Error-Response:
  401. * HTTP/1.1 400 Bad Request
  402. */
  403. public function dream()
  404. {
  405. $user = $this->getUser();
  406. // 梦想
  407. $dreams = $user->UserDream;
  408. if (count($dreams) == 0)
  409. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  410. foreach ($dreams as $dream){
  411. $dream->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  412. $dream->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  413. }
  414. return $this->api(compact('dreams'));
  415. }
  416. /**
  417. * @api {get} /api/my/collection 我的收藏(collection)
  418. * @apiDescription 我的收藏(recharge)
  419. * @apiGroup My
  420. * @apiPermission Passport
  421. * @apiVersion 0.1.0
  422. * @apiSuccessExample {json} Success-Response:
  423. * HTTP/1.1 200 OK
  424. * @apiErrorExample {json} Error-Response:
  425. * HTTP/1.1 400 Bad Request
  426. */
  427. public function collection()
  428. {
  429. $user = $this->getUser();
  430. $data = $user->myCollection;
  431. return $this->api(compact('data'));
  432. }
  433. /**
  434. * @api {get} /api/my/setting 设置(setting)
  435. * @apiDescription 设置(setting)
  436. * @apiGroup My
  437. * @apiPermission Passport
  438. * @apiVersion 0.1.0
  439. * @apiSuccessExample {json} Success-Response:
  440. * HTTP/1.1 200 OK
  441. *{
  442. * "status": true,
  443. ** "status_code": 0,
  444. * "message": "",
  445. * "data": {
  446. * "data": {
  447. * "key": "2511789", 电话
  448. * "value": "关于喵喵介绍" 介绍
  449. * }
  450. * }
  451. *}
  452. * @apiErrorExample {json} Error-Response:
  453. * HTTP/1.1 400 Bad Request
  454. */
  455. public function setting()
  456. {
  457. $data = BaseSettingsModel::where('category','miaomiao')->select('key','value')->first();
  458. return $this->api(compact('data'));
  459. }
  460. /**
  461. * @api {post} /api/my/add_dream 发布梦想(addDream)
  462. * @apiDescription 发布梦想(addDream)
  463. * @apiParam {string} pics 梦想图片 数组
  464. * @apiParam {string} video 梦想视频
  465. * @apiParam {string} dream 梦想标题
  466. * @apiParam {string} about 梦想介绍
  467. * @apiParam {int} money 梦想币
  468. * @apiParam {int} time 实现时间默认21*3600
  469. * @apiGroup My
  470. * @apiPermission Passport
  471. * @apiVersion 0.1.0
  472. * @apiSuccessExample {json} Success-Response:
  473. * {
  474. * "status": true,
  475. * "status_code": 0,
  476. * "message": "",
  477. * "data": ""
  478. *}
  479. * HTTP/1.1 200 OK
  480. * @apiErrorExample {json} Error-Response:
  481. * {
  482. * "status": false,
  483. * "status_code": 600,
  484. * "message": "保存用户数据失败",
  485. * "data": null
  486. * }
  487. * HTTP/1.1 400 Bad Request
  488. */
  489. public function addDream(Request $request)
  490. {
  491. $user = $this->getUser();
  492. $validator = \Validator::make($request->all(),
  493. [
  494. 'dream' => 'required',
  495. 'about' => 'required',
  496. 'money' => 'required|integer',
  497. 'time' => 'required|integer',
  498. ],
  499. [
  500. 'dream.required' => '梦想标题必填',
  501. 'about.required' => '梦想介绍必填',
  502. 'money.required' => '梦想币必填',
  503. 'time.required' => '实现时间必填',
  504. 'money.integer' => '梦想币必须是正整数',
  505. 'time.integer' => '实现时间必须是正整数',
  506. ]
  507. );
  508. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  509. \Log::debug($user);
  510. $data2 = $user->myCareNum;
  511. $care_num = count($data2);
  512. $setting = BaseSettingsModel::where('category','paihang')->first();
  513. $a = $setting?$setting->key:1;
  514. $b = $setting?$setting->value:1;
  515. $t = 21*3600 / 60;
  516. $data = $request->except('_token','pics');
  517. \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  518. if ($care_num == 0) {
  519. $data['score'] = (($a/$t) + $b)*100000000 ;
  520. }else{
  521. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  522. }
  523. $data['created_at'] = date('Y-m-d H:i:s');
  524. $data['updated_at'] = date('Y-m-d H:i:s');
  525. $dream_id = DreamInfoModel::insertGetId($data);
  526. if ($dream_id) {
  527. // 梦想创建成功 关联关系中最新动态+1
  528. $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  529. foreach ($info as $item) {
  530. $item->dream_num += 1;
  531. $item->updated_at = date('Y-m-d H:i:s');
  532. $item->save();
  533. }
  534. $data1['dream_id'] = $dream_id;
  535. $data1['user_id'] = $user->id;
  536. $data1['created_at'] = date('Y-m-d H:i:s');
  537. $data1['updated_at'] = date('Y-m-d H:i:s');
  538. $arr = $request->pics;
  539. $video = $request->video;
  540. if (empty($arr) && empty($video)) return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  541. $ok = UserDream::create($data1);
  542. $arr1 = [];
  543. if ($ok) {
  544. if (is_array($arr)) {
  545. foreach ($arr as $k => $v){
  546. $arr1[] = [
  547. 'dream_id'=>$dream_id,
  548. 'pic' => $v,
  549. 'created_at' =>date('Y-m-d H:i:s'),
  550. 'updated_at' =>date('Y-m-d H:i:s'),
  551. ];
  552. }
  553. DreamImages::insert($arr1);
  554. }
  555. return $this->api('');
  556. }else{
  557. DreamInfoModel::destroy($dream_id);
  558. return $this->error(ErrorCode::SAVE_USER_FAILED);
  559. }
  560. }else{
  561. return $this->error(ErrorCode::SAVE_USER_FAILED);
  562. }
  563. }
  564. }