laravel-admin 一个系统多个网址生成url
xuexi 2026-09-08 21:25:11 发表在:PHP 查看数:28
控制器代码
Encore\Admin\Grid\Displayers
<?php

namespace Encore\Admin\Grid\Displayers;

use Encore\Admin\Facades\Admin;

/**
 * Class QRCode.
 */
class QRCode extends AbstractDisplayer
{
    protected function addScript()
    {
        $script = <<<'SCRIPT'
$('.grid-column-qrcode').popover({
    html: true,
    container: 'body',
    trigger: 'focus'
});
SCRIPT;
        Admin::script($script);
    }

    public function display($formatter = null, $width = 150, $height = 150)
    {
        $this->addScript();

        $originalValue = $this->getColumn()->getOriginal();

        if ($formatter instanceof \Closure) {
            // 有闭包:交给外部完全自定义二维码内容,原始值、行对象传给闭包
            $content = call_user_func($formatter, $originalValue, $this->row);
        } else {
            // 没有传闭包,使用系统默认url
            $content = env('APP_URL').'/check?code='.$originalValue;
        }

        $img = sprintf(
            "<img src='https://api.qrserver.com/v1/create-qr-code/?size=%sx%s&data=%s' style='height:%spx;width:%spx;'/>",
            $width,
            $height,
            urlencode($content),
            $height,
            $width
        );

        return <<<HTML
<a href="javascript:void(0);" class="grid-column-qrcode text-muted" data-content="{$img}" data-toggle='popover' tabindex='0'>
    <i class="fa fa-qrcode"></i>
</a>&nbsp;{$this->getValue()}
HTML;
    }
}

```php
方案1
$grid->column('box_code','外箱编码')->qrcode(function($val, $row){
    // $val = box_code原始字段值
    return "shturl.cc/pa5LdHmhi6Dnz3prKatRZ".urlencode($val);
},180,180);

方案2:
$grid->column('sn','产品SN码')->qrcode(function($val){
    return "https://other.domain.com/scan?sn=".urlencode($val);
},160,160);
拿行里多个字段拼接 url:
$grid->column('code','编码')->qrcode(function($val,$row){
    return "shturl.cc/sRE3aMFM7cC".urlencode($val)."&b=".urlencode($row->other_field);
});

里面的teble

$grid->column('title', '内箱防伪码')->modal('内箱防伪码', function ($model) {
    $comments = $model->comments()
        ->orderBy('id', 'asc')
        ->take(1000)
        ->get()
        ->map(function ($comment, $index) {

            // 这里拼接二维码内容,你可以自定义url
            $qrText = env('APP_URL').'/check?code='.urlencode($comment->code);

            // 在线二维码图片
            $qrImgSrc = "https://api.qrserver.com/v1/create-qr-code/?size=140x140&data={$qrText}";

            $qrHtml = <<<HTML
<a href="javascript:void(0);" class="grid-column-qrcode text-muted" data-content="<img src='{$qrImgSrc}'/>" data-toggle='popover' tabindex='0'>
    <i class="fa fa-qrcode"></i>
</a>&nbsp;{$comment->code}
HTML;

            return [
                'sn'         => $index + 1,
                'id'         => $comment->id,
                'code'       => $qrHtml, // 输出带二维码图标的html
                'ci'         => $comment->ci,
                'created_at' => $comment->created_at
            ];
        });

    if ($comments->isEmpty()) {
        return '<div style="padding:20px;text-align:center;color:#999">暂无数据</div>';
    }

    $table = new Table(['序号','ID', '内箱防伪码','查询次数', '发布时间'], $comments->toArray());
    // 关闭转义!!否则html会原样输出字符串
    $table->escape = false;

    // 弹窗内需要引入popover脚本,否则点击二维码图标不弹出
    return <<<HTML
<div style="max-height:550px;overflow-y:auto;">
    {$table}
</div>
<script>
$('.grid-column-qrcode').popover({html:true, container: '.modal-body', trigger:'focus'});
</script>
HTML;
});
最近访问时间:2026-09-09 17:49:17
知识库:466条鸣谢:TAY  备案号:蜀ICP备2024090044号