UtilityController.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. $image = new \Think\Image();
  81. $image->open($image_dir.$file_name);
  82. //按照原图的比例生成一个最大为400*400的缩略图并保存为thumb.jpg, 实际会按比例自动缩放
  83. $image->thumb(400, 400)->save($image_dir.$thumb_image_name);
  84. ////{"filePath":"\/Uploads\/image\/goods\/2017-07-05\/","fileName":"7e414de26624c0a5ac7cd5b9bd5edfe3.jpg"}
  85. $result = array('filePath' =>$file_path ,'kufile_path' => $kufile_path,'fileName' => $file_name);
  86. echo json_encode($result);
  87. die();
  88. }
  89. /**
  90. *删除 旧的原图和缩略图,修改的情况下使用
  91. *
  92. */
  93. public function del_old_image(){
  94. $old_gallery_image=I('post.old_gallery_image');
  95. $old_product_image=I('post.old_product_image');
  96. if(!empty($old_gallery_image)){
  97. $old_image=I('post.old_gallery_image');
  98. $thumb_dir=ROOT_PATH.'Uploads/image/cache/gallery/';
  99. $image_dir=ROOT_PATH.'Uploads/image/gallery/';
  100. }elseif(!empty($old_product_image)){
  101. $old_image=I('post.old_product_image');
  102. $thumb_dir=ROOT_PATH.'Uploads/image/cache/product/';
  103. $image_dir=ROOT_PATH.'Uploads/image/product/';
  104. }
  105. if(!empty($old_image)){
  106. list($base_name, $ext) = explode(".", $old_image);
  107. $image = explode("/", $base_name);
  108. $image_name=end($image);
  109. $del_image=array();
  110. $del_image[]=$image_dir.$image_name.'.'.$ext;//原图
  111. $del_image[]=$thumb_dir.$image_name.'-100x100.'.$ext;//100x100
  112. foreach ($del_image as $k => $v) {
  113. if(is_file($v)){
  114. @unlink($v);
  115. }
  116. }
  117. }
  118. }
  119. public function file()
  120. {
  121. $do = I('get.do');
  122. if('group_list' == $do)
  123. {
  124. $uid = 1;
  125. if( is_agent_login() )
  126. {
  127. $uid = is_agent_login();
  128. $uid = $uid +1;
  129. }
  130. $group_list = M('core_attachment_group')->where( array('uid' => $uid) )->order('id asc')->select();
  131. $res = array(
  132. 'message' => array(
  133. 'errno' => 0,
  134. 'message' => $group_list
  135. ),
  136. 'redirect' => '',
  137. 'type' => 'ajax'
  138. );
  139. echo json_encode($res);
  140. die();
  141. }
  142. if( 'change_group' == $do )
  143. {
  144. $name = I('request.name');
  145. $id = I('request.id');
  146. M('core_attachment_group')->where( array('id' => $id) )->save( array('name' => $name) );
  147. //{"message":{"errno":0,"message":"\u66f4\u65b0\u6210\u529f"},"redirect":"","type":"ajax"}
  148. $res = array('message' => array('errno' => 0, 'message' => '更新成功'),
  149. 'redirect' =>'','type' => 'ajax'
  150. );
  151. echo json_encode( $res );
  152. die();
  153. }
  154. if('add_group' == $do)
  155. {
  156. $core_attachment_group_data = array();
  157. $core_attachment_group_data['name'] = '未命名';
  158. $core_attachment_group_data['uniacid'] = 0;
  159. $core_attachment_group_data['uid'] = 1;
  160. $id = M('core_attachment_group')->add( $core_attachment_group_data );
  161. $res = array('message' => array('errno' => 0, 'message' => array('id'=>$id),
  162. 'redirect' =>'','type' => 'ajax'
  163. ) );
  164. echo json_encode($res);
  165. //{"message":{"errno":0,"message":{"id":"27"}},"redirect":"","type":"ajax"}
  166. die();
  167. }
  168. if ($do == 'move_to_group') {
  169. $group_id = I('request.id');
  170. $ids = I('request.keys');
  171. M('core_attachment')->where( array('id' => array('in', $ids ) ) )->save( array('group_id' => $group_id) );
  172. $res = array('message' => array('errno' => 0, 'message' => '删除成功'),
  173. 'redirect' =>'','type' => 'ajax'
  174. );
  175. echo json_encode( $res );
  176. die();
  177. }
  178. if( 'del_group' == $do )
  179. {
  180. $id = I('request.id');
  181. M('core_attachment_group')->where( array('id' => $id) )->delete();
  182. $res = array('message' => array('errno' => 0, 'message' => '删除成功'),
  183. 'redirect' =>'','type' => 'ajax'
  184. );
  185. echo json_encode( $res );
  186. die();
  187. }
  188. if ($do == 'video') {
  189. $year = I('get.year');
  190. $month = I('get.month');
  191. $page = I('get.page',1);
  192. $groupid = I('get.groupid',1);
  193. $page_size = 10;
  194. $page = max(1, $page);
  195. $offset = ($page -1) * $page_size;
  196. $where = " type=2 ";
  197. if ($year || $month) {
  198. $start_time = strtotime("{$year}-{$month}-01");
  199. $end_time = strtotime('+1 month', $start_time);
  200. //createtime
  201. $where .= " createtime >= {$start_time} and createtime < {$end_time} ";
  202. }
  203. $total = M('core_attachment')->where($where)->count();
  204. $list = M('core_attachment')->where( $where )->order('id desc ')->limit($offset, $page_size)->select();
  205. if (!empty($list)) {
  206. foreach ($list as &$meterial) {
  207. $meterial['url'] = tomedia($meterial['attachment']);
  208. unset($meterial['uid']);
  209. }
  210. }
  211. $pager = pagination($total, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => 1));
  212. $result = array('items' => $list, 'pager' => $pager);
  213. $json_data = array();
  214. $json_data['message'] = array(
  215. 'errno' =>0,
  216. 'message' => $result
  217. );
  218. echo json_encode( $json_data );
  219. die();
  220. }
  221. if ($do == 'delete') {
  222. $ids_arr = I('request.id');
  223. foreach($ids_arr as $material_id)
  224. {
  225. M('core_attachment')->where( array('id' => $material_id) )->delete();
  226. }
  227. echo '{"message":{"errno":"0","message":"\u5220\u9664\u7d20\u6750\u6210\u529f"},"redirect":"","type":"ajax"}';
  228. die();
  229. }
  230. if ($do == 'image') {
  231. $year = I('get.year');
  232. $month = I('get.month');
  233. $page = I('get.page',1);
  234. $groupid = I('get.groupid',0);
  235. $page_size = 10;
  236. $page = max(1, $page);
  237. $offset = ($page -1) * $page_size;
  238. $where = " type=1 ";
  239. if( !empty($groupid) && $groupid > 0 )
  240. {
  241. $where .= " and group_id = {$groupid} ";
  242. }
  243. if ($year || $month) {
  244. $start_time = strtotime("{$year}-{$month}-01");
  245. $end_time = strtotime('+1 month', $start_time);
  246. //createtime
  247. $where .= " createtime >= {$start_time} and createtime < {$end_time} ";
  248. }
  249. $uid = 1;
  250. if( is_agent_login() )
  251. {
  252. $uid = is_agent_login();
  253. $uid = $uid +1;
  254. }
  255. $where .= " and uid = {$uid} ";
  256. $total = M('core_attachment')->where($where)->count();
  257. $list = M('core_attachment')->where( $where )->order('id desc ')->limit($offset, $page_size)->select();
  258. if (!empty($list)) {
  259. foreach ($list as &$meterial) {
  260. $meterial['url'] = tomedia($meterial['attachment']);
  261. unset($meterial['uid']);
  262. //$core_data['filename'] = base64_encode($originname);
  263. if( $meterial['filename'] == base64_encode(base64_decode($meterial['filename'])) )
  264. {
  265. $meterial['filename'] = base64_decode($meterial['filename']);
  266. }
  267. }
  268. }
  269. $pager = pagination($total, $page, $page_size,'',$context = array('before' => 5, 'after' => 4, 'isajax' => 1));
  270. $result = array('items' => $list, 'pager' => $pager);
  271. $json_data = array();
  272. $json_data['message'] = array(
  273. 'errno' =>0,
  274. 'message' => $result
  275. );
  276. echo json_encode( $json_data );
  277. die();
  278. }
  279. if('upload' == $do)
  280. {
  281. $dir='goods/';
  282. $type = I('get.upload_type');
  283. $type = in_array($type, array('image','audio','video')) ? $type : 'image';
  284. if (empty($_FILES['file']['name'])) {
  285. $result['message'] = '上传失败, 请选择要上传的文件!';
  286. die(json_encode($result));
  287. }
  288. if ($_FILES['file']['error'] != 0) {
  289. $result['message'] = '上传失败, 请重试.';
  290. die(json_encode($result));
  291. }
  292. $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  293. $ext = strtolower($ext);
  294. $size = intval($_FILES['file']['size']);
  295. $originname = $_FILES['file']['name'];
  296. $upload = new \Think\Upload();// 实例化上传类
  297. $upload->maxSize = 31457280 ;// 设置附件上传大小
  298. $upload->exts = array('jpg', 'gif', 'png', 'jpeg','mp4');// 设置附件上传类型
  299. $upload->rootPath = ATTACHMENT_ROOT.$dir;
  300. RecursiveMkdir($upload->rootPath);
  301. $info = $upload->upload();
  302. if(!$info) {
  303. $result['message'] = $file['message'];
  304. die(json_encode($result));
  305. }
  306. $filename = $dir.date('Y-m-d').'/'.$info['file']['savename'];
  307. $fullname = ATTACHMENT_ROOT . $filename;
  308. //attachment_type
  309. $attachment_type_arr = M('lionfish_comshop_config')->where( array('name' => 'attachment_type') )->find();
  310. if( $attachment_type_arr['value'] == 1 )
  311. {
  312. save_image_to_qiniu($fullname,'Uploads/image/'.$filename);
  313. }else if( $attachment_type_arr['value'] == 2 ){
  314. save_image_to_alioss($fullname,'Uploads/image/'.$filename);
  315. }else if( $attachment_type_arr['value'] == 3 )
  316. {
  317. save_image_to_txyun($fullname,'Uploads/image/'.$filename);
  318. }
  319. $group_id = I('get.group_id');
  320. $info = array(
  321. 'name' => $originname,
  322. 'ext' => $ext,
  323. 'filename' => $filename,
  324. 'attachment' => $filename,
  325. 'url' => tomedia($filename),
  326. 'is_image' => $type == 'image' ? 1 : 2,
  327. 'filesize' => filesize($fullname),
  328. 'group_id' => $group_id
  329. );
  330. $uid = 1;
  331. if( is_agent_login() )
  332. {
  333. $uid = is_agent_login();
  334. $uid = $uid +1;
  335. }
  336. $core_data = array();
  337. $core_data['uniacid'] = 0;
  338. $core_data['uid'] = $uid;
  339. $core_data['filename'] = base64_encode($originname);
  340. $core_data['attachment'] = $filename;
  341. $core_data['type'] = $type == 'image' ? 1 : 2;
  342. $core_data['createtime'] = time();
  343. $core_data['module_upload_dir'] = '';
  344. $core_data['group_id'] = $group_id;
  345. M('core_attachment')->add($core_data);
  346. $size = getimagesize($fullname);
  347. $info['width'] = $size[0];
  348. $info['height'] = $size[1];
  349. $info['state'] = 'SUCCESS';
  350. die(json_encode($info));
  351. }
  352. if('image' == $do)
  353. {
  354. }
  355. }
  356. /**
  357. *上传图片
  358. */
  359. public function upload_image(){
  360. $dir=I('get.dir');
  361. $dir .= '/'.date('Y-m-d');
  362. $this->del_old_image();
  363. $upload = new \Think\Upload();// 实例化上传类
  364. $image_dir=ROOT_PATH.'Uploads/image/'.$dir;
  365. RecursiveMkdir($image_dir);
  366. $upload->autoSub = false;
  367. $upload->maxSize = 3145728 ;// 设置附件上传大小
  368. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  369. $upload->rootPath = $image_dir.'/';
  370. $info = $upload->upload();
  371. if(!$info) {
  372. $data['result'] = false;
  373. $this->ajaxReturn($data);
  374. }else{// 上传成功
  375. $filename=$dir.'/'.$info['file']['savepath'].$info['file']['savename'];
  376. $data['image_thumb'] = resize($filename, 100, 100);
  377. $data['image'] = $filename;
  378. $this->ajaxReturn($data);
  379. }
  380. }
  381. //用于ckeditor图片上传
  382. function ckupload(){
  383. $upload = new \Think\Upload();// 实例化上传类
  384. $upload->maxSize = 3145728 ;// 设置附件上传大小
  385. $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
  386. $upload->rootPath = ROOT_PATH.'Uploads/image/goods_description/';
  387. RecursiveMkdir($upload->rootPath);
  388. $info = $upload->upload();
  389. if(!$info) {
  390. // 上传错误提示错误信息
  391. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$_GET['CKEditorFuncNum'].", '/', '上传失败," . $upload->getError() . "!');</script>";
  392. }else{// 上传成功
  393. $n=$_GET['CKEditorFuncNum'];
  394. $savepath=C('SITE_URL').'/Uploads/image/goods_description/'. $info['upload']['savepath'].$info['upload']['savename'];
  395. //下面的输出,会自动的将上传成功的文件路径,返回给编辑器。
  396. echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$n.",'$savepath','');</script>";
  397. }
  398. }
  399. }