DomainInfoController.php 4.0 KB
<?php

namespace App\Http\Controllers\Aside\Domain;

use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Domain\DomainInfoLogic;
use App\Http\Requests\Aside\Domain\DomainInfoRequest;
use App\Models\Aside\Domain\DomainInfo;
use App\Models\Aside\Domain\DomainInfoLog;
use Illuminate\Http\JsonResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * Class DomainInfoController
 * @package App\Http\Controllers\Aside  域名管理
 */
class DomainInfoController extends BaseController
{
    /**
     * 域名列表
     * @param int $deleted
     * @return JsonResponse
     */
    public function lists(int $deleted = DomainInfo::DELETED_NORMAL)
    {
        $size = request()->input('size', $this->row);
        $data = DomainInfo::query()->select(['id', 'domain', 'belong_to'])
            ->where('deleted', $deleted)
            ->orderBy('id', 'desc')
            ->paginate($size);
        return $this->response('success', Code::SUCCESS, $data);
    }

    /**
     * 获取软删除的数据
     * @return JsonResponse
     */
    public function delete_list()
    {
        return $this->lists(DomainInfo::DELETED_DELETE);
    }

    /**
     * 添加域名
     * @param DomainInfoRequest $domainInfoRequest
     * @param DomainInfoLogic $domainInfoLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function add(DomainInfoRequest $domainInfoRequest, DomainInfoLogic $domainInfoLogic)
    {
        $domainInfoRequest->validated();
        $domainInfoLogic->create();
        $this->response('域名添加成功!');
    }

    /**
     * 编辑域名
     * @param DomainInfoRequest $domainInfoRequest
     * @param DomainInfoLogic $domainInfoLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function edit(DomainInfoRequest $domainInfoRequest, DomainInfoLogic $domainInfoLogic)
    {
        $domainInfoRequest->validate([
            'id' => 'required|integer'
        ], [
            'id.required' => 'id不能为空',
            'id.integer'  => 'id参数错误'
        ]);
        $domainInfoLogic->update();
        $this->response('域名修改成功!');
    }

    /**
     * 删除
     * @param DomainInfoLogic $domainInfoLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function delete(DomainInfoLogic $domainInfoLogic)
    {
        $domainInfoLogic->get_batch_update();
        $this->response('服务器删除成功!');
    }

    /**
     * 恢复数据
     * @param DomainInfoLogic $domainInfoLogic
     * @return void
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     */
    public function restore(DomainInfoLogic $domainInfoLogic)
    {
        $domainInfoLogic->get_batch_update(DomainInfoLog::ACTION_RECOVER, DomainInfo::DELETED_DELETE);
        $this->response('服务器恢复成功!');
    }

    /**
     * 域名信息
     * @param int $deleted
     * @return JsonResponse
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getDomainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
    {
        $domainInfoLogic = new DomainInfoLogic();
        $data = $domainInfoLogic->domainInfo($deleted);
        if (!$data) {
            return $this->response('服务器信息不存在', Code::USER_ERROR);
        }
        return $this->success($data->toArray());
    }

    /**
     * 获取软删除域名信息
     * @return JsonResponse
     * @throws AsideGlobalException
     * @throws BsideGlobalException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getDeleteDomainInfo()
    {
        return $this->getDomainInfo(DomainInfo::DELETED_DELETE);
    }
}