Files
sbnews/server/app/api/controller/AppVersionController.php
2026-06-11 12:15:29 +08:00

116 lines
4.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\api\controller;
use app\common\model\app\AppVersion;
use think\response\Json;
/**
* APP 版本检查(无需登录)
* Class AppVersionController
* @package app\api\controller
*/
class AppVersionController extends BaseApiController
{
public array $notNeedLogin = ['check', 'latest'];
/**
* @notes 获取各平台最新可用版本(供下载页使用,无需登录)
* 请求示例:GET /appVersion/latest
* 返回:{ android: {...}, ios: {...} }
*/
public function latest(): Json
{
$result = ['android' => null, 'ios' => null];
foreach ([1 => 'android', 2 => 'ios'] as $platform => $key) {
$version = AppVersion::where('platform', $platform)
->where('status', 1)
->where('package_type', '<>', 1) // 排除 wgt 热更包,只取整包
->order(['version_code' => 'desc', 'id' => 'desc'])
->append(['platform_text', 'package_type_text'])
->find();
if ($version) {
$result[$key] = [
'version_name' => $version['version_name'],
'version_code' => $version['version_code'],
'package_type' => $version['package_type'],
'package_type_text' => $version['package_type_text'],
'download_url' => $version['download_url'],
'title' => $version['title'],
'update_content' => $version['update_content'],
'create_time' => $version['create_time'],
];
}
}
return $this->data($result);
}
/**
* @notes 检查 APP 是否有新版本
* 请求示例:GET /appVersion/check?platform=1&version_code=1012&package_type=1
* - platform: 1=Android 2=iOS(必填)
* - version_code: 当前 APP 版本数字(必填)
* - package_type: 1=wgt热更 2=apk整包 3=ipa(可选,传则只比对该包类型)
*/
public function check(): Json
{
$platform = (int) $this->request->get('platform/d', 0);
$versionCode = (int) $this->request->get('version_code/d', 0);
$packageType = (int) $this->request->get('package_type/d', 0);
if (!in_array($platform, [1, 2])) {
return $this->fail('参数错误:platform');
}
if ($versionCode <= 0) {
return $this->fail('参数错误:version_code');
}
$query = AppVersion::where('platform', $platform)
->where('status', 1)
->where('version_code', '>', $versionCode);
if ($packageType > 0) {
$query->where('package_type', $packageType);
}
// wgt 热更需校验原生最低版本(min_version_code <= 当前version_code
// 同时找出符合条件的最新版本
$latest = $query->order(['version_code' => 'desc', 'id' => 'desc'])
->append(['platform_text', 'package_type_text', 'update_type_text'])
->find();
if (empty($latest)) {
return $this->data([
'has_update' => false,
]);
}
// wgt 包:当前 APP 原生版本必须 >= min_version_code
if ($latest['package_type'] == 1 && $latest['min_version_code'] > 0 && $versionCode < $latest['min_version_code']) {
// 当前原生版本太低无法 wgt 热更,返回无更新(让用户走整包升级路径)
return $this->data([
'has_update' => false,
]);
}
return $this->data([
'has_update' => true,
'id' => $latest['id'],
'version_name' => $latest['version_name'],
'version_code' => $latest['version_code'],
'min_version_code' => $latest['min_version_code'],
'platform' => $latest['platform'],
'platform_text' => $latest['platform_text'],
'package_type' => $latest['package_type'],
'package_type_text' => $latest['package_type_text'],
'update_type' => $latest['update_type'],
'update_type_text' => $latest['update_type_text'],
'download_url' => $latest['download_url'],
'title' => $latest['title'],
'update_content' => $latest['update_content'],
]);
}
}