WebSiteController.php 3.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2022/11/1
 * Time: 10:31
 */
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Repositories\BtRepository;
use Illuminate\Http\Request;

/**
 * Class WebSiteControllers
 * @package App\Http\Controllers
 */
class WebSiteController extends Controller
{
    /**
     * 创建站点
     * @param Request $request
     * @return string
     */
    public function createSite(Request $request)
    {
        try {
//            $this->validate();
            $domain = $request->input('domain');
            $ssl_open = intval($request->input('ssl_open', 1)) ? 1 : 0;
            $ssl_auto = intval($request->input('ssl_auto', 1)) ? 1 : 0;
            $ssl_auto_day = intval($request->input('ssl_auto_day', 10));
            $ssl_auto_day = $ssl_auto_day > 15 ? 15 : max($ssl_auto_day, 5);
            if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL))
                return $this->error('请提交有效domain信息');

            $result = app(BtRepository::class)->createBtSite($domain, $ssl_open, $ssl_auto, $ssl_auto_day);
            return $result ? $this->success($result) : $this->error('创建站点失败,提交异步创建任务!');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 删除站点
     * @param Request $request
     * @return string
     */
    public function deleteSite(Request $request)
    {
        try {
//            $this->validate();
            $domain = $request->input('domain');
            if (empty($domain) || FALSE == filter_var($domain, FILTER_VALIDATE_URL))
                return $this->error('请提交有效domain信息');

            $result = app(BtRepository::class)->deleteBtSite($domain);
            return $result ? $this->success() : $this->error('删除站点失败,提交异步删除任务!');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 创建站点免费证书
     * @param Request $request
     * @return string
     */
    public function createSsl(Request $request)
    {
        try {
//            $this->validate();
            $host = $request->input('host');
            if (empty($host))
                return $this->error('请提交有效host信息');

            $result = app(BtRepository::class)->applySsl($host);
            return $result ? $this->success() : $this->error('创建ssl失败,提交异步任务!');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 更新站点免费证书
     * @param Request $request
     * @return string
     */
    public function renewalSsl(Request $request)
    {
        try {
//            $this->validate();
            $host = $request->input('host');
            if (empty($host))
                return $this->error('请提交有效host信息');

            $result = app(BtRepository::class)->renewalSsl($host);
            return $result ? $this->success() : $this->error('续签ssl失败,提交异步任务!');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}