UNPKG

geekbase

Version:

geek app base library

161 lines (146 loc) 4.82 kB
import {computed, thunk, action} from 'easy-peasy' import {Linking} from 'react-native' import RNFetchBlob from 'rn-fetch-blob' import {device} from "../geek-device" import Toast from '../geek-toast' import {installApk, writeStoragePermission} from '../geek-android-util' /** * systemUpdate 配置结构 appVersion: { ios: { versionNumber: "2.9.6", versionDescription: "修复bug", isMandatory: false, appStoreUrl: "https://apps.apple.com/cn/app/%E6%9E%81%E5%AE%A2%E6%95%B0%E5%AD%A6%E5%B8%AE/id1441643036" }, android: { versionNumber: "2.9.6", versionDescription: "修复bug", isMandatory: false, apkUrl: "https://profile-1257124244.cos.ap-chengdu.myqcloud.com/app_resource/geekApp.apk" } }, * **/ const choke = { callback: null, trigger(param) { this.callback && this.callback(param) } } export default { systemUpdate: null, isMandatory: computed((state) => { if (!state.systemUpdate) return false return state.systemUpdate[device.os].isMandatory }), description: computed((state) => { if (!state.systemUpdate) return [] return state.systemUpdate[device.os].versionDescription.split(' ') }), shouldUpdate: computed((state) => { if (!state.systemUpdate) return false return device.appVersion < state.systemUpdate[device.os].versionNumber }), url: computed((state) => { if (!state.systemUpdate) return '' if (device.os === 'android') { return state.systemUpdate.android.apkUrl } else { return state.systemUpdate.ios.appStoreUrl } }), isVisible: false, downloadProgress: '', downloadStatus: '', downloadPath: '', downloadBtnText: computed((state) => { switch (state.downloadStatus) { case 'downloading': return '正在下载 请勿退出' case 'downloaded': return '下载完成 点击安装' default: return '立即更新' } }), setVisible: action((state, payload) => { state.isVisible = payload }), setDownloadStatus: action((state, payload) => { state.downloadStatus = payload }), setDownloadProgress: action((state, payload) => { state.downloadProgress = payload }), setDownloadPath: action((state, payload) => { state.downloadPath = payload }), setSystemUpdate: action((state, payload) => { state.systemUpdate = payload }), checkSystemUpdate: thunk(async (actions, payload = true, helpers) => { const shouldUpdate = helpers.getState().shouldUpdate if (payload && !shouldUpdate) { Toast.msg('APP已是最新版本') } actions.setVisible(shouldUpdate) if (shouldUpdate) { return new Promise((resolve, reject) => { choke.callback = (isAgreeUpdate) => { resolve(isAgreeUpdate) } }) } return false }), declineSystemUpdate: thunk((actions, payload, helpers) => { actions.setVisible(false) choke.trigger(false) }), agreeSystemUpdate: thunk(async (actions, payload, helpers) => { const url = helpers.getState().url if (device.os === 'ios') { Linking.canOpenURL(url).then(supported => { if (supported) { return Linking.openURL(url) } }).catch(err => console.error('跳转应用市场报错', err)) } else { if (helpers.getState().downloadStatus === '') { console.log('下载状态 init') const canWrite = await writeStoragePermission() if (canWrite) { //下载成功后文件所在path const downloadDest = `${ RNFetchBlob.fs.dirs.DownloadDir }/GeekMathApp.apk` RNFetchBlob.config({ fileCache: true, path: downloadDest }).fetch('GET', url).progress((received, total) => { console.log('progress', received / total) if (!helpers.getState().downloadStatus) { actions.setDownloadStatus('downloading') } let rM = parseInt(received / 1024 / 1024) let tM = parseInt(total / 1024 / 1024) actions.setDownloadProgress(`${rM}M / ${tM}M`) if (parseFloat(received) >= parseFloat(total)) { console.log('》=', received, total) actions.setDownloadStatus('downloaded') } }).then(res => { actions.setDownloadStatus('downloaded') actions.setDownloadPath(res.path()) installApk(res.path()) }) } } else if (helpers.getState().downloadStatus === 'downloaded') { console.log('下载状态 已完成', helpers.getState().downloadPath) installApk(helpers.getState().downloadPath) } else { console.log('下载状态 正在下载') } } }) }