DeletePageService.php 6.4 KB
<?php


namespace App\Services\Html;

use App\Models\Domain\DomainInfo;
use App\Models\Project\DeleteHtmlModel;
use App\Models\Project\DeployBuild;
use App\Models\Project\DeployOptimize;
use App\Models\RouteMap\RouteDelete;
use App\Models\WebSetting\WebSetting;
use Illuminate\Support\Facades\Redis;

/**
 * C端删除页面服务
 */
class DeletePageService{
    /**
     * 删除文件
     */
    public function deleteDirectory($path): bool
    {
        if (!is_dir($path)) {
            try {
                unlink($path);
            } catch (\Exception $e) {
                return true;
            }
            return true;
        }
        $files = array_diff(scandir($path), array('.', '..'));
        foreach ($files as $file) {
            $filePath = $path . '/' . $file;
            if (is_dir($filePath)) {
                $this->deleteDirectory($filePath);
            } else {
                try {
                    unlink($filePath);
                } catch (\Exception $e) {
                    return true;
                }
            }
        }
        rmdir($path);
        return true;
    }

    /**
     * 获取项目ID下正式+测试域名
     */
    public function getAllProjectDomain($data)
    {
        if (Redis::get($data->project_id."_all_project_domain") != null){
            $allProjectDomain =Redis::get($data->project_id."_all_project_domain");
        }else{
            $domainArr = [];
            $onlineProject = DeployOptimize::where('project_id',$data->project_id)->first();
            if (!empty($onlineProject)){
                if ($onlineProject->domain != null){
                    $domainInfo = DomainInfo::where("id",$onlineProject->domain)->where("status",1)->first();
                    if (!empty($domainInfo)){
                        $domainArr[] = $domainInfo->domain;
                    }
                }
            }
            $testProject = DeployBuild::where('project_id',$data->project_id)->first();
            if (!empty($testProject)){
                $testDomain = $testProject->test_domain;
                if (strpos($testDomain, 'http://') !== false){
                    $testDomain = str_replace('http://', '', $testDomain);
                }
                if (strpos($testDomain, 'https://') !== false){
                    $testDomain = str_replace('https://', '', $testDomain);
                }
                $testDomain = str_replace('/', '', $testDomain);
                $domainArr[] = $testDomain;
            }
            if (!empty($domainArr)){
                $allProjectDomain = json_encode($domainArr);
                Redis::set($data->project_id."_all_project_domain", $allProjectDomain);
                Redis::expire($data->project_id."_all_project_domain", WebSetting::$redisExpireTime);
            }
        }
        return $allProjectDomain;

    }

    /**
     * 构造删除路径
     */
    public function deleteHtmlItems($data): bool
    {
        $getAllProjectDomain = $this->getAllProjectDomain($data);
        if ($getAllProjectDomain != null){
            $getAllProjectDomain = json_decode($getAllProjectDomain);
            $deleteHtml = DeleteHtmlModel::where("project_id",$data->project_id)->get();
            foreach ($getAllProjectDomain as $domainItem){
                if (!empty($deleteHtml)){
                    foreach ($deleteHtml as $v){
                        //国家
                        $pageService = new PageService();
                        $webCountry = $pageService->getCountryList($data->project_id);
                        if ($webCountry != null){
                            foreach ($webCountry as $item){
                                if ($v->route == "index"){
                                    $path = public_path($domainItem."/".$item->alias."/index.html");
                                }else{
                                    $path = public_path($domainItem."/".$item->alias."/".$v->route);
                                }

                                if (file_exists($path)){
                                    $this->deleteDirectory($path);
                                }
                            }
                        }
                        if ($v->route == "index"){
                            $path = public_path($domainItem."/index.html");
                        }else{
                            $path = public_path($domainItem."/".$v->route);
                        }

                        if (file_exists($path)){
                            $this->deleteDirectory($path);
                        }
                        RouteDelete::where("project_id",$data->project_id)->where("route",$v->route)->delete();
                    }
                }

                //删除C端固定页面
                $pageService = new PageService();
                $webCountry = $pageService->getCountryList($data->project_id);
                if ($webCountry != null){
                    foreach ($webCountry as $item) {
                        //删除C端固定小语种页面
                        $productsPath = public_path($domainItem . "/" . $item->alias . "/products");
                        $newsPath = public_path($domainItem . "/" . $item->alias . "/news");
                        $blogPath = public_path($domainItem . "/" . $item->alias . "/blog");
                        if (file_exists($productsPath)) {
                            $this->deleteDirectory($productsPath);
                        }
                        if (file_exists($newsPath)) {
                            $this->deleteDirectory($newsPath);
                        }
                        if (file_exists($blogPath)) {
                            $this->deleteDirectory($blogPath);
                        }
                    }
                }
                //删除C端固定主站页面
                $productsPath = public_path($domainItem."/products");
                $newsPath = public_path($domainItem."/news");
                $blogPath = public_path($domainItem."/blog");
                if (file_exists($productsPath)){
                    $this->deleteDirectory($productsPath);
                }
                if (file_exists($newsPath)){
                    $this->deleteDirectory($newsPath);
                }
                if (file_exists($blogPath)){
                    $this->deleteDirectory($blogPath);
                }
            }

        }
        return true;
    }
}