view.js 2.73 KB
/**
 * @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'

let http
const Page = ({ navigation }) => {
  const [version, setVersion] = useState('')
  const downUrl = useRef('')

  useEffect(() => {
    getCurVersion()
    return () => http && http.cancel()
  }, [])

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

  const getUpdate = async () => {

    const versionCode = await getVersionCode()

    http = getTopVersion(res => {
      if (res === null) return Toast.info('已是最新版本')
      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 getCurVersion = useCallback(async () => {
    const version = await getVersion()
    setVersion(version)
  }, [])

  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={() => 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