123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\FrontController;
- use App\Models\Banner;
- use App\Models\Member;
- use App\Models\Message;
- use App\Models\Notice;
- use App\Servers\CommonServer;
- use App\Servers\RedisDataServer;
- use Carbon\Carbon;
- class NewsController extends FrontController
- {
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * 获取轮播列表(数据缓存60秒)
- * @return \Illuminate\Http\JsonResponse
- */
- function banner()
- {
- $list = RedisDataServer::creatServer()->getData('banner_list', 'json');
- if (empty($list)) {
- $list = Banner::where('show', '1')->select(['id', 'image', 'title'])->orderBy('sort', 'desc')->get();
- foreach ($list as $value){
- $value->{'image'}='http://'.env('APP_HOST_API').$value->{'image'};
- }
- RedisDataServer::creatServer()->setData('banner_list', $list->toArray(), 'json');
- }
- return $this->apiResponseSuccess('获取成功', $list);
- }
- /**
- * 获取公告列表
- * @return \Illuminate\Http\JsonResponse
- */
- function notice()
- {
- $list = RedisDataServer::creatServer()->getData('notice_list', 'json');
- if (empty($list)) {
- $list = Notice::where('show', '1')->select(['id', 'title', 'new', 'created_at','content'])->orderBy('sort', 'desc')->get();
- $m_id = request()->member['id'];
- foreach ($list as $value) {
- $value->{'created_date'} = Carbon::parse($value->{'created_at'}, config('app.timezone'))->format('Y-m-d H:i');
- $value['is_read'] = $value->getIsRead($m_id);
- }
- RedisDataServer::creatServer()->setData('notice_list', $list->toArray(), 'json');
- }
- return $this->apiResponseSuccess('获取成功', $list);
- }
- /**
- * 获取公告详情
- * @return \Illuminate\Http\JsonResponse
- */
- function noticeDetails()
- {
- $id = request()->get('id', '1');
- if (empty($id)) {
- return $this->apiResponseError('缺少必要参数');
- }
- $info = Notice::where('show', '1')->where('id', $id)->select(['id', 'title', 'new', 'created_at', 'content'])->first();
- if (empty($info)) {
- return $this->apiResponseError('信息不存在');
- }
- $m_id = request()->member['id'];
- $info->setRead($m_id);
- $info->{'created_date'} = Carbon::parse($info->{'created_at'}, config('app.timezone'))->format('Y-m-d H:i');
- return $this->apiResponseSuccess('获取信息成功', $info);
- }
- /**
- * 获取关于我们
- */
- function getAboutUs(){
- $info=CommonServer::creatServer()->getConfigValue('about_us');
- return $this->apiResponseSuccess('获取数据成功',['info'=>$info]);
- }
- /**
- * 添加留言
- * @return \Illuminate\Http\JsonResponse
- */
- function addMsg(){
- $content=CommonServer::creatServer()->filtrationStr('content','');
- if(empty($content)){
- return $this->apiResponseError('请输入留言内容');
- }
- $m_id = request()->member['id'];
- $nickname = Member::where('id',$m_id)->value('nickname');
- $msg=Message::create([
- 'm_id' => $m_id,
- 'username' => $nickname,
- 'content' => $content,
- 'reply' => '',
- 'show' => '1',
- ]);
- if(empty($msg)){
- return $this->apiResponseError('留言失败');
- }else{
- return $this->apiResponseSuccess('留言成功',$msg);
- }
- }
- /**
- * 获取留言列表
- * @return \Illuminate\Http\JsonResponse
- */
- function getMsgList(){
- $m_id = request()->member['id'];
- $list=Message::where('m_id',$m_id)->where('show',1)->paginate(20);
- return $this->apiResponseSuccess('',['total'=>$list->total(),'items'=>$list->items()]);
- }
- }
|