Repository navigation
Expand file tree
/
Copy pathArticle.php
More file actions
executable file
·253 lines (214 loc) · 7.75 KB
/
Copy pathArticle.php
File metadata and controls
executable file
·253 lines (214 loc) · 7.75 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Auth, Request, Cache;
class Article extends Model
{
//
const REDIS_NEW_ARTICLE_CACHE = 'redis_new_article_cache_';
const REDIS_ARTICLE_CACHE = 'redis_article_cache_';
const REDIS_CATE_ARTICLE_CACHE = 'redis_cate_article_cache_';
const REDIS_USER_ARTICLE_CACHE = 'redis_user_article_cache_';
const REDIS_HOT_ARTICLE_CACHE = 'redis_hot_article_cache_';
const REDIS_SEARCH_ARTICLE_CACHE = 'redis_search_article_cache_';
const REDIS_TAG_ARTICLE_CACHE = 'redis_tag_article_cache_';
const REDIS_ARTICLE_PAGE_TAG = 'redis_article_page_tag';
protected $table = 'article';
static $cacheMinutes = 1440;
protected $fillable = [
'cate_id',
'user_id',
'title',
'content',
'tags',
'new_tags',
'summary',
'pic'
];
public function status()
{
return $this->hasOne('App\Model\ArticleStatus', 'art_id');
}
public function user()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
public function category()
{
return $this->hasOne('App\Model\Category', 'id', 'cate_id');
}
public static function getPage(){
return 1;
}
/**
* 范围查询
* @param $query
* @param $userId
* @return mixed
*/
public function scopeUserId($query, $userId)
{
return $query->where('user_id', '=', $userId);
}
/**
* 文件上传
* @param $field
* @return string
*/
public static function uploadImg($field)
{
if (Request::hasFile($field)) {
$pic = Request::file($field);
if ($pic->isValid()) {
$newName = md5(rand(1, 1000) . $pic->getClientOriginalName()) . "." . $pic->getClientOriginalExtension();
$pic->move('uploads', $newName);
return $newName;
}
}
return '';
}
/**
* 根据文章id获取文章
* @param $articleId
* @return \Illuminate\Support\Collection|null|static
*/
public static function getArticleModelByArticleId($articleId)
{
//if (empty($article = Cache::get(self::REDIS_ARTICLE_CACHE . $articleId))) {
$article = self::find($articleId);
// Cache::add(self::REDIS_ARTICLE_CACHE . $articleId, $article, self::$cacheMinutes);
//}
return $article;
}
/**
* 获取最新的文章
* @param int $limit 条数
* @return mixed
*/
public static function getNewsArticle($limit = 4)
{
$page = Article::getPage();
//$cacheName = $page.'_'.$limit;
//if (empty($model = Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->get(self::REDIS_NEW_ARTICLE_CACHE . $cacheName))) {
$model = self::select('id')->orderBy('id', 'DESC')->simplePaginate($limit);
// Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->put(self::REDIS_NEW_ARTICLE_CACHE . $cacheName, $model, self::$cacheMinutes);
//}
$articleList = array(
'data' => [],
);
foreach ($model as $key => $article) {
$articleList['data'][$key] = self::getArticleModelByArticleId($article->id);
}
$articleList['page'] = $model;
return $articleList;
}
/**
* 根据分类获取文章
* @param $catId
* @param int $limit
* @return mixed
*/
public static function getArticleListByCatId($catId, $limit = 10)
{
$page = Article::getPage();
//$cacheName = $page . '_' . $catId.'_'.$limit;
//if (empty($model = Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->get(self::REDIS_CATE_ARTICLE_CACHE . $cacheName))) {
$model = self::select('id')->where('cate_id', $catId)->orderBy('id', 'desc')->simplePaginate($limit);
// Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->put(self::REDIS_CATE_ARTICLE_CACHE . $cacheName, $model, self::$cacheMinutes);
//}
$articleList = array(
'data' => [],
);
foreach ($model as $key => $article) {
$articleList['data'][$key] = self::getArticleModelByArticleId($article->id);
}
$articleList['page'] = $model;
return $articleList;
}
/**
* 根据作者获取文章
* @param $userid
* @param int $limit
* @param bool $page
* @return null
*/
public static function getArticleModelByUserId($userId, $limit = 3)
{
//$cacheName = $userId.'_'.$limit;
//if (empty($model = Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->get(self::REDIS_USER_ARTICLE_CACHE . $cacheName))) {
$model = self::select('id')->userId($userId)->orderBy('id', 'DESC')->limit($limit)->get();
// Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->put(self::REDIS_USER_ARTICLE_CACHE . $cacheName, $model, self::$cacheMinutes);
//}
$articleList = [];
foreach ($model as $key => $article) {
$articleList[$key] = self::getArticleModelByArticleId($article->id);
}
return $articleList;
}
/**
* 获取热门文章
* @param int $limit
* @param bool $page
* @return mixed
*/
public static function getHotArticle($limit = 3)
{
//$cacheName = $limit;
//if (empty($model = Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->get(self::REDIS_HOT_ARTICLE_CACHE.$cacheName))) {
$select = [
'article.id',
'article.pic',
'article.title',
'article.created_at',
'article_status.view_number',
];
$model = self::select($select)->leftJoin('article_status', 'article.id', '=', 'article_status.art_id')->orderBy('article_status.view_number', 'desc')->limit($limit)->get();
// Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->add(self::REDIS_HOT_ARTICLE_CACHE.$cacheName, $model, self::$cacheMinutes);
//}
return $model;
}
/**
* 关键字搜索
* @todo 后期做成 Coreseek 分词搜索
* @param $keyword
* @return mixed
*/
public static function getArticleListByKeyword($keyword)
{
$page = Article::getPage();
//$cacheName = $page . '_' . md5($keyword);
//if (empty($model = Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->get(self::REDIS_SEARCH_ARTICLE_CACHE . $cacheName))) {
$model = self::select('id')->where('title', 'like', "%$keyword%")->orderBy('id', 'desc')->simplePaginate(10);
// Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->put(self::REDIS_SEARCH_ARTICLE_CACHE . $cacheName, $model, self::$cacheMinutes);
//}
$articleList = array(
'data' => [],
);
if(!empty($model)){
foreach ($model as $key => $article) {
$articleList['data'][$key] = self::getArticleModelByArticleId($article->id);
}
}
$articleList['page'] = $model;
return $articleList;
}
public static function getArticleListByTagId($tagId)
{
//if (empty($model = Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->get(self::REDIS_TAG_ARTICLE_CACHE . $tagId))) {
$model = self::select('id')->whereRaw(
'find_in_set(?, tags)',
[$tagId]
)->orderBy('id', 'desc')->simplePaginate(10);
// Cache::tags(self::REDIS_ARTICLE_PAGE_TAG)->put(self::REDIS_TAG_ARTICLE_CACHE . $tagId, $model, self::$cacheMinutes);
//}
$articleList = array(
'data' => [],
);
if(!empty($model)){
foreach ($model as $key => $article) {
$articleList['data'][$key] = self::getArticleModelByArticleId($article->id);
}
}
$articleList['page'] = $model;
return $articleList;
}
}