Thinkphp 坑点 Join 和with 不可连用

Join 下如果使用模型关联 即使用with会导致生成的SQL语句奇怪,即会把关联的ID混用,非常非常奇怪!!!
做search查询直接用join 即可

【不要使用关联 和with 再不行就用子查询】

发表在 None | 留下评论

thinkcmf 补齐 service

Model数据 用于关联

Service 处理数据逻辑 方便复用

Controller 处理请求调用Service

V 模版

Controller调用Service使用注入

Service 分页

#此处使用注入,方便测试 里面可以直接调用
public function index(UserService $userService)
{
    $page = $this->request->get('page', 1);
    
    // 只调用Service,不干别的
    $data = $userService->getList($page);
    
    $this->assign('data', $data);
    return $this->fetch();
}

service

 public function getList(int $page, int $limit=100): array
    {
        // 分页查询:paginate() 是 TP6 核心方法,CMF6 完全兼容
        /** @var Paginator $paginate */
        $paginate = User::where('status', 1)
            ->order('id', 'desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);

        // 格式化返回(适配前端/模板)
        return [
            'total' => $paginate->total(), // 总条数
            'list' => $paginate->items(),   // 当前页数据
            'page' => $paginate->currentPage(), // 当前页
            'limit' => $paginate->listRows(),   // 每页条数
            'pages' => $paginate->lastPage(),   // 总页数
        ];
    }



#或者简单点 不要花里胡哨

public function getList(int $page, int $limit=100)
{
    // 就这一句是分页核心
    return User::where('status',1)->order('id desc')->paginate($limit);
}
发表在 None | 留下评论

thinkcmf thinkphp6 原生JSON

code 的值 1 为success 值0为error

{
    "code": 0,
    "msg": "保存成功",
    "url": "/user/index",
    "data": {
        "name": "test"
    }
}

return $this->success(‘操作成功’, ”, $data);

文字、 URL 、数据;

发表在 None | 留下评论

acme.sh 添加SSL

首先LNMP下的SSL添加有问题 不如直接使用acme.sh反正也是用他的

安装:

curl https://get.acme.sh | sh -s email=your@email.com
#需要设置你的邮箱
source ~/.bashrc

acme.sh -v
#查看安装情况

使用letsencrypt

acme.sh --set-default-ca --server letsencrypt

修改你的nginx的conf

设置:.well-know (lnmp 里面的需要修改) 请注释掉之前的/.well-known/然后插入新的

location ^~ /.well-known/acme-challenge/ {
            allow all;
            default_type text/plain;
            root /home/wwwroot/quant.comic.org.cn;
        }

然后 lnmp nginx restart 实现nginx重启

acme.sh --issue -d abc.cn -w /home/wwwroot/abc.cn

安装好了会显示地址:

[Sun Apr 12 04:54:22 PM CST 2026] Your cert is in: /root/.acme.sh/abc.cn_ecc/abc.cn.cer
[Sun Apr 12 04:54:22 PM CST 2026] Your cert key is in: /root/.acme.sh/abc.cn_ecc/abc.cn.key
[Sun Apr 12 04:54:22 PM CST 2026] The intermediate CA cert is in: /root/.acme.sh/abc.cn_ecc/ca.cer
[Sun Apr 12 04:54:22 PM CST 2026] And the full-chain cert is in: /root/.acme.sh/abc.cn_ecc/fullchain.cer

#其中abc.cn是你的证书文件位置 在root的.acme.sh下面

nginx下创建ssl目录

mkdir /usr/local/nginx/conf/ssl

一键移动证书到NGINX

acme.sh --install-cert -d abc.cn \
--key-file /usr/local/nginx/conf/ssl/abc.cn.key \
--fullchain-file /usr/local/nginx/conf/ssl/abc.cn.crt \
--reloadcmd "nginx -t && service nginx reload"

重新配置conf 在lnmp的域名下配置

server
{
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name abc.cn;

    root  /home/wwwroot/abc.cn/public;
    index index.html index.htm index.php default.html default.htm default.php;

    # SSL 证书位置
    ssl_certificate /usr/local/nginx/conf/ssl/abc.cn.crt;
    ssl_certificate_key /usr/local/nginx/conf/ssl/abc.cn.key;

    include rewrite/thinkphp.conf;
    include enable-php-pathinfo.conf;

    location ~ .*\.(gif|jpg|jpeg,png,bmp,swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js,css)?$
    {
        expires      12h;
    }

    location ~ /\.
    {
        deny all;
    }

    access_log  /home/wwwlogs/abc.cn.log;
}

配置443端口

ufw allow 443

查看443 端口

netstat -lntp | grep 443

腾讯云 阿里云的话可能要在云服务器配置中打开443 端口

发表在 None | 留下评论

Thinkphp6下两表多个键相互关联

A 表 date mcode

B表 old_date mcode

上面两个表相互关联 A一对多B

Thinkphp6 下需要写成【大道至简 使用where 和$this->键】


public function today()
{
    return $this->hasMany(QuantnModel::class)
        ->where('mcode', $this->mcode)
        ->where('old_date', $this->date);
}

添加索引:

ALTER TABLE cmf_quantn ADD INDEX idx_mcode_old_date (mcode, old_date);

发表在 None | 留下评论