Skip to content

Commit

Permalink
更新:废弃插件启停功能,优化程序性能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Jan 14, 2023
1 parent c6e27c7 commit e0821d8
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 117 deletions.
31 changes: 25 additions & 6 deletions app/CodeFec/Plugins.php
Expand Up @@ -15,7 +15,7 @@

class Plugins
{
public static function GetAll(): array
public function GetAll(): array
{
$arr = getPath(plugin_path());
$plugin_arr = [];
Expand Down Expand Up @@ -45,21 +45,20 @@ public static function GetAll(): array
* @param string $dirName 插件目录名
* @return ?string
*/
public function getLogo(string $dirName): ?string
public function has_logo(string $dirName): ?bool
{
// 插件logo
$file = plugin_path($dirName . '/' . $dirName . '.png');
if (file_exists($file)) {
$image_info = getimagesize($file);
$image_data = fread(fopen($file, 'rb'), filesize($file));
return 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
return true;
}
return null;
return false;
}

// 获取已启用的插件列表
public function getEnPlugins()
{
return $this->get_all();
$plugins = ['Core', 'Mail', 'User', 'Topic', 'Comment', 'Search'];
if (! file_exists(BASE_PATH . '/app/CodeFec/storage/install.lock')) {
return $plugins;
Expand All @@ -76,4 +75,24 @@ public function getEnPlugins()
}
return array_values(array_unique(cache()->get('plugins.en')));
}

public function get_default()
{
return ['Core', 'Mail', 'User', 'Topic', 'Comment', 'Search'];
}

public function get_all()
{
$result = [];
foreach ($this->GetAll() as $key => $plugin) {
$result[] = $key;
}
$result = array_merge($result, $this->get_default());
return array_values(array_unique($result));
}

public function get_all_data()
{
return $this->GetAll();
}
}
7 changes: 7 additions & 0 deletions app/Controller/ApiController.php
Expand Up @@ -182,6 +182,13 @@ public function AdminPluginRemove(): array
if (! admin_auth()->check()) {
return Json_Api(419, false, ['msg' => '无权限']);
}
$default_path = [];
foreach(\plugins()->get_all() as $plugin){
$default_path[]=plugin_path($plugin);
}
if(in_array(request()->input('path'),$default_path)){
return Json_Api(401, false, ['msg' => '禁止卸载默认插件']);
}
if (request()->input('path') && is_dir(request()->input('path'))) {
\Swoole\Coroutine\System::exec('rm -rf ' . request()->input('path'));
System::exec('php CodeFec ClearCache');
Expand Down
32 changes: 29 additions & 3 deletions app/Controller/PluginsController.php
Expand Up @@ -8,6 +8,7 @@
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/

namespace App\Controller;

use Alchemy\Zippy\Zippy;
Expand All @@ -16,6 +17,8 @@
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Paginator\LengthAwarePaginator;
use Hyperf\Utils\Collection;
use Hyperf\Utils\Str;
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\System;
Expand All @@ -27,7 +30,7 @@ class PluginsController
#[GetMapping(path: '')]
public function index(): ResponseInterface
{
return view('admin.plugins.index');
return view('admin.plugins.index', ['page' => $this->page()]);
}

// 上传插件
Expand All @@ -38,6 +41,17 @@ public function upload()
return view('admin.plugins.upload');
}

#[GetMapping(path: "logo")]
public function logo()
{
$plugin = request()->input('plugin');
if (!file_exists(BASE_PATH . "/app/Plugins/" . $plugin . "/" . $plugin . ".png")) {
return admin_abort('图片不存在', 404);
}
$file = fread(fopen(BASE_PATH . "/app/Plugins/" . $plugin . "/" . $plugin . ".png", 'rb'), filesize(BASE_PATH . "/app/Plugins/" . $plugin . "/" . $plugin . ".png"));
return response()->raw($file);
}

// 上传插件

#[PostMapping(path: 'upload')]
Expand All @@ -58,7 +72,7 @@ public function upload_submit(PluginUpload $request)
$archiveTar = $zippy->open(plugin_path($getClientFilename));

// 解压
if (! is_dir(plugin_path($filename))) {
if (!is_dir(plugin_path($filename))) {
mkdir(plugin_path($filename), 0777);
}

Expand All @@ -69,7 +83,7 @@ public function upload_submit(PluginUpload $request)
foreach ($allDir as $value) {
if (file_exists($value . '/.dirName')) {
$dirname = file_get_contents($value . '/.dirName');
if (! $dirname) {
if (!$dirname) {
$this->removeFiles(plugin_path($getClientFilename), plugin_path($filename));
return redirect()->with('danger', '.dirName文件为空')->url('/admin/plugins/upload')->go();
}
Expand All @@ -89,4 +103,16 @@ public function removeFiles(...$values): void
\Swoole\Coroutine\System::exec('rm -rf "' . $value . '"');
}
}

private function page()
{
$currentPage = (int)request()->input('page', 1);
$perPage = (int)request()->input('per_page', 15);

// 这里根据 $currentPage 和 $perPage 进行数据查询,以下使用 Collection 代替
$collection = new Collection(plugins()->get_all_data());

$data = array_values($collection->forPage($currentPage, $perPage)->toArray());
return new LengthAwarePaginator($data, count(plugins()->get_all_data()), $perPage, $currentPage);
}
}
4 changes: 2 additions & 2 deletions config/autoload/middlewares.php
Expand Up @@ -22,10 +22,10 @@
\Hyperf\Session\Middleware\SessionMiddleware::class,
\Hyperf\Validation\Middleware\ValidationMiddleware::class,
CsrfMiddleware::class,
RouteRefuseMiddleware::class,
// RouteRefuseMiddleware::class,
\App\Middleware\Translator::class,
\App\Middleware\InstallMiddleware::class,
\App\Middleware\RewriteMiddleware::class,
// \App\Middleware\RewriteMiddleware::class,
\App\Plugins\User\src\Middleware\AuthMiddleware::class,
\App\Middleware\AfterMiddleware::class,
],
Expand Down
51 changes: 22 additions & 29 deletions public/js/app.js
Expand Up @@ -2200,40 +2200,33 @@ if (document.getElementById("vue-plugin-table")) {
},
methods: {
remove: function remove(name, path) {
if (this.switchs.indexOf(name) !== -1) {
sweetalert__WEBPACK_IMPORTED_MODULE_2___default()({
icon: "warning",
title: "安全起见,卸载插件前请先禁用插件"
});
} else {
axios__WEBPACK_IMPORTED_MODULE_1___default().post("/api/AdminPluginRemove", {
path: path,
_token: csrf_token
}).then(function (response) {
var data = response.data;
axios__WEBPACK_IMPORTED_MODULE_1___default().post("/api/AdminPluginRemove", {
path: path,
_token: csrf_token
}).then(function (response) {
var data = response.data;

if (data.success === true) {
sweetalert__WEBPACK_IMPORTED_MODULE_2___default()({
title: data.result.msg,
icon: "success"
});
setTimeout(function () {
location.reload();
}, 1200);
} else {
sweetalert__WEBPACK_IMPORTED_MODULE_2___default()({
title: data.result.msg,
icon: "error"
});
}
})["catch"](function (error) {
if (data.success === true) {
sweetalert__WEBPACK_IMPORTED_MODULE_2___default()({
title: "请求出错,详细查看控制台",
title: data.result.msg,
icon: "success"
});
setTimeout(function () {
location.reload();
}, 1200);
} else {
sweetalert__WEBPACK_IMPORTED_MODULE_2___default()({
title: data.result.msg,
icon: "error"
});
console.log(error);
}
})["catch"](function (error) {
sweetalert__WEBPACK_IMPORTED_MODULE_2___default()({
title: "请求出错,详细查看控制台",
icon: "error"
});
}
console.log(error);
});
},
// 迁移所有资源
migrateAll: function migrateAll() {
Expand Down
2 changes: 1 addition & 1 deletion public/mix-manifest.json
@@ -1,5 +1,5 @@
{
"/js/app.js": "/js/app.js?id=feb418431098fec9f264",
"/js/app.js": "/js/app.js?id=d1474c67549645021bc3",
"/js/install.js": "/js/install.js?id=e2a427dad25f612dc8fb",
"/js/vue.js": "/js/vue.js?id=c0ed491e9d9bbd0212ac",
"/js/admin/login.js": "/js/admin/login.js?id=da1c592d350b030d748d",
Expand Down
57 changes: 25 additions & 32 deletions resources/js/app.js
Expand Up @@ -114,42 +114,35 @@ if (document.getElementById("vue-plugin-table")) {
},
methods: {
remove(name, path) {
if (this.switchs.indexOf(name) !== -1) {
swal({
icon: "warning",
title: "安全起见,卸载插件前请先禁用插件",
});
} else {
axios
.post("/api/AdminPluginRemove", {
path: path,
_token: csrf_token
})
.then(function (response) {
var data = response.data;
if (data.success === true) {
swal({
title: data.result.msg,
icon: "success",
});
setTimeout(() => {
location.reload();
}, 1200);
} else {
swal({
title: data.result.msg,
icon: "error",
});
}
})
.catch(function (error) {
axios
.post("/api/AdminPluginRemove", {
path: path,
_token: csrf_token
})
.then(function (response) {
var data = response.data;
if (data.success === true) {
swal({
title: "请求出错,详细查看控制台",
title: data.result.msg,
icon: "success",
});
setTimeout(() => {
location.reload();
}, 1200);
} else {
swal({
title: data.result.msg,
icon: "error",
});
console.log(error);
}
})
.catch(function (error) {
swal({
title: "请求出错,详细查看控制台",
icon: "error",
});
}
console.log(error);
});
},
// 迁移所有资源
migrateAll() {
Expand Down

0 comments on commit e0821d8

Please sign in to comment.