Images.php 11 KB

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