main
Jing Li 2024-05-05 22:48:22 +08:00
parent cc696f9efe
commit e713c4c38a
1 changed files with 36 additions and 40 deletions

View File

@ -13,56 +13,52 @@ class AppVersionController extends Controller
public function latest(Request $request): array
{
// 客户端系统
$cliOs = AppOs::tryFrom((string) $request->header('app-cli-os'));
$os = AppOs::tryFrom((string) $request->header('app-cli-os'));
// 客户端版本
$cliVersion = (int) $request->header('app-cli-version', 0);
$version = (int) $request->header('app-cli-version', 0);
$data = ['android' => null, 'ios' => null];
$android = AppVersion::onlyReleased()
->where('os', AppOs::Android)
->latest('version')
->first();
foreach ([
AppOs::Android,
AppOs::Ios,
] as $os) {
/** @var \App\Models\AppVersion|null */
$appVersion = AppVersion::onlyReleased()
->where('os', $os)
->latest('version')
->first();
$ios = AppVersion::onlyReleased()
->where('os', AppOs::Ios)
->latest('version')
->first();
if (is_null($appVersion)) {
continue;
}
switch ($os) {
case AppOs::Android:
if (! $android->isApkUpdate()) {
$apkUpdateVersion = AppVersion::onlyReleased()
->where('os', AppOs::Android)
->where('version', '>', $version)
->where('update_strategy', AppUpdateStrategy::Apk)
->latest('version')
->first();
$json = AppVersionResource::make($appVersion)->resolve($request);
// 如果客户端版本和最新的版本之前有全量包更新的版本,则需全量包更新
if (! $appVersion->isApkUpdate() && $cliOs === $os && $appVersion->version > $cliVersion + 1) {
$isApkUpdate = AppVersion::onlyReleased()
->where('os', $cliOs)
->where('version', '>', $cliVersion)
->where('update_strategy', AppUpdateStrategy::Apk)
->exists();
if ($isApkUpdate) {
$json['update_strategy'] = AppUpdateStrategy::Apk;
if ((string) $appVersion->apk_url === '') {
$json['apk_url'] = AppVersion::getLatestApkUrl($cliOs, $cliVersion);
if ($apkUpdateVersion) {
$android = $apkUpdateVersion;
}
}
}
break;
// 如果客户端版本和最新的版本之间有强制更新的版本,则需强制更新
if (! $appVersion->is_force && $cliOs === $os && $appVersion->version > $cliVersion + 1) {
$json['is_force'] = AppVersion::onlyReleased()
->where('os', $cliOs)
->where('version', '>', $cliVersion)
->where('is_force', true)
->exists();
}
case AppOs::Ios:
if (! $ios->isApkUpdate()) {
$apkUpdateVersion = AppVersion::onlyReleased()
->where('os', AppOs::Ios)
->where('version', '>', $version)
->where('update_strategy', AppUpdateStrategy::Apk)
->latest('version')
->first();
$data[$os->value] = $json;
if ($apkUpdateVersion) {
$ios = $apkUpdateVersion;
}
}
break;
}
return $data;
return ['android' => $android, 'ios' => $ios];
}
}