Translate.php 5.8 KB
<?php
/**
 * @remark :
 * @name   :Translate.php
 * @author :lyh
 * @method :post
 * @time   :2024/5/21 9:32
 */

namespace App\Console\Commands\Test;

use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BTemplateCom;
use App\Models\Template\BTemplateCommon;
use App\Models\Template\Setting;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class Translate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'translate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'translate';
    public function handle(){
        $projectModel = new Project();
        $list = $projectModel->list(['id'=>452]);
        foreach ($list as $v){
            echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
            ProjectServer::useProject($v['id']);
            $translateModel = new \App\Models\WebSetting\Translate();
            $translateList = $translateModel->list(['id'=>['between',[150000,200000]]],'id',['id','url']);
            if(!empty($translateList)){
                foreach ($translateList as $value){
                    echo date('Y-m-d H:i:s') . '数据路由:'.$value['url'] . PHP_EOL;
                    $data = $this->getRouteSource($value['url']);
                    if($data === false){
                        continue;
                    }
                    $rs = $translateModel->edit($data,['id'=>$value['id']]);
                    gc_collect_cycles();
                }
            }
            DB::disconnect('custom_mysql');
        }
        DB::disconnect('custom_mysql');
    }

    /**
     * @remark :根据路由获取source+source_id
     * @name   :getRouteSource
     * @author :lyh
     * @method :post
     * @time   :2024/5/17 15:11
     */
    public function getRouteSource($route){
        $data = ['source'=>0,'source_id'=>0,'is_list'=>0,'is_custom'=>0];
        if(strtolower($route) == 'all'){
            return $data;
        }
        if($route == '/'){
            $data['source'] = 1;
            return $data;
        }
        $route = $this->handleRoute($route);
        $routeModel = new RouteMap();
        $routeInfo = $routeModel->read(['route'=>$route]);
        if($routeInfo === false){
            return false;
        }
        return $this->resultData($routeInfo,$data);
    }

    /**
     * @remark :处理路由
     * @name   :handleRoute
     * @author :lyh
     * @method :post
     * @time   :2024/1/18 17:25
     */
    public function handleRoute($url){
        $str = trim($url,'/');
        $route = 'index';
        if(!empty($str)){
            $arr = explode('/',$str);
            $num = count($arr);
            if($num == 1){
                $route = $arr[0];
            }elseif ($num == 2){
                if(ctype_digit($arr[1])){//是数字的情况
                    $route = $arr[0];
                }else{
                    $route = $arr[1];
                }
            }elseif($num == 3){
                if(ctype_digit($arr[2])){//是数字的情况
                    $route = $arr[0];
                }else{
                    if($arr[2] == 'page'){
                        $route = $arr[1];
                    }else{
                        $route = $arr[0];
                    }
                }
            }elseif ($num == 4){
                if(ctype_digit($arr[3])){//是数字的情况
                    $route = $arr[1];
                }
            }
        }
        return $route;
    }

    /**
     * @remark :返回数据
     * @name   :resultData
     * @author :lyh
     * @method :post
     * @time   :2024/5/20 11:54
     */
    public function resultData($routeInfo,$data){
        if($routeInfo['source'] == RouteMap::SOURCE_PAGE){
            if($routeInfo['source_id']){
                $data = ['source'=>9,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_PRODUCT){
            if($routeInfo['source_id']){
                $data = ['source'=>2,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_PRODUCT_CATE){
            if($routeInfo['source_id']){
                $data = ['source'=>2,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_BLOG){
            if($routeInfo['source_id']){
                $data = ['source'=>3,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_BLOG_CATE){
            if($routeInfo['source_id']){
                $data = ['source'=>3,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_NEWS){
            if($routeInfo['source_id']){
                $data = ['source'=>4,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_NEWS_CATE){
            if($routeInfo['source_id']){
                $data = ['source'=>4,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>0];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_MODULE){
            if($routeInfo['source_id']){
                $data = ['source'=>7,'source_id'=>$routeInfo['source_id'],'is_list'=>0,'is_custom'=>1];
            }
        }
        if($routeInfo['source'] == RouteMap::SOURCE_MODULE_CATE){
            if($routeInfo['source_id']){
                $data = ['source'=>7,'source_id'=>$routeInfo['source_id'],'is_list'=>1,'is_custom'=>1];
            }
        }
        return $data;
    }

}