helpers.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. use Dcat\Admin\Admin;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\MessageBag;
  8. use Symfony\Component\HttpFoundation\Response;
  9. if (! function_exists('admin_setting')) {
  10. /**
  11. * 获取或保存配置参数.
  12. *
  13. * @param string|array $key
  14. * @param mixed $default
  15. * @return \Dcat\Admin\Support\Setting|mixed
  16. */
  17. function admin_setting($key = null, $default = null)
  18. {
  19. if ($key === null) {
  20. return app('admin.setting');
  21. }
  22. if (is_array($key)) {
  23. app('admin.setting')->save($key);
  24. return;
  25. }
  26. return app('admin.setting')->get($key, $default);
  27. }
  28. }
  29. if (! function_exists('admin_setting_array')) {
  30. /**
  31. * 获取配置参数并转化为数组格式.
  32. *
  33. * @param string $key
  34. * @param mixed $default
  35. * @return \Dcat\Admin\Support\Setting|mixed
  36. */
  37. function admin_setting_array(?string $key, $default = [])
  38. {
  39. return app('admin.setting')->getArray($key, $default);
  40. }
  41. }
  42. if (! function_exists('admin_extension_setting')) {
  43. /**
  44. * 获取扩展配置参数.
  45. *
  46. * @param string $extension
  47. * @param string|array $key
  48. * @param mixed $default
  49. * @return mixed
  50. */
  51. function admin_extension_setting($extension, $key = null, $default = null)
  52. {
  53. $extension = app($extension);
  54. if ($extension instanceof Dcat\Admin\Extend\ServiceProvider) {
  55. return $extension->config($key, $default);
  56. }
  57. }
  58. }
  59. if (! function_exists('admin_section')) {
  60. /**
  61. * Get the string contents of a section.
  62. *
  63. * @param string $section
  64. * @param mixed $default
  65. * @param array $options
  66. * @return mixed
  67. */
  68. function admin_section(string $section, $default = null, array $options = [])
  69. {
  70. return app('admin.sections')->yieldContent($section, $default, $options);
  71. }
  72. }
  73. if (! function_exists('admin_has_section')) {
  74. /**
  75. * Check if section exists.
  76. *
  77. * @param string $section
  78. * @return mixed
  79. */
  80. function admin_has_section(string $section)
  81. {
  82. return app('admin.sections')->hasSection($section);
  83. }
  84. }
  85. if (! function_exists('admin_inject_section')) {
  86. /**
  87. * Injecting content into a section.
  88. *
  89. * @param string $section
  90. * @param mixed $content
  91. * @param bool $append
  92. * @param int $priority
  93. */
  94. function admin_inject_section(string $section, $content = null, bool $append = true, int $priority = 10)
  95. {
  96. app('admin.sections')->inject($section, $content, $append, $priority);
  97. }
  98. }
  99. if (! function_exists('admin_inject_section_if')) {
  100. /**
  101. * Injecting content into a section.
  102. *
  103. * @param mixed $condition
  104. * @param string $section
  105. * @param mixed $content
  106. * @param bool $append
  107. * @param int $priority
  108. */
  109. function admin_inject_section_if($condition, $section, $content = null, bool $append = false, int $priority = 10)
  110. {
  111. if ($condition) {
  112. app('admin.sections')->inject($section, $content, $append, $priority);
  113. }
  114. }
  115. }
  116. if (! function_exists('admin_has_default_section')) {
  117. /**
  118. * Check if default section exists.
  119. *
  120. * @param string $section
  121. * @return mixed
  122. */
  123. function admin_has_default_section(string $section)
  124. {
  125. return app('admin.sections')->hasDefaultSection($section);
  126. }
  127. }
  128. if (! function_exists('admin_inject_default_section')) {
  129. /**
  130. * Injecting content into a section.
  131. *
  132. * @param string $section
  133. * @param string|Renderable|Htmlable|callable $content
  134. */
  135. function admin_inject_default_section(string $section, $content)
  136. {
  137. app('admin.sections')->injectDefault($section, $content);
  138. }
  139. }
  140. if (! function_exists('admin_trans_field')) {
  141. /**
  142. * Translate the field name.
  143. *
  144. * @param $field
  145. * @param null $locale
  146. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  147. */
  148. function admin_trans_field($field, $locale = null)
  149. {
  150. return app('admin.translator')->transField($field, $locale);
  151. }
  152. }
  153. if (! function_exists('admin_trans_label')) {
  154. /**
  155. * Translate the label.
  156. *
  157. * @param $label
  158. * @param array $replace
  159. * @param null $locale
  160. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  161. */
  162. function admin_trans_label($label = null, $replace = [], $locale = null)
  163. {
  164. return app('admin.translator')->transLabel($label, $replace, $locale);
  165. }
  166. }
  167. if (! function_exists('admin_trans_option')) {
  168. /**
  169. * Translate the field name.
  170. *
  171. * @param $field
  172. * @param array $replace
  173. * @param null $locale
  174. * @return array|\Illuminate\Contracts\Translation\Translator|null|string
  175. */
  176. function admin_trans_option($optionValue, $field, $replace = [], $locale = null)
  177. {
  178. $slug = admin_controller_slug();
  179. return admin_trans("{$slug}.options.{$field}.{$optionValue}", $replace, $locale);
  180. }
  181. }
  182. if (! function_exists('admin_trans')) {
  183. /**
  184. * Translate the given message.
  185. *
  186. * @param string $key
  187. * @param array $replace
  188. * @param string $locale
  189. * @return \Illuminate\Contracts\Translation\Translator|string|array|null
  190. */
  191. function admin_trans($key, $replace = [], $locale = null)
  192. {
  193. return app('admin.translator')->trans($key, $replace, $locale);
  194. }
  195. }
  196. if (! function_exists('admin_controller_slug')) {
  197. /**
  198. * @return string
  199. */
  200. function admin_controller_slug()
  201. {
  202. static $slug = [];
  203. $controller = admin_controller_name();
  204. return $slug[$controller] ?? ($slug[$controller] = Helper::slug($controller));
  205. }
  206. }
  207. if (! function_exists('admin_controller_name')) {
  208. /**
  209. * Get the class "basename" of the current controller.
  210. *
  211. * @return string
  212. */
  213. function admin_controller_name()
  214. {
  215. return Helper::getControllerName();
  216. }
  217. }
  218. if (! function_exists('admin_path')) {
  219. /**
  220. * Get admin path.
  221. *
  222. * @param string $path
  223. * @return string
  224. */
  225. function admin_path($path = '')
  226. {
  227. return ucfirst(config('admin.directory')).($path ? DIRECTORY_SEPARATOR.$path : $path);
  228. }
  229. }
  230. if (! function_exists('admin_url')) {
  231. /**
  232. * Get admin url.
  233. *
  234. * @param string $path
  235. * @param mixed $parameters
  236. * @param bool $secure
  237. * @return string
  238. */
  239. function admin_url($path = '', $parameters = [], $secure = null)
  240. {
  241. if (url()->isValidUrl($path)) {
  242. return $path;
  243. }
  244. $secure = $secure ?: (config('admin.https') || config('admin.secure'));
  245. return url(admin_base_path($path), $parameters, $secure);
  246. }
  247. }
  248. if (! function_exists('admin_base_path')) {
  249. /**
  250. * Get admin url.
  251. *
  252. * @param string $path
  253. * @return string
  254. */
  255. function admin_base_path($path = '')
  256. {
  257. $prefix = '/'.trim(config('admin.route.prefix'), '/');
  258. $prefix = ($prefix == '/') ? '' : $prefix;
  259. $path = trim($path, '/');
  260. if (is_null($path) || strlen($path) == 0) {
  261. return $prefix ?: '/';
  262. }
  263. return $prefix.'/'.$path;
  264. }
  265. }
  266. if (! function_exists('admin_toastr')) {
  267. /**
  268. * Flash a toastr message bag to session.
  269. *
  270. * @param string $message
  271. * @param string $type
  272. * @param array $options
  273. */
  274. function admin_toastr($message = '', $type = 'success', $options = [])
  275. {
  276. $toastr = new MessageBag(get_defined_vars());
  277. session()->flash('dcat-admin-toastr', $toastr);
  278. }
  279. }
  280. if (! function_exists('admin_success')) {
  281. /**
  282. * Flash a success message bag to session.
  283. *
  284. * @param string $title
  285. * @param string $message
  286. */
  287. function admin_success($title, $message = '')
  288. {
  289. admin_info($title, $message, 'success');
  290. }
  291. }
  292. if (! function_exists('admin_error')) {
  293. /**
  294. * Flash a error message bag to session.
  295. *
  296. * @param string $title
  297. * @param string $message
  298. */
  299. function admin_error($title, $message = '')
  300. {
  301. admin_info($title, $message, 'error');
  302. }
  303. }
  304. if (! function_exists('admin_warning')) {
  305. /**
  306. * Flash a warning message bag to session.
  307. *
  308. * @param string $title
  309. * @param string $message
  310. */
  311. function admin_warning($title, $message = '')
  312. {
  313. admin_info($title, $message, 'warning');
  314. }
  315. }
  316. if (! function_exists('admin_info')) {
  317. /**
  318. * Flash a message bag to session.
  319. *
  320. * @param string $title
  321. * @param string $message
  322. * @param string $type
  323. */
  324. function admin_info($title, $message = '', $type = 'info')
  325. {
  326. $message = new MessageBag(get_defined_vars());
  327. session()->flash($type, $message);
  328. }
  329. }
  330. if (! function_exists('admin_asset')) {
  331. /**
  332. * @param $path
  333. * @return string
  334. */
  335. function admin_asset($path)
  336. {
  337. return Admin::asset()->url($path);
  338. }
  339. }
  340. if (! function_exists('admin_route')) {
  341. /**
  342. * 根据路由别名获取url.
  343. *
  344. * @param string|null $route
  345. * @param array $params
  346. * @param bool $absolute
  347. * @return string
  348. */
  349. function admin_route(?string $route, array $params = [], $absolute = true)
  350. {
  351. return Admin::app()->getRoute($route, $params, $absolute);
  352. }
  353. }
  354. if (! function_exists('admin_route_name')) {
  355. /**
  356. * 获取路由别名.
  357. *
  358. * @param string|null $route
  359. * @return string
  360. */
  361. function admin_route_name(?string $route)
  362. {
  363. return Admin::app()->getRoutePrefix().$route;
  364. }
  365. }
  366. if (! function_exists('admin_api_route_name')) {
  367. /**
  368. * 获取api的路由别名.
  369. *
  370. * @param string $route
  371. * @return string
  372. */
  373. function admin_api_route_name(?string $route = '')
  374. {
  375. return Admin::app()->getCurrentApiRoutePrefix().$route;
  376. }
  377. }
  378. if (! function_exists('admin_extension_path')) {
  379. /**
  380. * @param string $path
  381. * @return string
  382. */
  383. function admin_extension_path(string $path = '')
  384. {
  385. $dir = rtrim(config('admin.extension.dir'), '/') ?: base_path('dcat-admin-extensions');
  386. $path = ltrim($path, '/');
  387. return $path ? $dir.'/'.$path : $dir;
  388. }
  389. }
  390. if (! function_exists('admin_color')) {
  391. /**
  392. * @param string|null $color
  393. * @return string|\Dcat\Admin\Color
  394. */
  395. function admin_color(?string $color = null)
  396. {
  397. if ($color === null) {
  398. return Admin::color();
  399. }
  400. return Admin::color()->get($color);
  401. }
  402. }
  403. if (! function_exists('admin_view')) {
  404. /**
  405. * @param string $view
  406. * @param array $data
  407. * @return string
  408. *
  409. * @throws \Throwable
  410. */
  411. function admin_view($view, array $data = [])
  412. {
  413. return Admin::view($view, $data);
  414. }
  415. }
  416. if (! function_exists('admin_script')) {
  417. /**
  418. * @param string $js
  419. * @param bool $direct
  420. * @return void
  421. */
  422. function admin_script($script, bool $direct = false)
  423. {
  424. Admin::script($script, $direct);
  425. }
  426. }
  427. if (! function_exists('admin_style')) {
  428. /**
  429. * @param string $style
  430. * @return void
  431. */
  432. function admin_style($style)
  433. {
  434. Admin::style($style);
  435. }
  436. }
  437. if (! function_exists('admin_js')) {
  438. /**
  439. * @param string|array $js
  440. * @return void
  441. */
  442. function admin_js($js)
  443. {
  444. Admin::js($js);
  445. }
  446. }
  447. if (! function_exists('admin_css')) {
  448. /**
  449. * @param string|array $css
  450. * @return void
  451. */
  452. function admin_css($css)
  453. {
  454. Admin::css($css);
  455. }
  456. }
  457. if (! function_exists('admin_require_assets')) {
  458. /**
  459. * @param string|array $asset
  460. * @return void
  461. */
  462. function admin_require_assets($asset)
  463. {
  464. Admin::requireAssets($asset);
  465. }
  466. }
  467. if (! function_exists('admin_javascript')) {
  468. /**
  469. * 暂存JS代码,并使用唯一字符串代替.
  470. *
  471. * @param string $scripts
  472. * @return string
  473. */
  474. function admin_javascript(string $scripts)
  475. {
  476. return Dcat\Admin\Support\JavaScript::make($scripts);
  477. }
  478. }
  479. if (! function_exists('admin_javascript_json')) {
  480. /**
  481. * @param array|object $data
  482. * @return string
  483. */
  484. function admin_javascript_json($data)
  485. {
  486. return Dcat\Admin\Support\JavaScript::format($data);
  487. }
  488. }
  489. if (! function_exists('admin_exit')) {
  490. /**
  491. * 响应数据并中断后续逻辑.
  492. *
  493. * @param Response|string|array $response
  494. *
  495. * @throws \Illuminate\Http\Exceptions\HttpResponseException
  496. */
  497. function admin_exit($response = '')
  498. {
  499. Admin::exit($response);
  500. }
  501. }
  502. if (! function_exists('admin_redirect')) {
  503. /**
  504. * 跳转.
  505. *
  506. * @param string $to
  507. * @param int $statusCode
  508. * @param Request $request
  509. * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector
  510. */
  511. function admin_redirect($to, int $statusCode = 302, Request $request = null)
  512. {
  513. return Helper::redirect($to, $statusCode, $request);
  514. }
  515. }
  516. if (! function_exists('format_byte')) {
  517. /**
  518. * 文件单位换算.
  519. *
  520. * @param $input
  521. * @param int $dec
  522. * @return string
  523. */
  524. function format_byte($input, $dec = 0)
  525. {
  526. $prefix_arr = ['B', 'KB', 'MB', 'GB', 'TB'];
  527. $value = round($input, $dec);
  528. $i = 0;
  529. while ($value > 1024) {
  530. $value /= 1024;
  531. $i++;
  532. }
  533. return round($value, $dec).$prefix_arr[$i];
  534. }
  535. }