NoticesNotTokenController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Controllers\Notice\Front;
  3. use App\Http\Controllers\FrontController;
  4. use App\Http\Controllers\FrontNotTokenController;
  5. use App\Models\Message;
  6. use App\Models\Notice;
  7. use App\Models\NoticeRead;
  8. use App\Models\Notices;
  9. use Carbon\Carbon;
  10. // 首页公告接口
  11. class NoticesNotTokenController extends FrontController
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. /**
  18. * @return \Illuminate\Http\JsonResponse
  19. *
  20. * 首页公告标题
  21. */
  22. public function indexTitleLists()
  23. {
  24. $type=request()->get('type','1');
  25. $member_id = request()->get('m_id', '');
  26. $lists = Notices::select(['id', 'title', 'title_en', 'created_at', 'updated_at'])
  27. ->where([['show', '>=', 1]]) // 显示公告
  28. ->orderBy('id', 'DESC') // 最新正序排序
  29. // ->orderBy('sort', 'ASC') // 正序排序
  30. ->get();
  31. $data = array();
  32. $lang=request()->input('lang', 'zh');
  33. foreach ($lists as $key => $list){
  34. $data[$key]['id'] = $list->{'id'};
  35. if ( $lang == 'zh' ) {
  36. $data[$key]['title'] = $list->{'title'};
  37. }else{
  38. $data[$key]['title'] = $list->{'title_en'};
  39. }
  40. $data[$key]['created_at'] = Carbon::parse($list->{'created_at'}, config('app.timezone'))->format('Y.m.d');
  41. $data[$key]['updated_at'] = Carbon::parse($list->{'updated_at'}, config('app.timezone'))->format('Y.m.d');
  42. if(!empty($member_id)){
  43. $num = NoticeRead::where('member_id', $member_id)->where('n_id', $data[$key]['id'])->count();
  44. $data[$key]['is_read'] =$num>0?1:0;
  45. }else{
  46. $data[$key]['is_read'] =0;
  47. }
  48. }
  49. return $this->apiResponseSuccess($data, 'notice.index_title_lists');
  50. }
  51. /**
  52. * @param int $id ## 会员ID
  53. * @return \Illuminate\Http\JsonResponse
  54. *
  55. * 获取指定公告列表详情
  56. */
  57. public function detail($id = 0)
  58. {
  59. if ( empty($id) ) {
  60. return $this->apiResponseError([], 'notice.detail_error');
  61. }
  62. $notice = Notices::select(['id', 'title', 'title_en', 'content', 'content_en', 'created_at', 'updated_at'])
  63. ->where('show', '>=', 1)
  64. ->where('id', $id)
  65. ->first();
  66. if ( empty($notice) ) {
  67. return $this->apiResponseError([], 'notice.detail_error');
  68. }
  69. $data['id'] = $notice->{'id'};
  70. if ( request()->input('lang', 'zh') == 'zh' ) {
  71. $data['title'] = $notice->{'title'};
  72. $data['content'] = $notice->{'content'};
  73. }else{
  74. $data['title'] = $notice->{'title_en'};
  75. $data['content'] = $notice->{'content_en'};
  76. }
  77. $data['created_at'] = Carbon::parse($notice->{'created_at'}, config('app.timezone'))->format('Y.m.d');
  78. $data['updated_at'] = Carbon::parse($notice->{'updated_at'}, config('app.timezone'))->format('Y.m.d');
  79. $member_id = request()->get('m_id', '');
  80. if(!empty($member_id)){
  81. $num = NoticeRead::where('member_id', $member_id)->where('n_id',$data['id'])->count();
  82. if($num<=0)NoticeRead::create(['member_id'=> $member_id,'n_id'=> $data['id']]);
  83. }
  84. return $this->apiResponseSuccess($data, 'notice.detail_success');
  85. }
  86. /**
  87. * 获取未读数量
  88. * @return \Illuminate\Http\JsonResponse
  89. */
  90. function getNotReadNum()
  91. {
  92. $member_id = request()->get('m_id', '');
  93. if ($member_id) {
  94. $n_ids = Notices::where('type', '=', '5')->pluck('id')->toArray();
  95. if (empty($n_ids)) {
  96. $num = 0;
  97. } else {
  98. $num = NoticeRead::where('member_id', $member_id)->whereIn('n_id', $n_ids)->count();
  99. $num = count($n_ids) - $num;
  100. if ($num <= 0) $num = 0;
  101. }
  102. } else {
  103. $num = 0;
  104. }
  105. $msg_num=Message::where('member_id',$member_id)->where('is_new','1')->count();
  106. return $this->apiResponseSuccess(['notice_num'=>$num,'msg_num'=>$msg_num], 'notice.detail_success');
  107. }
  108. }