import RNFS,{
downloadFile,
DownloadDirectoryPath,
exists,
unlink
} from 'react-native-fs';
import * as c from '../../common/ScreenUtil';
import PermissionRequest from "../../common/PermissionRequest";
import RNFetchBlob from "rn-fetch-blob";
const android = RNFetchBlob.android;
import {store} from "../../redux/ConfigSotre";
import * as ActionTypes from "../../redux/ActionTypes";
const download = async (url,name)=>{
let allowed = await PermissionRequest.Permission(PermissionRequest.SDCARD);
if(allowed){
let toFile = DownloadDirectoryPath + '/' + name + '.apk'
let isHas = await exists(toFile)
if (isHas){
// 如果包含,则提示去安装
android.actionViewIntent(toFile, "application/vnd.android.package-archive");
return
}
let res = await downloadFile({
fromUrl: url,
toFile,
background: true,
progressInterval:500, // 0.5s回调一次进度方法,如果不设置,进度方法会频繁调用,导致下载过程中页面卡死无法操作
begin: (res) => {
},
progress: (res) => {
let percent = ((res.bytesWritten / res.contentLength) * 100).toFixed(2);
// 进度存redux,其他页面可以拿到进度进行显示
store.dispatch({type:ActionTypes.UPDATE_DOWNLOAD_APP_INFO,payload:{appProgress:percent}})
}
}).promise
console.log('下载成功-----',res);
if (res.statusCode == 200){
store.dispatch({type:ActionTypes.UPDATE_DOWNLOAD_APP_INFO,payload:{appProgress:0}})
exists(toFile).then(isHas=>{
if (isHas){
// 如果包含,则提示去安装
android.actionViewIntent(toFile, "application/vnd.android.package-archive");
}
})
}
}else {
c.toastshowWarning('请开启SD卡存储权限')
}
}
export default download
网友评论