DomainInfoController.php 4.7 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)
    {
        $request = $this->param;
        $search = [];
        $search_array = [
            'domain'           => 'domain',
            'belong'           => 'belong_to',
            'domain_time'      => 'domain_end_time',
            'certificate_time' => 'certificate_end_time'
        ];
        foreach ($search_array as $key => $item) {
            if (isset($request[$key]) && $request[$key]) {
                $search[$item] = $request[$key];
            }
        }
        // 每页条数
        $size = request()->input('size', $this->row);
        $query = DomainInfo::query()->select(['id', 'domain', 'belong_to']);
        if ($search) {
            foreach ($search as $key => $item) {
                $query = $query->Where("{$key}", 'like', "%{$item}%");
            }
        }
        $data = $query->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 info(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->info(DomainInfo::DELETED_DELETE);
    }
}