MyController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. * @apiPermission Passport
  124. * @apiVersion 0.1.0
  125. * @apiSuccessExample {json} Success-Response:
  126. * HTTP/1.1 200 OK
  127. *{
  128. * "status": true,
  129. * "status_code": 0,
  130. * "message": "",
  131. * "data": {
  132. * "emotion": [
  133. * {
  134. * "value": "1",
  135. * "name": "未婚"
  136. * },
  137. * ...
  138. * ],
  139. * "sex": [
  140. * {
  141. * "value": "0",
  142. * "name": "男"
  143. * },
  144. * ...
  145. * ]
  146. * }
  147. * }
  148. * @apiErrorExample {json} Error-Response:
  149. * HTTP/1.1 400 Bad Request
  150. * {
  151. * "status": false,
  152. * "status_code": 1500,
  153. * "message": "会员不存在",
  154. * "data": null
  155. * }
  156. */
  157. public function editUserInfo (Request $request)
  158. {
  159. $sex = BaseDictionaryOptionModel::where('dictionary_table_code','user_info')->
  160. where('dictionary_code','sex')->get();
  161. $emotion = BaseDictionaryOptionModel::where('dictionary_table_code','user_info')->
  162. where('dictionary_code','emotion')->get();
  163. return $this->api(compact('emotion','sex'));
  164. }
  165. /**
  166. * @api {post} /api/my/update_user_info 保存个人信息
  167. * @apiDescription 保存个人信息
  168. * @apiParam {string} pic 头像
  169. * @apiParam {int} sex 性别
  170. * @apiParam {string} signture 个性签名
  171. * @apiParam {int} emotion 情感状态
  172. * @apiParam {string} job 职业
  173. * @apiParam {int} tall 身高
  174. * @apiGroup My
  175. * @apiPermission Passport
  176. * @apiVersion 0.1.0
  177. * @apiSuccessExample {json} Success-Response:
  178. * HTTP/1.1 200 OK
  179. *{
  180. * "status": true,
  181. * "status_code": 0,
  182. * "message": "",
  183. * "data": ""
  184. *}
  185. * @apiErrorExample {json} Error-Response:
  186. * HTTP/1.1 400 Bad Request
  187. *{
  188. * "status": false,
  189. * "status_code": 600,
  190. * "message": "保存用户数据失败",
  191. * "data": null
  192. * }
  193. */
  194. public function updateUserInfo(Request $request)
  195. {
  196. $user = $this->getUser();
  197. $data = $request->except('_token');
  198. $ok = $user->update($data);
  199. if ($ok) {
  200. return $this->api('');
  201. }else{
  202. return $this->error(ErrorCode::SAVE_USER_FAILED);
  203. }
  204. }
  205. /**
  206. * @api {get} /api/my/recharge 充值(recharge)
  207. * @apiDescription 充值(recharge)
  208. * @apiGroup My
  209. * @apiPermission Passport
  210. * @apiVersion 0.1.0
  211. * @apiSuccessExample {json} Success-Response:
  212. * HTTP/1.1 200 OK
  213. * @apiErrorExample {json} Error-Response:
  214. * HTTP/1.1 400 Bad Request
  215. */
  216. public function recharge()
  217. {
  218. $user = $this->getUser();
  219. return $this->api(compact('user'));
  220. }
  221. /**
  222. * @api {get} /api/my/system_info 系统消息(systemInfo)
  223. * @apiDescription 系统消息(systemInfo)
  224. * @apiGroup My
  225. * @apiPermission Passport
  226. * @apiVersion 0.1.0
  227. * @apiSuccessExample {json} Success-Response:
  228. * HTTP/1.1 200 OK
  229. *{
  230. * "status": true,
  231. * "status_code": 0,
  232. * "message": "",
  233. * "data": {
  234. * "data": [
  235. * {
  236. * "nickname": "",充值人
  237. * "pivot": {
  238. * "coin": 0 捐赠梦想币数量
  239. * }
  240. * }
  241. *}
  242. * @apiErrorExample {json} Error-Response:
  243. * HTTP/1.1 400 Bad Request
  244. * {
  245. * "status": true,
  246. * "status_code": 0,
  247. * "message": "",
  248. * "data": {
  249. * "data": []
  250. * }
  251. *}
  252. */
  253. public function systemInfo()
  254. {
  255. $user = $this->getUser();
  256. $data = $user->SystemInfo;
  257. return $this->api(compact('data'));
  258. }
  259. /**
  260. * @api {get} /api/my/reply_my 回复我的(replyMy)
  261. * @apiDescription 回复我的(replyMy)
  262. * @apiGroup My
  263. * @apiPermission Passport
  264. * @apiVersion 0.1.0
  265. * @apiSuccessExample {json} Success-Response:
  266. * HTTP/1.1 200 OK
  267. *
  268. * {
  269. * "status": true,
  270. * "status_code": 0,
  271. * "message": "",
  272. * "data": {
  273. * "comments_infos": [
  274. * {
  275. * "id": 1,
  276. * "dream_id": 2,
  277. * "user_id": 2,
  278. * "level": 2,
  279. * "content": "啊哈", 评论内容
  280. * "created_at": null, 评论时间
  281. * "updated_at": null,
  282. * "deleted_at": null,
  283. * "dream_name": "去旅游去旅游2", 梦想介绍
  284. * "dream_pic": "aaaaa", 梦想图片
  285. * "progress": 0, 进度
  286. * "reviewer": "22222", 评论者
  287. * "reviewer_pic": "22222pic", 评论者头像
  288. * }
  289. * ]
  290. * }
  291. * }
  292. * @apiErrorExample {json} Error-Response:
  293. * HTTP/1.1 400 Bad Request
  294. */
  295. public function replyMy()
  296. {
  297. $user = $this->getUser();
  298. // 梦想
  299. $dreams = $user->UserDream;
  300. if (count($dreams) == 0)
  301. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  302. foreach ($dreams as $dream){
  303. $comments_infos = $dream->DreamInfo;
  304. if (count($comments_infos) > 0) {
  305. foreach ($comments_infos as $k => $value) {
  306. $value->dream_name = $dream->dream;
  307. $value->dream_about = $dream->about;
  308. $value->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  309. $value->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  310. $value->reviewer = $value->CommentUser->nickname;
  311. $value->reviewer_pic = $value->CommentUser->pic;
  312. }
  313. }
  314. }
  315. return $this->api(compact('comments_infos'));
  316. }
  317. /**
  318. * @api {post} /api/my/my_reply 我的回复(myReply)
  319. * @apiDescription 我的回复(recharge)
  320. * @apiGroup My
  321. * @apiParam {text} content 回复内容
  322. * @apiParam {int} comment_id 评论ID
  323. * @apiPermission Passport
  324. * @apiVersion 0.1.0
  325. * @apiSuccessExample {json} Success-Response:
  326. * {
  327. * "status": true,
  328. * "status_code": 0,
  329. * "message": "",
  330. * "data": ""
  331. *}
  332. * HTTP/1.1 200 OK
  333. * @apiErrorExample {json} Error-Response:
  334. * {
  335. * "status": false,
  336. * "status_code": 1000,
  337. * "message": "输入不正确",
  338. * "data": null
  339. *}
  340. *{
  341. * "status": false,
  342. * "status_code": 600,
  343. * "message": "保存用户数据失败",
  344. * "data": null
  345. *}
  346. * HTTP/1.1 400 Bad Request
  347. */
  348. public function myReply(Request $request)
  349. {
  350. $user = $this->getUser();
  351. $data = $request->except('_token');
  352. $data['user_id'] = $user->id;
  353. if (!$request->content)
  354. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  355. $ok = ReplyCommentsInfo::create($data);
  356. if ($ok) {
  357. return $this->api('');
  358. }else{
  359. return $this->error(ErrorCode::SAVE_USER_FAILED);
  360. }
  361. }
  362. /**
  363. * @api {get} /api/my/dream 我的梦想(dream)
  364. * @apiDescription 我的梦想(recharge)
  365. * @apiGroup My
  366. * @apiPermission Passport
  367. * @apiVersion 0.1.0
  368. * @apiSuccessExample {json} Success-Response:
  369. * {
  370. * "status": true,
  371. * "status_code": 0,
  372. * "message": "",
  373. * "data": {
  374. * "dreams": [
  375. * {
  376. * "id": 2,
  377. * "dream": "去旅游去旅游2",
  378. * "about": "欧冠胡234",
  379. * "dream_pic": "aaaaa",
  380. * "progress": 0, 进度
  381. * ]
  382. * }
  383. * }
  384. * HTTP/1.1 200 OK
  385. * @apiErrorExample {json} Error-Response:
  386. * HTTP/1.1 400 Bad Request
  387. */
  388. public function dream()
  389. {
  390. $user = $this->getUser();
  391. // 梦想
  392. $dreams = $user->UserDream;
  393. if (count($dreams) == 0)
  394. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  395. foreach ($dreams as $dream){
  396. $dream->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  397. $dream->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  398. }
  399. return $this->api(compact('dreams'));
  400. }
  401. /**
  402. * @api {get} /api/my/collection 我的收藏(collection)
  403. * @apiDescription 我的收藏(recharge)
  404. * @apiGroup My
  405. * @apiPermission Passport
  406. * @apiVersion 0.1.0
  407. * @apiSuccessExample {json} Success-Response:
  408. * HTTP/1.1 200 OK
  409. * @apiErrorExample {json} Error-Response:
  410. * HTTP/1.1 400 Bad Request
  411. */
  412. public function collection()
  413. {
  414. $user = $this->getUser();
  415. return $this->api(compact('user'));
  416. }
  417. /**
  418. * @api {get} /api/my/setting 设置(setting)
  419. * @apiDescription 设置(setting)
  420. * @apiGroup My
  421. * @apiPermission Passport
  422. * @apiVersion 0.1.0
  423. * @apiSuccessExample {json} Success-Response:
  424. * HTTP/1.1 200 OK
  425. * @apiErrorExample {json} Error-Response:
  426. * HTTP/1.1 400 Bad Request
  427. */
  428. public function setting()
  429. {
  430. $user = $this->getUser();
  431. return $this->api(compact('user'));
  432. }
  433. /**
  434. * @api {post} /api/my/add_dream 发布梦想(addDream)
  435. * @apiDescription 发布梦想(addDream)
  436. * @apiParam {string} pics 梦想图片 数组
  437. * @apiParam {string} video 梦想视频
  438. * @apiParam {string} dream 梦想标题
  439. * @apiParam {string} about 梦想介绍
  440. * @apiParam {int} money 梦想币
  441. * @apiParam {int} time 实现时间默认21*3600
  442. * @apiGroup My
  443. * @apiPermission Passport
  444. * @apiVersion 0.1.0
  445. * @apiSuccessExample {json} Success-Response:
  446. * {
  447. * "status": true,
  448. * "status_code": 0,
  449. * "message": "",
  450. * "data": ""
  451. *}
  452. * HTTP/1.1 200 OK
  453. * @apiErrorExample {json} Error-Response:
  454. * {
  455. * "status": false,
  456. * "status_code": 600,
  457. * "message": "保存用户数据失败",
  458. * "data": null
  459. * }
  460. * HTTP/1.1 400 Bad Request
  461. */
  462. public function addDream(Request $request)
  463. {
  464. $user = $this->getUser();
  465. $validator = \Validator::make($request->all(),
  466. [
  467. 'dream' => 'required',
  468. 'about' => 'required',
  469. 'money' => 'required|integer',
  470. 'time' => 'required|integer',
  471. ],
  472. [
  473. 'dream.required' => '梦想标题必填',
  474. 'about.required' => '梦想介绍必填',
  475. 'money.required' => '梦想币必填',
  476. 'time.required' => '实现时间必填',
  477. 'money.integer' => '梦想币必须是正整数',
  478. 'time.integer' => '实现时间必须是正整数',
  479. ]
  480. );
  481. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  482. \Log::debug($user);
  483. $data2 = $user->myCareNum;
  484. $care_num = count($data2);
  485. $setting = BaseSettingsModel::where('category','paihang')->first();
  486. $a = $setting?$setting->key:1;
  487. $b = $setting?$setting->value:1;
  488. $t = 21*3600 / 60;
  489. $data = $request->except('_token','pics');
  490. \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  491. if ($care_num == 0) {
  492. $data['score'] = (($a/$t) + $b)*100000000 ;
  493. }else{
  494. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  495. }
  496. $data['created_at'] = date('Y-m-d H:i:s');
  497. $data['updated_at'] = date('Y-m-d H:i:s');
  498. $dream_id = DreamInfoModel::insertGetId($data);
  499. if ($dream_id) {
  500. // 梦想创建成功 关联关系中最新动态+1
  501. $info = UserCareUser::where('other_user_id',$user->id)->get();
  502. foreach ($info as $item) {
  503. $item->dream_num += 1;
  504. $item->updated_at = date('Y-m-d H:i:s');
  505. $item->save();
  506. }
  507. $data1['dream_id'] = $dream_id;
  508. $data1['user_id'] = $user->id;
  509. $data1['created_at'] = date('Y-m-d H:i:s');
  510. $data1['updated_at'] = date('Y-m-d H:i:s');
  511. $arr = $request->pics;
  512. $video = $request->video;
  513. if (empty($arr) && empty($video)) return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  514. $ok = UserDream::create($data1);
  515. $arr1 = [];
  516. if ($ok) {
  517. if (is_array($arr)) {
  518. foreach ($arr as $k => $v){
  519. $arr1[] = [
  520. 'dream_id'=>$dream_id,
  521. 'pic' => $v,
  522. 'created_at' =>date('Y-m-d H:i:s'),
  523. 'updated_at' =>date('Y-m-d H:i:s'),
  524. ];
  525. }
  526. DreamImages::insert($arr1);
  527. }
  528. return $this->api('');
  529. }else{
  530. DreamInfoModel::destroy($dream_id);
  531. return $this->error(ErrorCode::SAVE_USER_FAILED);
  532. }
  533. }else{
  534. return $this->error(ErrorCode::SAVE_USER_FAILED);
  535. }
  536. }
  537. /**
  538. * @api {get} /api/my/dream_about 梦想介绍(dreamAbout)
  539. * @apiDescription 梦想介绍(dreamAbout)
  540. * @apiGroup My
  541. * @apiPermission Passport
  542. * @apiVersion 0.1.0
  543. * @apiSuccessExample {json} Success-Response:
  544. * HTTP/1.1 200 OK
  545. * @apiErrorExample {json} Error-Response:
  546. * HTTP/1.1 400 Bad Request
  547. */
  548. public function dreamAbout()
  549. {
  550. $user = $this->getUser();
  551. return $this->api(compact('user'));
  552. }
  553. }