ThemeModuleServices.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\diy;
  12. use app\dao\diy\ThemeModuleDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * 主题组件服务类
  17. */
  18. class ThemeModuleServices extends BaseServices
  19. {
  20. /**
  21. * 构造方法
  22. * @param ThemeModuleDao $dao
  23. */
  24. public function __construct(ThemeModuleDao $dao)
  25. {
  26. $this->dao = $dao;
  27. }
  28. /**
  29. * 获取组件列表
  30. * @param array $where
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function getModuleList(array $where)
  37. {
  38. $list = $this->dao->themeModuleList($where, '*', 1, 100, 'id desc');
  39. $count = $this->dao->themeModuleCount($where);
  40. return compact('list', 'count');
  41. }
  42. /**
  43. * 获取组件详情
  44. * @param int $id
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getModuleInfo(int $id)
  51. {
  52. $info = $this->dao->get($id);
  53. if (!$info) {
  54. throw new AdminException('数据不存在');
  55. }
  56. $info = $info->toArray();
  57. $info['data'] = $info['data'] ? json_decode($info['data'], true) : [];
  58. return $info;
  59. }
  60. /**
  61. * 保存组件数据(新增/编辑)
  62. * @param int $id
  63. * @param array $data
  64. * @return int
  65. */
  66. public function saveModule(int $id, array $data)
  67. {
  68. $save = [];
  69. if (isset($data['type'])) {
  70. $save['type'] = $data['type'];
  71. }
  72. if (isset($data['data'])) {
  73. $value = is_string($data['data']) ? $data['data'] : json_encode($data['data'], JSON_UNESCAPED_UNICODE);
  74. $save['data'] = $value;
  75. }
  76. if (!$save) {
  77. throw new AdminException('保存数据不能为空');
  78. }
  79. if ($id) {
  80. $this->dao->update($id, $save);
  81. } else {
  82. $id = $this->dao->insertGetId($save);
  83. }
  84. return $id;
  85. }
  86. /**
  87. * 删除组件
  88. * @param int $id
  89. * @return bool
  90. */
  91. public function deleteModule(int $id)
  92. {
  93. if (!$this->dao->get($id)) {
  94. throw new AdminException('数据不存在');
  95. }
  96. return (bool)$this->dao->delete($id);
  97. }
  98. }