forked from MrCoderStack/MrCoderStackBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleController.php
More file actions
149 lines (134 loc) · 5.9 KB
/
ArticleController.php
File metadata and controls
149 lines (134 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace App\Http\Controllers;
use App\Cate;
use App\Common\SphinxClient;
use Illuminate\Http\Request;
use Illuminate\Session\DatabaseSessionHandler;
use Illuminate\Support\Facades\DB;
use League\HTMLToMarkdown\HtmlConverter;
use App\Common\MyUpload;
use App\Article;
use App\Comment;
use App\Visit;
use App\Tag;
use App\User;
use Auth;
class ArticleController extends Controller
{
//文章列表
public function list()
{
$articles = Article::where('is_hidden', 0)->orderBy('created_at', 'desc')->paginate(10);
foreach ($articles as $article) {
$article->cover = imageURL($article->cover);
$article->content = str_limit(strip_tags($article->content_html), 150);
$article->created_at_date = $article->created_at->toDateString();
$article->updated_at_diff = $article->updated_at->diffForHumans();
}
$tags = Tag::all();
return view(env('BLOG_THEME') . '.articles.list', compact('articles', 'tags'));
}
//搜索
public function search(Request $request)
{
$keyword = urldecode(trim(strip_tags($request->key)));
$indexName = '*';
$sphinx = new SphinxClient ();
// 建立连接
$sphinx->SetServer('127.0.0.1', 9312);
$sphinx->SetConnectTimeout(3);
$sphinx->SetMaxQueryTime(2000);
$orgDatas = $sphinx->Query($keyword, $indexName);
$errors = $sphinx->GetLastError();
$articles = array();
if ($orgDatas['total'] > 0) {
$field = 'ar.id, ar.content_html, ar.title, ca.name';
$articles = Article::select(DB::RAW($field))->from('articles as ar')
->leftjoin('cates as ca', 'ar.cate_id', '=', 'ca.id')
->whereIn('ar.id', array_keys($orgDatas['matches']))->get();
}
foreach ($articles as $k => $article) {
$article->content_html = mb_substr($this->cutstr_html($article->content_html), 0, 100, 'utf-8');
}
return $result = ['code' => 200, 'data' => $articles];
}
function cutstr_html($string)
{
$string = strip_tags($string);
$string = trim($string);
$string = preg_replace("/\t/", "", $string);
$string = preg_replace("/\r\n/", "", $string);
$string = preg_replace("/\r/", "", $string);
$string = preg_replace("/\n/", "", $string);
$string = preg_replace("/ /", "", $string);
$string = preg_replace("//", "", $string);
return trim($string);
}
//文章详情
public function show(Request $request, $id)
{
$articleCount = Article::all()->count();
$catesCount = Cate::all()->count();
$tagsCount = Tag::all()->count();
$article = Article::findOrFail($id);
$article->increment('view');
$article->created_at_date = $article->created_at->toDateString();
$article->updated_at_date = $article->updated_at->toDateString();
$comments = $article->comments()->where('parent_id', 0)->orderBy('created_at', 'desc')->get();
$count = $article->comments()->count();
$article->words = mb_strlen(strip_tags($article->content_html), 'UTF8');
$article->read = ceil($article->words / 1000);
if ($article->cate_id) {
$article->cates = Cate::where('id', $article->cate_id)->first()->name;
}
//处理评论,关联回复
foreach ($comments as $comment) {
$comment->created_at_diff = $comment->created_at->diffForHumans();
if ($comment->name) {
$comment->avatar = mb_substr($comment->name, 0, 1, 'utf-8');
} else {
$comment->avatar = '匿';
$comment->name = '一位不愿意透露姓名的网友' . $comment->id . '#';
}
if ($comment->user_id == 1) {
$comment->master = User::select('name', 'avatar')->findOrFail(1);
$comment->master->avatar = imageURL($comment->master->avatar);
}
foreach ($comment->replys as $reply) {
$reply->created_at_diff = $reply->created_at->diffForHumans();
$commentModel = Comment::find($reply->target_id);
$reply->target_name = empty($commentModel->name) ? '一位不愿意透露姓名的网友' . $commentModel->id . '#' : $commentModel->name;
if ($reply->name) {
$reply->avatar = mb_substr($reply->name, 0, 1, 'utf-8');
} else {
$reply->avatar = '匿';
$reply->name = '一位不愿意透露姓名的网友' . $reply->id . '#';
}
if ($reply->user_id == 1) {
$reply->master = User::select('name', 'avatar')->findOrFail(1);
$reply->master->avatar = imageURL($reply->master->avatar);
}
}
}
//自动填写
$input = (object)[];
if (Auth::id()) {
$input = User::select('name', 'email', 'website')->findOrFail(Auth::id());
} else {
$comment = Comment::where('ip', $request->ip())->orderBy('created_at', 'desc')->select('name', 'email', 'website')->first();
$input = $comment ? $comment : $input;
}
preg_match_all('#<h2>(.*)</h2>#', $article->content_html, $outline);
$outline = $outline[1];
$preg = "#<h([0-9])>(.*?)</h([0-9])>#";
$replace = '<h$1 id="$2">$2</h$1>';
$content = preg_replace($preg, $replace, $article->content_html);
// $parse = new \Parsedown();
// echo $parse->text($article->content_markdown);
// $article->content_html = $parse->text($article->content_markdown);
// exit;
$article->content_html = $content;
$tags = $article->tags;
return view(env('BLOG_THEME') . '.articles.show', compact('article', 'comments', 'input', 'count', 'outline', 'tags', 'articleCount', 'catesCount', 'tagsCount'));
}
}