作者 邓超

自定义页面

<?php
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Models\BCustom;
/**
* 自定义 页面
* @author:dc
* @time 2023/5/9 10:08
* Class CustomController
* @package App\Http\Controllers\Bside
*/
class CustomController extends BaseController
{
/**
* 验证规则
* @var array[]
*/
private $verify = [
'role' => [
'name' => ['required','max:100'],
'title' => ['required','max:200'],
'keywords' => ['required','max:200'],
'description' => ['required','max:250'],
'html' => ['required'],
'url' => ['required','max:200'],
'status' => ['required','in:0,1'],
],
'message' => [
'name.required' => '名称必须',
'name.max' => '名称不能超过100个字符',
'title.required' => '网页标题必须',
'title.max' => '网页标题不能超过200个字符',
'keywords.required' => '网页关键字必须',
'keywords.max' => '网页关键字不能超过200个字符',
'description.required' => '网页描述必须',
'description.max' => '网页描述不能超过250个字符',
'url.required' => '链接必须',
'url.max' => '链接不能超过200个字符',
'status.required' => '状态选择错误',
'status.in' => '状态必须是显示/隐藏'
],
'attr' => [
]
];
/**
* 列表数据
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @author:dc
* @time 2023/5/8 16:37
*/
public function index(){
$lists = BCustom::_all($this->user['project_id'])->toArray();
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']);
// 保存
$id = BCustom::_save($this->user['project_id'],$data,$data['id']??0);
if($id===-1){
return $this->response('数据不存在','B_CUSTOM_NOTFOUND');
}
return $this->success(BCustom::_find($this->user['project_id'],$id,true));
}
/**
* 删除数据
* @return \Illuminate\Http\JsonResponse
* @author:dc
* @time 2023/5/9 9:20
*/
public function delete(){
$id = $this->param['id']??0;
$data = BCustom::_find($this->user['project_id'],$id);
if(empty($data)){
return $this->response('数据不存在','B_CUSTOM_NOTFOUND');
}
if($data->delete()){
return $this->response('删除成功',Code::SUCCESS);
}
}
}
... ...
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* b端控制, c端显示的自定义页面
* @author:dc
* @time 2023/5/8 16:14
* Class BCustom
* @package App\Models
*/
class BCustom extends Base
{
protected $table = 'gl_bside_template_custom';
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->name = $data['name'];
$model->title = $data['title'];
$model->keywords = $data['keywords'];
$model->description = $data['description'];
$model->url = $data['url'];
$model->status = $data['status'];
$model->html = $data['html'];
$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)
{
return static::where(function ($query) use ($project_id){
// 那个公司
$query->where('project_id',$project_id);
})
->get(['id','name','title','status','url','keywords','description','created_at','updated_at']);
}
/**
* 查询一条数据
* @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();
}
/**
* 是否有下级
* @param int $id
* @param int $project_id
* @return mixed
* @author:dc
* @time 2023/5/9 9:23
*/
public static function isChild(int $id,int $project_id=0)
{
return static::where(['pid'=>$id,'project_id'=>$project_id])->limit(1)->count();
}
}
... ...
<?php
namespace App\Models\Template;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 自定义 页面
* @author:dc
* @time 2023/5/4 17:18
* Class BCustom
* @package App\Models\Template
*/
class BCustom extends \App\Models\Base{
protected $table = 'gl_bside_template_custom';
use SoftDeletes;
const STATUS_ACTIVE = 1;
const STATUS_DISABLED = 0;
/**
* 读取列表
* @param $project_id
* @return mixed
* @author:dc
* @time 2023/5/4 17:22
*/
public static function _all($project_id){
return static::where([
'project_id' => $project_id
])->paginate(20);
}
}
... ... @@ -162,15 +162,23 @@ Route::middleware(['bloginauth'])->group(function () {
// 自定义页面
// 模板
Route::prefix('template')->group(function () {
Route::get('/', [\App\Http\Controllers\Bside\TemplateController::class, 'index'])->name('template_header_footer');
Route::get('/edit', [\App\Http\Controllers\Bside\TemplateController::class, 'edit_html'])->name('template_header_footer_edit');
Route::post('/edit', [\App\Http\Controllers\Bside\TemplateController::class, 'edit_save'])->name('template_header_footer_edit_save');
Route::get('/system', [\App\Http\Controllers\Bside\TemplateController::class, 'system_all_html'])->name('template_header_footer_system');
Route::get('/custom', [\App\Http\Controllers\Bside\TemplateController::class, 'custom'])->name('template_custom');
});
// 自定义页面,专题页
Route::prefix('custom')->group(function () {
Route::get('/', [\App\Http\Controllers\Bside\CustomController::class, 'index'])->name('bside_custom');
Route::post('/create', [\App\Http\Controllers\Bside\CustomController::class, 'create'])->name('bside_custom_create');
Route::post('/update', [\App\Http\Controllers\Bside\CustomController::class, 'update'])->name('bside_custom_update');
Route::delete('/delete', [\App\Http\Controllers\Bside\CustomController::class, 'delete'])->name('bside_custom_delete');
});
// 导航栏编辑
Route::prefix('nav')->group(function () {
Route::get('/', [\App\Http\Controllers\Bside\NavController::class, 'index'])->name('bside_nav');
... ...