IndexController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. namespace App\Http\Controllers\Home;
  3. use App\Http\Requests\Comment\Store;
  4. use App\Models\Category;
  5. use App\Models\Article;
  6. use App\Models\ArticleTag;
  7. use App\Models\Chat;
  8. use App\Models\Comment;
  9. use App\Models\Config;
  10. use App\Models\OauthUser;
  11. use App\Models\Tag;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Controller;
  14. class IndexController extends Controller
  15. {
  16. /**
  17. * 首页
  18. *
  19. * @param Article $articleModel
  20. * @return mixed
  21. */
  22. public function index(Article $articleModel)
  23. {
  24. $article = $articleModel->getHomeList();
  25. $assign = [
  26. 'category_id' => 'index',
  27. 'article' => $article,
  28. 'tagName' => '',
  29. 'title' => Config::where('name', 'WEB_TITLE')->value('value')
  30. ];
  31. return view('home/index/index', $assign);
  32. }
  33. /**
  34. * 文章详情
  35. *
  36. * @param $id
  37. * @param Article $article
  38. * @param Comment $commentModel
  39. * @return mixed
  40. */
  41. public function article($id, Article $article, Comment $commentModel)
  42. {
  43. // 获取文章数据
  44. $data = $article->getDataById($id);
  45. // 设置同一个用户访问同一篇文章只增加1个访问量
  46. $read = request()->cookie('read', []);
  47. // 判断是否已经记录过id
  48. if (array_key_exists($id, $read)) {
  49. // 判断点击本篇文章的时间是否已经超过一天
  50. if ($read[$id]-time() >= 86400) {
  51. $read[$id] = time();
  52. // 文章点击量+1
  53. $data->increment('click');
  54. }
  55. }else{
  56. $read[$id] = time();
  57. // 文章点击量+1
  58. $data->increment('click');
  59. }
  60. // 获取上一篇
  61. $prev = $article
  62. ->select('id', 'title')
  63. ->orderBy('created_at', 'asc')
  64. ->where('id', '>', $id)
  65. ->limit(1)
  66. ->first();
  67. // 获取下一篇
  68. $next = $article
  69. ->select('id', 'title')
  70. ->orderBy('created_at', 'desc')
  71. ->where('id', '<', $id)
  72. ->limit(1)
  73. ->first();
  74. // 获取评论
  75. $comment = $commentModel->getDataByArticleId($id);
  76. $assign = [
  77. 'category_id' => $data->category_id,
  78. 'data' => $data,
  79. 'prev' => $prev,
  80. 'next' => $next,
  81. 'comment' => $comment
  82. ];
  83. return response()->view('home/index/article', $assign)->cookie('read', $read, 1440);
  84. }
  85. /**
  86. * 获取栏目下的文章
  87. *
  88. * @param Article $articleModel
  89. * @param $id
  90. * @return mixed
  91. */
  92. public function category(Article $articleModel, $id)
  93. {
  94. $map = [
  95. 'articles.category_id' => $id
  96. ];
  97. $article = $articleModel->getHomeList($map);
  98. $categoryName = Category::where('id', $id)->value('name');
  99. $assign = [
  100. 'category_id' => $id,
  101. 'article' => $article,
  102. 'tagName' => '',
  103. 'title' => $categoryName
  104. ];
  105. return view('home/index/index', $assign);
  106. }
  107. /**
  108. * 获取标签下的文章
  109. *
  110. * @param $id
  111. * @param Article $articleModel
  112. * @return mixed
  113. */
  114. public function tag($id, Article $articleModel)
  115. {
  116. // 获取标签名
  117. $tagName = Tag::where('id', $id)->value('name');
  118. // 获取此标签下的文章id
  119. $articleIds = ArticleTag::where('tag_id', $id)
  120. ->pluck('article_id')
  121. ->toArray();
  122. // 获取文章数据
  123. $map = [
  124. 'articles.id' => ['in', $articleIds]
  125. ];
  126. $article = $articleModel->getHomeList($map);
  127. $assign = [
  128. 'category_id' => 'index',
  129. 'article' => $article,
  130. 'tagName' => $tagName,
  131. 'title' => $tagName
  132. ];
  133. return view('home/index/index', $assign);
  134. }
  135. /**
  136. * 随言碎语
  137. *
  138. * @return mixed
  139. */
  140. public function chat()
  141. {
  142. $chat = Chat::orderBy('created_at', 'desc')->get();
  143. $assign =[
  144. 'category_id' => 'chat',
  145. 'chat' => $chat
  146. ];
  147. return view('home/index/chat', $assign);
  148. }
  149. /**
  150. * 开源项目
  151. *
  152. * @return mixed
  153. */
  154. public function git()
  155. {
  156. $assign = [
  157. 'category_id' => 'git'
  158. ];
  159. return view('home/index/git', $assign);
  160. }
  161. /**
  162. * 文章评论
  163. *
  164. * @param Comment $commentModel
  165. */
  166. public function comment(Store $request, Comment $commentModel, OauthUser $oauthUserModel)
  167. {
  168. $data = $request->all();
  169. // 限制用户每天最多评论10条
  170. $date = date('Y-m-d', time());
  171. $count = $commentModel
  172. ->where('oauth_user_id', session('user.id'))
  173. ->whereBetween('created_at', [$date.' 00:00:00', $date.' 23:59:59'])
  174. ->count();
  175. if (session('user.is_admin') !=1 && $count > 10) {
  176. return ajaxReturn(400, '每天做多评论10条');
  177. }
  178. // 如果用户输入邮箱;则将邮箱记录入oauth_user表中
  179. $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
  180. if (preg_match($pattern, $data['email'])) {
  181. // 获取用户id
  182. $user_id = session('user.id');
  183. // 修改邮箱
  184. $oauthUserMap = [
  185. 'id' => $user_id
  186. ];
  187. $oauthUserData = [
  188. 'email' => $data['email']
  189. ];
  190. $oauthUserModel->editData($oauthUserMap, $oauthUserData);
  191. session(['user.email' => $data['email']]);
  192. unset($data['email']);
  193. }
  194. // 存储评论
  195. $id = $commentModel->addData($data);
  196. return ajaxReturn('200', ['id' => $id]);
  197. }
  198. /**
  199. * 检测是否登录
  200. */
  201. public function checkLogin()
  202. {
  203. if (empty(session('user.id'))) {
  204. return 0;
  205. } else {
  206. return 1;
  207. }
  208. }
  209. /**
  210. * 搜索文章
  211. *
  212. * @param Article $articleModel
  213. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  214. */
  215. public function search(Article $articleModel){
  216. $wd = request()->input('wd');
  217. $map = [
  218. 'title' => ['like', '%'.$wd.'%']
  219. ];
  220. $article = $articleModel->getHomeList($map);
  221. $assign = [
  222. 'category_id' => 'index',
  223. 'article' => $article,
  224. 'tagName' => '',
  225. 'title' => $wd
  226. ];
  227. return view('home/index/index', $assign);
  228. }
  229. /**
  230. * 用于做测试的方法
  231. */
  232. public function test()
  233. {
  234. }
  235. }