PHPackages                             githen/laravel-common - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. githen/laravel-common

ActiveLibrary

githen/laravel-common
=====================

基于Laravel的公用方法

v1.0.48(5mo ago)11.2kMITPHP

Since May 17Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/jiaoyu-cn/laravel-common)[ Packagist](https://packagist.org/packages/githen/laravel-common)[ RSS](/packages/githen-laravel-common/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (49)Used By (0)

laravel-common
==============

[](#laravel-common)

基于laravel的通用组件

[![image](https://camo.githubusercontent.com/81d789f684bbe11acab02b1f9202b738d0b474abff8b9288ade25b838c662580/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6a69616f79752d636e2f6c61726176656c2d74656e63656e742d766f646c61726176656c2d636f6d6d6f6e)](https://github.com/jiaoyu-cn/laravel-common/stargazers)[![image](https://camo.githubusercontent.com/0bf4fe0459472cd8019bf5261e7195a3f4b6e99a5720ae6c0b1ecb2e5965d7da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f6a69616f79752d636e2f6c61726176656c2d636f6d6d6f6e)](https://github.com/jiaoyu-cn/laravel-common/network/members)[![image](https://camo.githubusercontent.com/21d4a5da344da7485ec71d2da789ce552cd5b3fa6c302d32721ffe860d0d1071/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6a69616f79752d636e2f6c61726176656c2d636f6d6d6f6e)](https://github.com/jiaoyu-cn/laravel-common/issues)

安装
--

[](#安装)

```
composer require githen/laravel-common:~v1.0.0
```

功能说明
----

[](#功能说明)

### 服务状态检测

[](#服务状态检测)

调用方法

```
// token => ***  为钉钉群机器人的access_token
// secret => ***  为钉钉群机器人的加签,不填则走关键词或IP
// 以上两个参数可复用`logging.channels.dingding.with`中配置，可直接在`listen`方法中第二个参数为`true`即可。

app('jiaoyu.common.process')->listen([
'process' => [
    'nginx' => 'Nginx服务', // key 为匹配关键词  name 为显示名称
    'artisan,shixun' => '多层过滤' // key中,为多层过滤 a,b  => grep a | grep b
    ],
 'token' => '***',
 'secret' => '***'
]);
```

可以在任务调度中直接使用

```
 $schedule->call(function (){
    app('jiaoyu.common.process')->listen(['process' => ['nginx' => 'nginx']], true);
 })->everyFiveMinutes()->runInBackground();
```

### 执行SQL

[](#执行sql)

添加路由

```
Route::match(['get', 'post'], 'sql/{act?}', '\\Githen\\LaravelCommon\\App\\Controllers\\SqlController@act')->name('sql.act');
```

1. 可在路由配置中添加以下配置，完成SQL操作的控制器注入，并可通过访问` http://host/sql/` 来执行。
2. 可查看当前配置的数据库连通情况（暂只支持 `mysql`,`mongo`,`redis`），通过访问` http://host/sql/check` 来查看

### 钉钉WebHook消息发送

[](#钉钉webhook消息发送)

钉钉手册地址：[官方手册](https://open.dingtalk.com/document/orgapp/custom-robot-access)

调用方法

```
// token => ***  为钉钉群机器人的access_token
// secret => ***  为钉钉群机器人的加签,不填则走关键词或IP
// ->***()  ***为目前支持的方法，可参考接下文档
app('jiaoyu.common.dingding', ['token' => '*****','secret' => '****'])->***();
```

#### 发送消息信息

[](#发送消息信息)

> text(string $message, bool $isAll = false, array $mobilds = \[\], array $userIds = \[\])

名称必填类型备注message是string发送的消息内容isAll否bool是否@群内所有人mobilds否array通过手机号@群内成员，不在群内成功将被过滤userIds滞array通过userid@群内成员，不在群内成功将被过滤#### 发送链接消息

[](#发送链接消息)

> link(string $title, string $text, string $url, string $picUrl = '')

名称必填类型备注title是string消息标题text是string消息内容,太长只显示部分内容url是string点击跳转地址picUrl是string图片地址#### 发送markdown类型

[](#发送markdown类型)

> markdown(string $title, string $text, bool $isAll = false, array $mobiles = \[\], array $userIds = \[\])

名称必填类型备注title是string消息标题text是string消息内容isAll否bool是否@群内所有人mobilds否array通过手机号@群内成员，不在群内成功将被过滤userIds滞array通过userid@群内成员，不在群内成功将被过滤#### 整体/独立跳转ActionCard

[](#整体独立跳转actioncard)

> actionCard(string $title, string $text, array $btns, string $btnOrientation = '')

名称必填类型备注title是string消息标题text是string消息内容,太长只显示部分内容btns是array点击按钮，结构 \[\['title' =&gt; xxx, actionURL=&gt; 'http:// \*\*\*\*\*'\]...\]btnOrientation否string按钮排序，0:按钮竖直排列 1：按钮横向排列#### FeedCard类型消息

[](#feedcard类型消息)

> feedCard(array $links)
>
> $links的结构如下：

```
[
    ['title' => '我是标题2'， 'messageURL' => 'https://链接地址2', 'picURL' => '图片的地址2'],
    ['title' => '我是标题1'， 'messageURL' => 'https://链接地址1', 'picURL' => '图片的地址1'],
    ...
]
```

名称必填类型备注links.title是string单条信息文本。links.messageURL是string点击单条信息到跳转链接links.picURL是string单条信息后面图片的URL### 日志发送到钉钉

[](#日志发送到钉钉)

在`config/logging.php`中添加新的配置项，以新建基于钉钉的日志通道（目前本地环境是关闭发送消息）。

可通过修改本地`config`中的`app.evn`来发送本地报警信息。

```
        'dingding' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'with' => ['token' => '钉钉机器人的access_token','secret' => '不填则走关键词或IP'],
            'handler' => \Githen\LaravelCommon\Log\Handler\DingdingLogHandler::class
        ],
```

若需要此配置生效，需要在`stack.channels`中添加`dingding`关键词

### 日志信息查看

[](#日志信息查看)

此扩展中已完成了基本的日志及进程等功能，需要在`routes/web.php`中添加路由来访问此控制器。

```
// log/{act} 中 log关键字可自定义，{act}不可修改
// 路由名可随意修改

Route::get('log/{act?}', '\\Githen\\LaravelCommon\\App\\Controllers\\LogController@act')->name('log.act');
```

### SSL证书到期检测

[](#ssl证书到期检测)

注：以定时任务每天进行触发，通知规则为离过期还有7天以内时，每天发送钉钉通知；若7-30天时则每周一发送通知。

调用方法

```
// $param['dir'] => '/...'  需要检测的证书的绝对地址
// $param['token'] => ***  为钉钉群机器人的access_token,不填走默认配置：logging.channels.dingding.with.token
// $param['secret'] => ***  为钉钉群机器人的加签,不填则走关键词或IP
// $isDing => true/false  快到期时是否发送钉钉群通知，true发送，false不发送
app('jiaoyu.common.process')->ssl(['dir' => $dir, 'token' => $token, 'secret' => $secret], $isDing);
```

### PHPWord生成docx文档

[](#phpword生成docx文档)

调整了PHPWord中`TemplateProcessor`生成图的一些样式问题，[官方文档](https://phpoffice.github.io/PHPWord/index.html)测试代码如下：

```
use Githen\LaravelCommon\Extend\PHPWord\TemplateProcessor;

// $docFile 为模板docx文件
$templateProcessor = new TemplateProcessor($docFile);
//替换单个变量
$templateProcessor->setValue('name', 'aaa');
// 批量替换变量
$templateProcessor->setValues([
    // 汇总数据
    'name' => '周报', // 名称
    'dateLoop' => date('Y-m-d',strtotime('-7 days')).'~'.date('Y-m-d'), // 巡检周期
]);

// 图表测试数据
$categories = array('网站', '微信', '微博', '百家号', '头条');
// 文章总数
$series1 = array(10, 30, 20, 50, 40);
// 风险数
$series2 = array(1, 13, 12, 5, 34);

// 柱状图
$chart = new Chart('column',$categories, $series1,null,'文章总数'); // 创建时需要给默认数据
$chart->getStyle()
    ->setWidth(Converter::inchToEmu(6)) // 设置图宽
    ->setHeight(Converter::inchToEmu(3)) // 设置图高
    ->setShowAxisLabels(true) // 显示坐标标签
    ->setColors(['00B0F0','92D050']) // 设置颜色，循环展示
    ->setShowLegend(true) // 显示图例
    ->setLegendPosition('b') // 图例位置
    ->setShowGridY(true) // 展示Y轴参考线
    ->setDataLabelOptions(['showCatName' => false]); // 柱状图不展示名称
$chart->addSeries($categories, $series2, '风险文章数'); // 添加第二组数据
$templateProcessor->setChart('articleChartBar', $chart); // 将图添加到文档中

// 饼图，只支持一组数据
$chart1 = new Chart('pie',$categories, $series1,null,'文章总数');
$chart1->getStyle()
    ->setWidth(Converter::inchToEmu(6))
    ->setHeight(Converter::inchToEmu(3))
    ->setShowAxisLabels(true)
    ->setColors(['00B0F0','92D050','ff0000','00ff00','0000ff'])
    ->setShowLegend(true)
    ->setLegendPosition('b')
    ->setShowGridY(true)
    ->setDataLabelOptions(['showCatName' => false]);
$templateProcessor->setChart('articleChartPie', $chart1);

// 表格渲染
$testData = [
    ['tableType' => '网站', 'point' => '12','articleT' => 10],
    ['tableType' => '网站', 'point' => '12','articleT' => 10],
    ['tableType' => '网站', 'point' => '12','articleT' => 10],
    ['tableType' => '网站', 'point' => '12','articleT' => 10],
    ['tableType' => '', 'point' => '总计','articleT' => 10],
];
$templateProcessor->cloneRowAndSetValues('tableType', $testData);

// 区块渲染，PHPWord中块渲染只支持文本
$chineseNumbers = ['','一', '二', '三', '四', '五', '六', '七', '八', '九'];
$testData =[
    '一类' => ['一类1' => ['risk' => 130, 'suspect' => 20], '一类2' => ['risk' => 30, 'suspect' => 20]],
    '二类' => ['二类1' => ['risk' => 230, 'suspect' => 20], '二类2' => ['risk' => 40, 'suspect' => 20]],
    '三类' => ['三类1' => ['risk' => 330, 'suspect' => 20], '三类2' => ['risk' => 30, 'suspect' => 20]],
];
// 不存在图，只有纯文本，未处理数据，只关注参数
$templateProcessor->cloneBlock('riskDetail',count($testData), true, false, $testData);

//存在图则需要先生成重复的块代码，再执行替换
$templateProcessor->cloneBlock('riskDetail',count($testData), true, true);
$detailData = [];
$maxText = [];

$i = 1;
$max = 0;
foreach ($testData as $name => $detail){
    $detailText = [];
    foreach ($detail as $type => $tmp){
        $detailText[] = $type."风险{$tmp['risk']}个，包括疑似风险{$tmp['suspect']}个";
        if ($max setValue('riskIndex#'.$i, $chineseNumbers[$i]);
    $templateProcessor->setValue('riskName#'.$i, $name);
    $templateProcessor->setValue('riskCount#'.$i, count($detail));
    $templateProcessor->setValue('riskMax#'.$i, implode("、",$maxText));
    $templateProcessor->setValue('riskText#'.$i, implode(";",$detailText));
    $templateProcessor->setChart('riskChart#'.$i, $chart);

    $i++;
}

//  保存文件
$templateProcessor->saveAs( storage_path('sample.docx'));
```

### 清除Opcache缓存

[](#清除opcache缓存)

1. 添加清理路由

```
//路由名称及协议不可变
// 路由名称只可以为opcache.clear或 api.opcache.clear
Route::get('opcache/clear', '\\Githen\\LaravelCommon\\App\\Controllers\\OpcacheController@clear')->name('opcache.clear');
```

2.注册命令

```
// laravel6
// 在app/Console/Kernel.php的$commands中添加引用
use Githen\LaravelCommon\Commands\OpcacheClear;
protected $commands = [
    OpcacheClear::class,
];

// laravel11
// 在bootstrap/app.php文件的withCommands中添加引用
use Githen\LaravelCommon\Commands\OpcacheClear;

 ->withCommands([
        OpcacheClear::class,
    ])
```

3. 项目根目录添加`opcache.sh`脚本，以执行清理操作

```
#!/bin/bash
cd "$(dirname "$0")"

nohup bash -c "sleep 10; php artisan jiaoyu:opcache-clear" >> storage/logs/opcache.log 2>&1 &
echo $! > storage/logs/opcache.pid
disown
```

3. 部署时执行 在composer.json中添加执行脚本,Host为本项目的名称，`http://127.0.0.1` 域名不可替换,协议根据实际情况调整

```
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi",
            "@bash opcache.sh"
        ]
    }
```

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance70

Regular maintenance activity

Popularity19

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 73.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~19 days

Total

48

Last Release

174d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/51a06d2f182ca37da5fc95e1fa8988248962856bc62e65dc0a5fbb420af3e9af?d=identicon)[githen](/maintainers/githen)

---

Top Contributors

[![githen-cn](https://avatars.githubusercontent.com/u/3197947?v=4)](https://github.com/githen-cn "githen-cn (38 commits)")[![nanjishidu](https://avatars.githubusercontent.com/u/15984590?v=4)](https://github.com/nanjishidu "nanjishidu (13 commits)")[![pengzworld](https://avatars.githubusercontent.com/u/98011402?v=4)](https://github.com/pengzworld "pengzworld (1 commits)")

### Embed Badge

![Health badge](/badges/githen-laravel-common/health.svg)

```
[![Health](https://phpackages.com/badges/githen-laravel-common/health.svg)](https://phpackages.com/packages/githen-laravel-common)
```

###  Alternatives

[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[abydahana/aksara

Aksara is a CodeIgniter based CRUD Toolkit you can use to build complex applications become shorter, secure and more reliable just in a few lines of code. Serving both CMS or Framework, produce both HEADLESS (RESTful API) or TRADITIONAL (Browser Based), just by writing single controller. Yet it's reusable, scalable and ready to use!

1101.2k](/packages/abydahana-aksara)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
