ListController.class.php
6.17 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/21
* Time: 10:19
*/
namespace Api\Controller\Doc;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\RpcFavoriteHelper;
use Common\Service\FileService;
use Common\Service\RightService;
class ListController extends \Api\Controller\AbstractController
{
/**
* List
* @author tangxingguo
* @desc 文件列表接口
* @param Int file_id:true 文件ID(根目录传0)
* @param String file_name 文件名称关键字
* @return array
array(
'file_id' => 2, // 当前文件夹ID
'paths' => array( // 文件夹路径列表
'file_name' => '吃喝玩乐', // 文件夹名称
'file_id' => 1, // 文件夹ID
),
'total_file' => 10, // 文件总数
'list' => array( // 文件列表
'file_id' => 123, // 文件ID
'file_name' => '001.pdf', // 文件名称
'file_type' => 2, // 文件类型(1=文件夹;2=文件)
'at_id' => '8765432345', // 附件ID
'at_size' => '123141', // 附件尺寸(单位字节)
'at_suffix' => 'pdf', // 文件尾缀
'update_time' => 1493264288000, // 最后更新时间
'my_is_favorite' => 1, // 我是否收藏(1=未收藏,2=已收藏)
'my_is_download' => 1, // 我是否可下载(1=不可下载,2=可下载)
),
);
*/
public function Index_post()
{
// 验证规则
$rules = [
'file_id' => 'require|integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
$postData['file_name'] = I('post.file_name', '', 'trim');
$user = $this->_login->user;
// 当前目录信息
$fileServ = new FileService();
// 目录下载权限
$folderDownload = true;
if ($postData['file_id'] != 0) {
$file = $fileServ->get($postData['file_id']);
if (empty($file)) {
E('_ERR_FILE_DATA_IS_NULL');
}
// 文件路径列表
$paths = $fileServ->getPaths($postData['file_id']);
// 下载被关闭
if ($file['is_download'] != Constant::FILE_DOWNLOAD_RIGHT_ON) {
$folderDownload = false;
}
} else {
$paths = [];
}
// 获取人员可查阅的目录列表
$rightServ = new RightService();
$readFileList = [0];
$readList = $rightServ->getReadList($user);
if (!empty($readList)) {
$arr = array_column($readList, 'file_id');
$readFileList = array_merge($readFileList, $arr);
}
if (!in_array($postData['file_id'], $readFileList)) {
E('_ERR_FILE_NOT_RIGHT');
}
// 获取人员可下载的目录列表
$downloadList = $rightServ->getDownloadList($user);
$downloadFileList = [0];
if (!empty($downloadList)) {
$downloadFileList = array_merge($downloadFileList, array_column($downloadList, 'file_id'));
}
// 目录列表数据
$conds = [
'is_show' => Constant::FILE_STATUS_IS_SHOW,
'file_status' => Constant::FILE_STATUS_NORMAL,
];
if (strlen($postData['file_name']) > 0) {
$conds['parent_id'] = $readFileList;
$conds['file_name like ?'] = '%' . $postData['file_name'] . '%';
} else {
$conds['parent_id'] = $postData['file_id'];
}
// 排序:首先文件夹,文件更新时间倒叙
$orders = [
'`order`' => 'asc',
'file_type' => 'asc',
'update_time' => 'desc'
];
$list = $fileServ->list_by_conds($conds, [], $orders);
// 处理数据
$rpcFavorite = &RpcFavoriteHelper::instance();
foreach ($list as $k => $v) {
switch ($v['file_type']) {
// 文件夹根据权限判断是否展示
case Constant::FILE_TYPE_IS_FOLDER:
$list[$k]['my_is_download'] = $v['is_download'];
if (!in_array($v['file_id'], $readFileList)) {
unset($list[$k]);
}
break;
// 文件添加是否可下载、文件后缀返回,人员是否有文件对应目录查阅权限
case Constant::FILE_TYPE_IS_DOC:
// 是否可下载
$my_is_download = in_array($v['parent_id'], $downloadFileList) && $folderDownload ? Constant::FILE_MY_DOWNLOAD_RIGHT_YES : Constant::FILE_MY_DOWNLOAD_RIGHT_NO;
$list[$k]['my_is_download'] = $my_is_download;
$list[$k]['at_suffix'] = end(explode('.', $v['file_name']));
// 父级目录不在可查阅列表,删除文件
if (!in_array($v['parent_id'], $readFileList)) {
unset($list[$k]);
}
// 文件url加入鉴权参数
$params = [
'_id' => $v['file_id'],
'user' => $user,
];
$list[$k]['at_url'] = fileUrl($v['at_url'], $params);
$list[$k]['at_convert_url'] = fileUrl($v['at_convert_url'], $params);
break;
}
// 跳过不可见的文件夹或文件
if (!isset($list[$k])) {
continue;
}
// RPC查询收藏结果
$param = [
'uid' => $user['memUid'],
'dataId' => $v['file_id'],
];
$status = $rpcFavorite->getStatus($param);
$list[$k]['my_is_favorite'] = Constant::FILE_MY_FAVORITE_NO;
if (isset($status['collection']) && $status['collection'] == RpcFavoriteHelper::COLLECTION_YES) {
$list[$k]['my_is_favorite'] = Constant::FILE_MY_FAVORITE_YES;
}
}
$this->_result = [
'file_id' => $postData['file_id'],
'paths' => $paths,
'total_file' => empty($list) ? 0 : count($list),
'list' => empty($list) ? [] : array_values($list),
];
}
}