generated from liutk/owl-admin-base
107 lines
2.4 KiB
PHP
107 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\AppOs;
|
|
use App\Enums\AppUpdateStrategy;
|
|
use App\Traits\HasDateTimeFormatter;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class AppVersion extends Model
|
|
{
|
|
use Filterable, HasDateTimeFormatter, HasFactory;
|
|
|
|
protected $appends = [
|
|
'is_release',
|
|
];
|
|
|
|
protected $attributes = [
|
|
'is_force' => false,
|
|
];
|
|
|
|
protected $casts = [
|
|
'os' => AppOs::class,
|
|
'update_strategy' => AppUpdateStrategy::class,
|
|
'is_force' => 'bool',
|
|
'release_at' => 'datetime',
|
|
];
|
|
|
|
protected $fillable = [
|
|
'os',
|
|
'name',
|
|
'version',
|
|
'title',
|
|
'description',
|
|
'update_strategy',
|
|
'is_force',
|
|
'apk_url',
|
|
'wgt_url',
|
|
'release_at',
|
|
];
|
|
|
|
public function scopeOnlyReleased(Builder $query)
|
|
{
|
|
$query->whereNotNull('release_at')->where('release_at', '<=', now());
|
|
}
|
|
|
|
/**
|
|
* 是否是全量包升级
|
|
*/
|
|
public function isApkUpdate(): bool
|
|
{
|
|
return $this->update_strategy === AppUpdateStrategy::Apk;
|
|
}
|
|
|
|
/**
|
|
* 按客户端系统和版本号获取最新的全量包
|
|
*/
|
|
public static function getLatestApkUrl(AppOs $clientOs, int $clientVersion): string
|
|
{
|
|
return (string) static::OnlyReleased()
|
|
->where('os', $clientOs)
|
|
->where('version', '>', $clientVersion)
|
|
->whereNotNull('apk_url')
|
|
->latest('version')
|
|
->value('apk_url');
|
|
}
|
|
|
|
protected function apkUrl(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
set: function (mixed $value) {
|
|
if ((string) $value === '') {
|
|
$value = null;
|
|
}
|
|
|
|
return $value;
|
|
},
|
|
);
|
|
}
|
|
|
|
protected function wgtUrl(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
set: function (mixed $value) {
|
|
if ((string) $value === '') {
|
|
$value = null;
|
|
}
|
|
|
|
return $value;
|
|
},
|
|
);
|
|
}
|
|
|
|
protected function isRelease(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: function (mixed $value, $attributes) {
|
|
return (bool) $attributes['release_at'];
|
|
},
|
|
);
|
|
}
|
|
}
|