Images.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. $where['mer_id'] = $this->adminInfo['mer_id'];
  46. return Json::successful(SystemAttachmentModel::getImageList($where));
  47. }
  48. /**获取分类
  49. * @param string $name
  50. */
  51. public function get_image_cate($name = '')
  52. {
  53. return Json::successful(Category::getAll($name));
  54. }
  55. /**
  56. * 图片管理上传图片
  57. * @return \think\response\Json
  58. */
  59. public function upload()
  60. {
  61. $pid = $this->request->param('pid', session('pid'));
  62. $upload_type = $this->request->get('upload_type', sys_config('upload_type', 1));
  63. try {
  64. $path = make_path('attach', 2, true);
  65. $upload = new Upload((int)$upload_type, [
  66. 'accessKey' => sys_config('accessKey'),
  67. 'secretKey' => sys_config('secretKey'),
  68. 'uploadUrl' => sys_config('uploadUrl'),
  69. 'storageName' => sys_config('storage_name'),
  70. 'storageRegion' => sys_config('storage_region'),
  71. ]);
  72. $res = $upload->to($path)->validate()->move();
  73. if ($res === false) {
  74. return JsonService::fail('上传失败:' . $upload->getError());
  75. } else {
  76. $fileInfo = $upload->getUploadInfo();
  77. if ($fileInfo) {
  78. SystemAttachmentModel::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, $upload_type, $fileInfo['time'], 1, $this->adminInfo['mer_id']);
  79. }
  80. return JsonService::successful('上传成功', ['src' => $res->filePath]);
  81. }
  82. } catch (\Exception $e) {
  83. return JsonService::fail('上传失败:' . $e->getMessage());
  84. }
  85. }
  86. /**
  87. * ajax 提交删除
  88. */
  89. public function delete()
  90. {
  91. $request = app('request');
  92. $post = $request->post();
  93. if (empty($post['imageid']))
  94. Json::fail('还没选择要删除的图片呢?');
  95. foreach ($post['imageid'] as $v) {
  96. if ($v) self::deleteimganddata($v);
  97. }
  98. Json::successful('删除成功');
  99. }
  100. /**删除图片和数据记录
  101. * @param $att_id
  102. */
  103. public function deleteimganddata($att_id)
  104. {
  105. $attinfo = SystemAttachmentModel::get($att_id);
  106. if ($attinfo) {
  107. try {
  108. $upload = new Upload((int)$attinfo['image_type'], [
  109. 'accessKey' => sys_config('accessKey'),
  110. 'secretKey' => sys_config('secretKey'),
  111. 'uploadUrl' => sys_config('uploadUrl'),
  112. 'storageName' => sys_config('storage_name'),
  113. 'storageRegion' => sys_config('storage_region'),
  114. ]);
  115. if ($attinfo['image_type'] == 1) {
  116. if (strpos($attinfo['att_dir'], '/') == 0) {
  117. $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
  118. }
  119. $upload->delete($attinfo['att_dir']);
  120. } else {
  121. $upload->delete($attinfo['name']);
  122. }
  123. } catch (\Throwable $e) {
  124. }
  125. SystemAttachmentModel::where('att_id', $att_id)->delete();
  126. }
  127. }
  128. /**
  129. * 移动图片分类显示
  130. */
  131. public function moveimg($imgaes)
  132. {
  133. $formbuider = [];
  134. $formbuider[] = Form::hidden('imgaes', $imgaes);
  135. $formbuider[] = Form::select('pid', '选择分类')->setOptions(function () {
  136. $list = Category::getCateList();
  137. $options = [['value' => 0, 'label' => '所有分类']];
  138. foreach ($list as $id => $cateName) {
  139. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  140. }
  141. return $options;
  142. })->filterable(1);
  143. $form = Form::make_post_form('编辑分类', $formbuider, Url::buildUrl('moveImgCecate'));
  144. $this->assign(compact('form'));
  145. return $this->fetch('public/form-builder');
  146. }
  147. /**
  148. * 移动图片分类操作
  149. */
  150. public function moveImgCecate()
  151. {
  152. $data = Util::postMore([
  153. 'pid',
  154. 'imgaes'
  155. ]);
  156. if ($data['imgaes'] == '') return Json::fail('请选择图片');
  157. if (!$data['pid']) return Json::fail('请选择分类');
  158. $res = SystemAttachmentModel::where('att_id', 'in', $data['imgaes'])->update(['pid' => $data['pid']]);
  159. if ($res)
  160. Json::successful('移动成功');
  161. else
  162. Json::fail('移动失败!');
  163. }
  164. /**
  165. * ajax 添加分类
  166. */
  167. public function addcate($id = 0)
  168. {
  169. $formbuider = [];
  170. $formbuider[] = Form::select('pid', '上级分类', (string)$id)->setOptions(function () {
  171. $list = Category::getCateList(0);
  172. $options = [['value' => 0, 'label' => '所有分类']];
  173. foreach ($list as $id => $cateName) {
  174. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  175. }
  176. return $options;
  177. })->filterable(1);
  178. $formbuider[] = Form::input('name', '分类名称');
  179. $jsContent = <<<SCRIPT
  180. parent.SuccessCateg();
  181. parent.layer.close(parent.layer.getFrameIndex(window.name));
  182. SCRIPT;
  183. $form = Form::make_post_form('添加分类', $formbuider, Url::buildUrl('saveCate'), $jsContent);
  184. $this->assign(compact('form'));
  185. return $this->fetch('public/form-builder');
  186. }
  187. /**
  188. * 添加分类
  189. */
  190. public function saveCate()
  191. {
  192. $request = app('request');
  193. $post = $request->post();
  194. $data['pid'] = $post['pid'];
  195. $data['name'] = $post['name'];
  196. if (empty($post['name']))
  197. Json::fail('分类名称不能为空!');
  198. $res = Category::create($data);
  199. if ($res)
  200. Json::successful('添加成功');
  201. else
  202. Json::fail('添加失败!');
  203. }
  204. /**
  205. * 编辑分类
  206. */
  207. public function editcate($id)
  208. {
  209. $Category = Category::get($id);
  210. if (!$Category) return Json::fail('数据不存在!');
  211. $formbuider = [];
  212. $formbuider[] = Form::hidden('id', $id);
  213. $formbuider[] = Form::select('pid', '上级分类', (string)$Category->getData('pid'))->setOptions(function () use ($id) {
  214. $list = Category::getCateList();
  215. $options = [['value' => 0, 'label' => '所有分类']];
  216. foreach ($list as $id => $cateName) {
  217. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  218. }
  219. return $options;
  220. })->filterable(1);
  221. $formbuider[] = Form::input('name', '分类名称', $Category->getData('name'));
  222. $jsContent = <<<SCRIPT
  223. parent.SuccessCateg();
  224. parent.layer.close(parent.layer.getFrameIndex(window.name));
  225. SCRIPT;
  226. $form = Form::make_post_form('编辑分类', $formbuider, Url::buildUrl('updateCate'), $jsContent);
  227. $this->assign(compact('form'));
  228. return $this->fetch('public/form-builder');
  229. }
  230. /**
  231. * 更新分类
  232. * @param $id
  233. */
  234. public function updateCate($id)
  235. {
  236. $data = Util::postMore([
  237. 'pid',
  238. 'name'
  239. ]);
  240. if ($data['pid'] == '') return Json::fail('请选择父类');
  241. if (!$data['name']) return Json::fail('请输入分类名称');
  242. Category::edit($data, $id);
  243. return Json::successful('分类编辑成功!');
  244. }
  245. /**
  246. * 删除分类
  247. */
  248. public function deletecate($id)
  249. {
  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. }