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