-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArticle.php
149 lines (149 loc) · 5.74 KB
/
Article.php
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
header("Content-Type:text/html;charset=UTF-8");
// 乱码看三个地方,
// 1、数据库编码
// 2、页面编码
// 3、连接编码
// 三个一致了就毛事木有。
class Article
{
/**这是一个数据库的句柄
*
*/
private $_db;
/**
* 构造方法为数据库连接
*/
public function __construct($_db){
$this -> _db = $_db;
}
/**创建文章
*
*/
public function create($author,$title,$content)
{
if(empty($title)){
throw new Exception('文章标题不能为空',ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
}
if(empty($content)){
throw new Exception('文章内容不能为空',ErrorCode::ARTICLE_CONTENT_CANNOT_EMPTY);
}
if(empty($author)){
throw new Exception('作者名不能为空',ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
}
$sql = 'SELECT * FROM `zhuche` WHERE `username`=:author';
$stmt=$this -> _db ->prepare($sql);
$stmt -> bindParam(':author',$author);
$stmt -> execute();
$man = $stmt -> fetch(PDO::FETCH_ASSOC);
$sql = 'INSERT INTO `main_info`(`title`,`content`,`author`,`time`,`authorId`) VALUE (:title,:content,:author,:time,:authorid)';
$time = date('Y-m-d',time());
$stmt = $this -> _db -> prepare($sql);
$stmt -> bindParam(':title',$title);
$stmt -> bindParam(':content',$content);
$stmt -> bindParam(':author',$author);
$stmt -> bindParam(':time',$time);
$stmt -> bindParam(':authorid',$man['id']);
if(!$stmt->execute()){ //如果添加失败
throw new Exception('发表文章失败',ErrorCode::ARTICLE_CREATE_FAIL);
}
return ['articleId' => $this -> _db -> lastInsertId(),
'author' => $author,
'authorId' => $man['id'],
'title' => $title,
'content' => $content
];
}
/**编辑文章
*
*/
public function edit($articleId,$content,$title,$author) //这个文章的id需要与修改人的id匹配,一般用id而不是字符串的作者
{
$article = $this -> view($articleId);
if($article['author']!=$author){
throw new Exception('对不起,您无权操作这篇文章',ErrorCode::PERMISSION_DENIED);
}
$title = empty($title)?$article['title']:$title;
$content = empty($content)?$article['content']:$content;
if($article['content']==$content&&$article['title']==$title)
{
return $article;
}
$sql = 'UPDATE `main_info` SET `title`=:title,`content`=:content WHERE `id` = :articleId';
//更新的时候需要一个逗号
$stmt=$this -> _db ->prepare($sql);
$stmt -> bindParam(':content',$content);
$stmt -> bindParam(':title',$title);
$stmt -> bindParam(':articleId',$articleId);
if(!$stmt -> execute()){
throw new Exception('文章编辑失败',ErrorCode::ARTICLE_EDIT_FAIL);
}
return ['articleId' => $articleId,
'title' => $title,
'content' => $content,
'time' => $article['time']
];
}
/**
* 查看文章内容
*/
public function view($articleId){
if(empty($articleId)){
throw new Exception('文章ID不能为空',ErrorCode::ARTICLE_ID_CANNOT_EMPTY);
}
$sql = 'SELECT * FROM `main_info` WHERE `id`=:id';
$stmt=$this -> _db ->prepare($sql);
$stmt -> bindParam(':id',$articleId);
$stmt -> execute();
$article = $stmt -> fetch(PDO::FETCH_ASSOC);
if(empty($article)){
throw new Exception('文章不存在',ErrorCode::ARTICLE_NOT_FOUND);
}
return $article;
}
/**
* 删除文章
*/
public function delete($articleId,$author)
{
$article = $this -> view($articleId);
if($article['author']!=$author){
throw new Exception('您权限不足',ErrorCode::PERMISSION_DENIED);
}
$sql = 'SELECT * FROM `main_info` WHERE `id`=:articleId';
$stmt =$this -> _db ->prepare($sql);
$stmt -> bindParam(':articleId',$articleId);
$stmt -> execute(); //查成一个数组
$article = $stmt -> fetch(PDO::FETCH_ASSOC);
if(empty($article)){
throw new Exception('找不到此文章',ErrorCode::ARTICLE_NOT_FOUND);
}
$sql = 'DELETE FROM `main_info` WHERE `id`=:articleId AND `author`=:author';
$stmt =$this -> _db ->prepare($sql);
$stmt -> bindParam(':articleId',$articleId);
$stmt -> bindParam(':author',$author);
if(!$stmt->execute()){
throw new Exception('删除文章失败',ErrorCode::ARTICLE_DELETE_FALI);
}
return true;
}
public function getList($authorid, $page= 1,$size =10)
{
if($size>100){
throw new Exception('分页大小最多为100',15);
}
$limit = ($page-1)*$size;
$limit = $limit < 0 ? 0:$limit;
$sql = 'SELECT * FROM `main_info` WHERE `authorid`=:authorid LIMIT '.$limit.','.$size;
//这个只能根据键查找
//从limit开始(不算limit)的后offset个
$stmt= $this -> _db -> prepare($sql);
$stmt->bindParam(':authorid',$authorid);
// $stmt->bindParam(':limit',$limit);
// $stmt->bindParam(':offset',$size);
$stmt->execute();
$data = $stmt ->fetchAll(PDO::FETCH_ASSOC);
return $data;
}
}
?>