作者 lyh

gx

... ... @@ -37,95 +37,88 @@ class DomainInfoController extends BaseController
/**
* 添加域名
* @param DomainInfoRequest $domainInfoRequest
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
* @remark :保存域名
* @name :save
* @author :lyh
* @method :post
* @time :2023/8/1 15:36
*/
public function add(DomainInfoRequest $domainInfoRequest, DomainInfoLogic $domainInfoLogic)
public function save(DomainInfoLogic $domainInfoLogic)
{
$domainInfoRequest->validated();
$domainInfoLogic->create();
$this->response('域名添加成功!');
$this->verifyParam();
$domainInfoLogic->saveDomain();
$this->response('success');
}
/**
* 编辑域名
* @param DomainInfoRequest $domainInfoRequest
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
* @remark :验证字段
* @name :verifyParam
* @author :lyh
* @method :post
* @time :2023/8/1 15:32
*/
public function edit(DomainInfoRequest $domainInfoRequest, DomainInfoLogic $domainInfoLogic)
{
$domainInfoRequest->validate([
'id' => 'required|integer'
], [
'id.required' => 'id不能为空',
'id.integer' => 'id参数错误'
public function verifyParam(){
$this->request->validate([
'domain'=>'required',
'remark'=>'required',
'company'=>'required',
'belong_to'=>'required',
],[
'domain.required' => 'domain不能为空',
'remark.required' => '备注不能为空',
'company.required' => '所属项目不能为空',
'belong_to.required' => '域名不能为空'
]);
$domainInfoLogic->update();
$this->response('域名修改成功!');
return true;
}
/**
* 删除
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function delete(DomainInfoLogic $domainInfoLogic)
{
$domainInfoLogic->get_batch_update();
$this->response('域名删除成功!');
public function info(DomainInfoLogic $domainInfoLogic){
$this->request->validate([
'id'=>'required',
],[
'id.required' => 'id不能为空',
]);
$info = $domainInfoLogic->infoDomain();
$this->response('success',Code::SUCCESS,$info);
}
/**
* 恢复数据
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
* @remark :修改状态
* @name :status
* @author :lyh
* @method :post
* @time :2023/8/1 15:47
*/
public function restore(DomainInfoLogic $domainInfoLogic)
{
$domainInfoLogic->get_batch_update(DomainInfoLog::ACTION_RECOVER, DomainInfo::DELETED_DELETE);
$this->response('域名恢复成功!');
public function status(DomainInfoLogic $domainInfoLogic){
$this->request->validate([
'id'=>'required',
'status'=>'required'
],[
'id.required' => 'id不能为空',
'status.required' => 'id不能为空'
]);
$domainInfoLogic->editDomainStatus();
$this->response('success');
}
/**
* 域名信息
* @param int $deleted
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @remark :删除域名
* @name :del
* @author :lyh
* @method :post
* @time :2023/8/1 15:38
*/
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());
public function del(DomainInfoLogic $domainInfoLogic){
$this->request->validate([
'id'=>'required',
],[
'id.required' => 'id不能为空',
]);
$domainInfoLogic->delDomain();
$this->response('success');
}
/**
* 获取软删除域名信息
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getDeleteDomainInfo()
{
return $this->info(DomainInfo::DELETED_DELETE);
}
}
... ...
... ... @@ -19,240 +19,100 @@ use Illuminate\Support\Facades\DB;
class DomainInfoLogic extends BaseLogic
{
/**
* @var array
*/
private $param;
public function __construct()
{
parent::__construct();
$this->model = new DomainInfo();
$this->param = $this->requestAll;
}
/**
* 添加数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
* @remark :保存域名
* @name :createDomain
* @author :lyh
* @method :post
* @time :2023/8/1 14:52
*/
public function create()
public function saveDomain()
{
$request = $this->param ?? [];
if ($this->checkDomain($request['domain'])) {
return $this->fail('域名已存在!');
//验证域名
$this->verifyDomain($this->param['domain'],isset($this->param['id']) ?? '');
if(isset($this->param['id']) && !empty($this->param['id'])){
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
}else{
$rs = $this->model->add($this->param);
}
$domain = new DomainInfo();
$this->extracted($request, $domain, $domain->FieldsArray());
DB::beginTransaction();
if ($domain->save()) {
$original = $domain->getOriginal();
$original['belong_to'] = $request['belong_to'];
$original['status'] = $request['status'];
// 添加日志
$this->domain_action_log(DomainInfoLog::ACTION_ADD, $original, $original, '添加域名信息成功 - ID : ' . $domain->id);
DB::commit();
return $this->success();
if($rs === false){
$this->fail('error');
}
DB::rollBack();
return $this->fail('域名信息添加失败');
return $this->success();
}
/**
* 修改数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
* @remark :验证域名是否存在
* @name :verifyDomain
* @author :lyh
* @method :post
* @time :2023/8/1 14:59
*/
public function update()
{
$domain = $this->getDomain();
$original = $domain->getOriginal();
$original['belong_to'] = $domain->BelongToStr($original['belong_to']);
$original['status'] = $domain->StatusToStr($original['status']);
$request = $this->param;
$this->extracted($request, $domain, $original);
// 检查ip是否存在
if (array_key_exists('domain', $request)) {
if ($domain->domain != $request['domain']) {
if ($this->checkDomain($request['domain'])) {
$this->fail('域名信息修改失败,域名已存在', Code::USER_ERROR);
}
public function verifyDomain($domain,$id = ''){
if(!empty($id)){
$info = $this->model->read(['domain'=>$domain,'id'=>['!=',$id]]);
if ($info !== false) {
$this->fail('当前域名已存在');
}
}
DB::beginTransaction();
if ($domain->save()) {
$fields_array = $domain->FieldsArray();
$revised = $domain->getAttributes();
$diff = array_diff_assoc($original, $revised);
unset($diff['created_at']);
unset($diff['updated_at']);
unset($diff['deleted']);
$remarks = '';
if ($diff) {
$remarks .= '修改ID为 ' . $domain->id . ' 的服务器信息,修改内容为:';
foreach ($diff as $key => $value) {
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
}
} else {
$remarks .= '修改ID为 ' . $domain->id . ' 的域名信息,无修改';
}else{
$info = $this->model->read(['domain'=>$domain]);
if ($info !== false) {
$this->fail('当前域名已存在');
}
// 添加日志
$this->domain_action_log(DomainInfoLog::ACTION_UPDATE, $original, $revised, $remarks);
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail('域名信息修改失败');
return $this->success();
}
/**
* 根据ID获取数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
* @remark :修改当前域名状态
* @name :editStatus
* @author :lyh
* @method :post
* @time :2023/8/1 15:43
*/
public function getDomain(int $deleted = DomainInfo::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('ID不能为空');
public function editDomainStatus(){
$rs = $this->model->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
$data = DomainInfo::query()->where('deleted', $deleted)->find($id);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
return $this->success();
}
/**
* 检查域名是否存在
* @param $domain
* @return bool
* @remark :删除域名
* @name :delDomain
* @author :lyh
* @method :post
* @time :2023/8/1 15:41
*/
public function checkDomain($domain)
{
$usIp = DomainInfo::query()->where('domain', $domain)->first();
if ($usIp) {
return true;
} else {
return false;
public function delDomain(){
$this->param['id'] = ['in',$this->param['id']];
$rs = $this->model->del($this->param);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* 详情
* @param int $deleted
* @return array|Builder|Collection|Model
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function domainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('ID不能为空');
}
$data = DomainInfo::query()->where('deleted', $deleted)->find($id, ['domain', 'belong_to', 'status', 'domain_start_time', 'domain_end_time', 'certificate_start_time', 'certificate_end_time', 'created_at', 'updated_at']);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
}
/**
* 服务器操作日志
* @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
* @param array $original 原始数据
* @param array $revised 修改后数据
*/
public function domain_action_log(int $action = DomainInfoLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
{
$log = new DomainInfoLog();
$this->log($log, $action, $original, $revised, $remarks);
}
/**
* @param array $request
* @param $domain
* @param array $original
* @return void
*/
public function extracted(array $request, $domain, array $original)
{
$request = array_intersect_key($request, $original);
foreach ($request as $key => $value) {
$domain->$key = trim($value) ?? $original[$key];
}
}
/**
* 批量获取数据删除
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL)
{
$ids = (new ServerInformationLogic())->getIds();
$data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
$restore_ids = $data->pluck('id')->toArray();
$actionArr = DomainInfoLog::actionArr();
$actionStr = $actionArr[$action];
if (empty($restore_ids)) {
$this->fail($actionStr . '域名信息不存在!', Code::USER_ERROR);
}
$original = $data->toArray();
DB::beginTransaction();
try {
$update = $deleted == DomainInfo::DELETED_NORMAL ? DomainInfo::DELETED_DELETE : DomainInfo::DELETED_NORMAL;
DomainInfo::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
$this->domain_action_log($action, $original, $original, $actionStr . '域名信息 - ID : ' . implode(', ', $restore_ids));
DB::commit();
return $this->success();
} catch (\Exception $e) {
DB::rollBack();
return $this->fail('域名信息' . $actionStr . '失败', Code::USER_ERROR);
public function infoDomain(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者已被删除');
}
return $this->success($info);
}
/**
* 证书到期时间
* @return array
*/
public function getDomainCertificateTime($domain)
{
$domain = trim($domain);
$data = [];
$context = stream_context_create(['ssl' => ['capture_peer_cert' => true]]); // Notice: only 7.0.7+ supports this
$stream = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($stream) {
$params = stream_context_get_params($stream);
$peerCertificate = openssl_x509_parse($params['options']['ssl']['peer_certificate']);
if ($peerCertificate) {
$validFrom = date_create_from_format('U', $peerCertificate['validFrom_time_t']); // 有效期开始时间
$validTo = date_create_from_format('U', $peerCertificate['validTo_time_t']); // 有效期结束时间
$data['validFrom'] = $validFrom->format('Y-m-d H:i:s');
$data['validTo'] = $validTo->format('Y-m-d H:i:s');
}
}
return $data;
}
/**
* 获取所有正常域名信息
* @return Builder[]|Collection
*/
public function getAllDomain()
{
return DomainInfo::query()->where('status', 1)
->where('deleted', DomainInfo::DELETED_NORMAL)
->orderBy('updated_at', 'desc')
->get();
}
/**
* 域名到期时间
... ... @@ -280,21 +140,5 @@ class DomainInfoLogic extends BaseLogic
return $data;
}
/**
* 根据域名更新证书到期时间
* @param $id
* @param $updata
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function updateDomain($id, $updata)
{
$isRes = DomainInfo::query()->where('id', $id)->where('deleted', DomainInfo::DELETED_NORMAL)->update($updata);
if ($isRes) {
return $this->success();
} else {
return $this->fail('更新域名到期时间失败');
}
}
}
... ...
<?php
namespace App\Http\Requests\Aside\Domain;
use App\Models\Aside\Domain\DomainInfo;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class DomainInfoRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
Validator::extend('validate_domain', function ($attribute, $value, $parameters, $validator) {
return $this->validateDomain($value);
});
$domain = new DomainInfo();
$status_tot_keys = array_keys($domain->StatusToArray());
$belong_to_keys = array_keys($domain->BelongToArray());
# 编辑时不需要验证必填
$required = 'required';
if ($this->getRequestUri() != '/a/domain/add') {
$required = '';
}
return [
'domain' => $required . '|max:200|validate_domain',
'belong_to' => [
$required,
Rule::in($belong_to_keys)
],
'status' => [
$required,
Rule::in($status_tot_keys)
]
];
}
/**
* 验证给定的值是否是有效的域名。
*
* @param mixed $value
* @return bool
*/
public function validateDomain($value)
{
// 从域中删除任何空间或路径。
$domain = preg_replace('/\s|\/.*$/', '', $value);
// 验证域是否至少包含一个句点。
if (strpos($domain, '.') === false) {
return false;
}
// 验证域是否以句点开始或结束。
if (strpos($domain, '.') === 0 || strrpos($domain, '.') === strlen($domain) - 1) {
return false;
}
// 验证域是否不包含无效字符。
if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $domain)) {
return false;
}
// 验证域是否具有有效的顶级域。
$tld = substr($domain, strrpos($domain, '.') + 1);
if (!in_array($tld, ['com', 'net', 'org'])) { // 如有必要,添加更多TLD。
return false;
}
return true;
}
public function messages()
{
return [
'domain.required' => '域名不能为空',
'domain.max' => '域名长度不能超过200个字符',
'domain.validate_domain' => '域名格式不正确',
'belong_to.required' => '域名所属不能为空',
'belong_to.in' => '域名所属参数错误',
'status.required' => '域名状态不能为空',
'status.in' => '域名状态参数错误',
];
}
}
... ... @@ -4,10 +4,28 @@ namespace App\Models\Aside\Domain;
use App\Models\Base;
/**
* Class DomainInfo
*
* @package App\Models\Aside\DomainLogic
* @Author YiYuan-LIn
* @Date : 2019/5/16
* 域名信息模型
*/
class DomainInfo extends Base
{
public $btAction = [
'create_site' => '/site?action=AddSite',
];
protected $table = 'gl_domain_info';
// 软删除 0:正常 1:删除
/** @var int 软删除 - 正常 */
const DELETED_NORMAL = 0;
/** @var int 软删除 - 删除 */
const DELETED_DELETE = 1;
protected $hidden = [
'created_at',
'updated_at'
... ...
... ... @@ -172,17 +172,14 @@ Route::middleware(['aloginauth'])->group(function () {
});
});
// 域名
// 域名管理
Route::prefix('domain')->group(function () {
Route::get('/', [Aside\Domain\DomainInfoController::class, 'lists'])->name('admin.domain'); // 列表 | 搜索
Route::get('/info', [Aside\Domain\DomainInfoController::class, 'info'])->name('admin.domain_info'); // 详情
Route::get('/delete_info', [Aside\Domain\DomainInfoController::class, 'getDeleteDomainInfo'])->name('admin.domain_delete_info'); // 删除信息
Route::post('/add', [Aside\Domain\DomainInfoController::class, 'add'])->name('admin.domain_save'); // 添加
Route::post('/edit', [Aside\Domain\DomainInfoController::class, 'edit'])->name('admin.domain_edit'); // 编辑
Route::any('/delete', [Aside\Domain\DomainInfoController::class, 'delete'])->name('admin.domain_delete'); // 删除
Route::get('/delete_list', [Aside\Domain\DomainInfoController::class, 'delete_list'])->name('admin.domain_delete_list'); // 删除列表
Route::any('/restore', [Aside\Domain\DomainInfoController::class, 'restore'])->name('admin.domain_restore'); // 恢复
Route::get('/log', [Aside\Domain\DomainInfoLogController::class, 'lists'])->name('admin.domain_log_lists'); // 日志
Route::any('/', [Aside\Domain\DomainInfoController::class, 'lists'])->name('admin.domain'); // 列表 | 搜索
Route::any('/info', [Aside\Domain\DomainInfoController::class, 'info'])->name('admin.domain_info'); // 详情
Route::any('/save', [Aside\Domain\DomainInfoController::class, 'save'])->name('admin.domain_save');
Route::any('/status', [Aside\Domain\DomainInfoController::class, 'status'])->name('admin.domain_status');
Route::any('/del', [Aside\Domain\DomainInfoController::class, 'del'])->name('admin.domain_del');
Route::any('/log', [Aside\Domain\DomainInfoLogController::class, 'lists'])->name('admin.domain_log_lists'); // 日志
});
//图片操作
... ...