ProjectServer.php 14.4 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2023/4/17
 * Time: 15:16
 */

namespace App\Services;

use App\Models\CustomModule\CustomModule;
use App\Models\Project\AggregateKeywordAffix;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
 * Class ProjectServer
 * @package App\Services
 */
class ProjectServer
{
    public static $main404Html = '<main>
        <section data-section="section" data-screen="screen-large" class="section-404-wrap-block section-block-error404"
            id="sectionIdyxqu938">
            <div class="layout" data-unable="demo01-error404">
                <img src="https://ecdn6.globalso.com/upload/m/image_other/2023-10/6528a87e594db30162.png" alt=""/>
            </div>
            <p style="text-align: center">SORRY. THE PAGE HAS EITHER MOVED OR CANNOT BE FOUND.</p>
            <style>
                .section-block-error404 .layout {
                    height: 700px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                }
                .section-block-error404 img {
                    width: 400px;
                }
                @media only screen and (max-width:500px) {
                    .section-block-error404 img {
                        max-width: 100%;
                    }
                }
            </style>
            <script>
            </script>
        </section>
    </main>';
    /**
     * @param $project_id
     * @return Project|false
     */
    public static function useProject($project_id)
    {
        $project = Project::where(['id' => $project_id])->first();
        if (empty($project)){
            return false;
        }
        if(!$project->mysqlConfig){
            return false;
        }
        // 设置 database.connections.custom_mysql 配置
        config(['database.connections.custom_mysql.host' => $project->mysqlConfig->host]);
        config(['database.connections.custom_mysql.port' => $project->mysqlConfig->port]);
        if(env('SERVER_ID') == 7){
            config(['database.connections.custom_mysql.host' => env('DB_HOST')]);
            config(['database.connections.custom_mysql.port' => env('DB_PORT')]);
        }
        config(['database.connections.custom_mysql.database' => $project->databaseName()]);
        config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]);
        config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]);
        //清除现有的数据库连接配置
        DB::purge('custom_mysql');
        //重连
        DB::connection('custom_mysql')->reconnect();
        // 设置 redis 配置
        return $project;
    }


    /**
     * 创建数据库
     * @param $project
     * @return bool
     * @author zbj
     * @date 2023/4/23
     */
    public static function createDatabase($project)
    {
        $conn = new \mysqli(
            $project->mysqlConfig->host,
            $project->mysqlConfig->user,
            $project->mysqlConfig->password,
            '',
            $project->mysqlConfig->port
        );
        $conn->query("CREATE DATABASE IF NOT EXISTS {$project->databaseName()}");
        return true;
    }


    /**
     * @param $project
     * @return bool
     * @author zbj
     * @date 2023/4/23
     */
    public static function initTable()
    {
        $database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();
        $table = Schema::connection('custom_tmp_mysql')->getAllTables();
        $table = array_column($table, 'Tables_in_' . $database_name);
        foreach ($table as $v) {
            $has_table = Schema::connection('custom_mysql')->hasTable($v);
            if ($has_table) {
                continue;
            }
            $sql = DB::connection('custom_tmp_mysql')->select("SHOW CREATE TABLE {$v}");
            DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
        }
        return true;
    }

    /**
     * @remark :执行初始数据
     * @name   :saveInitParam
     * @author :lyh
     * @method :post
     * @time   :2023/9/19 14:45
     */
    public static function saveInitParam($project_id){
        self::initGroup($project_id);
        //初始化单页
        self::init404Page($project_id);
        //初始化模块数据
        self::initModule($project_id);
        //初始化切换栏
        self::initColumn();
        //初始化search页面
//        self::initSearchPage($project_id);
        //聚合页关键词前后缀
        self::initAggregateKeywordAffix($project_id);
        DB::disconnect('custom_mysql');
        return true;
    }

    /**
     * @remark :初始化模块
     * @name   :initModule
     * @author :lyh
     * @method :post
     * @time   :2023/12/29 9:34
     */
    public static function initModule($project_id){
        $info = DB::connection('custom_mysql')->table('gl_custom_module')->first();
        if(empty($info)){
            $data = [
                'name'=>'视频模块',
                'project_id'=>$project_id,
                'route'=>'video',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ];
            DB::connection('custom_mysql')->table('gl_custom_module')->insert($data);
        }
        return true;
    }

    /**
     * @remark :初始化产品切换栏
     * @name   :initColumn
     * @author :lyh
     * @method :post
     * @time   :2024/11/12 11:39
     */
    public static function initColumn(){
        $info = DB::connection('custom_mysql')->table('gl_product_column')->first();
        if(empty($info)){
            $data = [
                'column_name'=>'Product Details',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ];
            DB::connection('custom_mysql')->table('gl_product_column')->insert($data);
        }
        return true;
    }

    /**
     * @remark :菜单
     * @name   :initGroup
     * @author :lyh
     * @method :post
     * @time   :2023/12/29 9:30
     */
    public static function initGroup($project_id){
        $time = date('Y-m-d H:i:s');
        $info = DB::connection('custom_mysql')->table('gl_web_nav')->first();
        if(empty($info)) {
            $data = [
                ['project_id' => $project_id, 'name' => 'Home', 'url' => 'Home', 'location' => 'header', 'group_id' => 1, 'created_at' => $time, 'updated_at' => $time],
                ['project_id' => $project_id, 'name' => 'Products', 'url' => 'Products', 'location' => 'header', 'group_id' => 1, 'created_at' => $time, 'updated_at' => $time],
                ['project_id' => $project_id, 'name' => 'News', 'url' => 'News', 'location' => 'header', 'group_id' => 1, 'created_at' => $time, 'updated_at' => $time],
                ['project_id' => $project_id, 'name' => 'ABOUT US', 'url' => 'about-us', 'location' => 'footer', 'group_id' => 2, 'created_at' => $time, 'updated_at' => $time],
                ['project_id' => $project_id, 'name' => 'Contact Us', 'url' => 'contact-us', 'location' => 'footer', 'group_id' => 2, 'created_at' => $time, 'updated_at' => $time],
                ['project_id' => $project_id, 'name' => 'FAQ', 'url' => 'faq', 'location' => 'footer', 'group_id' => 2, 'created_at' => $time, 'updated_at' => $time],
            ];
            DB::connection('custom_mysql')->table('gl_web_nav')->insert($data);
        }
        //菜单组
        $info = DB::connection('custom_mysql')->table('gl_web_nav_group')->first();
        if(empty($info)) {
            $data = [
                ['id' => 1, 'project_id' => $project_id, 'name' => '全局顶部菜单', 'created_at' => $time, 'updated_at' => $time],
                ['id' => 2, 'project_id' => $project_id, 'name' => '底部菜单', 'created_at' => $time, 'updated_at' => $time],
            ];
            DB::connection('custom_mysql')->table('gl_web_nav_group')->insert($data);
        }
        return true;
    }

    /**
     * @remark :初始化404页面
     * @name   :init404Page
     * @author :lyh
     * @method :post
     * @time   :2023/12/29 9:32
     */
    public static function init404Page($project_id){
        $time = date('Y-m-d H:i:s');
        $info = DB::connection('custom_mysql')->table('gl_web_custom_template')->first();
        if(empty($info)) {
            $data = [
                'project_id' => $project_id,
                'name' => BCustomTemplate::NOT_FOUND_PAGE_URL,
                'status' => 1,
                'url' => BCustomTemplate::NOT_FOUND_PAGE_URL,
                'html' => self::$main404Html,
                'html_style' => '<style id="globalsojs-styles"></style>',
                'title' => '404-Page not found',
                'description' => 'Sorry. The page has either moved or cannot be found.',
                'created_at' => $time, 'updated_at' => $time];
            $id = DB::connection('custom_mysql')->table('gl_web_custom_template')->insertGetId($data);
            //路由
            $info = DB::connection('custom_mysql')->table('gl_route_map')->first();
            if(empty($info)) {
                $data = ['project_id' => $project_id, 'source' => RouteMap::SOURCE_PAGE, 'source_id' => $id, 'route' => BCustomTemplate::NOT_FOUND_PAGE_URL, 'created_at' => $time, 'updated_at' => $time];
                DB::connection('custom_mysql')->table('gl_route_map')->insert($data);
            }
        }
    }

    /**
     * @remark :初始化search页面
     * @name   :init404Page
     * @author :lyh
     * @method :post
     * @time   :2023/12/29 9:32
     */
    public static function initSearchPage($project_id){
        $time = date('Y-m-d H:i:s');
        $info = DB::connection('custom_mysql')->table('gl_web_custom_template')->first();
        if(empty($info)) {
            $data = [
                'project_id' => $project_id,
                'name' => 'search',
                'status' => 1,
                'url' => 'search',
                'html' => '',
                'html_style' =>'search',
                'title' => 'search',
                'description' => 'Sorry. The page has either moved or cannot be found.',
                'created_at' => $time, 'updated_at' => $time];
            $id = DB::connection('custom_mysql')->table('gl_web_custom_template')->insertGetId($data);
            //路由
            $info = DB::connection('custom_mysql')->table('gl_route_map')->first();
            if(empty($info)) {
                $data = ['project_id' => $project_id, 'source' => RouteMap::SOURCE_PAGE, 'source_id' => $id, 'route' => 'search', 'created_at' => $time, 'updated_at' => $time];
                DB::connection('custom_mysql')->table('gl_route_map')->insert($data);
            }
        }
    }

    /**
     * @remark :gl_aggregate_keyword_affix处理聚合页关键词前后缀
     * @name   :initAffix
     * @author :lyh
     * @method :post
     * @time   :2025/6/26 17:48
     */
    public static function initAggregateKeywordAffix($project_id){
        $time = date('Y-m-d H:i:s');
        $prefix = "How To"."\n"."Why"."\n"."Top 5"."\n"."Top 3"."\n"."Best Way To"."\n"."Methods To"."\n"."Top-Rated"."\n"."How To find"."\n"."why choose"."\n"."China Top"."\n"."Best Way To Choose"."\n"."Methods To choose"."\n"."10 tips"."\n"."Top 10"."\n"."How To Select"."\n"."How To Find The Best"."\n"."How To Evaluate"."\n"."How To Compare"."\n"."How To Identify"."\n"."How To Source"."\n"."How To Pick The Right"."\n"."How To Decide Between"."\n"."Why Opt For"."\n"."Why Invest In"."\n"."Why Consider"."\n"."Why Trust"."\n"."Why Professionals Prefer"."\n"."Top Picks For"."\n"."Best Practices For"."\n"."Best Ways To"."\n"."Best Options For"."\n"."Best Brands For"."\n"."Best Methods To"."\n"."Best Strategies For"."\n"."Effective Ways To"."\n"."Proven Methods To"."\n"."Simple Ways To"."\n"."Key Methods For"."\n"."Smart Ways To"."\n"."Practical Methods To"."\n"."Step-by-Step Guide To"."\n"."10 Essential Tips For"."\n"."7 Key Tips To"."\n"."Ultimate Guide To"."\n"."Expert Tips For"."\n"."Must-Know Tips For"."\n"."Quick Tips To"."\n"."Insider Tips For"."\n"."Leading Chinese"."\n"."Best Chinese"."\n"."China’s Best-Selling"."\n"."Why China’s"."\n"."How Chinese Manufacturers"."\n"."Exploring"."\n"."Uncovering"."\n"."Discovering"."\n"."Pinpointing"."\n"."Decoding"."\n"."Reasons to Choose"."\n"."Advantages Explained"."\n"."Where the Value Lies"."\n"."Not to Be Missed"."\n"."Must-Choose Reasons"."\n"."Excellence in"."\n"."Cutting-Edge"."\n"."Premier"."\n"."Champions of"."\n"."The Ultimate Guide to"."\n"."Practical Strategies for"."\n"."Making Smart Choices"."\n"."Key Steps to Choosing"."\n"."Finding Your Perfect Match"."\n"."Avoiding Pitfalls"."\n"."Tips & Tricks for"."\n"."In-Depth Analysis of"."\n"."Methods"."\n"."How-To Guide"."\n"."Step-by-Step Guide"."\n"."The Secret to"."\n"."Must-See List"."\n"."Curated Selection of"."\n"."Trending"."\n"."Popular"."\n"."Don't Miss These"."\n"."Comprehensive Comparison of"."\n"."The Clear Choice of"."\n"."Today's Choice of"."\n"."Expert Choice of";
        $suffix = "Manufacturer"."\n"."Supplier"."\n"."Application"."\n"."Products"."\n"."Service"."\n"."Factory"."\n"."Is The Best"."\n"."Stands Out"."\n"."Dominates"."\n"."in 2025"."\n"."Industry Leaders"."\n"."Industry Giant"."\n"."Market Leader"."\n"."For the Current Year"."\n"."Ahead of the Curve"."\n"."Trusted by Pros"."\n"."Pioneers in the Field"."\n"."Now Trending"."\n"."Sets the Industry Standard"."\n"."Leads the Global Market"."\n"."Delivers Unmatched Quality"."\n"."Guarantees Peak Performance"."\n"."Exceeds Industry Benchmarks"."\n"."Winning in 2025"."\n"."Where Innovation Meets 2025"."\n"."Your Trusted OEM Partner"."\n"."Supplies the World’s Top Brands"."\n"."Manufacturers You Can Rely On"."\n"."Factory-Direct Excellence"."\n"."Custom Solutions,"."\n"."Global Reach"."\n"."Where Service Meets Innovation"."\n"."Your End-to-End Solution"."\n"."More Than a Supplier - A Partner"."\n"."Service Backed by Expertise"."\n"."From Concept to Delivery"."\n"."Outperforms the Competition";
        $info = DB::table('gl_aggregate_keyword_affix')->where('project_id',$project_id)->first();
        if(empty($info)){
            $data = [
                'project_id'=>$project_id,
                'prefix'=>$prefix,
                'suffix'=>$suffix,
                'created_at' => $time,
                'updated_at' => $time
            ];
            DB::table('gl_aggregate_keyword_affix')->insert($data);
        }
        return true;
    }
}