AttachmentService.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\sys;
  12. use app\dict\sys\FileDict;
  13. use app\dict\sys\IconDict;
  14. use app\model\sys\SysAttachment;
  15. use app\model\sys\SysAttachmentCategory;
  16. use app\service\core\sys\CoreAttachmentService;
  17. use core\base\BaseAdminService;
  18. use core\exception\AdminException;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * 附件服务层
  24. * Class CoreAgreementService
  25. * @package app\service\core\sys
  26. */
  27. class AttachmentService extends BaseAdminService
  28. {
  29. public $core_attachment_service;
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $this->model = new SysAttachment();
  34. $this->core_attachment_service = new CoreAttachmentService();
  35. }
  36. /**
  37. * 新增素材
  38. * @param array $data
  39. */
  40. public function add(array $data)
  41. {
  42. $data[ 'site_id' ] = $this->site_id;
  43. return $this->core_attachment_service->add($data);
  44. }
  45. /**
  46. *
  47. * /**
  48. * 编辑素材
  49. * @param int $att_id
  50. * @param array $data
  51. * @return SysAttachment
  52. */
  53. public function edit(int $att_id, array $data)
  54. {
  55. return $this->core_attachment_service->edit($this->site_id, $att_id, $data);
  56. }
  57. /**
  58. * 修改附件分组
  59. * @param $att_id
  60. * @param $cate_id
  61. * @return bool
  62. */
  63. public function modifyCategory($att_id, $cate_id)
  64. {
  65. $where = array (
  66. [ 'att_id', '=', $att_id ],
  67. [ 'site_id', '=', $this->site_id ],
  68. );
  69. $this->model->where($where)->update([ 'cate_id' => $cate_id, 'update_time' => time() ]);
  70. return true;
  71. }
  72. /**
  73. * 批量更新附件分组
  74. * @param $att_ids
  75. * @param $cate_id
  76. * @return bool
  77. */
  78. public function batchModifyCategory($att_ids, $cate_id)
  79. {
  80. $where = array (
  81. [ 'att_id', 'in', is_string($att_ids) ? explode($att_ids) : $att_ids ],
  82. [ 'site_id', '=', $this->site_id ],
  83. );
  84. $this->model->where($where)->update([ 'cate_id' => $cate_id, 'update_time' => time() ]);
  85. return true;
  86. }
  87. /**
  88. * 删除素材
  89. * @param int $att_id
  90. * @return mixed
  91. */
  92. public function del(int $att_id)
  93. {
  94. return $this->core_attachment_service->del($this->site_id, $att_id);
  95. }
  96. /**
  97. * 批量删除
  98. * @param $data
  99. * @return true|null
  100. */
  101. public function delAll($data)
  102. {
  103. return $this->core_attachment_service->delAll($this->site_id, $data);
  104. }
  105. /**
  106. * 管理端获取附件列表
  107. * @param array $data
  108. * @return array
  109. */
  110. public function getPage(array $data)
  111. {
  112. $where = array (
  113. [ 'site_id', '=', $this->site_id ]
  114. );
  115. if (!empty($data[ 'att_type' ])) {
  116. $where[] = [ 'att_type', '=', $data[ 'att_type' ] ];
  117. }
  118. if (!empty($data[ 'cate_id' ])) {
  119. $where[] = [ 'cate_id', '=', $data[ 'cate_id' ] ];
  120. }
  121. if (!empty($data[ 'real_name' ])) {
  122. $where[] = [ 'real_name', 'like', '%' . $data[ 'real_name' ] . '%' ];
  123. }
  124. return $this->getPageList($this->model, $where, 'att_id,path,real_name,att_type,url', 'att_id desc', each:function($item, $key)
  125. {
  126. $item[ 'thumb' ] = get_thumb_images($this->site_id, $item[ 'url' ], FileDict::SMALL);
  127. });
  128. }
  129. /**
  130. * 新增素材组
  131. * @param array $data
  132. * @return mixed
  133. */
  134. public function addCategory(array $data)
  135. {
  136. $data[ 'site_id' ] = $this->site_id;
  137. $category_model = new SysAttachmentCategory();
  138. $attachment = $category_model->create($data);
  139. if (!$attachment->id)
  140. throw new AdminException('ADD_FAIL');//创建失败
  141. return $attachment->att_id;
  142. }
  143. /**
  144. * 素材组模型对象
  145. * @param int $site_id
  146. * @param int $id
  147. * @return mixed
  148. */
  149. public function findCategory(int $site_id, int $id)
  150. {
  151. $where = array (
  152. [ 'site_id', '=', $site_id ],
  153. [ 'id', '=', $id ]
  154. );
  155. $category_model = new SysAttachmentCategory();
  156. $category = $category_model->where($where)->findOrEmpty();
  157. if ($category->isEmpty())
  158. throw new AdminException('ATTACHMENT_GROUP_NOT_EXIST');
  159. return $category;
  160. }
  161. /**
  162. * 编辑素材组
  163. * @param int $id
  164. * @param array $data
  165. * @return SysAttachmentCategory
  166. */
  167. public function editCategory(int $id, array $data)
  168. {
  169. $where = array (
  170. [ 'site_id', '=', $this->site_id ],
  171. [ 'id', '=', $id ]
  172. );
  173. $category_model = new SysAttachmentCategory();
  174. return $category_model->where($where)->update($data);
  175. }
  176. /**
  177. * 删除素材组
  178. * @param int $id
  179. * @return mixed
  180. * @throws DbException
  181. */
  182. public function delCategory(int $id)
  183. {
  184. //查询是否有下级菜单或按钮
  185. $category = $this->findCategory($this->site_id, $id);
  186. if ($this->model->where([ [ 'cate_id', '=', $id ] ])->count() > 0)
  187. throw new AdminException('ATTACHMENT_GROUP_HAS_IMAGE');
  188. //下级存在图片不能删除
  189. return $category->delete();
  190. }
  191. /**
  192. * 管理端获取附件组列表
  193. * @param array $data
  194. * @return array
  195. * @throws DbException
  196. */
  197. public function getCategoryPage(array $data)
  198. {
  199. $where = array (
  200. [ 'site_id', '=', $this->site_id ]
  201. );
  202. if (!empty($data[ 'type' ])) {
  203. $where[] = [ 'type', '=', $data[ 'type' ] ];
  204. }
  205. if (!empty($data[ 'name' ])) {
  206. $where[] = [ 'name', 'like', '%' . $data[ 'name' ] . '%' ];
  207. }
  208. return $this->getPageList(( new SysAttachmentCategory() ), $where, 'id,name', 'id desc');
  209. }
  210. /**
  211. * 获取分组列表
  212. * @param array $data
  213. * @return array
  214. * @throws DataNotFoundException
  215. * @throws DbException
  216. * @throws ModelNotFoundException
  217. */
  218. public function getCategoryList(array $data)
  219. {
  220. $where = array (
  221. [ 'site_id', '=', $this->site_id ]
  222. );
  223. if (!empty($data[ 'type' ])) {
  224. $where[] = [ 'type', '=', $data[ 'type' ] ];
  225. }
  226. if (!empty($data[ 'name' ])) {
  227. $where[] = [ 'name', 'like', '%' . $data[ 'name' ] . '%' ];
  228. }
  229. return SysAttachmentCategory::where($where)->field('id,name,type')->order('id desc')->select()->toArray();
  230. }
  231. /**
  232. * 获取图标库分类列表
  233. * @param array $data
  234. * @return array|null
  235. */
  236. public function getIconCategoryList(array $data)
  237. {
  238. $icon_list = IconDict::getIcon();
  239. foreach ($icon_list as $k => $v) {
  240. unset($icon_list[ $k ][ 'glyphs' ]);
  241. if (!empty($data[ 'name' ]) && !str_contains($v['name'], $data['name'])) {
  242. unset($icon_list[ $k ]);
  243. }
  244. }
  245. return array_values($icon_list);
  246. }
  247. /**
  248. * 获取图标库列表
  249. * @param array $data
  250. * @return array|null
  251. */
  252. public function getIconList(array $data)
  253. {
  254. $icon_list = IconDict::getIcon();
  255. $res = [
  256. 'current_page' => intval($data[ 'page' ]),
  257. 'per_page' => intval($data[ 'limit' ]),
  258. 'data' => [],
  259. 'total' => 0
  260. ];
  261. $temp_data = [];
  262. foreach ($icon_list as $v) {
  263. $icon = $v[ 'glyphs' ]; // 图标列表
  264. foreach ($icon as $ck => $cv) {
  265. // 素材表中数据保持要一致
  266. $icon[ $ck ][ 'att_id' ] = $cv[ 'icon_id' ];
  267. $icon[ $ck ][ 'url' ] = $v[ 'font_family' ] . '-' . $v[ 'css_prefix_text' ] . $cv[ 'font_class' ];
  268. $icon[ $ck ][ 'real_name' ] = $cv[ 'name' ];
  269. // 查询名称
  270. if (!empty($data[ 'real_name' ]) && !str_contains($cv['name'], $data['real_name'])) {
  271. unset($icon[ $ck ]);
  272. }
  273. }
  274. $icon = array_values($icon);
  275. if (!empty($data[ 'cate_id' ]) && $data[ 'cate_id' ] == $v[ 'id' ]) {
  276. // 查询指定分类下的图标
  277. $temp_data = $icon;
  278. break;
  279. } else {
  280. // 查询全部图标
  281. $temp_data = array_merge($temp_data, $icon);
  282. }
  283. }
  284. // 手动分页
  285. $res[ 'total' ] = count($temp_data); // 总数量
  286. $start = ( $res[ 'current_page' ] - 1 ) * $res[ 'per_page' ]; // 数组下标从0 开始
  287. $icon_list = array_slice($temp_data, $start, $res[ 'per_page' ]);
  288. $res[ 'data' ] = $icon_list;
  289. return $res;
  290. }
  291. }