Browse Source

1.BasePage增加跳转页面并清空路由栈

zhangmengjie 5 years ago
parent
commit
cfd98c53c9
4 changed files with 107 additions and 109 deletions
  1. 97 86
      pages/BasePage.js
  2. 6 20
      pages/Login.js
  3. 3 3
      pages/SchoolAge.js
  4. 1 0
      pages/utils/commonutil.js

+ 97 - 86
pages/BasePage.js

@@ -6,102 +6,113 @@
  * @flow
  */
 
-import React, { Component } from "react";
+import React, { Component } from 'react';
 import {
-  Platform,
-  StyleSheet,
-  Text,
-  View,
-  Image,
-  TouchableOpacity,
-  ImageBackground,
-  Button,
-  Dimensions,
-  DeviceEventEmitter
-} from "react-native";
-import AndroidUtil from "../util/AndroidUtil";
-
+	Platform,
+	StyleSheet,
+	Text,
+	View,
+	Image,
+	TouchableOpacity,
+	ImageBackground,
+	Button,
+	Dimensions,
+	DeviceEventEmitter
+} from 'react-native';
+import AndroidUtil from '../util/AndroidUtil';
+import { NavigationActions, StackActions } from 'react-navigation';
+import commonUtil from '../pages/utils/commonutil';
 
 type Props = {};
-var width = Dimensions.get("window").width;
-var height = Dimensions.get("window").height;
+var width = Dimensions.get('window').width;
+var height = Dimensions.get('window').height;
 export default class BasePage extends Component<Props> {
-  render() {
-    return (
-      <ImageBackground
-        style={{
-          flex: 1,
-          width: "100%",
-          height: "100%",
-          Background: "#F8F8F8"
-        }}
-      />
-    );
-  }
-  getWindowHeight() {
-    return height;
-  }
-  getWindowWidth() {
-    return width;
-  }
+	render() {
+		return (
+			<ImageBackground
+				style={{
+					flex: 1,
+					width: '100%',
+					height: '100%',
+					Background: '#F8F8F8'
+				}}
+			/>
+		);
+	}
+	getWindowHeight() {
+		return height;
+	}
+	getWindowWidth() {
+		return width;
+	}
+
+	clearPageToNext = (name, obj) => {
+		const resetAction = StackActions.reset({
+			index: 0,
+			actions: [
+				NavigationActions.navigate({ routeName: name }) //要跳转到的页面名字
+			]
+		});
+		this.props.navigation.dispatch(resetAction, obj);
+	};
 
-  toNextPage= (params,obj) => {
-    //跳转之前移除当前界面的监听
-    this.removeListener();
-    this.props.navigation.navigate(params,obj);
-  }
-  Toast(params) {
-    AndroidUtil.showToast(params, AndroidUtil.SHORT);
-  }
-  toWebPage(json) {
-    AndroidUtil.toWebActivity(json);
-  }
-  testJssss() {
-    //测试调用安卓方法然后安卓方法调用JS下面的监听
-    AndroidUtil.testJS();
-  }
-  goBack() {
-    //返回上一页
-    this.props.navigation.goBack();
-  }
+	toNextPage = (params, obj) => {
+		//跳转之前移除当前界面的监听
+		this.removeListener();
+		this.props.navigation.navigate(params, obj);
+	};
+	Toast(params) {
+		AndroidUtil.showToast(params, AndroidUtil.SHORT);
+	}
+	toWebPage(json) {
+		AndroidUtil.toWebActivity(json);
+	}
+	testJssss() {
+		//测试调用安卓方法然后安卓方法调用JS下面的监听
+		AndroidUtil.testJS();
+	}
+	goBack() {
+		//返回上一页
+		this.props.navigation.goBack();
+	}
 
-  removeListener() {
-    if (this.testJSaaa) {
-      this.testJSaaa.remove();
-    }
-    if (this.toJsByAndroid) {
-      this.toJsByAndroid.remove();
-    }
-  }
+	removeListener() {
+		if (this.testJSaaa) {
+			this.testJSaaa.remove();
+		}
+		if (this.toJsByAndroid) {
+			this.toJsByAndroid.remove();
+		}
+	}
 
-  componentWillMount() {
-    //监听事件名为EventName的事件
-    this.testJSaaa = DeviceEventEmitter.addListener("testJSaaa", e => {
-      //e是原生传过来的参数,ceshi是安卓里面put的key
-      alert(e.ceshi);
-    });
+	componentWillMount() {
+		//监听事件名为EventName的事件
+		this.testJSaaa = DeviceEventEmitter.addListener('testJSaaa', (e) => {
+			//e是原生传过来的参数,ceshi是安卓里面put的key
+			alert(e.ceshi);
+		});
 
-    this.toJsByAndroid = DeviceEventEmitter.addListener("toJsByAndroid", e => {
-      alert(e.name);
-      alert(e.sex);
-      alert(e.age);
-    });
-  }
+		this.toJsByAndroid = DeviceEventEmitter.addListener('toJsByAndroid', (e) => {
+			alert(e.name);
+			alert(e.sex);
+			alert(e.age);
+		});
+	}
 
-  componentWillUnmount() {
-    // 移除监听
-    this.removeListener();
-  }
+	componentWillUnmount() {
+		// 移除监听
+		this.removeListener();
+	}
 }
 
 const styles = StyleSheet.create({
-  title_text: {
-    justifyContent: "center",
-    alignItems: "center",
-    color: "red",
-    fontSize: 30,
-    textAlign: "center",
-    marginTop: 30,
-    marginBottom: 50
-  }
+	title_text: {
+		justifyContent: 'center',
+		alignItems: 'center',
+		color: 'red',
+		fontSize: 30,
+		textAlign: 'center',
+		marginTop: 30,
+		marginBottom: 50
+	}
 });

