MyController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. if (count($dreams) == 0)
  308. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  309. $comments_infos = [];
  310. foreach ($dreams as $dream){
  311. $comments_info = $dream->DreamInfo;
  312. if (count($comments_info) > 0) {
  313. foreach ($comments_info as $k => $value) {
  314. $value->dream_name = $dream->dream;
  315. $value->dream_about = $dream->about;
  316. $value->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  317. $value->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  318. $value->reviewer = $value->CommentUser->nickname;
  319. $value->reviewer_pic = $value->CommentUser->pic;
  320. }
  321. $comments_infos[] = $comments_info;
  322. }
  323. }
  324. return $this->api(compact('comments_infos'));
  325. }
  326. /**
  327. * @api {post} /api/my/my_reply 我的回复(myReply)
  328. * @apiDescription 我的回复(recharge)
  329. * @apiGroup My
  330. * @apiParam {text} content 回复内容
  331. * @apiParam {int} comment_id 评论ID
  332. * @apiPermission Passport
  333. * @apiVersion 0.1.0
  334. * @apiSuccessExample {json} Success-Response:
  335. * {
  336. * "status": true,
  337. * "status_code": 0,
  338. * "message": "",
  339. * "data": ""
  340. *}
  341. * HTTP/1.1 200 OK
  342. * @apiErrorExample {json} Error-Response:
  343. * {
  344. * "status": false,
  345. * "status_code": 1000,
  346. * "message": "输入不正确",
  347. * "data": null
  348. *}
  349. *{
  350. * "status": false,
  351. * "status_code": 600,
  352. * "message": "保存用户数据失败",
  353. * "data": null
  354. *}
  355. * HTTP/1.1 400 Bad Request
  356. */
  357. public function myReply(Request $request)
  358. {
  359. $user = $this->getUser();
  360. $data = $request->except('_token');
  361. $data['user_id'] = $user->id;
  362. if (!$request->content)
  363. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS);
  364. $ok = ReplyCommentsInfo::create($data);
  365. if ($ok) {
  366. return $this->api('');
  367. }else{
  368. return $this->error(ErrorCode::SAVE_USER_FAILED);
  369. }
  370. }
  371. /**
  372. * @api {get} /api/my/dream 我的梦想(dream)
  373. * @apiDescription 我的梦想(recharge)
  374. * @apiGroup My
  375. * @apiPermission Passport
  376. * @apiVersion 0.1.0
  377. * @apiSuccessExample {json} Success-Response:
  378. * {
  379. * "status": true,
  380. * "status_code": 0,
  381. * "message": "",
  382. * "data": {
  383. * "dreams": [
  384. * {
  385. * "id": 2,
  386. * "dream": "去旅游去旅游2",
  387. * "about": "欧冠胡234",
  388. * "dream_pic": "aaaaa", 封面图片
  389. * "progress": 0, 进度
  390. * ]
  391. * }
  392. * }
  393. * HTTP/1.1 200 OK
  394. * @apiErrorExample {json} Error-Response:
  395. * HTTP/1.1 400 Bad Request
  396. */
  397. public function dream()
  398. {
  399. $user = $this->getUser();
  400. // 梦想
  401. $dreams = $user->UserDream;
  402. if (count($dreams) == 0)
  403. return $this->error(ErrorCode::DREAM_NOT_EXIST);
  404. foreach ($dreams as $dream){
  405. $dream->dream_pic = count($dream->dreamImgsFirst) > 0 ? $dream->dreamImgsFirst->pic : '';
  406. $dream->progress = $dream->money == 0 ? 0 : floor($dream->get_money/$dream->money);
  407. }
  408. return $this->api(compact('dreams'));
  409. }
  410. /**
  411. * @api {get} /api/my/collection 我的收藏(collection)
  412. * @apiDescription 我的收藏(recharge)
  413. * @apiGroup My
  414. * @apiPermission Passport
  415. * @apiVersion 0.1.0
  416. * @apiSuccessExample {json} Success-Response:
  417. * HTTP/1.1 200 OK
  418. * @apiErrorExample {json} Error-Response:
  419. * HTTP/1.1 400 Bad Request
  420. */
  421. public function collection()
  422. {
  423. $user = $this->getUser();
  424. return $this->api(compact('user'));
  425. }
  426. /**
  427. * @api {get} /api/my/setting 设置(setting)
  428. * @apiDescription 设置(setting)
  429. * @apiGroup My
  430. * @apiPermission Passport
  431. * @apiVersion 0.1.0
  432. * @apiSuccessExample {json} Success-Response:
  433. * HTTP/1.1 200 OK
  434. * @apiErrorExample {json} Error-Response:
  435. * HTTP/1.1 400 Bad Request
  436. */
  437. public function setting()
  438. {
  439. $user = $this->getUser();
  440. return $this->api(compact('user'));
  441. }
  442. /**
  443. * @api {post} /api/my/add_dream 发布梦想(addDream)
  444. * @apiDescription 发布梦想(addDream)
  445. * @apiParam {string} pics 梦想图片 数组
  446. * @apiParam {string} video 梦想视频
  447. * @apiParam {string} dream 梦想标题
  448. * @apiParam {string} about 梦想介绍
  449. * @apiParam {int} money 梦想币
  450. * @apiParam {int} time 实现时间默认21*3600
  451. * @apiGroup My
  452. * @apiPermission Passport
  453. * @apiVersion 0.1.0
  454. * @apiSuccessExample {json} Success-Response:
  455. * {
  456. * "status": true,
  457. * "status_code": 0,
  458. * "message": "",
  459. * "data": ""
  460. *}
  461. * HTTP/1.1 200 OK
  462. * @apiErrorExample {json} Error-Response:
  463. * {
  464. * "status": false,
  465. * "status_code": 600,
  466. * "message": "保存用户数据失败",
  467. * "data": null
  468. * }
  469. * HTTP/1.1 400 Bad Request
  470. */
  471. public function addDream(Request $request)
  472. {
  473. $user = $this->getUser();
  474. $validator = \Validator::make($request->all(),
  475. [
  476. 'dream' => 'required',
  477. 'about' => 'required',
  478. 'money' => 'required|integer',
  479. 'time' => 'required|integer',
  480. ],
  481. [
  482. 'dream.required' => '梦想标题必填',
  483. 'about.required' => '梦想介绍必填',
  484. 'money.required' => '梦想币必填',
  485. 'time.required' => '实现时间必填',
  486. 'money.integer' => '梦想币必须是正整数',
  487. 'time.integer' => '实现时间必须是正整数',
  488. ]
  489. );
  490. if($validator->fails()) return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  491. \Log::debug($user);
  492. $data2 = $user->myCareNum;
  493. $care_num = count($data2);
  494. $setting = BaseSettingsModel::where('category','paihang')->first();
  495. $a = $setting?$setting->key:1;
  496. $b = $setting?$setting->value:1;
  497. $t = 21*3600 / 60;
  498. $data = $request->except('_token','pics');
  499. \Log::debug(' care_num:'.$care_num.' a:'.$a.' b:'.$b.' t:'.$t);
  500. if ($care_num == 0) {
  501. $data['score'] = (($a/$t) + $b)*100000000 ;
  502. }else{
  503. $data['score'] = (log($care_num) + ($a/$t) + $b)*100000000 ;
  504. }
  505. $data['created_at'] = date('Y-m-d H:i:s');
  506. $data['updated_at'] = date('Y-m-d H:i:s');
  507. $dream_id = DreamInfoModel::insertGetId($data);
  508. if ($dream_id) {
  509. // 梦想创建成功 关联关系中最新动态+1
  510. $info = UserCareUser::where('other_user_id',$user->id)->paginate(20);
  511. foreach ($info as $item) {
  512. $item->dream_num += 1;
  513. $item->updated_at = date('Y-m-d H:i:s');
  514. $item->save();
  515. }
  516. $data1['dream_id'] = $dream_id;
  517. $data1['user_id'] = $user->id;
  518. $data1['created_at'] = date('Y-m-d H:i:s');
  519. $data1['updated_at'] = date('Y-m-d H:i:s');
  520. $arr = $request->pics;
  521. $video = $request->video;
  522. if (empty($arr) && empty($video)) return $this->error(ErrorCode::ATTACHMENT_NOT_EXIST);
  523. $ok = UserDream::create($data1);
  524. $arr1 = [];
  525. if ($ok) {
  526. if (is_array($arr)) {
  527. foreach ($arr as $k => $v){
  528. $arr1[] = [
  529. 'dream_id'=>$dream_id,
  530. 'pic' => $v,
  531. 'created_at' =>date('Y-m-d H:i:s'),
  532. 'updated_at' =>date('Y-m-d H:i:s'),
  533. ];
  534. }
  535. DreamImages::insert($arr1);
  536. }
  537. return $this->api('');
  538. }else{
  539. DreamInfoModel::destroy($dream_id);
  540. return $this->error(ErrorCode::SAVE_USER_FAILED);
  541. }
  542. }else{
  543. return $this->error(ErrorCode::SAVE_USER_FAILED);
  544. }
  545. }
  546. }