作者 邓超

自定义页面

  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Bside;
  4 +
  5 +
  6 +use App\Enums\Common\Code;
  7 +use App\Models\BCustom;
  8 +
  9 +/**
  10 + * 自定义 页面
  11 + * @author:dc
  12 + * @time 2023/5/9 10:08
  13 + * Class CustomController
  14 + * @package App\Http\Controllers\Bside
  15 + */
  16 +class CustomController extends BaseController
  17 +{
  18 +
  19 + /**
  20 + * 验证规则
  21 + * @var array[]
  22 + */
  23 + private $verify = [
  24 + 'role' => [
  25 + 'name' => ['required','max:100'],
  26 + 'title' => ['required','max:200'],
  27 + 'keywords' => ['required','max:200'],
  28 + 'description' => ['required','max:250'],
  29 + 'html' => ['required'],
  30 + 'url' => ['required','max:200'],
  31 + 'status' => ['required','in:0,1'],
  32 + ],
  33 + 'message' => [
  34 + 'name.required' => '名称必须',
  35 + 'name.max' => '名称不能超过100个字符',
  36 + 'title.required' => '网页标题必须',
  37 + 'title.max' => '网页标题不能超过200个字符',
  38 + 'keywords.required' => '网页关键字必须',
  39 + 'keywords.max' => '网页关键字不能超过200个字符',
  40 + 'description.required' => '网页描述必须',
  41 + 'description.max' => '网页描述不能超过250个字符',
  42 +
  43 + 'url.required' => '链接必须',
  44 + 'url.max' => '链接不能超过200个字符',
  45 +
  46 + 'status.required' => '状态选择错误',
  47 + 'status.in' => '状态必须是显示/隐藏'
  48 + ],
  49 + 'attr' => [
  50 +
  51 + ]
  52 + ];
  53 +
  54 + /**
  55 + * 列表数据
  56 + * @throws \Psr\Container\ContainerExceptionInterface
  57 + * @throws \Psr\Container\NotFoundExceptionInterface
  58 + * @author:dc
  59 + * @time 2023/5/8 16:37
  60 + */
  61 + public function index(){
  62 +
  63 +
  64 + $lists = BCustom::_all($this->user['project_id'])->toArray();
  65 +
  66 +
  67 + return $this->success($lists);
  68 +
  69 + }
  70 +
  71 +
  72 +
  73 + /**
  74 + * 创建数据
  75 + * @author:dc
  76 + * @time 2023/5/8 16:39
  77 + */
  78 + public function create(){
  79 + return $this->save();
  80 + }
  81 +
  82 +
  83 + /**
  84 + * 修改
  85 + * @return \Illuminate\Http\JsonResponse
  86 + * @throws \Illuminate\Validation\ValidationException
  87 + * @throws \Psr\Container\ContainerExceptionInterface
  88 + * @throws \Psr\Container\NotFoundExceptionInterface
  89 + * @author:dc
  90 + * @time 2023/5/8 17:06
  91 + */
  92 + public function update(){
  93 + $this->verify['role']['id'] = ['required','integer','gt:0'];
  94 + $this->verify['message']['id.gt'] = $this->verify['message']['id.integer'] = $this->verify['message']['id.required'] = '编辑导航数据不存在';
  95 + return $this->save();
  96 + }
  97 +
  98 + /**
  99 + * 新增修改
  100 + * @return \Illuminate\Http\JsonResponse
  101 + * @throws \Illuminate\Validation\ValidationException
  102 + * @throws \Psr\Container\ContainerExceptionInterface
  103 + * @throws \Psr\Container\NotFoundExceptionInterface
  104 + * @author:dc
  105 + * @time 2023/5/8 17:06
  106 + */
  107 + private function save(){
  108 + $data = $this->validate(request() ,$this->verify['role'],$this->verify['message']);
  109 +
  110 + // 保存
  111 + $id = BCustom::_save($this->user['project_id'],$data,$data['id']??0);
  112 +
  113 + if($id===-1){
  114 + return $this->response('数据不存在','B_CUSTOM_NOTFOUND');
  115 + }
  116 +
  117 + return $this->success(BCustom::_find($this->user['project_id'],$id,true));
  118 + }
  119 +
  120 +
  121 + /**
  122 + * 删除数据
  123 + * @return \Illuminate\Http\JsonResponse
  124 + * @author:dc
  125 + * @time 2023/5/9 9:20
  126 + */
  127 + public function delete(){
  128 + $id = $this->param['id']??0;
  129 + $data = BCustom::_find($this->user['project_id'],$id);
  130 +
  131 + if(empty($data)){
  132 + return $this->response('数据不存在','B_CUSTOM_NOTFOUND');
  133 + }
  134 +
  135 +
  136 + if($data->delete()){
  137 + return $this->response('删除成功',Code::SUCCESS);
  138 + }
  139 +
  140 + }
  141 +
  142 +
  143 +
  144 +
  145 +}
  1 +<?php
  2 +
  3 +namespace App\Models;
  4 +
  5 +use Illuminate\Database\Eloquent\SoftDeletes;
  6 +
  7 +/**
  8 + * b端控制, c端显示的自定义页面
  9 + * @author:dc
  10 + * @time 2023/5/8 16:14
  11 + * Class BCustom
  12 + * @package App\Models
  13 + */
  14 +class BCustom extends Base
  15 +{
  16 +
  17 + protected $table = 'gl_bside_template_custom';
  18 +
  19 + use SoftDeletes;
  20 +
  21 + public $hidden = ['deleted_at','project_id'];
  22 +
  23 +
  24 + /**
  25 + * 显示
  26 + */
  27 + const STATUS_ACTIVE = 1;
  28 +
  29 + /**
  30 + * 隐藏
  31 + */
  32 + const STATUS_DISABLED = 0;
  33 +
  34 +
  35 + /**
  36 + * 创建或者新增导航栏
  37 + * @param int $project_id
  38 + * @param array $data
  39 + * @param int $id
  40 + * @return int
  41 + * @author:dc
  42 + * @time 2023/5/8 16:24
  43 + */
  44 + public static function _save(int $project_id, array $data, int $id = 0):int {
  45 + if($id){
  46 + $model = static::where('id',$id)->where('project_id', $project_id)->first();
  47 + if(!$model){
  48 + return -1;
  49 + }
  50 + }else{
  51 + $model = new static();
  52 + $model->project_id = $project_id;
  53 +
  54 + }
  55 +
  56 + $model->name = $data['name'];
  57 + $model->title = $data['title'];
  58 + $model->keywords = $data['keywords'];
  59 + $model->description = $data['description'];
  60 + $model->url = $data['url'];
  61 + $model->status = $data['status'];
  62 + $model->html = $data['html'];
  63 +
  64 + $model->save();
  65 +
  66 + return $model->id;
  67 + }
  68 +
  69 +
  70 + /**
  71 + * 删除
  72 + * @param int $project_id
  73 + * @param int $id
  74 + * @return mixed
  75 + * @author:dc
  76 + * @time 2023/5/8 16:27
  77 + */
  78 + public static function _del(int $project_id, int $id){
  79 + return static::where(['project_id'=>$project_id,'id'=>$id])->delete();
  80 + }
  81 +
  82 +
  83 + /**
  84 + * 查询当前项目下的所有信息
  85 + * @param int $project_id
  86 + * @return mixed
  87 + * @author:dc
  88 + * @time 2023/5/8 16:29
  89 + */
  90 + public static function _all(int $project_id)
  91 + {
  92 + return static::where(function ($query) use ($project_id){
  93 + // 那个公司
  94 + $query->where('project_id',$project_id);
  95 + })
  96 + ->get(['id','name','title','status','url','keywords','description','created_at','updated_at']);
  97 + }
  98 +
  99 + /**
  100 + * 查询一条数据
  101 + * @param int $project_id
  102 + * @param int $id
  103 + * @return mixed
  104 + * @author:dc
  105 + * @time 2023/5/8 17:04
  106 + */
  107 + public static function _find(int $project_id, int $id, $array = false)
  108 + {
  109 + $data = static::where(['id'=>$id,'project_id'=>$project_id])->first();
  110 + if($data){
  111 + return $array ? $data->toArray() : $data;
  112 + }
  113 + return [];
  114 + }
  115 +
  116 +
  117 + /**
  118 + * 是否存在
  119 + * @param int $project_id
  120 + * @param int $id
  121 + * @return mixed
  122 + * @author:dc
  123 + * @time 2023/5/8 17:24
  124 + */
  125 + public static function _check(int $project_id, int $id)
  126 + {
  127 + return static::where(['id'=>$id,'project_id'=>$project_id])->count();
  128 + }
  129 +
  130 + /**
  131 + * 是否有下级
  132 + * @param int $id
  133 + * @param int $project_id
  134 + * @return mixed
  135 + * @author:dc
  136 + * @time 2023/5/9 9:23
  137 + */
  138 + public static function isChild(int $id,int $project_id=0)
  139 + {
  140 + return static::where(['pid'=>$id,'project_id'=>$project_id])->limit(1)->count();
  141 + }
  142 +}
