Images.php 9.1 KB

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