functions.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. use Webman\Route;
  8. use support\Cache;
  9. use support\Response;
  10. use Tinywan\Jwt\JwtToken;
  11. use plugin\saiadmin\exception\ApiException;
  12. use plugin\saiadmin\app\model\system\SystemDictData;
  13. use plugin\saiadmin\app\logic\system\SystemConfigLogic;
  14. if (!function_exists('getCurrentInfo')) {
  15. /**
  16. * 获取当前登录用户
  17. */
  18. function getCurrentInfo(): bool|array
  19. {
  20. if (!request()) {
  21. return false;
  22. }
  23. try {
  24. $token = JwtToken::getExtend();
  25. } catch (\Throwable $e) {
  26. return false;
  27. }
  28. return $token;
  29. }
  30. }
  31. if (!function_exists('fastRoute')) {
  32. /**
  33. * 快速注册路由[index|save|update|read|changeStatus|destroy|import|export]
  34. * @param string $name
  35. * @param string $controller
  36. * @return void
  37. */
  38. function fastRoute(string $name, string $controller): void
  39. {
  40. $name = trim($name, '/');
  41. if (method_exists($controller, 'index')) Route::get("/$name/index", [$controller, 'index']);
  42. if (method_exists($controller, 'save')) Route::post("/$name/save", [$controller, 'save']);
  43. if (method_exists($controller, 'update')) Route::put("/$name/update", [$controller, 'update']);
  44. if (method_exists($controller, 'read')) Route::get("/$name/read", [$controller, 'read']);
  45. if (method_exists($controller, 'changeStatus')) Route::post("/$name/changeStatus", [$controller, 'changeStatus']);
  46. if (method_exists($controller, 'destroy')) Route::delete("/$name/destroy", [$controller, 'destroy']);
  47. if (method_exists($controller, 'import')) Route::post("/$name/import", [$controller, 'import']);
  48. if (method_exists($controller, 'export')) Route::post("/$name/export", [$controller, 'export']);
  49. }
  50. }
  51. if (!function_exists('downloadFile')) {
  52. /**
  53. * 下载模板
  54. * @param $file_name
  55. * @return Response
  56. */
  57. function downloadFile($file_name): Response
  58. {
  59. $base_dir = config('plugin.saiadmin.saithink.template',base_path().'/public/template');
  60. if (file_exists($base_dir. DIRECTORY_SEPARATOR.$file_name)) {
  61. return response()->download($base_dir. DIRECTORY_SEPARATOR.$file_name, urlencode($file_name));
  62. } else {
  63. throw new ApiException('模板不存在');
  64. }
  65. }
  66. }
  67. if (!function_exists('formatBytes')) {
  68. /**
  69. * 根据字节计算大小
  70. * @param $bytes
  71. * @return string
  72. */
  73. function formatBytes($bytes): string
  74. {
  75. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  76. for ($i = 0; $bytes > 1024; $i++) {
  77. $bytes /= 1024;
  78. }
  79. return round($bytes, 2) . ' ' . $units[$i];
  80. }
  81. }
  82. if (!function_exists('getConfigGroup')) {
  83. /**
  84. * 读取配置组
  85. * @param $group
  86. * @return mixed
  87. */
  88. function getConfigGroup($group): mixed
  89. {
  90. $logic = new SystemConfigLogic();
  91. return $logic->getGroup($group);
  92. }
  93. }
  94. if (!function_exists('dictDataList')) {
  95. /**
  96. * 根据字典编码获取字典列表
  97. * @param string $code 字典编码
  98. * @return array
  99. */
  100. function dictDataList(string $code): array
  101. {
  102. $data = Cache::get($code);
  103. if ($data) {
  104. return $data;
  105. }
  106. $model = new SystemDictData;
  107. $query = $model->where('status', 1)->where('code', $code)->field('id, label, value')->order('sort desc');
  108. $data = $query->select()->toArray();
  109. Cache::set($code, $data);
  110. return $data;
  111. }
  112. }
  113. if (!function_exists('dbSourceList')) {
  114. /**
  115. * 数据源列表
  116. * @return array
  117. */
  118. function dbSourceList(): array
  119. {
  120. $data = config('think-orm.connections');
  121. if (empty($data)) {
  122. $data = config('thinkorm.connections');
  123. }
  124. $list = [];
  125. foreach ($data as $k => $v) {
  126. $list[] = $k;
  127. }
  128. return $list;
  129. }
  130. }
  131. if (!function_exists('defaultDbSource')) {
  132. /**
  133. * 获取默认数据源
  134. * @return string
  135. */
  136. function defaultDbSource(): string
  137. {
  138. $config = config('think-orm');
  139. if (empty($config)) {
  140. $config = config('thinkorm');
  141. }
  142. return $config['default'] ?? 'mysql';
  143. }
  144. }
  145. if (!function_exists('dbSource')) {
  146. /**
  147. * 数据源
  148. * @return array
  149. */
  150. function dbSource(): array
  151. {
  152. $data = config('think-orm.connections');
  153. if (empty($data)) {
  154. $data = config('thinkorm.connections');
  155. }
  156. return $data;
  157. }
  158. }