Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
xutl committed Apr 17, 2022
1 parent 0c4ad7a commit aec7225
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 112 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# laravel-baidu-ping

百度自动推送

[![Packagist](https://img.shields.io/packagist/l/larva/laravel-baidu-push.svg?maxAge=2592000)](https://packagist.org/packages/larva/laravel-baidu-push)
[![Total Downloads](https://img.shields.io/packagist/dt/larva/laravel-baidu-push.svg?style=flat-square)](https://packagist.org/packages/larva/laravel-baidu-push)


## Installation

```bash
Expand All @@ -17,12 +17,13 @@ composer require larva/laravel-baidu-push -vv
//add services.php
'baidu'=>[
//百度站长平台
'site' => '',//网站域名HTTPS网站需要包含 https://
'queue' => '',//异步处理的队列名称
'site_token' => '',//网站Token
]
```

## 使用

```php
\Larva\Baidu\Push\BaiduPush::push('https://www.aa.com');
\Larva\Baidu\Push\BaiduPush::push('https://www.aa.com/sss.html');
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php
/**
* This is NOT a freeware, use is subject to license terms.
*
* @copyright Copyright (c) 2010-2099 Jinan Larva Information Technology Co., Ltd.
*/

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
return new class() extends Migration {
/**
* Run the migrations.
*
Expand All @@ -15,14 +19,16 @@ public function up()
{
Schema::create('baidu_pushes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('url')->unique();
$table->string('type');
$table->string('site')->comment('网站');
$table->string('url')->comment('要推送的Url');
$table->string('type')->comment('推送类型');
$table->tinyInteger('status')->default(0)->nullable()->index();
$table->string('msg')->nullable();
$table->unsignedInteger('failures')->nullable()->default(0)->comment('失败计数');
$table->boolean('included')->nullable()->default(false)->comment('是否已收录');
$table->timestamp('push_at', 0)->nullable()->comment('推送时间');
$table->timestamp('created_at', 0)->nullable()->comment('创建时间');
$table->timestamp('push_at')->nullable()->comment('推送时间');
$table->timestamp('created_at')->nullable()->comment('创建时间');

$table->unique(['site', 'url']);
});
}

Expand Down
18 changes: 9 additions & 9 deletions src/BaiduPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,31 @@ class BaiduPush
/**
* 推送 Url 给百度
* @param string $url
* @return \Larva\Baidu\Push\Models\BaiduPush
* @return Models\BaiduPush
*/
public static function push(string $url)
{
return \Larva\Baidu\Push\Models\BaiduPush::firstOrCreate(['url' => $url, 'type' => \Larva\Baidu\Push\Models\BaiduPush::TYPE_SITE]);
return Models\BaiduPush::firstOrCreate(['url' => $url, 'type' => Models\BaiduPush::TYPE_SITE]);
}

/**
* 推送 Url 给百度
* @param string $url
* @return \Larva\Baidu\Push\Models\BaiduPush
* @return Models\BaiduPush
*/
public static function dailyPush(string $url)
{
return \Larva\Baidu\Push\Models\BaiduPush::firstOrCreate(['url' => $url, 'type' => \Larva\Baidu\Push\Models\BaiduPush::TYPE_DAILY]);
return Models\BaiduPush::firstOrCreate(['url' => $url, 'type' => Models\BaiduPush::TYPE_DAILY]);
}

/**
* 推送 Url 给百度
* @param string $url
*/
public static function update(string $url)
public static function update(string $url): void
{
if (($ping = \Larva\Baidu\Push\Models\BaiduPush::query()->where('url', '=', $url)->first()) != null) {
$ping->update(['status' => \Larva\Baidu\Push\Models\BaiduPush::STATUS_PENDING]);
if (($ping = Models\BaiduPush::query()->where('url', '=', $url)->first()) != null) {
$ping->update(['status' => Models\BaiduPush::STATUS_PENDING]);
UpdateJob::dispatch($ping);
} else {
static::push($url);
Expand All @@ -55,9 +55,9 @@ public static function update(string $url)
* @param string $url
* @throws \Exception
*/
public static function delete(string $url)
public static function delete(string $url): void
{
if (($ping = \Larva\Baidu\Push\Models\BaiduPush::query()->where('url', '=', $url)->first()) != null) {
if (($ping = Models\BaiduPush::query()->where('url', '=', $url)->first()) != null) {
$ping->delete();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/BaiduPushServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Larva\Baidu\Push;

use Illuminate\Support\ServiceProvider;
use Larva\Baidu\Push\Commands\Push;
use Larva\Baidu\Push\Commands\PushRetry;

/**
* Class BaiduPushServiceProvider
Expand All @@ -27,16 +29,14 @@ public function boot()
$this->commands('command.baidu.push.retry');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}

\Larva\Baidu\Push\Models\BaiduPush::observe(\Larva\Baidu\Push\Observers\BaiduPushObserver::class);
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
public function register(): void
{
$this->registerCommand();
}
Expand All @@ -45,14 +45,14 @@ public function register()
* Register the MNS queue command.
* @return void
*/
private function registerCommand()
private function registerCommand(): void
{
$this->app->singleton('command.baidu.push', function () {
return new \Larva\Baidu\Push\Commands\Push();
return new Push();
});

$this->app->singleton('command.baidu.push.retry', function () {
return new \Larva\Baidu\Push\Commands\PushRetry();
return new PushRetry();
});
}
}
3 changes: 3 additions & 0 deletions src/HasBaiduPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

namespace Larva\Baidu\Push;

use Illuminate\Database\Eloquent\Model;

/**
* 使用百度推送
* @mixin Model
* @property \Illuminate\Database\Eloquent\Model $this
* @author Tongle Xu <xutongle@gmail.com>
*/
Expand Down
15 changes: 5 additions & 10 deletions src/Jobs/DeleteJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,12 @@ class DeleteJob implements ShouldQueue
/**
* @var BaiduPush
*/
protected $baiduPush;
protected BaiduPush $baiduPush;

/**
* @var string
*/
protected $site;

/**
* @var string
*/
protected $token;
protected string $token;

/**
* Create a new job instance.
Expand All @@ -56,10 +51,10 @@ public function __construct(BaiduPush $baiduPush)
{
$this->baiduPush = $baiduPush;
if (function_exists('settings')) {
$this->site = config('app.url');
$this->onQueue(settings('baidu.queue', 'default'));
$this->token = settings('baidu.site_token');
} else {
$this->site = config('services.baidu.site');
$this->onQueue(config('services.baidu.queue', 'default'));
$this->token = config('services.baidu.site_token');
}
}
Expand All @@ -74,7 +69,7 @@ public function handle()
try {
Http::acceptJson()
->withBody($this->baiduPush->url, 'text/plain')
->post("http://data.zz.baidu.com/del?site={$this->site}&token={$this->token}");
->post("http://data.zz.baidu.com/del?site={$this->baiduPush->site}&token={$this->token}");
} catch (\Exception $e) {
Log::error($e->getMessage());
}
Expand Down
19 changes: 7 additions & 12 deletions src/Jobs/PushJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,17 @@ class PushJob implements ShouldQueue
*
* @var int
*/
public $tries = 2;
public int $tries = 2;

/**
* @var BaiduPush
*/
protected $baiduPush;
protected BaiduPush $baiduPush;

/**
* @var string
*/
protected $site;

/**
* @var string
*/
protected $token;
protected string $token;

/**
* Create a new job instance.
Expand All @@ -55,10 +50,10 @@ public function __construct(BaiduPush $baiduPush)
{
$this->baiduPush = $baiduPush;
if (function_exists('settings')) {
$this->site = config('app.url');
$this->onQueue(settings('baidu.queue', 'default'));
$this->token = settings('baidu.site_token');
} else {
$this->site = config('services.baidu.site');
$this->onQueue(config('services.baidu.queue', 'default'));
$this->token = config('services.baidu.site_token');
}
}
Expand All @@ -74,7 +69,7 @@ public function handle()
if ($this->baiduPush->type == BaiduPush::TYPE_SITE) {
$response = Http::acceptJson()
->withBody($this->baiduPush->url, 'text/plain')
->post("http://data.zz.baidu.com/urls?site={$this->site}&token={$this->token}");
->post("http://data.zz.baidu.com/urls?site={$this->baiduPush->site}&token={$this->token}");
if (isset($response['error'])) {
$this->baiduPush->setFailure($response['error'] . ':' . $response['message']);
} else {
Expand All @@ -83,7 +78,7 @@ public function handle()
} elseif ($this->baiduPush->type == BaiduPush::TYPE_DAILY) {
$response = Http::acceptJson()
->withBody($this->baiduPush->url, 'text/plain')
->post("http://data.zz.baidu.com/urls?site={$this->site}&token={$this->token}&type=daily");
->post("http://data.zz.baidu.com/urls?site={$this->baiduPush->site}&token={$this->token}&type=daily");
if (isset($response['error'])) {
$this->baiduPush->setFailure($response['error'] . ':' . $response['message']);
} else {
Expand Down
17 changes: 6 additions & 11 deletions src/Jobs/UpdateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,17 @@ class UpdateJob implements ShouldQueue
*
* @var int
*/
public $tries = 2;
public int $tries = 2;

/**
* @var BaiduPush
*/
protected $baiduPush;
protected BaiduPush $baiduPush;

/**
* @var string
*/
protected $site;

/**
* @var string
*/
protected $token;
protected string $token;

/**
* Create a new job instance.
Expand All @@ -55,10 +50,10 @@ public function __construct(BaiduPush $baiduPush)
{
$this->baiduPush = $baiduPush;
if (function_exists('settings')) {
$this->site = config('app.url');
$this->onQueue(settings('baidu.queue', 'default'));
$this->token = settings('baidu.site_token');
} else {
$this->site = config('services.baidu.site');
$this->onQueue(config('services.baidu.queue', 'default'));
$this->token = config('services.baidu.site_token');
}
}
Expand All @@ -73,7 +68,7 @@ public function handle()
try {
$response = Http::acceptJson()
->withBody($this->baiduPush->url, 'text/plain')
->post("http://data.zz.baidu.com/update?site={$this->site}&token={$this->token}");
->post("http://data.zz.baidu.com/update?site={$this->baiduPush->site}&token={$this->token}");
if (isset($response['error'])) {
$this->baiduPush->setFailure($response['error'] . ':' . $response['message']);
} else {
Expand Down
37 changes: 25 additions & 12 deletions src/Models/BaiduPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Larva\Baidu\Push\Jobs\DeleteJob;
use Larva\Baidu\Push\Jobs\PushJob;

/**
* 百度推送
* @property int $id ID
* @property string $type 推送类型
* @property string $site 网站Url
* @property string $url 推送Url
* @property int $status 推送状态
* @property string $msg 返回消息
* @property int $failures 失败次数
* @property bool $included 是否已经收录
* @property Carbon|null $push_at 推送时间
*
* @property-read boolean $failure 是否失败
Expand Down Expand Up @@ -61,7 +63,7 @@ class BaiduPush extends Model
* @var array
*/
protected $fillable = [
'url', 'type', 'status', 'msg', 'failures', 'push_at', 'included'
'site', 'url', 'type', 'status', 'msg', 'failures', 'push_at',
];

/**
Expand All @@ -70,9 +72,29 @@ class BaiduPush extends Model
* @var array
*/
protected $attributes = [
'status' => self::STATUS_PENDING
'status' => self::STATUS_PENDING,
'failures' => 0
];

/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (BaiduPush $model) {
$arr = parse_url($model->url);
$model->site = $arr['scheme'] . '://' . $arr['host'];
});
static::created(function (BaiduPush $model) {
PushJob::dispatch($model);
});
static::deleted(function (BaiduPush $model) {
DeleteJob::dispatch($model);
});
}

/**
* 为数组 / JSON 序列化准备日期。
*
Expand Down Expand Up @@ -133,13 +155,4 @@ public function setSuccess(): bool
{
return $this->update(['status' => static::STATUS_SUCCESS, 'msg' => 'ok', 'failures' => 0, 'push_at' => $this->freshTimestamp()]);
}

/**
* 设置为已收录
* @return bool
*/
public function setIncluded(): bool
{
return $this->update(['included' => true]);
}
}

0 comments on commit aec7225

Please sign in to comment.