115 lines
3.3 KiB
PHP
115 lines
3.3 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | saiadmin [ saiadmin快速开发框架 ]
|
|
// +----------------------------------------------------------------------
|
|
// | Author: sai <1430792918@qq.com>
|
|
// +----------------------------------------------------------------------
|
|
use Webman\Route;
|
|
use support\Response;
|
|
use Tinywan\Jwt\JwtToken;
|
|
use plugin\saiadmin\exception\ApiException;
|
|
use plugin\saiadmin\app\cache\ConfigCache;
|
|
use plugin\saiadmin\app\cache\DictCache;
|
|
|
|
if (!function_exists('getCurrentInfo')) {
|
|
/**
|
|
* 获取当前登录用户
|
|
*/
|
|
function getCurrentInfo(): bool|array
|
|
{
|
|
if (!request()) {
|
|
return false;
|
|
}
|
|
try {
|
|
$token = JwtToken::getExtend();
|
|
} catch (\Throwable $e) {
|
|
return false;
|
|
}
|
|
return $token;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('fastRoute')) {
|
|
/**
|
|
* 快速注册路由[index|save|update|read|destroy|import|export]
|
|
* @param string $name
|
|
* @param string $controller
|
|
* @return void
|
|
*/
|
|
function fastRoute(string $name, string $controller): void
|
|
{
|
|
$name = trim($name, '/');
|
|
if (method_exists($controller, 'index'))
|
|
Route::get("/$name/index", [$controller, 'index']);
|
|
if (method_exists($controller, 'save'))
|
|
Route::post("/$name/save", [$controller, 'save']);
|
|
if (method_exists($controller, 'update'))
|
|
Route::put("/$name/update", [$controller, 'update']);
|
|
if (method_exists($controller, 'read'))
|
|
Route::get("/$name/read", [$controller, 'read']);
|
|
if (method_exists($controller, 'destroy'))
|
|
Route::delete("/$name/destroy", [$controller, 'destroy']);
|
|
if (method_exists($controller, 'import'))
|
|
Route::post("/$name/import", [$controller, 'import']);
|
|
if (method_exists($controller, 'export'))
|
|
Route::post("/$name/export", [$controller, 'export']);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('downloadFile')) {
|
|
/**
|
|
* 下载模板
|
|
* @param $file_name
|
|
* @return Response
|
|
*/
|
|
function downloadFile($file_name): Response
|
|
{
|
|
$base_dir = config('plugin.saiadmin.saithink.template', base_path() . '/public/template');
|
|
if (file_exists($base_dir . DIRECTORY_SEPARATOR . $file_name)) {
|
|
return response()->download($base_dir . DIRECTORY_SEPARATOR . $file_name, urlencode($file_name));
|
|
} else {
|
|
throw new ApiException('Template not found');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('formatBytes')) {
|
|
/**
|
|
* 根据字节计算大小
|
|
* @param $bytes
|
|
* @return string
|
|
*/
|
|
function formatBytes($bytes): string
|
|
{
|
|
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
for ($i = 0; $bytes > 1024; $i++) {
|
|
$bytes /= 1024;
|
|
}
|
|
return round($bytes, 2) . ' ' . $units[$i];
|
|
}
|
|
}
|
|
|
|
if (!function_exists('getConfigGroup')) {
|
|
/**
|
|
* 读取配置组
|
|
* @param $group
|
|
* @return array
|
|
*/
|
|
function getConfigGroup($group): array
|
|
{
|
|
return ConfigCache::getConfig($group);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('dictDataList')) {
|
|
/**
|
|
* 根据字典编码获取字典列表
|
|
* @param string $code 字典编码
|
|
* @return array
|
|
*/
|
|
function dictDataList(string $code): array
|
|
{
|
|
return DictCache::getDict($code);
|
|
}
|
|
}
|