UtilityController.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class UtilityController extends CommonController{
  16. //删除图片和缩略图
  17. public function del_image($dir,$image,$type){
  18. $image_dir=ROOT_PATH.'Uploads/image/'.$dir.'/';
  19. $thumb_dir=ROOT_PATH.'Uploads/image/cache/'.$dir.'/';
  20. list($base_name, $ext) = explode(".", $image);
  21. $image = explode("/", $base_name);
  22. $image_name=end($image);
  23. if($type=='product'){
  24. $del_image=array();
  25. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  26. $del_image[]=$thumb_dir.$image_name.'-50x50.'.$ext;
  27. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  28. $del_image[]=$thumb_dir.$image_name.'-255x255.'.$ext;
  29. }elseif($type=='gallery'){
  30. $del_image=array();
  31. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  32. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  33. $del_image[]=$thumb_dir.$image_name.'-127x127.'.$ext;
  34. }elseif($type=='blog'){
  35. $del_image=array();
  36. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  37. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  38. $del_image[]=$thumb_dir.$image_name.'-280x140.'.$ext;
  39. }elseif($type=='blog_gallery'){
  40. $del_image=array();
  41. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  42. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;
  43. }
  44. if(!empty($del_image)){
  45. foreach ($del_image as $k => $v) {
  46. if(is_file($v)){
  47. @unlink($v);
  48. }
  49. }
  50. }
  51. }
  52. /**
  53. * 二进制上传数据
  54. */
  55. public function upload_binaryFile()
  56. {
  57. $data = file_get_contents("php://input");
  58. $get_data = I('get.');
  59. $dir = I('get.dir','goods');
  60. $type = I('get.type');
  61. $name = I('get.name');
  62. $image_dir = ROOT_PATH.'Uploads/image/'.$dir;
  63. $image_dir .= '/'.date('Y-m-d').'/';
  64. $file_path = C('SITE_URL').'/Uploads/image/'.$dir.'/'.date('Y-m-d').'/';
  65. $kufile_path = $dir.'/'.date('Y-m-d').'/';
  66. RecursiveMkdir($image_dir);
  67. $file_name = md5($name.time()).'.png';
  68. switch($type)
  69. {
  70. case 'image/jpeg':
  71. $file_name = md5($name.time()).'.jpg';
  72. break;
  73. case 'image/png':
  74. $file_name = md5($name.time()).'.png';
  75. break;
  76. }
  77. $thumb_arr = explode('.',$file_name);
  78. $thumb_image_name = $thumb_arr[0].'_thumb.'.$thumb_arr[1];
  79. file_put_contents($image_dir.$file_name, $data);
  80. //fileinfo 检测begin
  81. $fip = finfo_open(FILEINFO_MIME_TYPE);
  82. $min_result = finfo_file($fip , $image_dir.$file_name );
  83. fclose( $fip );
  84. $min_type_arr = array();
  85. $min_type_arr[] = 'image/jpeg';
  86. $min_type_arr[] = 'image/gif';
  87. $min_type_arr[] = 'image/jpg';
  88. $min_type_arr[] = 'image/png';
  89. $min_type_arr[] = 'video/mp4';
  90. if( !in_array($min_result , $min_type_arr ) )
  91. {
  92. die();
  93. }
  94. $image = new \Think\Image();
  95. $image->open($image_dir.$file_name);
  96. //按照原图的比例生成一个最大为400*400的缩略图并保存为thumb.jpg, 实际会按比例自动缩放
  97. $image->thumb(400, 400)->save($image_dir.$thumb_image_name);
  98. ////{"filePath":"\/Uploads\/image\/goods\/2017-07-05\/","fileName":"7e414de26624c0a5ac7cd5b9bd5edfe3.jpg"}
  99. $result = array('filePath' =>$file_path ,'kufile_path' => $kufile_path,'fileName' => $file_name);
  100. echo json_encode($result);
  101. die();
  102. }
  103. /**
  104. *删除 旧的原图和缩略图,修改的情况下使用
  105. *
  106. */
  107. public function del_old_image(){
  108. $old_gallery_image=I('post.old_gallery_image');
  109. $old_product_image=I('post.old_product_image');
  110. if(!empty($old_gallery_image)){
  111. $old_image=I('post.old_gallery_image');
  112. $thumb_dir=ROOT_PATH.'Uploads/image/cache/gallery/';
  113. $image_dir=ROOT_PATH.'Uploads/image/gallery/';
  114. }elseif(!empty($old_product_image)){
  115. $old_image=I('post.old_product_image');
  116. $thumb_dir=ROOT_PATH.'Uploads/image/cache/product/';
  117. $image_dir=ROOT_PATH.'Uploads/image/product/';
  118. }
  119. if(!empty($old_image)){
  120. list($base_name, $ext) = explode(".", $old_image);
  121. $image = explode("/", $base_name);
  122. $image_name=end($image);
  123. $del_image=array();
  124. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  125. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;//100x100
  126. foreach ($del_image as $k => $v) {
  127. if(is_file($v)){
  128. @unlink($v);
  129. }
  130. }
  131. }
  132. }
  133. public function file()
  134. {
  135. $do = I('get.do');
  136. if('group_list' == $do)
  137. {
  138. $uid = 1;
  139. if( is_agent_login() )
  140. {
  141. $uid = is_agent_login();
  142. $uid = $uid +1;
  143. }
  144. $group_list = M('core_attachment_group')->where( array('uid' => $uid) )->order('id asc')->select();
  145. $res = array(
  146. 'message' => array(
  147. 'errno' => 0,
  148. 'message' => $group_list
  149. ),
  150. 'redirect' => '',
  151. 'type' => 'ajax'
  152. );
  153. echo json_encode($res);
  154. die();
  155. }
  156. if( 'change_group' == $do )
  157. {
  158. $name = I('request.name');
  159. $id = I('request.id');
  160. M('core_attachment_group')->where( array('id' => $id) )->save( array('name' => $name) );
  161. //{"message":{"errno":0,"message":"\u66f4\u65b0\u6210\u529f"},"redirect":"","type":"ajax"}
  162. $res = array('message' => array('errno' => 0, 'message' => '更新成功'),
  163. 'redirect' =>'','type' => 'ajax'
  164. );
  165. echo json_encode( $res );
  166. die();
  167. }
  168. if('add_group' == $do)
  169. {
  170. $core_attachment_group_data = array();
  171. $core_attachment_group_data['name'] = '未命名';
  172. $core_attachment_group_data['uniacid'] = 0;
  173. $core_attachment_group_data['uid'] = 1;
  174. $id = M('core_attachment_group')->add( $core_attachment_group_data );
  175. $res = array('message' => array('errno' => 0, 'message' => array('id'=>$id),
  176. 'redirect' =>'','type' => 'ajax'
  177. ) );
  178. echo json_encode($res);
  179. //{"message":{"errno":0,"message":{"id":"27"}},"redirect":"","type":"ajax"}
  180. die();
  181. }
  182. if ($do == 'move_to_group') {
  183. $group_id = I('request.id');
  184. $ids = I('request.keys');
  185. M('core_attachment')->where( array('id' => array('in', $ids ) ) )->save( array('group_id' => $group_id) );
  186. $res = array('message' => array('errno' => 0, 'message' => '删除成功'),
  187. 'redirect' =>'','type' => 'ajax'
  188. );
  189. echo json_encode( $res );
  190. die();
  191. }
  192. if( 'del_group' == $do )
  193. {
  194. $id = I('request.id');
  195. M('core_attachment_group')->where( array('id' => $id) )->delete();
  196. $res = array('message' => array('errno' => 0, 'message' => '删除成功'),
  197. 'redirect' =>'','type' => 'ajax'
  198. );
  199. echo json_encode( $res );
  200. die();
  201. }
  202. if ($do == 'video') {
  203. $year = I('get.year');
  204. $month = I('get.month');
  205. $page = I('get.page',1);
  206. $groupid = I('get.groupid',1);
  207. $page_size = 10;
  208. $page = max(1, $page);
  209. $offset = ($page -1) * $page_size;
  210. $where = " type=2 ";
  211. if ($year || $month) {
  212. $start_time = strtotime("{$year}-{$month}-01");
  213. $end_time = strtotime('+1 month', $start_time);
  214. //createtime
  215. $where .= " createtime >= {$start_time} and createtime < {$end_time} ";
  216. }
  217. $total = M('core_attachment')->where($where)->count();
  218. $list = M('core_attachment')->where( $where )->order('id desc ')->limit($offset, $page_size)->select();
  219. if (!empty($list)) {
  220. foreach ($list as &$meterial) {
  221. $meterial['url'] = tomedia($meterial['attachment']);
  222. unset($meterial['uid']);
  223. }
  224. }
  225. $pager = pagination($total, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => 1));
  226. $result = array('items' => $list, 'pager' => $pager);
  227. $json_data = array();
  228. $json_data['message'] = array(
  229. 'errno' =>0,
  230. 'message' => $result
  231. );
  232. echo json_encode( $json_data );
  233. die();
  234. }
  235. if ($do == 'delete') {
  236. $ids_arr = I('request.id');
  237. foreach($ids_arr as $material_id)
  238. {
  239. M('core_attachment')->where( array('id' => $material_id) )->delete();
  240. }
  241. echo '{"message":{"errno":"0","message":"\u5220\u9664\u7d20\u6750\u6210\u529f"},"redirect":"","type":"ajax"}';
  242. die();
  243. }
  244. if ($do == 'image') {
  245. $year = I('get.year');
  246. $month = I('get.month');
  247. $page = I('get.page',1);
  248. $groupid = I('get.groupid',0);
  249. $page_size = 10;
  250. $page = max(1, $page);
  251. $offset = ($page -1) * $page_size;
  252. $where = " type=1 ";
  253. if( !empty($groupid) && $groupid > 0 )
  254. {
  255. $where .= " and group_id = {$groupid} ";
  256. }
  257. if($groupid == 0 ){
  258. $where .= " and group_id = -1 ";
  259. }
  260. if ($year || $month) {
  261. $start_time = strtotime("{$year}-{$month}-01");
  262. $end_time = strtotime('+1 month', $start_time);
  263. //createtime
  264. $where .= " and createtime >= {$start_time} and createtime < {$end_time} ";
  265. }
  266. $uid = 1;
  267. if( is_agent_login() )
  268. {
  269. $uid = is_agent_login();
  270. $uid = $uid +1;
  271. }
  272. $where .= " and uid = {$uid} ";
  273. $total = M('core_attachment')->where($where)->count();
  274. $list = M('core_attachment')->where( $where )->order('id desc ')->limit($offset, $page_size)->select();
  275. if (!empty($list)) {
  276. foreach ($list as &$meterial) {
  277. $meterial['url'] = tomedia($meterial['attachment']);
  278. unset($meterial['uid']);
  279. //$core_data['filename'] = base64_encode($originname);
  280. if( $meterial['filename'] == base64_encode(base64_decode($meterial['filename'])) )
  281. {
  282. $meterial['filename'] = base64_decode($meterial['filename']);
  283. }
  284. }
  285. }
  286. $pager = pagination($total, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => 1));
  287. $result = array('items' => $list, 'pager' => $pager);
  288. $json_data = array();
  289. $json_data['message'] = array(
  290. 'errno' =>0,
  291. 'message' => $result
  292. );
  293. echo json_encode( $json_data );
  294. die();
  295. }
  296. if('upload' == $do)
  297. {
  298. $dir='goods/';
  299. $type = I('get.upload_type');
  300. $type = in_array($type, array('image','audio','video')) ? $type : 'image';
  301. if (empty($_FILES['file']['name'])) {
  302. $result['message'] = '上传失败, 请选择要上传的文件!';
  303. die(json_encode($result));
  304. }
  305. if ($_FILES['file']['error'] != 0) {
  306. $result['message'] = '上传失败, 请重试.';
  307. die(json_encode($result));
  308. }
  309. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  310. $ext = strtolower($ext);
  311. $size = intval($_FILES['file']['size']);
  312. $originname = $_FILES['file']['name'];
  313. //fileinfo 检测begin
  314. $fip = finfo_open(FILEINFO_MIME_TYPE);
  315. $min_result = finfo_file($fip , $_FILES['file']['tmp_name']);
  316. fclose( $fip );
  317. $min_type_arr = array();
  318. $min_type_arr[] = 'image/jpeg';
  319. $min_type_arr[] = 'image/gif';
  320. $min_type_arr[] = 'image/jpg';
  321. $min_type_arr[] = 'image/png';
  322. $min_type_arr[] = 'video/mp4';
  323. if( !in_array($min_result , $min_type_arr ) )
  324. {
  325. die();
  326. }
  327. //fileinfo 检测end
  328. if( !in_array($ext, array('jpg', 'gif', 'png', 'jpeg','mp4') ) )
  329. {
  330. die();
  331. }
  332. $upload = new \Think\Upload();// 实例化上传类
  333. $upload->maxSize = 31457280 ;// 设置附件上传大小
  334. $upload->exts = array('jpg', 'gif', 'png', 'jpeg','mp4');// 设置附件上传类型
  335. $upload->rootPath = ATTACHMENT_ROOT.$dir;
  336. RecursiveMkdir($upload->rootPath);
  337. $info = $upload->upload();
  338. if(!$info) {
  339. $result['message'] = $file['message'];
  340. die(json_encode($result));
  341. }
  342. $filename = $dir.date('Y-m-d').'/'.$info['file']['savename'];
  343. $fullname = ATTACHMENT_ROOT . $filename;
  344. //ext
  345. if($ext == 'mp4'){
  346. if(filesize($fullname)>30000000){
  347. $result['message'] = '上传失败, 上传的视频应不大于30M!';
  348. die(json_encode($result));
  349. }
  350. }else{
  351. if(filesize($fullname)>10000000){
  352. $result['message'] = '上传失败, 上传的图片应不大于10M!';
  353. die(json_encode($result));
  354. }
  355. }
  356. //attachment_type
  357. $attachment_type_arr = M('lionfish_comshop_config')->where( array('name' => 'attachment_type') )->find();
  358. if( $attachment_type_arr['value'] == 1 )
  359. {
  360. save_image_to_qiniu($fullname,'Uploads/image/'.$filename);
  361. }else if( $attachment_type_arr['value'] == 2 ){
  362. save_image_to_alioss($fullname,'Uploads/image/'.$filename);
  363. }else if( $attachment_type_arr['value'] == 3 )
  364. {
  365. save_image_to_txyun($fullname,'Uploads/image/'.$filename);
  366. }
  367. $group_id = I('get.group_id');
  368. $info = array(
  369. 'name' => $originname,
  370. 'ext' => $ext,
  371. 'filename' => $filename,
  372. 'attachment' => $filename,
  373. 'url' => tomedia($filename),
  374. 'is_image' => $type == 'image' ? 1 : 2,
  375. 'filesize' => filesize($fullname),
  376. 'group_id' => $group_id
  377. );
  378. $uid = 1;
  379. if( is_agent_login() )
  380. {
  381. $uid = is_agent_login();
  382. $uid = $uid +1;
  383. }
  384. $core_data = array();
  385. $core_data['uniacid'] = 0;
  386. $core_data['uid'] = $uid;
  387. $core_data['filename'] = base64_encode($originname);
  388. $core_data['attachment'] = $filename;
  389. $core_data['type'] = $type == 'image' ? 1 : 2;
  390. $core_data['createtime'] = time();
  391. $core_data['module_upload_dir'] = '';
  392. $core_data['group_id'] = $group_id;
  393. M('core_attachment')->add($core_data);
  394. $size = getimagesize($fullname);
  395. $info['width'] = $size[0];
  396. $info['height'] = $size[1];
  397. $info['state'] = 'SUCCESS';
  398. die(json_encode($info));
  399. }
  400. if('image' == $do)
  401. {
  402. }
  403. }
  404. /**
  405. *上传图片
  406. */
  407. public function upload_image(){
  408. $dir=I('get.dir');
  409. $dir .= '/'.date('Y-m-d');
  410. $this->del_old_image();
  411. $upload = new \Think\Upload();// 实例化上传类
  412. $image_dir=ROOT_PATH.'Uploads/image/'.$dir;
  413. RecursiveMkdir($image_dir);
  414. $min_type_arr = array();
  415. $min_type_arr[] = 'image/jpeg';
  416. $min_type_arr[] = 'image/gif';
  417. $min_type_arr[] = 'image/jpeg';
  418. $min_type_arr[] = 'image/png';
  419. $min_type_arr[] = 'video/mp4';
  420. $upload->autoSub = false;
  421. $upload->maxSize = 3145728 ;// 设置附件上传大小
  422. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  423. $upload->mimes = $min_type_arr;// 设置附件上传类型
  424. $upload->rootPath = $image_dir.'/';
  425. $info = $upload->upload();
  426. if(!$info) {
  427. $data['result'] = false;
  428. $this->ajaxReturn($data);
  429. }else{// 上传成功
  430. $filename=$dir.'/'.$info['file']['savepath'].$info['file']['savename'];
  431. $data['image_thumb'] = resize($filename, 100, 100);
  432. $data['image'] = $filename;
  433. $this->ajaxReturn($data);
  434. }
  435. }
  436. //用于ckeditor图片上传
  437. function ckupload(){
  438. $upload = new \Think\Upload();// 实例化上传类
  439. $upload->maxSize = 3145728 ;// 设置附件上传大小
  440. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  441. $upload->rootPath = ROOT_PATH.'Uploads/image/goods_description/';
  442. RecursiveMkdir($upload->rootPath);
  443. $info = $upload->upload();
  444. if(!$info) {
  445. // 上传错误提示错误信息
  446. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$_GET['CKEditorFuncNum'].", '/', '上传失败," . $upload->getError() . "!');</script>";
  447. }else{// 上传成功
  448. $n=$_GET['CKEditorFuncNum'];
  449. $savepath=C('SITE_URL').'/Uploads/image/goods_description/'. $info['upload']['savepath'].$info['upload']['savename'];
  450. //下面的输出,会自动的将上传成功的文件路径,返回给编辑器。
  451. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$n.",'$savepath','');</script>";
  452. }
  453. }
  454. }