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 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 ([ $ios = AppVersion::onlyReleased()
AppOs::Android, ->where('os', AppOs::Ios)
AppOs::Ios, ->latest('version')
] as $os) { ->first();
/** @var \App\Models\AppVersion|null */
$appVersion = AppVersion::onlyReleased()
->where('os', $os)
->latest('version')
->first();
if (is_null($appVersion)) { switch ($os) {
continue; 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 ($apkUpdateVersion) {
$android = $apkUpdateVersion;
// 如果客户端版本和最新的版本之前有全量包更新的版本,则需全量包更新
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);
} }
} }
} break;
// 如果客户端版本和最新的版本之间有强制更新的版本,则需强制更新 case AppOs::Ios:
if (! $appVersion->is_force && $cliOs === $os && $appVersion->version > $cliVersion + 1) { if (! $ios->isApkUpdate()) {
$json['is_force'] = AppVersion::onlyReleased() $apkUpdateVersion = AppVersion::onlyReleased()
->where('os', $cliOs) ->where('os', AppOs::Ios)
->where('version', '>', $cliVersion) ->where('version', '>', $version)
->where('is_force', true) ->where('update_strategy', AppUpdateStrategy::Apk)
->exists(); ->latest('version')
} ->first();
$data[$os->value] = $json; if ($apkUpdateVersion) {
$ios = $apkUpdateVersion;
}
}
break;
} }
return $data; return ['android' => $android, 'ios' => $ios];
} }
} }