SplashScreen.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React, {Component} from 'react';
  2. import {NavigationActions} from 'react-navigation';
  3. import StorageUtil from '../utils/StorageUtil';
  4. import CountEmitter from '../event/CountEmitter';
  5. import WebIM from '../Lib/WebIM';
  6. import Toast from '@remobile/react-native-toast';
  7. import {
  8. Animated,
  9. Dimensions,
  10. Image,
  11. StatusBar,
  12. StyleSheet,
  13. Text,
  14. TouchableOpacity,
  15. View
  16. } from 'react-native';
  17. const {width, height} = Dimensions.get('window');
  18. export default class SplashScreen extends Component {
  19. constructor(props) {
  20. super(props);
  21. this.isAutoLogin = false;
  22. this.state = {
  23. fadeAnim: new Animated.Value(0),
  24. hasLogin: false
  25. };
  26. }
  27. render() {
  28. return (
  29. <View>
  30. <StatusBar backgroundColor="#000000"/>
  31. <Image source={require("../../images/splash.jpg")} style={{width: width, height: height}}/>
  32. {
  33. this.state.hasLogin ? (null) : (
  34. <Animated.View style={[styles.buttonContainer, {opacity: this.state.fadeAnim}]}>
  35. <TouchableOpacity style={{flex: 1}} activeOpacity={0.6} onPress={() => {
  36. this.props.navigation.navigate('Login')
  37. }}>
  38. <View style={[styles.btnLogin, styles.btnColumn]}>
  39. <Text style={styles.button}>登录</Text>
  40. </View>
  41. </TouchableOpacity>
  42. <TouchableOpacity style={{flex: 1}} activeOpacity={0.6} onPress={() => {
  43. this.props.navigation.navigate('Register')
  44. }}>
  45. <View style={[styles.btnRegister, styles.btnColumn]}>
  46. <Text style={[styles.button, {color: '#FFFFFF'}]}>注册</Text>
  47. </View>
  48. </TouchableOpacity>
  49. </Animated.View>
  50. )
  51. }
  52. </View>
  53. );
  54. }
  55. componentWillMount() {
  56. this._isMount = true;
  57. }
  58. componentDidMount() {
  59. // 这里不要用this.state.hasLogin判断
  60. StorageUtil.get('hasLogin', (error, object) => {
  61. if (!error && object != null && object.hasLogin) {
  62. if (this._isMount) {
  63. this.setState({hasLogin: object.hasLogin});
  64. }
  65. // 已登录,直接登录聊天服务器
  66. Toast.showShortCenter('自动登录中...');
  67. this.autoLogin();
  68. } else {
  69. // 未登录,需要先登录自己的服务器,再登录聊天服务器
  70. Animated.timing(this.state.fadeAnim, {
  71. duration: 2000,
  72. toValue: 1
  73. }).start();//开始
  74. }
  75. });
  76. }
  77. autoLogin() {
  78. StorageUtil.get('username', (error, object) => {
  79. if (!error && object && object.username) {
  80. let username = object.username;
  81. let password = '';
  82. StorageUtil.get('password', (error, object) => {
  83. if (!error && object && object.password) {
  84. password = object.password;
  85. // 只有在自动登录时才注册环信的监听器
  86. this.registerHXListener();
  87. this.loginToHX(username, password);
  88. } else {
  89. Toast.showShortCenter('数据异常');
  90. }
  91. });
  92. } else {
  93. Toast.showShortCenter('数据异常');
  94. }
  95. });
  96. }
  97. loginToHX(username, password) {
  98. this.isAutoLogin = true;
  99. if (WebIM.conn.isOpened()) {
  100. WebIM.conn.close('logout');
  101. }
  102. WebIM.conn.open({
  103. apiUrl: WebIM.config.apiURL,
  104. user: username,
  105. pwd: password,
  106. appKey: WebIM.config.appkey
  107. });
  108. }
  109. componentWillUnmount() {
  110. this._isMount = false;
  111. // 取消定时器
  112. this.timer && clearTimeout(this.timer);
  113. }
  114. registerHXListener() { // 注册环信的消息监听器,只有在自动登录时才注册
  115. WebIM.conn.listen({
  116. // xmpp连接成功
  117. onOpened: (msg) => {
  118. // 登录环信服务器成功后回调这里,关闭当前页面并跳转到HomeScreen
  119. const resetAction = NavigationActions.reset({
  120. index: 0,
  121. actions: [
  122. NavigationActions.navigate({routeName: 'Home'})
  123. ]
  124. });
  125. this.props.navigation.dispatch(resetAction);
  126. },
  127. // 各种异常
  128. onError: (error) => {
  129. Toast.showShortCenter('登录聊天服务器出错');
  130. console.log('onError: ' + JSON.stringify(error))
  131. },
  132. // 连接断开
  133. onClosed: (msg) => {
  134. // Toast.showShortCenter('与聊天服务器连接断开');
  135. },
  136. });
  137. }
  138. }
  139. const styles = StyleSheet.create({
  140. buttonContainer: {
  141. position: 'absolute',
  142. bottom: 50,
  143. flex: 1,
  144. flexDirection: 'row',
  145. paddingLeft: 20,
  146. paddingRight: 20,
  147. },
  148. btnColumn: {
  149. flex: 1,
  150. paddingLeft: 10,
  151. paddingRight: 10,
  152. justifyContent: 'center',
  153. alignItems: 'center',
  154. borderRadius: 3,
  155. },
  156. button: {
  157. flex: 1,
  158. paddingTop: 10,
  159. paddingBottom: 10,
  160. fontSize: 16,
  161. },
  162. btnLogin: {
  163. backgroundColor: '#FFFFFF',
  164. marginRight: 15,
  165. },
  166. btnRegister: {
  167. backgroundColor: '#00BC0C',
  168. marginLeft: 15,
  169. }
  170. });