MyController.php 17 KB

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