1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

避免在debug模式下调用

This commit is contained in:
sunnylqm 2019-10-04 22:27:33 +08:00
parent c40bf71032
commit 08ac354ba5
3 changed files with 35 additions and 23 deletions

View File

@ -49,6 +49,10 @@ export default class App extends Component {
};
checkUpdate = () => {
if (__DEV__) {
// 开发模式不支持热更新,跳过检查
return;
}
checkUpdate(appKey).then(info => {
if (info.expired) {
Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [
@ -63,7 +67,7 @@ export default class App extends Component {
]);
}
}).catch(err => {
Alert.alert('提示', '检查更新失败.');
console.warn(err);
});
};

View File

@ -87,7 +87,7 @@ import _updateConfig from './update.json';
const {appKey} = _updateConfig[Platform.OS];
class MyProject extends Component {
componentWillMount(){
componentDidMount(){
if (isFirstTime) {
Alert.alert('提示', '这是当前版本第一次启动,是否要模拟启动失败?失败将回滚到上一版本', [
{text: '是', onPress: ()=>{throw new Error('模拟启动失败,请重启应用')}},
@ -109,6 +109,10 @@ class MyProject extends Component {
});
};
checkUpdate = () => {
if (__DEV__) {
// 开发模式不支持热更新,跳过检查
return;
}
checkUpdate(appKey).then(info => {
if (info.expired) {
Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [
@ -123,7 +127,7 @@ class MyProject extends Component {
]);
}
}).catch(err => {
Alert.alert('提示', '更新失败.');
console.warn(err);
});
};
render() {

View File

@ -1,11 +1,7 @@
/**
* Created by tdzl2003 on 4/4/16.
*/
import { NativeAppEventEmitter, NativeModules } from 'react-native';
const { HotUpdate } = NativeModules;
const HotUpdate = require('react-native').NativeModules.HotUpdate;
import {NativeAppEventEmitter} from 'react-native';
let host = 'https://update.reactnative.cn/api';
const host = 'https://update.reactnative.cn/api';
export const downloadRootDir = HotUpdate.downloadRootDir;
export const packageVersion = HotUpdate.packageVersion;
@ -36,11 +32,20 @@ There is available update:
diffUrl: 'http://update-packages.reactnative.cn/hash',
}
*/
function assertRelease() {
if (__DEV__) {
throw new Error('react-native-update can only run on RELEASE version.');
}
}
export async function checkUpdate(APPKEY) {
assertRelease();
const resp = await fetch(`${host}/checkUpdate/${APPKEY}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
@ -53,10 +58,11 @@ export async function checkUpdate(APPKEY) {
throw new Error((await resp.json()).message);
}
return await resp.json();
return resp.json();
}
export async function downloadUpdate(options) {
assertRelease();
if (!options.update) {
return;
}
@ -81,23 +87,21 @@ export async function downloadUpdate(options) {
return options.hash;
}
export async function switchVersion(hash) {
HotUpdate.reloadUpdate({hashName:hash});
export function switchVersion(hash) {
assertRelease();
HotUpdate.reloadUpdate({ hashName: hash });
}
export async function switchVersionLater(hash) {
HotUpdate.setNeedUpdate({hashName:hash});
export function switchVersionLater(hash) {
assertRelease();
HotUpdate.setNeedUpdate({ hashName: hash });
}
export function markSuccess() {
assertRelease();
HotUpdate.markSuccess();
}
NativeAppEventEmitter.addListener('RCTHotUpdateDownloadProgress', params => {});
NativeAppEventEmitter.addListener('RCTHotUpdateDownloadProgress',(params)=>{
})
NativeAppEventEmitter.addListener('RCTHotUpdateUnzipProgress',(params)=>{
})
NativeAppEventEmitter.addListener('RCTHotUpdateUnzipProgress', params => {});