MyController.php 17 KB

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