ArticleController.php
6.3 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
<?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 api\portal\controller;
//use cmf\controller\HomeBaseController;
use app\portal\model\MemberModel;
use app\portal\model\UserModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;
/**
* @title 文章接口
* @description 接口说明
* @group 接口分组
*/
class ArticleController extends CommonController
{
/*
* 长度处理
* */
public function changeLen($text,$number,$numberNext){
$change_html = strip_tags(htmlspecialchars_decode($text));
$sub_text_first = mb_substr($change_html,0,$number,'utf-8');
if(strlen($change_html) >= $numberNext){
$sub_text = $sub_text_first.'...';
}else{
$sub_text = $sub_text_first;
}
return $sub_text;
}
/**
* @title 文章列表
* @description 接口说明
* @author 开发者
* @url /api/portal/Article/index
* @method POST
* @param name:articleType type:int require:1 default: other: desc:类型
* @param name:page type:int require:1 default: other: desc:页码
* @return data:''@
* @data post_id:文章id
* @data category_id:类型id
* @data name:类型名
* @data post_title:文章标题
*
*/
public function index(Request $request)
{
$size = 8;
$cart_arr = array();
$choose = array();
$page = $request->param('page');
$where_cate['status'] = 1;
$where_connect['j.status'] = 1;
$where_connect['p.post_status'] = 1;
$where_connect['j.category_id'] = $_POST['articleType'];
$article_list = Db::name('PortalCategoryPost')->alias('j')
->where($where_connect)
->field("j.post_id,j.category_id , c.name , p.post_title,p.create_time")
->join("PortalCategory c","c.id = j.category_id")
->join("PortalPost p","p.id = j.post_id")
->page("$page,$size")
->select()->toArray();
foreach ($article_list as $artk=>$artv){
$article_list[$artk]['post_title'] = $this->changeLen($artv['post_title'],25,70);
$article_list[$artk]['create_time'] = date('Y-m-d H:i:s',$artv['create_time']);
}
$where_next['j.status'] = 1;
$where_next['p.post_status'] = 1;
$article_next = Db::name('PortalCategoryPost')->alias('j')
->where($where_next)
->field("j.post_id,j.category_id , c.name , p.post_title,p.create_time")
->join("PortalCategory c","c.id = j.category_id")
->join("PortalPost p","p.id = j.post_id")
->select()->toArray();
foreach ($article_next as $nextk=>$nextv){
if(!in_array($nextv['category_id'],$choose)){
$middle['id'] = $nextv['category_id'];
$middle['name'] = $this->changeLen($nextv['name'],10,30);
$choose[] = $nextv['category_id'];
$cart_arr[] = $middle;
}
}
$final['cate'] = $cart_arr;
if(empty($article_list)){
$this->apiResponse('1','暂无数据');
}else{
$final['art'] = $article_list;
}
$count = Db::name('PortalCategoryPost')->alias('j')
->where($where_connect)
->field("j.post_id,j.category_id , c.name , p.post_title,p.create_time")
->join("PortalCategory c","c.id = j.category_id")
->join("PortalPost p","p.id = j.post_id")
->count();
$all_page = ceil($count/$size);
$final['all_page'] = $all_page;
$this->apiResponse('1','成功',$final);
}
/**
* @title 文章详情
* @description 接口说明
* @author 开发者
* @url /api/portal/Article/detail
* @method POST
*
* @param name:post_id type:int require:1 default: other: desc:文章id
*
* @return content:''@!
* @content thumb:文章图片
* @content title:文章标题
* @content content:文章内容
* @content update_time:创建时间
* @content category_id:类型id
* @content name:类型名
* @return hot:''@
* @hot title:文章标题
* @hot article_id:文章id
*
*/
public function detail(Request $request)
{
$where_art['p.id'] = $request->param('post_id');
$where_art['p.post_status'] = 1;
$article_list = Db::name('PortalCategoryPost')
->alias('j')
->where($where_art)
->field("p.post_title as title,p.post_content as content,p.more,p.update_time ,
c.name,c.id as category_id")
->join("PortalCategory c","c.id = j.category_id")
->join("PortalPost p","p.id = j.post_id")
->find();
if(empty($article_list)){
$this->apiResponse('1','暂无数据');
}
// 处理图片
$thumb = json_decode($article_list['more'],true);
$article_list['thumb'] = $thumb['thumbnail'];
unset($article_list['more']);
// 处理内容
$article_list['content'] = htmlspecialchars_decode($article_list['content']);
$article_list['create_time'] = date('Y-m-d',$article_list['update_time']);
$final['content'] = $article_list;
// 热门文章
$where_hot['p.recommended'] = 1;
$where_hot['p.post_status'] = 1;
$hotArticle = Db::name('PortalCategoryPost')
->alias('j')
->where($where_hot)
->field("p.post_title as title,p.id as article_id")
// ->join("PortalCategory c","c.id = j.category_id")
->join("PortalPost p","p.id = j.post_id")
->select();
$final['hot'] = $hotArticle;
$this->apiResponse('1','成功',$final);
}
}