UsedDayCountExportController.class.php
3.88 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
<?php
/**
* 企业每日资费使用情况导出
*/
namespace Apicp\Controller\HomePage;
use Com\PythonExcel;
use VcySDK\Enterprise;
use VcySDK\Service;
class UsedDayCountExportController extends AbstractController
{
/**
* @author zhoutao
* @desc 企业每日资费使用情况导出
* @param Int beginTime:true 开始日期
* @param Int endTime:true 结束日期
*/
public function index()
{
$enterpriseSdk = new Enterprise(Service::instance());
$tempList = $enterpriseSdk->useDayCountExport([
'beginTime' => I('get.beginTime'),
'endTime' => I('get.endTime'),
]);
if (empty($tempList)) {
return true;
}
$list = [];
foreach ($tempList as $item) {
$list[] = [
rgmdate($item['cutdTime'], 'Y-m-d H:i:s'),
$this->convertUnit($item['cutdAddDisk'], 2),
$this->convertUnit($item['cutdSurplusDisk'], 2),
$this->convertUnit($item['cutdAddNetwork'], 2),
$this->convertUnit($item['cutdSurplusNetwork'], 2),
];
}
$filename = rgmdate(NOW_TIME, 'Ymd') . '_企业每日资费使用情况';
$filename = $filename . '.xls';
PythonExcel::instance()->write(get_sitedir() . $filename, [
'日期', '当日使用空间容量', '剩余空间容量', '当日使用访问流量', '剩余访问流量'
], $list);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename="' . $filename . '"');
// 根据浏览器类型来判断是否需要特殊处理中文字符
$encoded_filename = urlencode($filename);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $ua) || preg_match("/rv:11.0/", $ua) || preg_match("/Edge/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
} else {
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
header("Content-Transfer-Encoding:binary");
echo file_get_contents(get_sitedir() . $filename);
exit;
}
/**
* 字节 -> 吉, 加单位
* @param $num int 数量
* @param $decimals int 精确到几位小数点
* @return float|string
*/
protected function convertUnit($num, $decimals)
{
$p = 0;
$format = 'bytes';
$unit = 1024;
if ($num >= 0 && $num < $unit) {
return $num . ' ' . $format;
}
if ($num >= $unit && $num < $unit ** 2) {
$p = 1;
$format = 'KB';
}
if ($num >= $unit ** 2 && $num < $unit ** 3) {
$p = 2;
$format = 'MB';
}
if ($num >= $unit ** 3 && $num < $unit ** 4) {
$p = 3;
$format = 'GB';
}
if ($num >= $unit ** 4 && $num < $unit ** 5) {
$p = 4;
$format = 'TB';
}
$num /= $unit ** $p;
return $this->notRounded($num, $decimals) . ' ' . $format;
}
/**
* 精确到小数点位数 但是不 四舍五入
* @param $num
* @param $decimals
* @return float|int
*/
protected function notRounded($num, $decimals)
{
$len = 10 ** $decimals;
return floor($num * $len) / $len;
}
}