RN 热更更新

46 阅读1分钟
import CodePush from 'react-native-code-push'
import { env } from '@/config/env'
import { EnvTypes } from '@/config/AppConfig'
import { Alert, Platform } from 'react-native'
import { setItem } from '@/utils/storage'
import { EventTypes } from 'react-native-gesture-handler/lib/typescript/web/interfaces'
import OpenNative from './OpenNative'

export const codePushLogKey = {
	then: 'then',
	then_start_updata: 'then_start_updata',
	then_has_updata: 'then_hasUpdata',
	then_no_updata: 'no_update',
	then_start_async: 'then_start_async',
	err: 'err',

	async_check_has: 'async_check_has',
	async_download: 'async_download',
	async_user_action: 'async_install',
	async_cancel: 'async_cancel',
	async_install: 'async_install',
	async_up_to_date: 'async_error',
	async_installed: 'async_installed',
	async_error: 'async_error',
}
function startReleaseLog(key: string, value: string) {
	if (env !== EnvTypes.prod) {
		console.log('codePush_', value)
		OpenNative.showToast(value)
		setItem('codePush_' + codePushLogKey.async_check_has, value)
	}
}
const codePushStatusDidChange = (syncStatus: CodePush.SyncStatus) => {
	switch (syncStatus) {
		case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
			startReleaseLog(codePushLogKey.async_check_has, '有更新')
			break
		case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
			startReleaseLog(codePushLogKey.async_download, '下载安装包')
			break
		case CodePush.SyncStatus.AWAITING_USER_ACTION:
			startReleaseLog(codePushLogKey.async_user_action, '等待用户协作')
			break
		case CodePush.SyncStatus.INSTALLING_UPDATE:
			startReleaseLog(codePushLogKey.async_install, '安装中')
			break
		case CodePush.SyncStatus.UP_TO_DATE:
			startReleaseLog(codePushLogKey.async_user_action, '已经是最新版本了')
			break
		case CodePush.SyncStatus.UPDATE_IGNORED:
			startReleaseLog(codePushLogKey.async_cancel, '已经是最新版本了')
			break
		case CodePush.SyncStatus.UPDATE_INSTALLED:
			startReleaseLog(codePushLogKey.async_installed, '更新完成')
			break
		case CodePush.SyncStatus.UNKNOWN_ERROR:
			startReleaseLog(codePushLogKey.async_error, '未知错误')
			break
	}
}

const codePushDownloadDidProgress = (progress: any) => {
	if (!progress?.receivedBytes) {
		return
	}
	if (!progress?.totalBytes) {
		return
	}
	if (progress) {
		let currProgress = parseFloat(progress.receivedBytes / progress.totalBytes).toFixed(2)
		if (currProgress >= 1) {
		}
	}
}

export const checkHotUpdate = () => {
	if (__DEV__ || (env !== EnvTypes.prod && env !== EnvTypes.hot)) {
		return
	}
	CodePush.checkForUpdate()
		.then((update) => {
			startReleaseLog(codePushLogKey.then, 'code-push-then')
			if (update) {
				startReleaseLog(codePushLogKey.then_has_updata, '有更新')
				if (Platform.OS === 'ios') {
					//苹果必须采用静默更新
					CodePush.sync(
						{ updateDialog: undefined, installMode: CodePush.InstallMode.ON_NEXT_RESTART },
						codePushStatusDidChange,
						codePushDownloadDidProgress
					)
				} else {
					CodePush.sync(
						{ updateDialog: undefined, installMode: CodePush.InstallMode.IMMEDIATE },
						codePushStatusDidChange,
						codePushDownloadDidProgress
					)
				}
			} else {
				startReleaseLog(codePushLogKey.then_no_updata, 'codepush-没有更新')
			}
		})
		.catch((e) => {
			console.log('codePush_', e)
			startReleaseLog(codePushLogKey.err, 'err检测更新介面請求失敗' + e.toString())
		})
}