+ 6 - 20
pages/Login.js

@@ -17,8 +17,8 @@ export default class Login extends BasePage {
 	state = {
 		phone_num: '',
 		verification_text: '获取验证码',
-		verification_code: '',
-		http_verification_code: '',
+		verification_code: '9999',
+		http_verification_code: '9999',
 		deviceCode: ''
 	};
 	render() {
@@ -122,23 +122,7 @@ export default class Login extends BasePage {
 		}
 	}
 
-	//绑定手机号
 	clickOK() {
-		//请求报错
-		//参数
-		/**
-         * 
-         * {
-	        "channel": "006",
-	        "deviceCode": "fb3dcd333348ZX1G42CPJD",
-	        "mobile": "15810271473",
-	        "sign": "9999"
-            }   
-         * 
-         * 
-         * 
-         * 
-         */
 		if (!commonutil.isPoneAvailable(this.state.phone_num)) {
 			ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
 			return;
@@ -160,7 +144,8 @@ export default class Login extends BasePage {
 			};
 			http_user.mobileLoginAndReg(option).then((result) => {
 				if (result.code == 200) {
-					ToastAndroid.show(result.message, ToastAndroid.SHORT);
+					// ToastAndroid.show("", ToastAndroid.SHORT);
+					this.clearPageToNext('SchoolAge');
 				} else {
 					ToastAndroid.show(result.message, ToastAndroid.SHORT);
 				}
@@ -203,7 +188,8 @@ export default class Login extends BasePage {
 			http_user.wechatLogin(option).then((result) => {
 				if (result.code == 200) {
 					//登陆成功了,可以存储用户数据到本地
-					ToastAndroid.show(result.message, ToastAndroid.SHORT);
+					// ToastAndroid.show(result.message, ToastAndroid.SHORT);
+					this.clearPageToNext('SchoolAge');
 				} else {
 					ToastAndroid.show(result.message, ToastAndroid.SHORT);
 				}

+ 3 - 3
pages/SchoolAge.js

@@ -68,15 +68,15 @@ export default class SchoolAge extends BasePage {
 	}
 
 	pres() {
-		this.toNextPage('MainPage', { index: 0 });
+		this.clearPageToNext('MainPage', { index: 0 });
 	}
 
 	primary() {
-		this.toNextPage('MainPage', { index: 1 });
+		this.clearPageToNext('MainPage', { index: 1 });
 	}
 
 	middle() {
-		this.toNextPage('MainPage', { index: 2 });
+		this.clearPageToNext('MainPage', { index: 2 });
 	}
 }
 

+ 1 - 0
pages/utils/commonutil.js

@@ -10,4 +10,5 @@ export default class commonutil {
 			return true;
 		}
 	}
+
 }