MyController.php 17 KB

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