Helper.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\utils;
  8. /**
  9. * 帮助类
  10. */
  11. class Helper
  12. {
  13. /**
  14. * 数据树形化
  15. * @param array $data 数据
  16. * @param string $childrenname 子数据名
  17. * @param string $keyName 数据key名
  18. * @param string $pidName 数据上级key名
  19. * @return array
  20. */
  21. public static function makeTree(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'parent_id')
  22. {
  23. $list = [];
  24. foreach ($data as $value) {
  25. $list[$value[$keyName]] = $value;
  26. }
  27. $tree = []; //格式化好的树
  28. foreach ($list as $item) {
  29. if (isset($list[$item[$pidName]])) {
  30. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  31. } else {
  32. $tree[] = &$list[$item[$keyName]];
  33. }
  34. }
  35. return $tree;
  36. }
  37. /**
  38. * 生成Arco菜单
  39. * @param array $data 数据
  40. * @param string $childrenname 子数据名
  41. * @param string $keyName 数据key名
  42. * @param string $pidName 数据上级key名
  43. * @return array
  44. */
  45. public static function makeArcoMenus(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'parent_id')
  46. {
  47. $list = [];
  48. foreach ($data as $value) {
  49. if ($value['type'] === 'M'){
  50. $path = '/'.$value['route'];
  51. $layout = isset($value['is_layout']) ? $value['is_layout'] : 1;
  52. $temp = [
  53. $keyName => $value[$keyName],
  54. $pidName => $value[$pidName],
  55. 'name' => $value['route'],
  56. 'path' => $path,
  57. 'component' => $value['component'],
  58. 'redirect' => $value['redirect'],
  59. 'meta' => [
  60. 'title' => $value['name'],
  61. 'type' => $value['type'],
  62. 'hidden' => $value['is_hidden'] === 1,
  63. 'layout' => $layout === 1,
  64. 'hiddenBreadcrumb' => false,
  65. 'icon' => $value['icon'],
  66. ],
  67. ];
  68. $list[$value[$keyName]] = $temp;
  69. }
  70. if ($value['type'] === 'I' || $value['type'] === 'L'){
  71. $temp = [
  72. $keyName => $value[$keyName],
  73. $pidName => $value[$pidName],
  74. 'name' => $value['code'],
  75. 'path' => $value['route'],
  76. 'meta' => [
  77. 'title' => $value['name'],
  78. 'type' => $value['type'],
  79. 'hidden' => $value['is_hidden'] === 1,
  80. 'hiddenBreadcrumb' => false,
  81. 'icon' => $value['icon'],
  82. ],
  83. ];
  84. $list[$value[$keyName]] = $temp;
  85. }
  86. }
  87. $tree = []; //格式化好的树
  88. foreach ($list as $item) {
  89. if (isset($list[$item[$pidName]])) {
  90. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  91. } else {
  92. $tree[] = &$list[$item[$keyName]];
  93. }
  94. }
  95. return $tree;
  96. }
  97. /**
  98. * 生成Artd菜单
  99. * @param array $data
  100. * @param string $childrenname
  101. * @param string $keyName
  102. * @param string $pidName
  103. * @return array
  104. */
  105. public static function makeArtdMenus(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'parent_id')
  106. {
  107. $list = [];
  108. foreach ($data as $value) {
  109. $component = '';
  110. if ($value['type'] === 1) {
  111. $component = '/index/index';
  112. }
  113. if ($value['type'] === 2) {
  114. $component = $value['component'];
  115. }
  116. $temp = [
  117. $keyName => $value[$keyName],
  118. $pidName => $value[$pidName],
  119. 'name' => $value['code'],
  120. 'path' => $value['path'],
  121. 'component' => $component,
  122. 'meta' => [
  123. 'title' => $value['name'],
  124. 'icon' => $value['icon'],
  125. 'isIframe' => $value['is_iframe'] === 1,
  126. 'keepAlive' => $value['is_keep_alive'] === 1,
  127. 'isHide' => $value['is_hidden'] === 1,
  128. 'fixedTab' => $value['is_fixed_tab'] === 1,
  129. 'isFullPage' => $value['is_full_page'] === 1,
  130. ],
  131. ];
  132. if ($value['type'] === 4) {
  133. $temp['path'] = '/outside/Iframe';
  134. $temp['meta']['link'] = $value['link_url'];
  135. }
  136. $list[$value[$keyName]] = $temp;
  137. }
  138. $tree = [];
  139. foreach ($list as $item) {
  140. if (isset($list[$item[$pidName]])) {
  141. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  142. } else {
  143. $tree[] = &$list[$item[$keyName]];
  144. }
  145. }
  146. return $tree;
  147. }
  148. /**
  149. * 下划线转驼峰
  150. */
  151. public static function camelize($uncamelized_words,$separator='_')
  152. {
  153. $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
  154. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
  155. }
  156. /**
  157. * 驼峰命名转下划线命名
  158. */
  159. public static function uncamelize($camelCaps,$separator='_')
  160. {
  161. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  162. }
  163. /**
  164. * 转换为驼峰
  165. * @param string $value
  166. * @return string
  167. */
  168. public static function camel(string $value): string
  169. {
  170. static $cache = [];
  171. $key = $value;
  172. if (isset($cache[$key])) {
  173. return $cache[$key];
  174. }
  175. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  176. return $cache[$key] = str_replace(' ', '', $value);
  177. }
  178. /**
  179. * 获取业务名称
  180. * @param string $tableName
  181. * @return mixed
  182. */
  183. public static function get_business(string $tableName)
  184. {
  185. $start = strrpos($tableName,'_');
  186. if ($start !== false) {
  187. $result = substr($tableName, $start + 1);
  188. } else {
  189. $result = $tableName;
  190. }
  191. return static::camelize($result);
  192. }
  193. /**
  194. * 获取业务名称
  195. * @param string $tableName
  196. * @return mixed
  197. */
  198. public static function get_big_business(string $tableName)
  199. {
  200. $start = strrpos($tableName,'_');
  201. $result = substr($tableName, $start + 1);
  202. return static::camel($result);
  203. }
  204. /**
  205. * 只替换一次字符串
  206. * @param $needle
  207. * @param $replace
  208. * @param $haystack
  209. * @return array|mixed|string|string[]
  210. */
  211. public static function str_replace_once($needle, $replace, $haystack)
  212. {
  213. $pos = strpos($haystack, $needle);
  214. if ($pos === false) {
  215. return $haystack;
  216. }
  217. return substr_replace($haystack, $replace, $pos, strlen($needle));
  218. }
  219. /**
  220. * 遍历目录
  221. * @param $template_name
  222. * @return array
  223. */
  224. public static function get_dir($template_name)
  225. {
  226. $dir = base_path($template_name);
  227. $fileDir = [];
  228. if (is_dir($dir)){
  229. if ($dh = opendir($dir)){
  230. while (($file = readdir($dh)) !== false){
  231. if($file != "." && $file != ".."){
  232. array_push($fileDir, $file);
  233. }
  234. }
  235. closedir($dh);
  236. }
  237. }
  238. return $fileDir;
  239. }
  240. }