Images.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\admin\controller\widget;
  3. use crmeb\services\storage\COS;
  4. use crmeb\services\storage\OSS;
  5. use crmeb\services\storage\Qiniu;
  6. use think\facade\Route as Url;
  7. use app\admin\model\system\SystemAttachment as SystemAttachmentModel;
  8. use app\admin\model\system\SystemAttachmentCategory as Category;
  9. use app\admin\controller\AuthController;
  10. use crmeb\services\UploadService as Upload;
  11. use crmeb\services\JsonService as Json;
  12. use crmeb\services\UtilService as Util;
  13. use crmeb\services\FormBuilder as Form;
  14. /**
  15. * TODO 附件控制器
  16. * Class Images
  17. * @package app\admin\controller\widget
  18. */
  19. class Images extends AuthController
  20. {
  21. /**
  22. * 附件列表
  23. * @return \think\response\Json
  24. */
  25. public function index()
  26. {
  27. $pid = request()->param('pid');
  28. if($pid === NULL)
  29. {
  30. $pid = session('pid') ? session('pid') : 0;
  31. }
  32. session('pid',$pid);
  33. $this->assign('pid',$pid);
  34. //// //TODO 分类标题
  35. // $typearray = Category::getAll();
  36. // $this->assign(compact('typearray'));
  37. // $this->assign(SystemAttachmentModel::getAll($pid));
  38. return $this->fetch('widget/images');
  39. }
  40. /**获取图片列表
  41. *
  42. */
  43. public function get_image_list()
  44. {
  45. $where = Util::getMore([
  46. ['page',1],
  47. ['limit',18],
  48. ['pid',0]
  49. ]);
  50. return Json::successful(SystemAttachmentModel::getImageList($where));
  51. }
  52. /**获取分类
  53. * @param string $name
  54. */
  55. public function get_image_cate($name = '')
  56. {
  57. return Json::successful(Category::getAll($name));
  58. }
  59. /**
  60. * 图片管理上传图片
  61. * @return \think\response\Json
  62. */
  63. public function upload()
  64. {
  65. $pid = input('pid')!= NULL ?input('pid'):session('pid');
  66. $upload_type = $this->request->get('upload_type',0);
  67. try{
  68. $path = make_path('attach');
  69. $res = Upload::image('file',$path,true,true,null,'uniqid',$upload_type);
  70. if(is_object($res) && $res->status === false){
  71. $info = array(
  72. 'code' =>400,
  73. 'msg' =>'上传失败:'.$res->error,
  74. 'src' =>''
  75. );
  76. }else if(is_string($res)){
  77. $info = array(
  78. 'code' =>400,
  79. 'msg' =>'上传失败:'.$res,
  80. 'src' =>''
  81. );
  82. }else if(is_array($res)){
  83. $res['dir'] = str_replace('\\','/',$res['dir']);
  84. $res['thumb_path'] = str_replace('\\','/',$res['thumb_path']);
  85. SystemAttachmentModel::attachmentAdd($res['name'],$res['size'],$res['type'],$res['dir'],$res['thumb_path'],$pid,$res['image_type'],$res['time']);
  86. $info = array(
  87. 'code' =>200,
  88. 'msg' =>'上传成功',
  89. 'src' =>$res['dir']
  90. );
  91. }
  92. }catch (\Exception $e){
  93. $info = [
  94. 'code' =>400,
  95. 'msg' =>'上传失败:'.$e->getMessage(),
  96. 'src' =>''
  97. ];
  98. }
  99. echo json_encode($info);
  100. }
  101. /**
  102. * ajax 提交删除
  103. */
  104. public function delete(){
  105. $request = app('request');
  106. $post = $request->post();
  107. if(empty($post['imageid'] ))
  108. Json::fail('还没选择要删除的图片呢?');
  109. foreach ($post['imageid'] as $v){
  110. self::deleteimganddata($v);
  111. }
  112. Json::successful('删除成功');
  113. }
  114. /**删除图片和数据记录
  115. * @param $att_id
  116. */
  117. public function deleteimganddata($att_id){
  118. $attinfo = SystemAttachmentModel::get($att_id)->toArray();
  119. if($attinfo){
  120. if($attinfo['image_type'] == 1){
  121. @unlink(app()->getRootPath().ltrim($attinfo['att_dir'],'.'));
  122. @unlink(app()->getRootPath().ltrim($attinfo['satt_dir'],'.'));
  123. }else if($attinfo['image_type'] == 2){
  124. Qiniu::delete($attinfo['name']);
  125. }else if($attinfo['image_type'] == 3){
  126. OSS::delete($attinfo['name']);
  127. }else if($attinfo['image_type'] == 4){
  128. COS::delete($attinfo['name']);
  129. }
  130. SystemAttachmentModel::where('att_id', $att_id)->delete();
  131. }
  132. }
  133. /**
  134. * 移动图片分类显示
  135. */
  136. public function moveimg($imgaes){
  137. $formbuider = [];
  138. $formbuider[] = Form::hidden('imgaes',$imgaes);
  139. $formbuider[] = Form::select('pid','选择分类')->setOptions(function (){
  140. $list = Category::getCateList();
  141. $options = [['value'=>0,'label'=>'所有分类']];
  142. foreach ($list as $id=>$cateName){
  143. $options[] = ['label'=>$cateName['html'].$cateName['name'],'value'=>$cateName['id']];
  144. }
  145. return $options;
  146. })->filterable(1);
  147. $form = Form::make_post_form('编辑分类',$formbuider,Url::buildUrl('moveImgCecate'));
  148. $this->assign(compact('form'));
  149. return $this->fetch('public/form-builder');
  150. }
  151. /**
  152. * 移动图片分类操作
  153. */
  154. public function moveImgCecate()
  155. {
  156. $data = Util::postMore([
  157. 'pid',
  158. 'imgaes'
  159. ]);
  160. if($data['imgaes'] == '') return Json::fail('请选择图片');
  161. if(!$data['pid']) return Json::fail('请选择分类');
  162. $res = SystemAttachmentModel::where('att_id','in',$data['imgaes'])->update(['pid'=>$data['pid']]);
  163. if($res)
  164. Json::successful('移动成功');
  165. else
  166. Json::fail('移动失败!');
  167. }
  168. /**
  169. * ajax 添加分类
  170. */
  171. public function addcate($id = 0){
  172. $formbuider = [];
  173. $formbuider[] = Form::select('pid','上级分类',(string)$id)->setOptions(function (){
  174. $list = Category::getCateList(0);
  175. $options = [['value'=>0,'label'=>'所有分类']];
  176. foreach ($list as $id=>$cateName){
  177. $options[] = ['label'=>$cateName['html'].$cateName['name'],'value'=>$cateName['id']];
  178. }
  179. return $options;
  180. })->filterable(1);
  181. $formbuider[] = Form::input('name','分类名称');
  182. $jsContent = <<<SCRIPT
  183. parent.SuccessCateg();
  184. parent.layer.close(parent.layer.getFrameIndex(window.name));
  185. SCRIPT;
  186. $form = Form::make_post_form('添加分类',$formbuider,Url::buildUrl('saveCate'),$jsContent);
  187. $this->assign(compact('form'));
  188. return $this->fetch('public/form-builder');
  189. }
  190. /**
  191. * 添加分类
  192. */
  193. public function saveCate(){
  194. $request = app('request');
  195. $post = $request->post();
  196. $data['pid'] = $post['pid'];
  197. $data['name'] = $post['name'];
  198. if(empty($post['name'] ))
  199. Json::fail('分类名称不能为空!');
  200. $res = Category::create($data);
  201. if($res)
  202. Json::successful('添加成功');
  203. else
  204. Json::fail('添加失败!');
  205. }
  206. /**
  207. * 编辑分类
  208. */
  209. public function editcate($id){
  210. $Category = Category::get($id);
  211. if(!$Category) return Json::fail('数据不存在!');
  212. $formbuider = [];
  213. $formbuider[] = Form::hidden('id',$id);
  214. $formbuider[] = Form::select('pid','上级分类',(string)$Category->getData('pid'))->setOptions(function ()use($id){
  215. $list = Category::getCateList();
  216. $options = [['value'=>0,'label'=>'所有分类']];
  217. foreach ($list as $id=>$cateName){
  218. $options[] = ['label'=>$cateName['html'].$cateName['name'],'value'=>$cateName['id']];
  219. }
  220. return $options;
  221. })->filterable(1);
  222. $formbuider[] = Form::input('name','分类名称',$Category->getData('name'));
  223. $jsContent = <<<SCRIPT
  224. parent.SuccessCateg();
  225. parent.layer.close(parent.layer.getFrameIndex(window.name));
  226. SCRIPT;
  227. $form = Form::make_post_form('编辑分类',$formbuider,Url::buildUrl('updateCate'),$jsContent);
  228. $this->assign(compact('form'));
  229. return $this->fetch('public/form-builder');
  230. }
  231. /**
  232. * 更新分类
  233. * @param $id
  234. */
  235. public function updateCate($id)
  236. {
  237. $data = Util::postMore([
  238. 'pid',
  239. 'name'
  240. ]);
  241. if($data['pid'] == '') return Json::fail('请选择父类');
  242. if(!$data['name']) return Json::fail('请输入分类名称');
  243. Category::edit($data,$id);
  244. return Json::successful('分类编辑成功!');
  245. }
  246. /**
  247. * 删除分类
  248. */
  249. public function deletecate($id){
  250. $chdcount = Category::where('pid',$id)->count();
  251. if($chdcount) return Json::fail('有子栏目不能删除');
  252. $chdcount = SystemAttachmentModel::where('pid',$id)->count();
  253. if($chdcount) return Json::fail('栏目内有图片不能删除');
  254. if(Category::del($id))
  255. return Json::successful('删除成功!');
  256. else
  257. return Json::fail('删除失败');
  258. }
  259. }