作者 邓超

导航栏 nav

... ... @@ -88,3 +88,71 @@ if(!function_exists('_get_child')){
return $new_arr ? $new_arr : false;
}
}
/**
* 把返回的数据集转换成Tree
* @param $list array 数据列表
* @param string|int $pk 主键|root
* @param string $pid 父id
* @param string $child 子键
* @param int $root 获取哪个id下面
* @param bool $empty_child 当子数据不存在,是否要返回空子数据
* @return array
*/
function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0,$empty_child=true) {
// 如果是数字,则是root
if(is_numeric($pk)){
$root = $pk;
$pk = 'id';
}
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
if($empty_child){
$list[$key][$child] = [];
}
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$refer[$parentId][$child][] = & $list[$key];
}
}
}
}
return $tree;
}
/**
* tree数据转list
* @param $tree
* @param string $child
* @return array
* @author:dc
* @time 2022/1/11 10:13
*/
function tree_to_list($tree, $child='_child'){
$lists = [];
foreach ($tree as $item){
$c = $item[$child]??[];
unset($item[$child]);
$lists[] = $item;
if ($c){
$lists = array_merge($lists,tree_to_list($c, $child));
}
}
return $lists;
}
... ...
<?php
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Models\BNav;
/**
* 导航栏目 b端编辑 c端显示
* @author:dc
* @time 2023/5/8 16:31
* Class NavController
* @package App\Http\Controllers\Bside
*/
class NavController extends BaseController
{
/**
* 验证规则
* @var array[]
*/
private $verify = [
'role' => [
'pid' => ['required','integer','gte:0'],
'name' => ['required','max:100'],
'location' => ['required','in:header,footer'],
'url' => ['required','max:200'],
'status' => ['required','in:0,1'],
'target' => ['required','in:0,1'],
'sort' => ['required','integer','gte:0']
],
'message' => [
'pid.required' => '上级选择错误',
'pid.gte' => '上级选择错误',
'pid.integer' => '上级选择错误',
'name.required' => '名称必须',
'name.max' => '名称不能超过100个字符',
'location.required' => '位置选择错误',
'location.in' => '位置选择错误',
'url.required' => '链接必须',
'url.max' => '链接不能超过200个字符',
'status.required' => '状态选择错误',
'status.in' => '状态必须是显示/隐藏',
'target.required' => '打开方式必须',
'target.in' => '打开方式选择错误',
'sort.required' => '排序必须',
'sort.integer' => '排序必须是一个数字',
'sort.gte' => '排序必须大于等于0',
],
'attr' => [
]
];
/**
* 列表数据
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @author:dc
* @time 2023/5/8 16:37
*/
public function index(){
$isTree = $this->param['tree']??false;
$lists = BNav::_all($this->user['project_id'])->toArray();
if($isTree){
$lists = list_to_tree($lists);
}
return $this->success($lists);
}
/**
* 创建导航栏
* @author:dc
* @time 2023/5/8 16:39
*/
public function create(){
return $this->save();
}
/**
* 修改
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @author:dc
* @time 2023/5/8 17:06
*/
public function update(){
$this->verify['role']['id'] = ['required','integer','gt:0'];
$this->verify['message']['id.gt'] = $this->verify['message']['id.integer'] = $this->verify['message']['id.required'] = '编辑导航数据不存在';
return $this->save();
}
/**
* 新增修改
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @author:dc
* @time 2023/5/8 17:06
*/
private function save(){
$data = $this->validate(request() ,$this->verify['role'],$this->verify['message']);
if($data['pid']){
// 验证是否存在上级
$all = BNav::_all($this->user['project_id'],$data['location']);
if(!$all->where('id',$data['pid'])->count()){
return $this->response('上级栏目不存在','B_NAV_PID_NOTFOUND');
}
// 上级不允许是自己的下级
if(!empty($data['id'])){
$all = list_to_tree($all->toArray(),$data['id']);
$all = tree_to_list($all);
if(in_array($data['pid'],array_column($all,'id'))){
return $this->response('上级栏目不允许为本身的下级','B_NAV_PID_IS_CHILD');
}
}
}
// 保存
$id = BNav::_save($this->user['project_id'],$data,$data['id']??0);
if($id===-1){
return $this->response('导航数据不存在','B_NAV_NOTFOUND');
}
return $this->success(BNav::_find($this->user['project_id'],$id,true));
}
public function delete(){
}
}
... ...
... ... @@ -7,6 +7,7 @@ use App\Exceptions\BsideGlobalException;
use App\Models\Template\AHeadFoot;
use App\Models\Template\BCustom;
use App\Models\Template\BHeadFoot;
use App\Models\Template\BTemplate;
use Illuminate\Support\Facades\DB;
/**
... ... @@ -30,16 +31,13 @@ class TemplateController extends BaseController
*/
public function index(){
$data = BHeadFoot::_get($this->user['project_id']);
$data = BTemplate::_get($this->user['project_id']);
// todo::这里要进行html的替换
return $this->success([
'header' => $data[BHeadFoot::TYPE_HEADER]??'',
'footer' => $data[BHeadFoot::TYPE_FOOTER]??'',
]);
return $this->success($data);
}
... ...
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* b端控制, c端显示的导航
* @author:dc
* @time 2023/5/8 16:14
* Class BNav
* @package App\Models
*/
class BNav extends Base
{
protected $table = 'gl_bside_nav';
use SoftDeletes;
public $hidden = ['deleted_at','project_id'];
/**
* 显示
*/
const STATUS_ACTIVE = 1;
/**
* 隐藏
*/
const STATUS_DISABLED = 0;
/**
* 创建或者新增导航栏
* @param int $project_id
* @param array $data
* @param int $id
* @return int
* @author:dc
* @time 2023/5/8 16:24
*/
public static function _save(int $project_id, array $data, int $id = 0):int {
if($id){
$model = static::where('id',$id)->where('project_id', $project_id)->first();
if(!$model){
return -1;
}
}else{
$model = new static();
$model->project_id = $project_id;
}
$model->pid = $data['pid'];
$model->name = $data['name'];
$model->location = $data['location'];
$model->url = $data['url'];
$model->status = $data['status'];
$model->target = $data['target'];
$model->sort = $data['sort'];
$model->save();
return $model->id;
}
/**
* 删除
* @param int $project_id
* @param int $id
* @return mixed
* @author:dc
* @time 2023/5/8 16:27
*/
public static function _del(int $project_id, int $id){
return static::where(['project_id'=>$project_id,'id'=>$id])->delete();
}
/**
* 查询当前项目下的所有栏目信息
* @param int $project_id
* @return mixed
* @author:dc
* @time 2023/5/8 16:29
*/
public static function _all(int $project_id, string $location = null)
{
return static::where(function ($query) use ($project_id,$location){
// 那个公司
$query->where('project_id',$project_id);
// 显示位置
$location && $query->where('location',$location);
})
->orderBy('sort')
->get();
}
/**
* 查询一条数据
* @param int $project_id
* @param int $id
* @return mixed
* @author:dc
* @time 2023/5/8 17:04
*/
public static function _find(int $project_id, int $id, $array = false)
{
$data = static::where(['id'=>$id,'project_id'=>$project_id])->first();
if($data){
return $array ? $data->toArray() : $data;
}
return [];
}
/**
* 是否存在
* @param int $project_id
* @param int $id
* @return mixed
* @author:dc
* @time 2023/5/8 17:24
*/
public static function _check(int $project_id, int $id)
{
return static::where(['id'=>$id,'project_id'=>$project_id])->count();
}
}
... ...
... ... @@ -3,23 +3,17 @@
namespace App\Models\Template;
/**
* 头部底部
* 自定义 界面
* @author:dc
* @time 2023/5/4 15:52
* Class AHeadFoot
* @time 2023/5/8 13:52
* Class BTemplate
* @package App\Models\Template
*/
class BHeadFoot extends \App\Models\Base{
class BTemplate extends \App\Models\Base{
protected $table = 'gl_bside_template_header_footer';
protected $table = 'gl_bside_template_html';
const TYPE_HEADER = 'H';
const TYPE_FOOTER = 'F';
// const STATUS_ACTIVE = 1;
// const STATUS_DISABLED = 0;
/**
* @param $project_id
... ... @@ -28,12 +22,12 @@ class BHeadFoot extends \App\Models\Base{
* @time 2023/5/4 16:13
*/
public static function _get($project_id){
return static::where(['project_id'=>$project_id,'is_use'=>1])->get(['html','type'])->pluck('html','type')->toArray();
return static::where(['project_id'=>$project_id])->get(['html','type'])->pluck('html','type')->toArray();
}
public static function _all($project_id){
return static::where(['project_id'=>$project_id,'is_use'=>1])->get();
return static::where(['project_id'=>$project_id])->get();
}
... ... @@ -46,12 +40,11 @@ class BHeadFoot extends \App\Models\Base{
* @time 2023/5/4 17:50
*/
public static function _save($project_id,$type,$html){
$data = static::where(['project_id'=>$project_id,'is_use'=>1])->first();
$data = static::where(['project_id'=>$project_id,'type'=>$type])->first();
if(!$data){
$data = new static();
$data->project_id = $project_id;
$data->type = $type;
$data->is_use = 1;
}
$data->html = $html;
... ...
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);
... ...
... ... @@ -171,6 +171,14 @@ Route::middleware(['bloginauth'])->group(function () {
Route::get('/custom', [\App\Http\Controllers\Bside\TemplateController::class, 'custom'])->name('template_custom');
});
// 导航栏编辑
Route::prefix('nav')->group(function () {
Route::get('/', [\App\Http\Controllers\Bside\NavController::class, 'index'])->name('bside_nav');
Route::post('/create', [\App\Http\Controllers\Bside\NavController::class, 'create'])->name('bside_nav_create');
Route::post('/update', [\App\Http\Controllers\Bside\NavController::class, 'update'])->name('bside_nav_update');
Route::get('/delete', [\App\Http\Controllers\Bside\NavController::class, 'delete'])->name('bside_nav_delete');
});
... ...