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 | 留下评论

Mysql索引太好用了

以前数据库计量小,几万条顶天了,最近在股票日线,差不多要160万条的数据没有索引,慢得不得了,差不多vps查个k线要2秒起步 。我一直以为是VPS性能太差,结果加上索引就到几百ms完成。

适合查大量的数据 按照日期排列

添加一个索引 mcode 和date

ALTER TABLE cmf_quant ADD INDEX idx_mcode_date (mcode, date);

添加索引并按照分数排列

ALTER TABLE kline_day
ADD INDEX idx_date_score (trade_date, score DESC);

查看当前索引

SHOW INDEX FROM cmf_quant;

删除索引

DROP INDEX 索引名 ON 表名;

DROP INDEX idx_mcode_date ON cmf_quant;

唯一索引:UNIQUE 关键词

ALTER TABLE cmf_quant ADD UNIQUE INDEX uk_code_date (code, date); 

发表在 None | 留下评论