NewsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\FrontController;
  4. use App\Models\Banner;
  5. use App\Models\Member;
  6. use App\Models\Message;
  7. use App\Models\Notice;
  8. use App\Servers\CommonServer;
  9. use App\Servers\RedisDataServer;
  10. use Carbon\Carbon;
  11. class NewsController extends FrontController
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. /**
  18. * 获取轮播列表(数据缓存60秒)
  19. * @return \Illuminate\Http\JsonResponse
  20. */
  21. function banner()
  22. {
  23. $list = RedisDataServer::creatServer()->getData('banner_list', 'json');
  24. if (empty($list)) {
  25. $list = Banner::where('show', '1')->select(['id', 'image', 'title'])->orderBy('sort', 'desc')->get();
  26. foreach ($list as $value){
  27. $value->{'image'}='http://'.env('APP_HOST_API').$value->{'image'};
  28. }
  29. RedisDataServer::creatServer()->setData('banner_list', $list->toArray(), 'json');
  30. }
  31. return $this->apiResponseSuccess('获取成功', $list);
  32. }
  33. /**
  34. * 获取公告列表
  35. * @return \Illuminate\Http\JsonResponse
  36. */
  37. function notice()
  38. {
  39. $list = RedisDataServer::creatServer()->getData('notice_list', 'json');
  40. if (empty($list)) {
  41. $list = Notice::where('show', '1')->select(['id', 'title', 'new', 'created_at','content'])->orderBy('sort', 'desc')->get();
  42. $m_id = request()->member['id'];
  43. foreach ($list as $value) {
  44. $value->{'created_date'} = Carbon::parse($value->{'created_at'}, config('app.timezone'))->format('Y-m-d H:i');
  45. $value['is_read'] = $value->getIsRead($m_id);
  46. }
  47. RedisDataServer::creatServer()->setData('notice_list', $list->toArray(), 'json');
  48. }
  49. return $this->apiResponseSuccess('获取成功', $list);
  50. }
  51. /**
  52. * 获取公告详情
  53. * @return \Illuminate\Http\JsonResponse
  54. */
  55. function noticeDetails()
  56. {
  57. $id = request()->get('id', '1');
  58. if (empty($id)) {
  59. return $this->apiResponseError('缺少必要参数');
  60. }
  61. $info = Notice::where('show', '1')->where('id', $id)->select(['id', 'title', 'new', 'created_at', 'content'])->first();
  62. if (empty($info)) {
  63. return $this->apiResponseError('信息不存在');
  64. }
  65. $m_id = request()->member['id'];
  66. $info->setRead($m_id);
  67. $info->{'created_date'} = Carbon::parse($info->{'created_at'}, config('app.timezone'))->format('Y-m-d H:i');
  68. return $this->apiResponseSuccess('获取信息成功', $info);
  69. }
  70. /**
  71. * 获取关于我们
  72. */
  73. function getAboutUs(){
  74. $info=CommonServer::creatServer()->getConfigValue('about_us');
  75. return $this->apiResponseSuccess('获取数据成功',['info'=>$info]);
  76. }
  77. /**
  78. * 添加留言
  79. * @return \Illuminate\Http\JsonResponse
  80. */
  81. function addMsg(){
  82. $content=CommonServer::creatServer()->filtrationStr('content','');
  83. if(empty($content)){
  84. return $this->apiResponseError('请输入留言内容');
  85. }
  86. $m_id = request()->member['id'];
  87. $nickname = Member::where('id',$m_id)->value('nickname');
  88. $msg=Message::create([
  89. 'm_id' => $m_id,
  90. 'username' => $nickname,
  91. 'content' => $content,
  92. 'reply' => '',
  93. 'show' => '1',
  94. ]);
  95. if(empty($msg)){
  96. return $this->apiResponseError('留言失败');
  97. }else{
  98. return $this->apiResponseSuccess('留言成功',$msg);
  99. }
  100. }
  101. /**
  102. * 获取留言列表
  103. * @return \Illuminate\Http\JsonResponse
  104. */
  105. function getMsgList(){
  106. $m_id = request()->member['id'];
  107. $list=Message::where('m_id',$m_id)->where('show',1)->paginate(20);
  108. return $this->apiResponseSuccess('',['total'=>$list->total(),'items'=>$list->items()]);
  109. }
  110. }