-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_news.php
171 lines (161 loc) · 5.61 KB
/
api_news.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Created by PhpStorm.
* User: thinkpad
* Date: 2016/2/15
* Time: 21:59
*/
if (!defined('IN_DZZ')) {
exit('Access Denied');
}
define('CURSCRIPT', 'dzz');
define('CURMODULE', 'news');
define('NOROBOT', TRUE);
if (!in_array($_GET['action'], array('category', 'list', 'view'))) {
$_GET['action'] = 'category';
}
if(!$_G['uid'])
json_error('请先登录');
$action = $_GET['action'];
//判断用户访问权限
include libfile('function/organization');
include libfile('function/news', '', 'dzz/news');
$perm = getPermByUid($_G['uid']);
$result = true;
$sql="1";
if($perm<2){
//阅读范围查询语句
if($_G['uid']<1){
$sql.=" and n.orgids='' and n.uids=''";
}else{
$sql.=" and ( n.authorid=%d OR (";
$param[]=$_G['uid'];
$sql_gid=array("n.orgids=''");
if($orgarr=getDepartmentByUid($_G['uid'])){ //获取当前用户所在的部门数组
foreach($orgarr as $value){
foreach($value as $value1){
$sql_gid[]="FIND_IN_SET(%d,orgids)";
$param[]=$value1['orgid'];
}
}
}else{
$sql_gid[]="FIND_IN_SET(%s,orgids)";
$param[]='other';
}
$sql.="(".implode(' OR ',$sql_gid).") and ( n.uids='' OR FIND_IN_SET(%d,n.uids))";
$sql.="))";
$param[]=$_G['uid'];
}
}
switch ($action) {
case 'category':
$message = 'success';
$catid = empty($_GET['catid']) ? 0 : intval($_GET['catid']);
$data = catList($catid,$sql,$param);
break;
case 'list':
$catid = empty($_GET['catid']) ? 0 : intval($_GET['catid']);
$isHistory = empty($_GET['isHistory']) ? 0 : 1;
if($catid==0)
json_error('分类不存在');
$subids=C::t('news_cat')->getSonByCatid($catid);
$sql.=' and catid IN(%n)';
$param[]=$subids;
$sql.=" and status='1'";
$sql = !$isHistory ? $sql . ' and v.vid is null' : $sql;
$data = listView($sql,$param);
$message = 'success';
break;
case 'view':
$newid = empty($_GET['newid'])?0:intval($_GET['newid']);
if(!$news=C::t('news')->fetch($newid))
json_error('信息不存在或者已被删除');
if(!getViewPerm($news)){
json_error('您没有查看此信息的权限,请联系管理员');
}
//更新查阅
C::t('news')->increase($newid,array('views'=>1));
if($_G['uid']){
if($vid=DB::result_first("select vid from %t where newid=%d and uid=%d",array('news_viewer',$newid,$_G['uid']))){
DB::query("update %t SET views=views+1 where vid=%d",array('news_viewer',$vid));
}else{
$addviewer=array('newid'=>$newid,
'uid'=>$_G['uid'],
'username'=>$_G['username'],
'dateline'=>TIMESTAMP
);
C::t('news_viewer')->insert($addviewer);
}
}
$news['dateline'] = dgmdate($news[dateline],'u');
$data = $news;
$message = 'success';
break;
default:
$result = false;
$message = '不存在的方法';
break;
}
if ($result)
json_success($message, $data);
else
json_error($message);
function catList($catid = 0,$sql,$param)
{
global $_G;
//查询
$params = array('news');
$params[] = 'news_viewer';
$params[] = $_G['uid'];
$params = $param ? array_merge_recursive($params, $param) : $params;
foreach (C::t('news_cat')->fetch_all_by_pid($catid) as $value) {
$catids = $common='';
$result[$value['catid']] = $value;
$sun = C::t('news_cat')->fetch_all_by_pid($value['catid']);
if ($sun) {
foreach($sun as $s){
$catids .= $common.$s['catid'];
$common = ',';
}
$result[$value['catid']]['sub'] = $sun;
}else{
$catids = $value['catid'];
}
//每个分类下未读消息个数
$count = DB::fetch_first("select count(1) as count from %t n LEFT JOIN %t v ON n.newid=v.newid and v.uid=%d where $sql and n.catid in ({$catids}) and v.vid is NULL ", $params);
$result[$value['catid']]['unread_count'] = $count['count'];
}
return $result;
}
function listView($sql,$param){
global $_G;
//查询
$orderby="ORDER BY n.istop DESC , n.dateline DESC";
$perpage=2;
$page = empty($_GET['page'])?1:intval($_GET['page']);
$start=($page-1)*$perpage;
$params = array('news');
$params[] = 'news_viewer';
$params[] = $_G['uid'];
$params = array_merge_recursive($params,$param);
if($count=DB::result_first("select count(*) from %t n LEFT JOIN %t v ON n.newid=v.newid and v.uid=%d where $sql",$params)){
foreach(DB::fetch_all("select n.*,v.vid as isread from %t n LEFT JOIN %t v ON n.newid=v.newid and v.uid=%d where $sql $orderby limit $start,$perpage",$params) as $value){
$today=strtotime(dgmdate(TIMESTAMP,'Y-m-d'));
if($value['topendtime']<$today){
if($value['istop']){
$updatearr[]=$value['newid'];
}
$value['istop']=0;
}
if($value['highlightendtime']<$today ){
$value['ishighlight']=0;
}
if($value['opuid'] && $opuser=getuserbyuid($value['opuid'])){
$value['opauthor']=$opuser['username'];
}
$value['dateDiff'] = dgmdate($value[dateline],'u');
$data[]=$value;
}
}
return array('list'=>$data,'perpage'=>$perpage,'page'=>$page,'count'=>$count);
}