1 -<?php  
2 -  
3 -namespace App\Models\Template;  
4 -  
5 -use Illuminate\Database\Eloquent\SoftDeletes;  
6 -  
7 -/**  
8 - * 自定义 页面  
9 - * @author:dc  
10 - * @time 2023/5/4 17:18  
11 - * Class BCustom  
12 - * @package App\Models\Template  
13 - */  
14 -class BCustom extends \App\Models\Base{  
15 -  
16 - protected $table = 'gl_bside_template_custom';  
17 -  
18 - use SoftDeletes;  
19 -  
20 -  
21 - const STATUS_ACTIVE = 1;  
22 -  
23 - const STATUS_DISABLED = 0;  
24 -  
25 -  
26 - /**  
27 - * 读取列表  
28 - * @param $project_id  
29 - * @return mixed  
30 - * @author:dc  
31 - * @time 2023/5/4 17:22  
32 - */  
33 - public static function _all($project_id){  
34 - return static::where([  
35 - 'project_id' => $project_id  
36 - ])->paginate(20);  
37 - }  
38 -  
39 -  
40 -  
41 -  
42 -  
43 -  
44 -}  
@@ -162,15 +162,23 @@ Route::middleware(['bloginauth'])->group(function () { @@ -162,15 +162,23 @@ Route::middleware(['bloginauth'])->group(function () {
162 162
163 163
164 164
165 - // 自定义页面 165 + // 模板
166 Route::prefix('template')->group(function () { 166 Route::prefix('template')->group(function () {
167 Route::get('/', [\App\Http\Controllers\Bside\TemplateController::class, 'index'])->name('template_header_footer'); 167 Route::get('/', [\App\Http\Controllers\Bside\TemplateController::class, 'index'])->name('template_header_footer');
168 Route::get('/edit', [\App\Http\Controllers\Bside\TemplateController::class, 'edit_html'])->name('template_header_footer_edit'); 168 Route::get('/edit', [\App\Http\Controllers\Bside\TemplateController::class, 'edit_html'])->name('template_header_footer_edit');
169 Route::post('/edit', [\App\Http\Controllers\Bside\TemplateController::class, 'edit_save'])->name('template_header_footer_edit_save'); 169 Route::post('/edit', [\App\Http\Controllers\Bside\TemplateController::class, 'edit_save'])->name('template_header_footer_edit_save');
170 Route::get('/system', [\App\Http\Controllers\Bside\TemplateController::class, 'system_all_html'])->name('template_header_footer_system'); 170 Route::get('/system', [\App\Http\Controllers\Bside\TemplateController::class, 'system_all_html'])->name('template_header_footer_system');
171 - Route::get('/custom', [\App\Http\Controllers\Bside\TemplateController::class, 'custom'])->name('template_custom');  
172 }); 171 });
173 172
  173 + // 自定义页面,专题页
  174 + Route::prefix('custom')->group(function () {
  175 + Route::get('/', [\App\Http\Controllers\Bside\CustomController::class, 'index'])->name('bside_custom');
  176 + Route::post('/create', [\App\Http\Controllers\Bside\CustomController::class, 'create'])->name('bside_custom_create');
  177 + Route::post('/update', [\App\Http\Controllers\Bside\CustomController::class, 'update'])->name('bside_custom_update');
  178 + Route::delete('/delete', [\App\Http\Controllers\Bside\CustomController::class, 'delete'])->name('bside_custom_delete');
  179 + });
  180 +
  181 +
174 // 导航栏编辑 182 // 导航栏编辑
175 Route::prefix('nav')->group(function () { 183 Route::prefix('nav')->group(function () {
176 Route::get('/', [\App\Http\Controllers\Bside\NavController::class, 'index'])->name('bside_nav'); 184 Route::get('/', [\App\Http\Controllers\Bside\NavController::class, 'index'])->name('bside_nav');