functions.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. use Webman\Route;
  8. use support\Response;
  9. use Tinywan\Jwt\JwtToken;
  10. use plugin\saiadmin\exception\ApiException;
  11. use plugin\saiadmin\app\cache\ConfigCache;
  12. use plugin\saiadmin\app\cache\DictCache;
  13. if (!function_exists('getCurrentInfo')) {
  14. /**
  15. * 获取当前登录用户
  16. */
  17. function getCurrentInfo(): bool|array
  18. {
  19. if (!request()) {
  20. return false;
  21. }
  22. try {
  23. $token = JwtToken::getExtend();
  24. } catch (\Throwable $e) {
  25. return false;
  26. }
  27. return $token;
  28. }
  29. }
  30. if (!function_exists('fastRoute')) {
  31. /**
  32. * 快速注册路由[index|save|update|read|destroy|import|export]
  33. * @param string $name
  34. * @param string $controller
  35. * @return void
  36. */
  37. function fastRoute(string $name, string $controller): void
  38. {
  39. $name = trim($name, '/');
  40. if (method_exists($controller, 'index'))
  41. Route::get("/$name/index", [$controller, 'index']);
  42. if (method_exists($controller, 'save'))
  43. Route::post("/$name/save", [$controller, 'save']);
  44. if (method_exists($controller, 'update'))
  45. Route::put("/$name/update", [$controller, 'update']);
  46. if (method_exists($controller, 'read'))
  47. Route::get("/$name/read", [$controller, 'read']);
  48. if (method_exists($controller, 'destroy'))
  49. Route::delete("/$name/destroy", [$controller, 'destroy']);
  50. if (method_exists($controller, 'import'))
  51. Route::post("/$name/import", [$controller, 'import']);
  52. if (method_exists($controller, 'export'))
  53. Route::post("/$name/export", [$controller, 'export']);
  54. }
  55. }
  56. if (!function_exists('downloadFile')) {
  57. /**
  58. * 下载模板
  59. * @param $file_name
  60. * @return Response
  61. */
  62. function downloadFile($file_name): Response
  63. {
  64. $base_dir = config('plugin.saiadmin.saithink.template', base_path() . '/public/template');
  65. if (file_exists($base_dir . DIRECTORY_SEPARATOR . $file_name)) {
  66. return response()->download($base_dir . DIRECTORY_SEPARATOR . $file_name, urlencode($file_name));
  67. } else {
  68. throw new ApiException('模板不存在');
  69. }
  70. }
  71. }
  72. if (!function_exists('formatBytes')) {
  73. /**
  74. * 根据字节计算大小
  75. * @param $bytes
  76. * @return string
  77. */
  78. function formatBytes($bytes): string
  79. {
  80. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  81. for ($i = 0; $bytes > 1024; $i++) {
  82. $bytes /= 1024;
  83. }
  84. return round($bytes, 2) . ' ' . $units[$i];
  85. }
  86. }
  87. if (!function_exists('getConfigGroup')) {
  88. /**
  89. * 读取配置组
  90. * @param $group
  91. * @return array
  92. */
  93. function getConfigGroup($group): array
  94. {
  95. return ConfigCache::getConfig($group);
  96. }
  97. }
  98. if (!function_exists('dictDataList')) {
  99. /**
  100. * 根据字典编码获取字典列表
  101. * @param string $code 字典编码
  102. * @return array
  103. */
  104. function dictDataList(string $code): array
  105. {
  106. return DictCache::getDict($code);
  107. }
  108. }