ApiService.php
14.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\service;
use app\portal\model\PortalPostModel;
use app\portal\model\PortalCategoryModel;
use think\Db;
class ApiService
{
/**
* 功能:查询文章列表,支持分页;<br>
* 注:此方法查询时关联两个表portal_category_post(category_post),portal_post(post);在指定排序(order),指定查询条件(where)最好指定一下表别名
* @param array $param 查询参数<pre>
* array(
* 'category_ids'=>'',
* 'where'=>'',
* 'limit'=>'',
* 'order'=>'',
* 'page'=>'',
* 'relation'=>''
* )
* 字段说明:
* category_ids:文章所在分类,可指定一个或多个分类id,以英文逗号分隔,如1或1,2,3 默认值为全部
* field:调用指定的字段@todo
* 如只调用posts表里的id和post_title字段可以是post.id,post.post_title; 默认全部,
* 此方法查询时关联两个表portal_category_post(category_post),portal_post(post);
* 所以最好指定一下表名,以防字段冲突
* limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始)
* order:排序方式,如按posts表里的published_time字段倒序排列:post.published_time desc
* where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突,查询条件(只支持数组),格式和thinkPHP的where方法一样,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突;
* </pre>
* @return array 包括分页的文章列表<pre>
* 格式:
* array(
* "articles"=>array(),//文章列表,array
* "page"=>"",//生成的分页html,不分页则没有此项
* "total"=>100, //符合条件的文章总数,不分页则没有此项
* "total_pages"=>5 // 总页数,不分页则没有此项
* )</pre>
*/
public static function articles($param)
{
$portalPostModel = new PortalPostModel();
$where = [
'post.published_time' => [['> time', 0], ['<', time()]],
'post.post_status' => 1,
'post.post_type' => 1,
'post.delete_time' => 0
];
$paramWhere = empty($param['where']) ? '' : $param['where'];
$limit = empty($param['limit']) ? 10 : $param['limit'];
$order = empty($param['order']) ? '' : $param['order'];
$page = isset($param['page']) ? $param['page'] : false;
$relation = empty($param['relation']) ? '' : $param['relation'];
$categoryIds = empty($param['category_ids']) ? '' : $param['category_ids'];
$join = [
//['__USER__ user', 'post.user_id = user.id'],
];
if (!empty($categoryIds)) {
$field = !empty($param['field']) ? $param['field'] : 'post.*,min(category_post.category_id) as category_id';
array_push($join, ['__PORTAL_CATEGORY_POST__ category_post', 'post.id = category_post.post_id']);
if (!is_array($categoryIds)) {
$categoryIds = explode(',', $categoryIds);
}
if (count($categoryIds) == 1) {
$where['category_post.category_id'] = $categoryIds[0];
} else {
$where['category_post.category_id'] = ['in', $categoryIds];
}
} else {
$field = !empty($param['field']) ? $param['field'] : 'post.*,min(category_post.category_id) as category_id';
array_push($join, ['__PORTAL_CATEGORY_POST__ category_post', 'post.id = category_post.post_id']);
}
$articles = $portalPostModel->alias('post')->field($field)
->join($join)
->where($where)
->where($paramWhere)
->order($order)
->group('post.id');
$return = [];
if (empty($page)) {
$articles = $articles->limit($limit)->select();
if (!empty($relation) && !empty($articles['items'])) {
$articles->load($relation);
}
$return['articles'] = $articles;
} else {
if (is_array($page)) {
if (empty($page['list_rows'])) {
$page['list_rows'] = 10;
}
$articles = $articles->paginate($page);
} else {
$articles = $articles->paginate(intval($page));
}
if (!empty($relation) && !empty($articles['items'])) {
$articles->load($relation);
}
$articles->appends(request()->get());
$articles->appends(request()->post());
$return['articles'] = $articles->items();
$return['page'] = $articles->render();
$return['total'] = $articles->total();
$return['total_pages'] = $articles->lastPage();
}
return $return;
}
/**
* 功能:查询标签文章列表,支持分页;<br>
* 注:此方法查询时关联两个表portal_tag_post(tag_post),portal_post(post);在指定排序(order),指定查询条件(where)最好指定一下表别名
* @param array $param 查询参数<pre>
* array(
* 'tag_id'=>'',
* 'where'=>'',
* 'limit'=>'',
* 'order'=>'',
* 'page'=>'',
* 'relation'=>''
* )
* 字段说明:
* field:调用指定的字段@todo
* 如只调用posts表里的id和post_title字段可以是post.id,post.post_title; 默认全部,
* 此方法查询时关联两个表portal_tag_post(category_post),portal_post(post);
* 所以最好指定一下表名,以防字段冲突
* limit:数据条数,默认值为10,可以指定从第几条开始,如3,8(表示共调用8条,从第3条开始)
* order:排序方式,如按posts表里的published_time字段倒序排列:post.published_time desc
* where:查询条件,字符串形式,和sql语句一样,请在事先做好安全过滤,最好使用第二个参数$where的数组形式进行过滤,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突,查询条件(只支持数组),格式和thinkPHP的where方法一样,此方法查询时关联多个表,所以最好指定一下表名,以防字段冲突;
* </pre>
* @return array 包括分页的文章列表<pre>
* 格式:
* array(
* "articles"=>array(),//文章列表,array
* "page"=>"",//生成的分页html,不分页则没有此项
* "total"=>100, //符合条件的文章总数,不分页则没有此项
* "total_pages"=>5 // 总页数,不分页则没有此项
* )</pre>
*/
public static function tagArticles($param)
{
$portalPostModel = new PortalPostModel();
$where = [
'post.published_time' => [['> time', 0], ['<', time()]],
'post.post_status' => 1,
'post.post_type' => 1,
'post.delete_time' => 0
];
$paramWhere = empty($param['where']) ? '' : $param['where'];
$limit = empty($param['limit']) ? 10 : $param['limit'];
$order = empty($param['order']) ? '' : $param['order'];
$page = isset($param['page']) ? $param['page'] : false;
$relation = empty($param['relation']) ? '' : $param['relation'];
$tagId = empty($param['tag_id']) ? '' : $param['tag_id'];
$join = [
//['__USER__ user', 'post.user_id = user.id'],
];
if (empty($tagId)) {
return null;
} else {
$field = !empty($param['field']) ? $param['field'] : 'post.*';
array_push($join, ['__PORTAL_TAG_POST__ tag_post', 'post.id = tag_post.post_id']);
$where['tag_post.tag_id'] = $tagId;
}
$articles = $portalPostModel->alias('post')->field($field)
->join($join)
->where($where)
->where($paramWhere)
->order($order);
$return = [];
if (empty($page)) {
$articles = $articles->limit($limit)->select();
if (!empty($relation) && !empty($articles['items'])) {
$articles->load($relation);
}
$return['articles'] = $articles;
} else {
if (is_array($page)) {
if (empty($page['list_rows'])) {
$page['list_rows'] = 10;
}
$articles = $articles->paginate($page);
} else {
$articles = $articles->paginate(intval($page));
}
if (!empty($relation) && !empty($articles['items'])) {
$articles->load($relation);
}
$articles->appends(request()->get());
$articles->appends(request()->post());
$return['articles'] = $articles->items();
$return['page'] = $articles->render();
$return['total'] = $articles->total();
$return['total_pages'] = $articles->lastPage();
}
return $return;
}
/**
* 获取指定id的文章
* @param int $id
* @return array|false|\PDOStatement|string|\think\Model
*/
public static function article($id)
{
$portalPostModel = new PortalPostModel();
$where = [
'published_time' => [['> time', 0], ['<', time()]],
'post_status' => 1,
'post_type' => 1,
'id' => $id,
'delete_time' => 0
];
return $portalPostModel->where($where)->find();
}
/**
* 获取指定条件的页面列表
* @param array $param 查询参数<pre>
* array(
* 'where'=>'',
* 'order'=>'',
* )</pre>
* @return false|\PDOStatement|string|\think\Collection
*/
public static function pages($param)
{
$paramWhere = empty($param['where']) ? '' : $param['where'];
$order = empty($param['order']) ? '' : $param['order'];
$portalPostModel = new PortalPostModel();
$where = [
'published_time' => [['> time', 0], ['<', time()]],
'post_status' => 1,
'post_type' => 2, //页面
'delete_time' => 0
];
return $portalPostModel
->where($where)
->where($paramWhere)
->order($order)
->select();
}
/**
* 获取指定id的页面
* @param int $id 页面的id
* @return array|false|\PDOStatement|string|\think\Model 返回符合条件的页面
*/
public static function page($id)
{
$portalPostModel = new PortalPostModel();
$where = [
'published_time' => [['> time', 0], ['<', time()]],
'post_status' => 1,
'post_type' => 2,
'id' => $id,
'delete_time' => 0
];
return $portalPostModel->where($where)->find();
}
/**
* 返回指定分类
* @param int $id 分类id
* @return array 返回符合条件的分类
*/
public static function category($id)
{
$portalCategoryModel = new PortalCategoryModel();
$where = [
'status' => 1,
'delete_time' => 0,
'id' => $id
];
return $portalCategoryModel->where($where)->find();
}
/**
* 返回指定分类下的子分类
* @param int $categoryId 分类id
* @return false|\PDOStatement|string|\think\Collection 返回指定分类下的子分类
*/
public static function subCategories($categoryId)
{
$portalCategoryModel = new PortalCategoryModel();
$where = [
'status' => 1,
'delete_time' => 0,
'parent_id' => $categoryId
];
return $portalCategoryModel->where($where)->select();
}
/**
* 返回指定分类下的所有子分类
* @param int $categoryId 分类id
* @return array 返回指定分类下的所有子分类
*/
public static function allSubCategories($categoryId)
{
$portalCategoryModel = new PortalCategoryModel();
$categoryId = intval($categoryId);
if ($categoryId !== 0) {
$category = $portalCategoryModel->field('path')->where('id', $categoryId)->find();
if (empty($category)) {
return [];
}
$categoryPath = $category['path'];
} else {
$categoryPath = 0;
}
$where = [
'status' => 1,
'delete_time' => 0,
'path' => ['like', "$categoryPath-%"]
];
return $portalCategoryModel->where($where)->select();
}
/**
* 返回符合条件的所有分类
* @param array $param 查询参数<pre>
* array(
* 'where'=>'',
* 'order'=>'',
* )</pre>
* @return false|\PDOStatement|string|\think\Collection
*/
public static function categories($param)
{
$paramWhere = empty($param['where']) ? '' : $param['where'];
$order = empty($param['order']) ? '' : $param['order'];
$portalCategoryModel = new PortalCategoryModel();
$where = [
'status' => 1,
'delete_time' => 0,
];
return $portalCategoryModel
->where($where)
->where($paramWhere)
->order($order)
->select();
}
/**
* 获取面包屑数据
* @param int $categoryId 当前文章所在分类,或者当前分类的id
* @param boolean $withCurrent 是否获取当前分类
* @return array 面包屑数据
*/
public static function breadcrumb($categoryId, $withCurrent = false)
{
$data = [];
$portalCategoryModel = new PortalCategoryModel();
$path = $portalCategoryModel->where(['id' => $categoryId])->value('path');
if (!empty($path)) {
$parents = explode('-', $path);
if (!$withCurrent) {
array_pop($parents);
}
if (!empty($parents)) {
$data = $portalCategoryModel->where(['id' => ['in', $parents]])->order('path ASC')->select();
}
}
return $data;
}
}