Images.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\widget;
  12. use Api\AliyunOss;
  13. use app\admin\model\system\SystemAttachment as SystemAttachmentModel;
  14. use app\admin\model\system\SystemAttachmentCategory as Category;
  15. use app\admin\controller\AuthController;
  16. use service\SystemConfigService;
  17. use service\JsonService as Json;
  18. use service\FormBuilder as Form;
  19. use think\Url;
  20. /**
  21. * TODO 附件控制器
  22. * Class Images
  23. * @package app\admin\controller\widget
  24. */
  25. class Images extends AuthController
  26. {
  27. /**
  28. * 初始化
  29. */
  30. protected function init()
  31. {
  32. return AliyunOss::instance([
  33. 'AccessKey' => SystemConfigService::get('accessKeyId'),
  34. 'AccessKeySecret' => SystemConfigService::get('accessKeySecret'),
  35. 'OssEndpoint' => SystemConfigService::get('end_point'),
  36. 'OssBucket' => SystemConfigService::get('OssBucket'),
  37. 'uploadUrl' => SystemConfigService::get('uploadUrl'),
  38. ]);
  39. }
  40. /**
  41. * 附件列表
  42. * @return \think\response\Json
  43. */
  44. public function index()
  45. {
  46. $pid = request()->param('pid');
  47. if ($pid === NULL) {
  48. $pid = session('pid') ? session('pid') : 0;
  49. }
  50. session('pid', $pid);
  51. $this->assign('pid', $pid);
  52. $this->assign('maxLength', $this->request->get('max_count', 0));
  53. $this->assign('fodder', $this->request->param('fodder', $this->request->get('fodder','')));
  54. return $this->fetch('widget/images');
  55. }
  56. /**获取图片列表
  57. *
  58. */
  59. public function get_image_list()
  60. {
  61. $where = parent::getMore([
  62. ['page', 1],
  63. ['limit', 18],
  64. ['pid', 0]
  65. ]);
  66. return Json::successful(SystemAttachmentModel::getImageList($where));
  67. }
  68. /**获取分类
  69. * @param string $name
  70. */
  71. public function get_image_cate($name = '')
  72. {
  73. return Json::successful(Category::getAll($name));
  74. }
  75. /**
  76. * 图片管理上传图片
  77. * @return \think\response\Json
  78. */
  79. public function upload()
  80. {
  81. $pid = input('pid') != NULL ? input('pid') : session('pid');
  82. try {
  83. $aliyunOss = $this->init();
  84. $res = $aliyunOss->upload('file');
  85. if ($res) {
  86. SystemAttachmentModel::attachmentAdd($res['key'], 0, 'image/jpg', $res['url'], $res['url'], $pid, 1, time());
  87. return Json::successful(['url' => $res]);
  88. } else {
  89. return Json::fail($aliyunOss->getErrorInfo()['msg']);
  90. }
  91. } catch (\Exception $e) {
  92. return Json::fail('上传失败:' . $e->getMessage());
  93. }
  94. }
  95. /**
  96. * ajax 提交删除
  97. */
  98. public function delete()
  99. {
  100. $post = $this->request->post();
  101. if (empty($post['imageid']))
  102. Json::fail('还没选择要删除的图片呢?');
  103. foreach ($post['imageid'] as $v) {
  104. if ($v) self::deleteimganddata($v);
  105. }
  106. Json::successful('删除成功');
  107. }
  108. /**删除图片和数据记录
  109. * @param $att_id
  110. */
  111. public function deleteimganddata($att_id)
  112. {
  113. $attinfo = SystemAttachmentModel::get($att_id);
  114. if ($attinfo) {
  115. try {
  116. $this->init()->delOssFile($attinfo->name);
  117. } catch (\Throwable $e) {
  118. }
  119. $attinfo->delete();
  120. }
  121. }
  122. /**
  123. * 移动图片分类显示
  124. */
  125. public function moveimg($imgaes)
  126. {
  127. $formbuider = [];
  128. $formbuider[] = Form::hidden('imgaes', $imgaes);
  129. $formbuider[] = Form::select('pid', '选择分类')->setOptions(function () {
  130. $list = Category::getCateList();
  131. $options = [['value' => 0, 'label' => '所有分类']];
  132. foreach ($list as $id => $cateName) {
  133. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  134. }
  135. return $options;
  136. })->filterable(1);
  137. $form = Form::make_post_form('编辑分类', $formbuider, Url::build('moveImgCecate'));
  138. $this->assign(compact('form'));
  139. return $this->fetch('public/form-builder');
  140. }
  141. /**
  142. * 移动图片分类操作
  143. */
  144. public function moveImgCecate()
  145. {
  146. $data = parent::postMore([
  147. 'pid',
  148. 'imgaes'
  149. ]);
  150. if ($data['imgaes'] == '') return Json::fail('请选择图片');
  151. if (!$data['pid']) return Json::fail('请选择分类');
  152. $res = SystemAttachmentModel::where('att_id', 'in', $data['imgaes'])->update(['pid' => $data['pid']]);
  153. if ($res)
  154. Json::successful('移动成功');
  155. else
  156. Json::fail('移动失败!');
  157. }
  158. /**
  159. * ajax 添加分类
  160. */
  161. public function addcate($id = 0)
  162. {
  163. $formbuider = [];
  164. $formbuider[] = Form::select('pid', '上级分类', (string)$id)->setOptions(function () {
  165. $list = Category::getCateList(0);
  166. $options = [['value' => 0, 'label' => '所有分类']];
  167. foreach ($list as $id => $cateName) {
  168. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  169. }
  170. return $options;
  171. })->filterable(1);
  172. $formbuider[] = Form::input('name', '分类名称');
  173. $jsContent = <<<SCRIPT
  174. parent.SuccessCateg();
  175. parent.layer.close(parent.layer.getFrameIndex(window.name));
  176. SCRIPT;
  177. $form = Form::make_post_form('添加分类', $formbuider, Url::build('saveCate'), $jsContent);
  178. $this->assign(compact('form'));
  179. return $this->fetch('public/form-builder');
  180. }
  181. /**
  182. * 添加分类
  183. */
  184. public function saveCate()
  185. {
  186. $post = $this->request->post();
  187. $data['pid'] = $post['pid'];
  188. $data['name'] = $post['name'];
  189. if (empty($post['name']))
  190. Json::fail('分类名称不能为空!');
  191. $res = Category::create($data);
  192. if ($res)
  193. Json::successful('添加成功');
  194. else
  195. Json::fail('添加失败!');
  196. }
  197. /**
  198. * 编辑分类
  199. */
  200. public function editcate($id)
  201. {
  202. $Category = Category::get($id);
  203. if (!$Category) return Json::fail('数据不存在!');
  204. $formbuider = [];
  205. $formbuider[] = Form::hidden('id', $id);
  206. $formbuider[] = Form::select('pid', '上级分类', (string)$Category->getData('pid'))->setOptions(function () use ($id) {
  207. $list = Category::getCateList();
  208. $options = [['value' => 0, 'label' => '所有分类']];
  209. foreach ($list as $id => $cateName) {
  210. $options[] = ['label' => $cateName['html'] . $cateName['name'], 'value' => $cateName['id']];
  211. }
  212. return $options;
  213. })->filterable(1);
  214. $formbuider[] = Form::input('name', '分类名称', $Category->getData('name'));
  215. $jsContent = <<<SCRIPT
  216. parent.SuccessCateg();
  217. parent.layer.close(parent.layer.getFrameIndex(window.name));
  218. SCRIPT;
  219. $form = Form::make_post_form('编辑分类', $formbuider, Url::build('updateCate'), $jsContent);
  220. $this->assign(compact('form'));
  221. return $this->fetch('public/form-builder');
  222. }
  223. /**
  224. * 更新分类
  225. * @param $id
  226. */
  227. public function updateCate($id)
  228. {
  229. $data = parent::postMore([
  230. 'pid',
  231. 'name'
  232. ]);
  233. if ($data['pid'] == '') return Json::fail('请选择父类');
  234. if (!$data['name']) return Json::fail('请输入分类名称');
  235. Category::edit($data, $id);
  236. return Json::successful('分类编辑成功!');
  237. }
  238. /**
  239. * 删除分类
  240. */
  241. public function deletecate($id)
  242. {
  243. $chdcount = Category::where('pid', $id)->count();
  244. if ($chdcount) return Json::fail('有子栏目不能删除');
  245. $chdcount = SystemAttachmentModel::where('pid', $id)->count();
  246. if ($chdcount) return Json::fail('栏目内有图片不能删除');
  247. if (Category::del($id)) {
  248. SystemAttachmentModel::where(['pid' => $id])->update(['pid' => 0]);
  249. return Json::successful('删除成功!');
  250. } else
  251. return Json::fail('删除失败');
  252. }
  253. /**
  254. * 获取签名
  255. */
  256. public function get_signature()
  257. {
  258. return Json::successful($this->init()->getSignature());
  259. }
  260. /**
  261. * 删除阿里云oss
  262. * @param $key
  263. */
  264. public function del_oss_key($key = '', $url = '')
  265. {
  266. if (!$key && !$url) {
  267. return Json::fail('删除失败');
  268. }
  269. if ($url) {
  270. $key = SystemAttachmentModel::where(['att_dir' => $url])->value('name');
  271. }
  272. $res = $this->init()->delOssFile($key);
  273. if ($res) {
  274. return Json::successful('删除成功');
  275. } else {
  276. return Json::fail('删除失败');
  277. }
  278. }
  279. }