UtilityController.class.php 16 KB

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