view.tsx 3.33 KB
/* eslint-disable @typescript-eslint/camelcase */
/**
 * @author sheng
 * @description 关于信巴迪
 * @date 2020/09/03
 */

import React, { useCallback, useEffect, useState, useRef } from 'react'
import { View, Image, Text, Linking } from 'react-native'
import { Toast, ModalManager, Divider, Cell } from 'react-native-mb-ui'
import { styles } from './style'
import { getVersion, getVersionCode, setUnit } from '../../libs/utils'
import { getTopVersion } from '../../api/version'
import { APP } from '../../config'
import { RouteList } from '../../router/route'
import { NavigationProp } from '@react-navigation/native'

let http
const Page: React.FC<{ navigation: NavigationProp<RouteList, 'About'> }> = ({ navigation }) => {
    const [version, setVersion] = useState('')
    const downUrl = useRef('')

    useEffect(() => {
        (async (): Promise<void> => {
            const version = await getVersion()
            setVersion(version)
        })()
        return (): void => http && http.cancel()
    }, [])

    /** 打开下载链接 */
    const openDown = (): void => {
        Linking.openURL(downUrl.current)
            .then(() => { })
            .catch(() => Toast.info('打开下载地址失败,请重试'))
    }

    const getUpdate = async (): Promise<void> => {

        const versionCode = await getVersionCode()

        http = getTopVersion(res => {
            if (res === null) {
                Toast.info('已是最新版本')
                return
            }
            const { version_code, down, version } = res
            downUrl.current = down

            if (version_code > versionCode) {
                ModalManager.alert({
                    title: '提示',
                    content: `发现新版本${version}需要更新,是否更新`,
                    button: [
                        { text: '取消' },
                        { text: '立即更新', onPress: openDown }
                    ]
                })
            } else
                Toast.info('已是最新版本')

        }, err => Toast.info(err))

    }

    const goPage = useCallback(() => {
        navigation.navigate('Privacy')
    }, [navigation])

    return (
        <View style={styles.container}>
            <View style={{ flex: 1 }}>
                <View style={styles.header}>
                    <Image
                        source={require('../../assets/images/ic_launcher.png')}
                        style={styles.logo}
                    />
                    <Text style={styles.name}>{APP.appName}</Text>
                    <Text style={styles.version}>Version {version}</Text>
                </View>
                <View style={styles.center}>
                    <Divider style={{ marginHorizontal: setUnit(25) }} />
                    <Cell label={'功能介绍'} onClick={(): void => navigation.navigate('Features')} />
                    <Cell label={'版本更新'} onClick={getUpdate} />
                </View>
            </View>
            <View style={styles.footer}>
                <Text style={styles.private} onPress={goPage}>《商城平台隐私权政策》</Text>
                <Text style={styles.copyright}>
                    {`广州信巴迪信息科技有限公司  版权所有\nCopyright© 粤ICP备18005051号-1`}
                </Text>
            </View>
        </View>
    )
}

export default Page