Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28d030e
commit 8686eb2
Showing
21 changed files
with
1,052 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
|
||
namespace App\Controller\Admin; | ||
|
||
use App\Controller\AbstractController; | ||
use App\Middleware\AdminMiddleware; | ||
use Hyperf\HttpServer\Annotation\Controller; | ||
use Hyperf\HttpServer\Annotation\GetMapping; | ||
use Hyperf\HttpServer\Annotation\Middleware; | ||
use Hyperf\HttpServer\Annotation\PostMapping; | ||
use Hyperf\Paginator\LengthAwarePaginator; | ||
use Hyperf\Utils\Collection; | ||
|
||
#[Controller(prefix: '/admin/setting/menu')] | ||
#[Middleware(AdminMiddleware::class)] | ||
class MenuController extends AbstractController | ||
{ | ||
#[GetMapping(path: '')] | ||
public function index() | ||
{ | ||
return view('admin.setting.menu.index', ['page' => $this->page()]); | ||
} | ||
|
||
#[GetMapping(path: 'create')] | ||
public function create() | ||
{ | ||
return view('admin.setting.menu.create'); | ||
} | ||
|
||
#[GetMapping(path: '{id}/edit')] | ||
public function edit($id) | ||
{ | ||
if (!in_array($id, _menu_keys())) { | ||
return admin_abort('菜单不存在', 403); | ||
} | ||
$data = _menu_get_data($id); | ||
if (arr_has($data, 'quanxian')) { | ||
unset($data['quanxian']); | ||
} | ||
return view('admin.setting.menu.edit', ['data' => $data]); | ||
} | ||
|
||
#[PostMapping(path: 'create')] | ||
public function store() | ||
{ | ||
$data = $this->request->input('data'); | ||
$data = json_decode($data, true); | ||
if (arr_has($data, 'parent_id') && data_get($data, 'parent_id') && !in_array($data['parent_id'], _menu_keys())) { | ||
return Json_Api(403, false, ['msg' => '上级id不存在']); | ||
} | ||
$data['sort'] = (int)$data['sort']; | ||
if (arr_has($data, 'parent_id') && data_get($data, 'parent_id')) { | ||
$data['parent_id'] = (int)$data['parent_id']; | ||
} else { | ||
unset($data['parent_id']); | ||
} | ||
if (arr_has($data, 'parent_id') && data_get($data, 'parent_id') && arr_has(_menu_get_data($data['parent_id']), 'parent_id')) { | ||
return Json_Api(403, false, ['msg' => '子菜单不能作为上级菜单使用']); | ||
} | ||
$prefix_name = config('cache.default.prefix') . 'menu'; | ||
redis()->hSetNx($prefix_name, (string)((int)max(_menu_keys()) + 1), _menu_instance()->serialize($data)); | ||
return Json_Api(200, true, ['msg' => '创建成功!']); | ||
} | ||
|
||
#[PostMapping(path: 'update')] | ||
public function update() | ||
{ | ||
$data = $this->request->input('data'); | ||
$data = json_decode($data, true); | ||
if (!in_array($this->request->input('id'), _menu_keys())) { | ||
return Json_Api(403, false, ['msg' => '被修改的菜单id不存在']); | ||
} | ||
if (arr_has($data, 'parent_id') && data_get($data, 'parent_id') && !in_array($data['parent_id'], _menu_keys())) { | ||
return Json_Api(403, false, ['msg' => '上级id不存在']); | ||
} | ||
$data['sort'] = (int)$data['sort']; | ||
if (arr_has($data, 'parent_id') && data_get($data, 'parent_id')) { | ||
$data['parent_id'] = (int)$data['parent_id']; | ||
} else { | ||
unset($data['parent_id']); | ||
} | ||
if (arr_has($data, 'parent_id') && data_get($data, 'parent_id') && arr_has(_menu_get_data($data['parent_id']), 'parent_id')) { | ||
return Json_Api(403, false, ['msg' => '子菜单不能作为上级菜单使用']); | ||
} | ||
$_data = _menu_get_data($this->request->input('id')); | ||
$data = array_merge($_data, $data); | ||
if (arr_has($data, 'quanxian')) { | ||
$data['quanxian'] = _menu_instance()->serialize($data['quanxian']); | ||
} | ||
$prefix_name = config('cache.default.prefix') . 'menu'; | ||
redis()->hDel($prefix_name, (string)$this->request->input('id')); | ||
redis()->hSetNx($prefix_name, (string)$this->request->input('id'), _menu_instance()->serialize($data)); | ||
return Json_Api(200, true, ['msg' => '修改成功!']); | ||
} | ||
|
||
#[PostMapping(path: "delete")] | ||
public function delete() | ||
{ | ||
$id = $this->request->input('id'); | ||
if (!in_array($this->request->input('id'), _menu_keys())) { | ||
return Json_Api(403, false, ['msg' => '被删除的菜单id不存在']); | ||
} | ||
if (arr_has(Itf()->get('menu'), $id)) { | ||
return Json_Api(403, false, ['msg' => '这是不可删除的菜单']); | ||
} | ||
$prefix_name = config('cache.default.prefix') . 'menu'; | ||
redis()->hDel($prefix_name, (string)$this->request->input('id')); | ||
return Json_Api(200, true, ['msg' => '删除成功!']); | ||
} | ||
|
||
private function page() | ||
{ | ||
//_menu() | ||
$currentPage = (int)request()->input('page', 1); | ||
$perPage = (int)request()->input('per_page', 15); | ||
|
||
// 这里根据 $currentPage 和 $perPage 进行数据查询,以下使用 Collection 代替 | ||
$collection = new Collection(_menu()); | ||
|
||
$data = array_values($collection->forPage($currentPage, $perPage)->toArray()); | ||
return new LengthAwarePaginator($data, count(_menu()), $perPage, $currentPage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
namespace App\Plugins\Core; | ||
|
||
use Hyperf\Utils\Arr; | ||
use Swoole\Coroutine\System; | ||
|
||
class Menu | ||
{ | ||
/** | ||
* handle. | ||
* @throws \RedisException | ||
* @return array | ||
*/ | ||
public function get() | ||
{ | ||
$menu = []; | ||
foreach ($this->db() as $id => $data) { | ||
$data['id'] = (int) $id; | ||
$menu[$id] = $data; | ||
} | ||
$keys = array_keys($menu); | ||
$news_key = array_column($menu, 'sort'); | ||
array_multisort($news_key, SORT_ASC, $menu, $keys); | ||
return array_combine($keys, $menu); | ||
} | ||
|
||
/** | ||
* get all keys. | ||
* @throws \RedisException | ||
* @return int[]|string[] | ||
*/ | ||
public function get_keys() | ||
{ | ||
return array_keys($this->get()); | ||
} | ||
|
||
/** | ||
* get menu data. | ||
* @param mixed $id | ||
* @throws \RedisException | ||
* @return null|mixed | ||
*/ | ||
public function get_data($id) | ||
{ | ||
$id = (int) $id; | ||
if (! in_array($id, $this->get_keys())) { | ||
return null; | ||
} | ||
return $this->get()[$id]; | ||
} | ||
|
||
/** | ||
* get all Itf menu. | ||
*/ | ||
private function Itf(): array | ||
{ | ||
$menu = []; | ||
foreach (Itf()->get('menu') as $k => $value) { | ||
$k = core_Itf_id('menu', $k); | ||
if (Arr::has($value, 'quanxian') && $value['quanxian'] instanceof \Closure) { | ||
$value['quanxian'] = $this->serialize($value['quanxian']); | ||
} | ||
$value['Itf'] = true; | ||
$menu[$k] = $value; | ||
} | ||
return $menu; | ||
} | ||
|
||
/** | ||
* get all database menu. | ||
* @throws \RedisException | ||
*/ | ||
private function db(): array | ||
{ | ||
$prefix_name = config('cache.default.prefix') . 'menu'; | ||
foreach ($this->Itf() as $id => $item) { | ||
if (! redis()->hExists($prefix_name, (string) $id)) { | ||
redis()->hSetNx($prefix_name, (string) $id, $this->serialize($item)); | ||
} | ||
} | ||
|
||
Itf()->_del('menu'); | ||
$all = redis()->hGetAll($prefix_name); | ||
$result = []; | ||
foreach ($all as $id => $data) { | ||
$_data = $this->unserialize($data); | ||
if (Arr::has($_data, 'quanxian')) { | ||
$_data['quanxian'] = $this->unserialize($_data['quanxian']); | ||
} | ||
if (! arr_has($_data, 'sort')) { | ||
$_data['sort'] = $id; | ||
} | ||
if (! arr_has($_data, 'hidden')) { | ||
$_data['hidden'] = false; | ||
} | ||
$result[(int) $id] = $_data; | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param $data | ||
* @return string | ||
*/ | ||
public function serialize($data): string | ||
{ | ||
return \Opis\Closure\serialize($data); | ||
} | ||
|
||
/** | ||
* @param $data | ||
* @return mixed | ||
*/ | ||
private function unserialize($data) | ||
{ | ||
return @\Opis\Closure\unserialize($data, ['allowed_classes' => true]); | ||
} | ||
|
||
|
||
public function backup() | ||
{ | ||
|
||
$menu_serialize = $this->serialize($this->get()); | ||
$menu = json_encode($this->get(),JSON_PRETTY_PRINT,JSON_UNESCAPED_UNICODE); | ||
if (! is_dir(BASE_PATH . '/runtime/backup')) { | ||
System::exec('cd ' . BASE_PATH . '/runtime' . '&& mkdir ' . 'backup'); | ||
} | ||
if (! is_dir(BASE_PATH . '/runtime/backup/menu')) { | ||
System::exec('cd ' . BASE_PATH . '/runtime/backup' . '&& mkdir ' . 'menu'); | ||
} | ||
file_put_contents(BASE_PATH."/runtime/backup/menu/menu_serialize.txt",$menu_serialize); | ||
file_put_contents(BASE_PATH."/runtime/backup/menu/menu.json",$menu); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.