Skip to content

Commit

Permalink
Merge pull request #6 from webman-tech/5-支持二级目录访问
Browse files Browse the repository at this point in the history
5 支持二级目录访问
  • Loading branch information
krissss committed Jan 10, 2024
2 parents aa1031c + 7607153 commit cf5713b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 13 deletions.
40 changes: 38 additions & 2 deletions src/Amis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WebmanTech\AmisAdmin;

use Webman\Http\Request;
use WebmanTech\AmisAdmin\Helper\ArrayHelper;
use WebmanTech\AmisAdmin\Helper\ConfigHelper;
use support\view\Raw;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function renderApp(array $schema = [])
$defaultData = [
'view' => 'amis-app',
'view_path' => '../vendor/webman-tech/amis-admin/src', // 相对 app 目录
'assets' => ConfigHelper::get('assets', []),
'assets' => $this->getAssets(),
];
$appData = ConfigHelper::get('app', []);
if (isset($appData['amisJSON']) && is_callable($appData['amisJSON'])) {
Expand Down Expand Up @@ -74,7 +75,7 @@ public function renderPage(string $title, array $schema = [])
$defaultData = [
'view' => 'amis-page',
'view_path' => '../vendor/webman-tech/amis-admin/src', // 相对 app 目录
'assets' => ConfigHelper::get('assets', []),
'assets' => $this->getAssets(),
];
$pageData = ConfigHelper::get('page', []);
if (isset($appData['amisJSON']) && is_callable($appData['amisJSON'])) {
Expand All @@ -97,4 +98,39 @@ public function renderPage(string $title, array $schema = [])

return Raw::render($data['view'], $data, $app);
}

/**
* 获取请求接口的路劲
* @param Request $request
* @return string
*/
public function getRequestPath(Request $request): string
{
if ($requestPathGetter = ConfigHelper::get('request_path_getter')) {
return $requestPathGetter($request);
}

return $request->path();
}

private function getAssets(): array
{
$assets = ConfigHelper::get('assets', []);

$assets['js'] = $assets['js'] ?? [];
if (is_callable($assets['js'])) {
$assets['js'] = call_user_func($assets['js']);
}
$assets['js'] = array_map(function ($item) {
if (is_string($item)) {
$item = ['type' => 'js', 'content' => $item];
}
if (!is_array($item) && !isset($item['type'], $item['content'])) {
throw new \InvalidArgumentException('js 配置错误');
}
return $item;
}, $assets['js']);

return $assets;
}
}
2 changes: 1 addition & 1 deletion src/Controller/AmisSourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function amisPage(Request $request): Amis\Page
*/
protected function amisCrud(Request $request): Amis\Crud
{
$routePrefix = $request->path();
$routePrefix = amis()->getRequestPath($request);

$crud = Amis\Crud::make()
->config($this->crudConfig())
Expand Down
5 changes: 2 additions & 3 deletions src/Controller/RenderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
use WebmanTech\AmisAdmin\Amis;
use WebmanTech\AmisAdmin\Helper\ArrayHelper;
use WebmanTech\AmisAdmin\Helper\ConfigHelper;
use support\Container;

class RenderController
{
public function app()
{
return Container::get(Amis::class)->renderApp();
return amis()->renderApp();
}

public function login()
Expand Down Expand Up @@ -87,6 +86,6 @@ public function login()
}
$schema = ArrayHelper::merge($schema, $data['schema']);

return Container::get(Amis::class)->renderPage($data['title'], $schema);
return amis()->renderPage($data['title'], $schema);
}
}
17 changes: 14 additions & 3 deletions src/config/plugin/webman-tech/amis-admin/amis.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
'js' => [
$amisAssetBaseUrl . 'sdk.js',
'https://unpkg.com/history@4.10.1/umd/history.js', // 使用 app 必须
// 可以添加复杂的 script 脚本
/*[
'type' => 'script',
'content' => <<<JS
window.xxx = xxx;
JS,
]*/
],
/**
* 切换主题
Expand Down Expand Up @@ -59,7 +66,7 @@
'amisJSON' => [
'brandName' => config('app.name', 'App Admin'),
'logo' => '/favicon.ico',
'api' => '/admin/pages', // 修改成获取菜单的路由
'api' => route('admin.pages'), // 修改成获取菜单的路由
],
'title' => config('app.name'),
],
Expand All @@ -79,8 +86,8 @@
'page_login' => function() {
return [
//'background' => '#eee', // 可以使用图片, 'url(http://xxxx)'
'login_api' => '/admin/auth/login',
'success_redirect' => '/admin',
'login_api' => route('admin.login'),
'success_redirect' => route('admin'),
];
},
/**
Expand All @@ -101,4 +108,8 @@
*/
'validator' => fn() => new \WebmanTech\AmisAdmin\Validator\NullValidator(),
//'validator' => fn() => new \WebmanTech\AmisAdmin\Validator\LaravelValidator(\support\Container::get(\Illuminate\Contracts\Validation\Factory::class)),
/**
* 用于获取当前请求的路径,当部署到二级目录时有用
*/
'request_path_getter' => null,
];
12 changes: 11 additions & 1 deletion src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
use support\Container;
use Webman\Http\Response;

if (!function_exists('amis')) {
/**
* @return Amis
*/
function amis(): Amis
{
return Container::get(Amis::class);
}
}

if (!function_exists('amis_response')) {
/**
* @param array $data
Expand All @@ -13,6 +23,6 @@
*/
function amis_response(array $data, string $msg = '', array $extra = [])
{
return Container::get(Amis::class)->response($data, $msg, $extra);
return amis()->response($data, $msg, $extra);
}
}
10 changes: 7 additions & 3 deletions src/view/_amis-basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@
</head>
<body>
<div id="root" class="app-wrapper"></div>
<?php foreach ($assets['js'] as $js): ?>
<script src="<?= $js ?>"></script>
<?php foreach ($assets['js'] as $item): ?>
<?php if($item['type'] === 'js'): ?>
<script src="<?= $item['content'] ?>"></script>
<?php elseif ($item['type'] === 'script'): ?>
<script><?= $item['content'] ?></script>
<?php endif; ?>
<?php endforeach; ?>
<script type="text/javascript">
(function () {
<?= $script ?? '' ?>
<?= $script ?? '' ?>

const amis = amisRequire('amis/embed');

Expand Down

0 comments on commit cf5713b

Please sign in to comment.