AppframeController.class.php
13.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
namespace Common\Controller;
use Think\Controller;
class AppframeController extends Controller {
function _initialize() {
$this->assign("waitSecond", 3);
$time=time();
$this->assign("js_debug",APP_DEBUG?"?v=$time":"");
}
// 民族列表
public function nation() {
$nation = M('Nation')
->select();
$this->assign('nation', $nation);
}
// 地区列表
public function region() {
$region = M('Region')
->select();
$this->assign('region', $region);
$region_mobile = array();
foreach($region as $k=>$v) {
$region_mobile[$k]['value'] = $v['id'];
$region_mobile[$k]['label'] = $v['name'];
}
$this->assign('region_mobile', json_encode($region_mobile));
}
// 一级分类列表
public function product() {
$where['parent'] = 0;
$product = M('Product')
->where($where)
->select();
$this->assign('product', $product);
}
// 二级分类列表
public function product_son($id) {
$where['parent'] = $id;
$product_son = M('Product')
->where($where)
->select();
$this->assign('product_son', $product_son);
}
/**
* 时间格式化
*/
public function formateData($time){
$now_time = date("Y-m-d H:i:s");
$now_time = strtotime($now_time);
$dur = $now_time - $time;
if($dur < 60){
return $dur.'秒前';
}else{
if($dur < 3600){
return floor($dur/60).'分钟前';
}else{
if($dur < 86400){
return floor($dur/3600).'小时前';
}else{
return floor($dur/86400).'天前';
}
}
}
}
/*
* TP上传操作
* @author ShiO
* @date 2015年9月21日 14:14:09
* 20M = 20971520B
* 5M = 5242880B
*/
protected function upload($savePath, $maxSize = 20971520, $exts = array('jpg', 'gif', 'png', 'jpeg', 'mp4'), $saveName = array()) {
if (!$saveName) {
$saveName = time() . mt_rand(1000, 9999);
} elseif ($saveName === true) {
$saveName = array('uniqid', '');
}
$upload = new \Think\Upload();
// 实例化上传类
$upload->maxSize = $maxSize;
// 设置附件上传大小
$upload->exts = $exts;
// 设置附件上传类型
$upload->savePath = $savePath . '/';
// 设置附件上传(子)目录
$upload->autoSub = true;
// 开启子目录保存
$upload->subName = array('date', 'Ymd');
// 自动子目录命名规则
$upload->saveName = $saveName;
// 文件命名规则
$upload->rootPath = './'.C("UPLOADPATH");
// 文件上传根目录
// 上传文件
$info = $upload->upload();
if (!$info) {// 上传错误提示错误信息
return array(false, $upload->getError());
} else {// 上传成功
return array(true, $info);
}
}
/*
* TP上传操作(生成图片缩略图)
* @author lz
* @date 2017年12月23日
*/
protected function image($imagePath) {
$image = new \Think\Image();
$path = explode(".", $imagePath);
$filename = $path[0]."_thumb";
$image->open('.'.C('UPLOADS_PATH').$imagePath);
// 生成一个居中裁剪为150*150的缩略图并保存为_thumb.jpg
if($image->thumb(C('THUMB_WIDTH'), C('THUMB_HEIGHT'),\Think\Image::IMAGE_THUMB_CENTER)->save('.'.C('UPLOADS_PATH').$filename.".".$path[1])){
return $filename.".".$path[1];
}
}
/*
* TP上传操作(并验证是否有重复文件,如果有则不再上传)
* @author lz
* 增加防止重复的文件上传方式,后缀名判断
*/
protected function uploadCommon($uploadType = 'file', $maxSize = 20971520, $uploadModule = '', $returnJson = true) {
// 判断文件是否重复
$this->attachmentModel = D('Common/Attachment');
list($fileInfo, $attachmentId) = $this->_uploadCheckExists();
if ($fileInfo) {
if (!$returnJson) {
return array(true, $fileInfo, C('UPLOADS_PATH') . $fileInfo, $attachmentId);
}
$this->ajaxReturn(array('code' => 0, 'msg' => '上传成功', 'data' => array('path' => $fileInfo, 'link' => C('UPLOADS_PATH') . $fileInfo, 'attachment_id' => $attachmentId)));
}
if ($uploadType == 'file') {
$allFileType = array();
$allFileType = array_merge($allFileType, C('UPLOAD_IMAGE_EXT'));
$allFileType = array_merge($allFileType, C('UPLOAD_AUDIO_EXT'));
$allFileType = array_merge($allFileType, C('UPLOAD_VIDEO_EXT'));
$allFileType = array_merge($allFileType, C('UPLOAD_FILE_EXT'));
list($status, $info) = $this->upload($uploadModule, $maxSize, $allFileType);
} else {
list($status, $info) = $this->upload($uploadModule, $maxSize, C('UPLOAD_' . strtoupper($uploadType) . '_EXT'));
}
if (!$status) {
if (!$returnJson) {
return array(false, $info, '', '');
}
$this->ajaxReturn(array('code' => 9, 'msg' => $info));
}
$info = current($info);
//判断是否有图
$savePath = $info['savepath'] . $info['savename'];
$originName = $info['name'];
$saveName = $info['savename'];
$fileType = $info['type'];
$ext = $info['ext'];
$size = $info['size'];
$md5 = $info['md5'];
$sha1 = $info['sha1'];
$attachmentData = array('origin_name' => $originName, 'save_name' => $saveName, 'save_path' => $savePath, 'upload_module' => $uploadModule, 'type' => $fileType, 'ext' => $ext, 'size' => $size, 'md5' => $md5, 'sha1' => $sha1);
$attachmentId = $this->attachmentModel->addFileUpload($attachmentData);
if (!$returnJson) {
return array(true, $savePath, C('UPLOADS_PATH') . $savePath, $attachmentId);exit;
}
//返回文件地址和名给JS作回调用
$this->ajaxReturn(array('code' => 0, 'msg' => '上传成功', 'data' => array('path' => $savePath, 'link' => C('UPLOADS_PATH') . $savePath, 'attachment_id' => $attachmentId), 'info'=>$info));
}
// 节省空间利器,检测文件是否已经上传,如果已经上传则不再新增了
private function _uploadCheckExists() {
$file = current($_FILES);
$fileHash = md5_file($file['tmp_name']);
if (!$fileHash) {
return false;
}
list($fileInfo, $attachmentId) = $this->attachmentModel->getFileUploadInfoByHash($fileHash);
return array($fileInfo, $attachmentId);
}
/**
* Ajax方式返回数据到客户端
* @access protected
* @param mixed $data 要返回的数据
* @param String $type AJAX返回数据格式
* @return void
*/
protected function ajaxReturn($data, $type = '',$json_option=0) {
$data['referer'] = $data['url'] ? $data['url'] : "";
$data['state'] = !empty($data['status']) ? "success" : "fail";
if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
switch (strtoupper($type)){
case 'JSON' :
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data,$json_option));
case 'XML' :
// 返回xml格式数据
header('Content-Type:text/xml; charset=utf-8');
exit(xml_encode($data));
case 'JSONP':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
$handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
exit($handler.'('.json_encode($data,$json_option).');');
case 'EVAL' :
// 返回可执行的js脚本
header('Content-Type:text/html; charset=utf-8');
exit($data);
case 'AJAX_UPLOAD':
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:text/html; charset=utf-8');
exit(json_encode($data,$json_option));
default :
// 用于扩展其他返回格式数据
Hook::listen('ajax_return',$data);
}
}
/**
*
* @param number $totalSize 总数
* @param number $pageSize 总页数
* @param number $currentPage 当前页
* @param number $listRows 每页显示条数
* @param string $pageParam 分页参数
* @param string $pageLink 分页链接
* @param string $static 是否为静态链接
*/
protected function page($totalSize = 1, $pageSize = 0, $currentPage = 1, $listRows = 6, $pageParam = '', $pageLink = '', $static = FALSE) {
if ($pageSize == 0) {
$pageSize = C("PAGE_LISTROWS");
}
if (empty($pageParam)) {
$pageParam = C("VAR_PAGE");
}
$page = new \Page($totalSize, $pageSize, $currentPage, $listRows, $pageParam, $pageLink, $static);
$page->setLinkWraper("li");
if(sp_is_mobile()){
$page->SetPager('default', '{prev} {list} {next}', array("listlong" => "4", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
}else{
$page->SetPager('default', '{first}{prev} {liststart}{list}{listend} {next}{last}', array("listlong" => "4", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
}
return $page;
}
/**
*
* @param number $totalSize 总数
* @param number $pageSize 总页数
* @param number $currentPage 当前页
* @param number $listRows 每页显示条数
* @param string $pageParam 分页参数
* @param string $pageLink 分页链接
* @param string $static 是否为静态链接
*/
protected function pages($totalSize = 1, $pageSize = 0, $currentPage = 1, $listRows = 6, $pageParam = '', $pageLink = '', $static = FALSE) {
if ($pageSize == 0) {
$pageSize = C("PAGE_LISTROWS");
}
if (empty($pageParam)) {
$pageParam = C("VAR_PAGE");
}
$page = new \Page($totalSize, $pageSize, $currentPage, $listRows, $pageParam, $pageLink, $static);
$page->setLinkWraper("");
if(sp_is_mobile()){
$page->SetPager('new', '{prev} {list} {next}', array("listlong" => "4", "prev" => "<", "next" => ">", "list" => "*", "disabledclass" => ""));
}else{
$page->SetPager('new', '{prev} {liststart}{list}{listend} {next}', array("listlong" => "10", "prev" => "<", "next" => ">", "list" => "*", "disabledclass" => ""));
}
return $page;
}
/**
*
* @param number $totalSize 总数
* @param number $pageSize 总页数
* @param number $currentPage 当前页
* @param number $listRows 每页显示条数
* @param string $pageParam 分页参数
* @param string $pageLink 分页链接
* @param string $static 是否为静态链接
*/
protected function page_goods($totalSize = 1, $pageSize = 0, $currentPage = 1, $listRows = 6, $pageParam = '', $pageLink = '', $static = FALSE) {
if ($pageSize == 0) {
$pageSize = C("PAGE_LISTROWS");
}
if (empty($pageParam)) {
$pageParam = C("VAR_PAGE");
}
$page = new \Page($totalSize, $pageSize, $currentPage, $listRows, $pageParam, $pageLink, $static);
$page->setLinkWraper("li");
if(sp_is_mobile()){
$page->SetPager('goods', '{prev} {list} {next}', array("listlong" => "4", "prev" => "<", "next" => ">", "list" => "*", "disabledclass" => ""));
}else{
$page->SetPager('goods', '{first}{prev} {liststart}{list}{listend} {next}{last}', array("listlong" => "10", "first" => "<<", "last" => ">>", "prev" => "<", "next" => ">", "list" => "*", "disabledclass" => ""));
}
return $page;
}
//空操作
public function _empty() {
$this->error('该页面不存在!');
}
/**
* 检查操作频率
* @param int $duration 距离最后一次操作的时长
*/
protected function check_last_action($duration){
$action=MODULE_NAME."-".CONTROLLER_NAME."-".ACTION_NAME;
$time=time();
$session_last_action=session('last_action');
if(!empty($session_last_action['action']) && $action==$session_last_action['action']){
$mduration=$time-$session_last_action['time'];
if($duration>$mduration){
$this->error("您的操作太过频繁,请稍后再试~~~");
}else{
session('last_action.time',$time);
}
}else{
session('last_action.action',$action);
session('last_action.time',$time);
}
}
/**
* 模板主题设置
* @access protected
* @param string $theme 模版主题
* @return Action
*/
public function theme($theme){
$this->theme=$theme;
return $this;
}
}