RouteMap.php 5.7 KB
<?php

namespace App\Models\RouteMap;

use App\Helper\Translate;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Base;
use App\Models\Project\Project;
use App\Models\Template\BTemplate;

/**
 * 路由映射表
 * Class RouteMap
 * @package App\Http\Models
 * @author zbj
 * @date 2023/4/17
 */
class RouteMap extends Base
{
    //设置关联表名
    protected $table = 'gl_route_map';
    //连接数据库
    protected $connection = 'custom_mysql';
    //路由类型
    const SOURCE_PRODUCT = 'product';
    const SOURCE_PRODUCT_CATE = 'product_category';
    const SOURCE_PRODUCT_KEYWORD = 'product_keyword';
    const SOURCE_PAGE = 'page';  //单页面
    const SOURCE_INDEX = 'index';  //首页
    const SOURCE_BLOG = 'blog';
    const SOURCE_BLOG_CATE = 'blog_category';
    const SOURCE_NEWS = 'news';
    const SOURCE_NEWS_CATE = 'news_category';
    //自定义模块
    const SOURCE_MODULE = 'module';
    const SOURCE_AI_BLOG = 'ai_blog';
    const SOURCE_AI_VIDEO = 'ai_video';
    const SOURCE_AI_BLOG_AUTHOR = 'ai_blog_author';//ai博客作者
    const SOURCE_AI_BLOG_LIST = 'ai_blog_list';
    //自定义模块分类
    const SOURCE_MODULE_CATE = 'module_category';

    //路由二级目录
    const PATH_NEWS_CATE = 'news_catalog';
    const PATH_BLOG_CATE = 'blog_catalog';
    const PATH_MODULE_CATE = 'module_category_route';//扩展模块

    const SOURCE_NAV = 'nav';

    /**
     * @remark :(新增与编辑)处理路由route
     * @name   :generateRoute
     * @author :lyh
     * @method :post
     * @time   :2025/3/12 9:30
     * @param  :title:路由 source:模块类型 source_id:对应数据id
     * @notes  :route_len:路由长度; suffix:路由后缀
     */
    public static function generateRoute($title, $source, $source_id, $project_id){
        //发现路由是.html结尾时,不做任何处理
        $last5Chars = substr($title, -5);
        if($last5Chars == '.html'){
            return $title;
        }
        //处理其他情况(俄语+中文时翻译为英文)
        if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $title) || contains_russian($title)){
            $title = Translate::tran($title, 'en');
        }
        //过滤特殊字符
        $sign = generateRoute($title);
        //查看当前数据路由是否存在
        $routeInfo = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first();
        $suffix = '';//设置路由后缀
        $route_len = 180;//默认设置字符为180
        if(empty($routeInfo)){
            if($source == self::SOURCE_PRODUCT){
                $suffix = '-product';
            }
            $route_len = 65;
        }
        $length = strlen($sign);
        if($length > $route_len){
            $sign = trim(mb_substr($sign, 0, $route_len, 'UTF-8'),'-');
        }
        $i=1;//路由重复时拼接
        $route = $sign.$suffix;
        while(self::isExist($route, $source_id, $project_id)){
            $route = $sign .'-'.$i.$suffix;
            $i++;
        }
        return $route;
    }

    /**
     * @remark :验证路由是否重复
     * @name   :isExist
     * @author :lyh
     * @method :post
     * @time   :2025/3/12 9:51
     * @param  :route:路由 source:模块类型 source_id:对应数据id
     */
    protected static function isExist($route, $source_id, $project_id){
        $fixed = ['api']; //固定的路由
        if(in_array($route, $fixed)){
            return true;
        }
        $where = [
            'project_id' => $project_id, 'route' => $route,
        ];
        $route = self::where($where)->first();
        if($route){
            if($route->source_id != $source_id){
                return true;
            }
        }
        return false;
    }

    /**
     * @remark :保存路由
     * @name   :setRoute
     * @author :lyh
     * @method :post
     * @time   :2025/3/12 9:54
     * @param  :title:路由 source:模块类型 source_id:对应数据id
     */
    public static function setRoute($title, $source, $source_id, $project_id = 0){
        $route = self::generateRoute($title, $source, $source_id, $project_id);
        if(!$route){
            return time().$project_id;
        }
        try {
            $route_map = self::where(['project_id' => $project_id, 'source' => $source, 'source_id'=>$source_id])->first();
            if(!$route_map){
                $route_map = new self();
                $route_map->source = $source;
                $route_map->source_id = $source_id;
                $route_map->project_id = $project_id;
            }
            $route_map->route = $route;
            if($source == self::SOURCE_NEWS){
                $route_map->path = self::SOURCE_NEWS;
            }elseif ($source == self::SOURCE_BLOG){
                $route_map->path = self::SOURCE_BLOG.'s';
            }elseif($source == self::SOURCE_MODULE){
                $route_map->path = self::PATH_MODULE_CATE;
            }
            $route_map->save();
        }catch (\Exception $e){
            throw new \Exception('路由映射失败');
        }
       return $route;
    }

    /**
     * @param $source
     * @param $source_id
     * @param $project_id
     * @return mixed
     * @author zbj
     * @date 2023/4/17
     */
    public static function getRoute($source, $source_id, $project_id){
        return self::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->value('route');
    }

    /**
     * @param $source
     * @param $source_id
     * @param $project_id
     * @return mixed
     * @author zbj
     * @date 2023/4/17
     */
    public static function delRoute($source, $source_id, $project_id){
        return self::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->delete();
    }
}