MyController.php 17 KB

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