fastadmin接口输出图片 自动拼接网站URL

先自定义常量

复制代码
1.文件接口路径 修改核心文件  application\common\controller\Api.php
    /**
     * 构造方法
     * @access public
     * @param Request $request Request 对象
     */
    public function __construct(Request $request = null)
    {
        $this->request = is_null($request) ? Request::instance() : $request;

        // 控制器初始化
        $this->_initialize();
       //<--代码位置 -->
        if (!defined('IMG_PRE')) {
            define("IMG_PRE", $this->request->domain());//定义获取域名常量
//            define("IMG_PRE", 'https://yunlingshidai.oss-cn-shanghai.aliyuncs.com');//也可以是云端域名
        }
        //<--代码位置 -->
        // 前置操作方法
        if ($this->beforeActionList) {
            foreach ($this->beforeActionList as $method => $options) {
                is_numeric($method) ?
                    $this->beforeAction($options) :
                    $this->beforeAction($method, $options);
            }
        }
    }

ffastadmin 接口输出图片

复制代码
    //修改器
    public function getImageAttr($value)
    {
        if(empty($value)){
            return '';
        }
        $domain=defined("IMG_PRE")?IMG_PRE:'';
        $preg = "/^http(s)?:\\/\\/.+/";
        if(preg_match($preg,$value))
        {
            $data=$value;
        }else{
            $data=$domain.$value;
        }
        return $data;
    }
相关推荐
I'm Jie19 小时前
【已解决】SqlAlchemy 插入 MySQL JSON 字段时 None 变为 ‘null‘ 字符串,WHERE IS NULL 失效
数据库·python·mysql·json·fastapi·sqlalchemy
Li emily1 天前
成功接入A股实时行情API获取实时市场数据
人工智能·python·金融·fastapi
岱宗夫up3 天前
什么是FastAPI?
前端·python·前端框架·fastapi
BestSongC3 天前
行人摔倒检测系统 - 后端文档(1)
vue.js·人工智能·yolo·目标检测·fastapi
feasibility.3 天前
打造实时电商数据大屏:Vue 3 + FastAPI + WebSocket
大数据·前端·javascript·vue.js·websocket·fastapi·数据可视化
dyxal3 天前
FastAPI-MCP:让AI轻松“对话”你的后端服务
人工智能·fastapi
栀椩3 天前
Docker容器化部署FastAPI项目:从开发到生产实践
docker·容器·fastapi
PD我是你的真爱粉4 天前
FastAPI依赖注入
python·fastapi
青翎_4 天前
我把编程老师 “装” 进电脑了:用 AI 从 0 学会编程,每天只要 1 小时
人工智能·后端·python·mysql·fastapi
恒云客5 天前
FastAPI 容器化UV部署
前端·fastapi·uv