DiyRouteService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\diy;
  12. use app\dict\diy\LinkDict;
  13. use app\model\diy\DiyRoute;
  14. use core\base\BaseAdminService;
  15. /**
  16. * 自定义路由表服务层
  17. * Class DiyRouteService
  18. * @package app\service\admin\diy
  19. */
  20. class DiyRouteService extends BaseAdminService
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->model = new DiyRoute();
  26. }
  27. /**
  28. * 获取自定义路由列表
  29. * @param array $where
  30. * @return array
  31. */
  32. public function getList(array $where = [])
  33. {
  34. $condition = [];
  35. if (!empty($where[ 'addon' ])) {
  36. $condition[ 'addon' ] = $where[ 'addon' ];
  37. }
  38. $link = LinkDict::getLink($condition);
  39. $diy_route_list = [];
  40. $sort = 0;
  41. foreach ($link as $k => $v) {
  42. if (!empty($v[ 'child_list' ])) {
  43. foreach ($v[ 'child_list' ] as $ck => $cv) {
  44. if (!empty($cv[ 'url' ])) {
  45. $is_add = true;
  46. if (isset($where[ 'title' ]) && $where[ 'title' ] !='' && !str_contains($cv[ 'title' ], $where[ 'title' ])) {
  47. $is_add = false;
  48. }
  49. if (!empty($where[ 'url' ]) && $where[ 'url' ] != $cv[ 'url' ]) {
  50. $is_add = false;
  51. }
  52. if (!empty($v[ 'addon_info' ]) && !empty($where[ 'addon_name' ]) && $where[ 'addon_name' ] != $v[ 'addon_info' ][ 'key' ]) {
  53. $is_add = false;
  54. }
  55. $item = [
  56. 'addon_info' => $v[ 'addon_info' ] ?? '',
  57. 'title' => $cv[ 'title' ],
  58. 'name' => $cv[ 'name' ],
  59. 'parent' => $k,
  60. 'page' => $cv[ 'url' ],
  61. 'is_share' => $cv[ 'is_share' ],
  62. 'action' => $cv[ 'action' ] ?? '',
  63. 'sort' => ++$sort
  64. ];
  65. if ($is_add) {
  66. $diy_route_list[] = $item;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. return $diy_route_list;
  73. }
  74. /**
  75. * 获取自定义路由表列表
  76. * @param array $where
  77. * @return array
  78. */
  79. public function getPage(array $where = [])
  80. {
  81. $where[] = [ 'site_id', '=', $this->site_id ];
  82. $field = 'id,title,name,page,share,is_share,sort';
  83. $order = '';
  84. $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ "title" ], $where)->field($field)->order($order);
  85. return $this->pageQuery($search_model);
  86. }
  87. /**
  88. * 获取自定义路由表信息
  89. * @param int $id
  90. * @return array
  91. */
  92. public function getInfo(int $id)
  93. {
  94. $field = 'title,name,page,share,is_share,sort';
  95. return $this->model->field($field)->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  96. }
  97. /**
  98. * 获取自定义路由表信息
  99. * @param string $name
  100. * @return array
  101. */
  102. public function getInfoByName(string $name)
  103. {
  104. $field = 'id,title,name,page,share,is_share,sort';
  105. return $this->model->field($field)->where([ [ 'name', '=', $name ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  106. }
  107. /**
  108. * 添加自定义路由表
  109. * @param array $data
  110. * @return mixed
  111. */
  112. public function add(array $data)
  113. {
  114. $data[ 'site_id' ] = $this->site_id;
  115. $res = $this->model->create($data);
  116. return $res->id;
  117. }
  118. /**
  119. * 自定义路由表编辑
  120. * @param int $id
  121. * @param array $data
  122. * @return bool
  123. */
  124. public function edit(int $id, array $data)
  125. {
  126. $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
  127. return true;
  128. }
  129. /**
  130. * 删除自定义路由表
  131. * @param int $id
  132. * @return bool
  133. */
  134. public function del(int $id)
  135. {
  136. return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->delete();
  137. }
  138. /**
  139. * 修改分享内容
  140. * @param $data
  141. * @return bool
  142. */
  143. public function modifyShare($data)
  144. {
  145. $field = 'id';
  146. $data[ 'site_id' ] = $this->site_id;
  147. $info = $this->model->field($field)->where([ [ 'name', '=', $data[ 'name' ] ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  148. if (!empty($info)) {
  149. $this->model->where([ [ 'id', '=', $info[ 'id' ] ], [ 'site_id', '=', $this->site_id ] ])->update([ 'share' => $data[ 'share' ] ]);
  150. } else {
  151. $this->model->create($data);
  152. }
  153. return true;
  154. }
  155. /**
  156. * 获取路由列表(存在的应用插件列表)
  157. * @return array|null
  158. */
  159. public function getApps()
  160. {
  161. $link = LinkDict::getLink([
  162. 'query' => 'addon'
  163. ]);
  164. return $link;
  165. }
  166. }