view.js 2.83 KB
/**
 * @author sheng 
 * @description 关于信巴迪
 * @date 2020/09/03
 */

import React, { useCallback, useEffect, useState, useRef } from 'react'
import { View, Image, Text, Alert, Linking } from 'react-native'
import { XbdCell, XbdDivider, XbdToast } from 'rn-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 => {
        XbdToast.show('打开下载地址失败,请重试')
      })
  }

  const getUpdate = async () => {
    const versionCode = await getVersionCode()

    http = getTopVersion(APP.APP_CODE, res => {
      if (res === null) return
      const { version_code, down, version } = res
      downUrl.current = down
      
      if (version_code > versionCode) {
        Alert.alert('提示', `发现新版本${version}需要更新,是否更新`, [
          { text: '取消' },
          { text: '立即更新', onPress: openDown }
        ])
      } else {
        XbdToast.show('已是最新版本')
      }
    })
  }

  /**获取当前版本号 */
  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}>商品交易所</Text>
          <Text style={styles.version}>Version {version}</Text>
        </View>
        <View style={styles.center}>
          <XbdDivider style={{ marginHorizontal: setUnit(25) }} />
          <XbdCell
            title={'功能介绍'}
            onClick={() => navigation.navigate('Features')}
            padLeft={setUnit(25)}
            padRight={setUnit(25)}
          />
          <XbdCell
            title={'版本更新'}
            onClick={getUpdate}
            padLeft={setUnit(25)}
            padRight={setUnit(25)}
          />
        </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