import React,{useEffect,useRef,useState} from 'react'; import {ActivityIndicator,Pressable,ScrollView,Text,View} from 'react-native'; import {SafeAreaProvider,SafeAreaView} from 'react-native-safe-area-context'; import * as FileSystem from 'expo-file-system/legacy'; import * as SecureStore from 'expo-secure-store'; import {getOfficialPackage,officialAppOrigin,officialAppInternalOrigin,officialDownloadOrigin,type OfficialAppPackage} from './core/official-app-update'; import {downloadLegacyPackage} from './core/legacy-package-download'; /** Compatibility entry: verified download inside the app, then Android performs the signed upgrade. */ export default function LegacyUpgrade(){ const [release,setRelease]=useState(null),[error,setError]=useState(''),[attempt,setAttempt]=useState(0),[busy,setBusy]=useState(false),[progress,setProgress]=useState(0),[phase,setPhase]=useState('正在下载'); const origin=useRef(officialAppOrigin),ctl=useRef(null),alive=useRef(true),locked=useRef(false); const [selectedOrigin,setSelectedOrigin]=useState(officialAppOrigin); useEffect(()=>{alive.current=true;const controller=new AbortController(),timer=setTimeout(()=>controller.abort(),15000);setError('');setRelease(null); void(async()=>{const selected=await SecureStore.getItemAsync('cmvr.prod.connection').catch(()=>null);if(controller.signal.aborted)throw Error('取消检查');try{origin.current=officialDownloadOrigin((selected||officialAppOrigin).replace(/\/prod-api$/,''));}catch{origin.current=officialAppOrigin;}if(alive.current)setSelectedOrigin(origin.current);return getOfficialPackage(controller.signal,undefined,fetch,origin.current);})().then(value=>{if(alive.current&&!controller.signal.aborted)setRelease(value);}).catch(()=>{if(alive.current&&!controller.signal.aborted)setError('暂时无法连接更新服务,请检查当前内网或外网连接后重试。');}).finally(()=>clearTimeout(timer)); return()=>{alive.current=false;controller.abort();ctl.current?.abort();clearTimeout(timer);};},[attempt]); async function install(){if(!release||locked.current)return;locked.current=true;setBusy(true);setError('');setProgress(0);const controller=new AbortController();ctl.current=controller; try{const uri=await downloadLegacyPackage(release,origin.current,FileSystem,controller.signal,(stage,value)=>{if(alive.current){setPhase(stage==='verifying'?'正在校验':'正在下载');setProgress(value);}});if(controller.signal.aborted||!alive.current)return;const {File}=await import('expo-file-system');await new File(uri).preview({mimeType:'application/vnd.android.package-archive'});} catch(e){if(alive.current)setError(e instanceof Error?e.message:'更新未完成,请重试。');}finally{locked.current=false;if(alive.current)setBusy(false);} } async function changeConnection(value:string){if(busy)return;try{await SecureStore.setItemAsync('cmvr.prod.connection',officialDownloadOrigin(value)+'/prod-api');setSelectedOrigin(value);setAttempt(n=>n+1);}catch{setError('未能保存连接方式,请重试。');}} return 设备科综合管理系统升级至正式新版 {[[officialAppOrigin,'外网'],[officialAppInternalOrigin,'公司内网']].map(([value,label])=>void changeConnection(value)} style={{flex:1,padding:12,borderRadius:8,alignItems:'center',backgroundColor:selectedOrigin===value?'#3563d9':'#edf2fb'}}>{label})} 安装包在应用内下载,沿用本机保存的内网或外网连接,下载完成后由手机系统确认安装。 {release?<>版本 {release.versionName} · {(release.size/1024**2).toFixed(1)} MB{release.notes.map((note,index)=>• {note})}:!error?:null} {busy&&{phase} {progress}%} {!!error&&{error}} {if(busy)ctl.current?.abort();else void install();}} style={{backgroundColor:!release?'#aac0f3':'#3563d9',paddingVertical:14,borderRadius:10,alignItems:'center'}}>{busy?'取消下载':'下载并安装'} {!busy&&setAttempt(n=>n+1)} style={{padding:10,alignItems:'center'}}>重新检查} ; }