CustomController.php 4.4 KB
<?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(){

        // 每页数量
        $limit = intval($this->param['limit']??20);


        $lists = BCustom::_all($this->user['project_id'],$limit)->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);
        }

    }



    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author:dc
     * @time 2023/5/10 14:10
     */
    public function html($id)
    {
        $data = BCustom::_find($this->user['project_id'],$id);
        if(!$data){
            return $this->response('数据不存在','B_CUSTOM_NOTFOUND');
        }
        if($this->isPost()){
            $html = $this->param['html']??'';

            $data->html = $html;

            $data->save();

        }


        return $this->response('',Code::SUCCESS,$data['html']);

    }




}