DiyRouteService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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[ 'child_list' ])) {
  45. foreach ($cv[ 'child_list' ] as $ck2 => $cv2) {
  46. $is_add = true;
  47. if (isset($where[ 'title' ]) && $where[ 'title' ] != '' && !str_contains($cv2[ 'title' ], $where[ 'title' ])) {
  48. $is_add = false;
  49. }
  50. if (!empty($where[ 'url' ]) && $where[ 'url' ] != $cv2[ 'url' ]) {
  51. $is_add = false;
  52. }
  53. if (!empty($v[ 'addon_info' ]) && !empty($where[ 'addon_name' ]) && $where[ 'addon_name' ] != $v[ 'addon_info' ][ 'key' ]) {
  54. $is_add = false;
  55. }
  56. $item = [
  57. 'addon_info' => $v[ 'addon_info' ] ?? '',
  58. 'title' => $cv2[ 'title' ],
  59. 'name' => $cv2[ 'name' ],
  60. 'parent' => $k,
  61. 'page' => $cv2[ 'url' ],
  62. 'is_share' => $cv2[ 'is_share' ],
  63. 'action' => $cv2[ 'action' ] ?? '',
  64. 'sort' => ++$sort
  65. ];
  66. if ($is_add) {
  67. $diy_route_list[] = $item;
  68. }
  69. }
  70. } else if (!empty($cv[ 'url' ])) {
  71. $is_add = true;
  72. if (isset($where[ 'title' ]) && $where[ 'title' ] != '' && !str_contains($cv[ 'title' ], $where[ 'title' ])) {
  73. $is_add = false;
  74. }
  75. if (!empty($where[ 'url' ]) && $where[ 'url' ] != $cv[ 'url' ]) {
  76. $is_add = false;
  77. }
  78. if (!empty($v[ 'addon_info' ]) && !empty($where[ 'addon_name' ]) && $where[ 'addon_name' ] != $v[ 'addon_info' ][ 'key' ]) {
  79. $is_add = false;
  80. }
  81. $item = [
  82. 'addon_info' => $v[ 'addon_info' ] ?? '',
  83. 'title' => $cv[ 'title' ],
  84. 'name' => $cv[ 'name' ],
  85. 'parent' => $k,
  86. 'page' => $cv[ 'url' ],
  87. 'is_share' => $cv[ 'is_share' ],
  88. 'action' => $cv[ 'action' ] ?? '',
  89. 'sort' => ++$sort
  90. ];
  91. if ($is_add) {
  92. $diy_route_list[] = $item;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. return $diy_route_list;
  99. }
  100. /**
  101. * 获取自定义路由表列表
  102. * @param array $where
  103. * @return array
  104. */
  105. public function getPage(array $where = [])
  106. {
  107. $where[] = [ 'site_id', '=', $this->site_id ];
  108. $field = 'id,title,name,page,share,is_share,sort';
  109. $order = '';
  110. $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ "title" ], $where)->field($field)->order($order);
  111. return $this->pageQuery($search_model);
  112. }
  113. /**
  114. * 获取自定义路由表信息
  115. * @param int $id
  116. * @return array
  117. */
  118. public function getInfo(int $id)
  119. {
  120. $field = 'title,name,page,share,is_share,sort';
  121. return $this->model->field($field)->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  122. }
  123. /**
  124. * 获取自定义路由表信息
  125. * @param string $name
  126. * @return array
  127. */
  128. public function getInfoByName(string $name)
  129. {
  130. $field = 'id,title,name,page,share,is_share,sort';
  131. return $this->model->field($field)->where([ [ 'name', '=', $name ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  132. }
  133. /**
  134. * 添加自定义路由表
  135. * @param array $data
  136. * @return mixed
  137. */
  138. public function add(array $data)
  139. {
  140. $data[ 'site_id' ] = $this->site_id;
  141. $res = $this->model->create($data);
  142. return $res->id;
  143. }
  144. /**
  145. * 自定义路由表编辑
  146. * @param int $id
  147. * @param array $data
  148. * @return bool
  149. */
  150. public function edit(int $id, array $data)
  151. {
  152. $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
  153. return true;
  154. }
  155. /**
  156. * 删除自定义路由表
  157. * @param int $id
  158. * @return bool
  159. */
  160. public function del(int $id)
  161. {
  162. return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->delete();
  163. }
  164. /**
  165. * 修改分享内容
  166. * @param $data
  167. * @return bool
  168. */
  169. public function modifyShare($data)
  170. {
  171. $field = 'id';
  172. $data[ 'site_id' ] = $this->site_id;
  173. $info = $this->model->field($field)->where([ [ 'name', '=', $data[ 'name' ] ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  174. if (!empty($info)) {
  175. $this->model->where([ [ 'id', '=', $info[ 'id' ] ], [ 'site_id', '=', $this->site_id ] ])->update([ 'share' => $data[ 'share' ] ]);
  176. } else {
  177. $this->model->create($data);
  178. }
  179. return true;
  180. }
  181. /**
  182. * 获取路由列表(存在的应用插件列表)
  183. * @return array|null
  184. */
  185. public function getApps()
  186. {
  187. $link = LinkDict::getLink([
  188. 'query' => 'addon'
  189. ]);
  190. return $link;
  191. }
  192. }