罗广聪

注册已经完成,登录有问题

@@ -72,11 +72,11 @@ export default class InsuranceCircle extends Component { @@ -72,11 +72,11 @@ export default class InsuranceCircle extends Component {
72 comments: [] 72 comments: []
73 } 73 }
74 ], 74 ],
75 - testData: [], 75 + testData: null,
76 }; 76 };
77 } 77 }
78 78
79 - componentWillMount() { 79 + componentDidMount() {
80 var that = this; 80 var that = this;
81 return fetch(`https://mpay.brae.co/test/insurance/article`,{ 81 return fetch(`https://mpay.brae.co/test/insurance/article`,{
82 method: "POST", 82 method: "POST",
@@ -86,7 +86,7 @@ export default class InsuranceCircle extends Component { @@ -86,7 +86,7 @@ export default class InsuranceCircle extends Component {
86 }, 86 },
87 }) 87 })
88 .then((resp) => { 88 .then((resp) => {
89 - console.log("测试接口",resp); 89 + // console.log("测试接口",resp);
90 // return false; 90 // return false;
91 if (resp.status === 200) { 91 if (resp.status === 200) {
92 return resp.json(); 92 return resp.json();
@@ -8,7 +8,8 @@ import { @@ -8,7 +8,8 @@ import {
8 TouchableOpacity, 8 TouchableOpacity,
9 ScrollView, 9 ScrollView,
10 TextInput, 10 TextInput,
11 - KeyboardAvoidingView 11 + KeyboardAvoidingView,
  12 + AsyncStorage
12 } from "react-native"; 13 } from "react-native";
13 14
14 export default class Login extends Component { 15 export default class Login extends Component {
@@ -18,11 +19,24 @@ export default class Login extends Component { @@ -18,11 +19,24 @@ export default class Login extends Component {
18 phone: "", 19 phone: "",
19 password: "", 20 password: "",
20 nickname: "", 21 nickname: "",
  22 + id: "",
21 // 处于登录还是注册页面 23 // 处于登录还是注册页面
22 isRegister: false, 24 isRegister: false,
23 - testData: null, 25 + IS_LOGIN: ""
24 }; 26 };
25 } 27 }
  28 + componentWillMount() {
  29 + var _that = this;
  30 + AsyncStorage.getItem("IS_LOGIN", (err, result) => {
  31 + if (err) {
  32 + console.error(err);
  33 + }
  34 + _that.setState({
  35 + IS_LOGIN: result
  36 + });
  37 + console.log("Login页面的AsyncStorage", result);
  38 + });
  39 + }
26 componentDidMount() { 40 componentDidMount() {
27 console.log("thisprops", this.props); 41 console.log("thisprops", this.props);
28 } 42 }
@@ -32,6 +46,7 @@ export default class Login extends Component { @@ -32,6 +46,7 @@ export default class Login extends Component {
32 <View style={styles.container}> 46 <View style={styles.container}>
33 <KeyboardAvoidingView behavior="padding" style={styles.KAVContainer}> 47 <KeyboardAvoidingView behavior="padding" style={styles.KAVContainer}>
34 <Text style={styles.appName}>保护神</Text> 48 <Text style={styles.appName}>保护神</Text>
  49 + {/* 手机号 */}
35 <View style={styles.inputField}> 50 <View style={styles.inputField}>
36 <Image 51 <Image
37 style={styles.icon} 52 style={styles.icon}
@@ -50,6 +65,7 @@ export default class Login extends Component { @@ -50,6 +65,7 @@ export default class Login extends Component {
50 keyboardType="default" 65 keyboardType="default"
51 /> 66 />
52 </View> 67 </View>
  68 + {/* 密码 */}
53 <View style={styles.inputField}> 69 <View style={styles.inputField}>
54 <Image 70 <Image
55 style={styles.icon} 71 style={styles.icon}
@@ -69,7 +85,8 @@ export default class Login extends Component { @@ -69,7 +85,8 @@ export default class Login extends Component {
69 /> 85 />
70 </View> 86 </View>
71 {/* 昵称 */} 87 {/* 昵称 */}
72 - {/* <View style={styles.inputField}> 88 + {this.state.isRegister ? (
  89 + <View style={styles.inputField}>
73 <Image 90 <Image
74 style={styles.icon} 91 style={styles.icon}
75 source={require("../assets/login/ic_password2.png")} 92 source={require("../assets/login/ic_password2.png")}
@@ -85,13 +102,15 @@ export default class Login extends Component { @@ -85,13 +102,15 @@ export default class Login extends Component {
85 clearButtonMode="always" 102 clearButtonMode="always"
86 keyboardType="default" 103 keyboardType="default"
87 /> 104 />
88 - </View> */} 105 + </View>
  106 + ) : null}
  107 +
89 {/* 登录/注册 按钮 */} 108 {/* 登录/注册 按钮 */}
90 {!this.state.isRegister ? ( 109 {!this.state.isRegister ? (
91 <TouchableOpacity 110 <TouchableOpacity
92 style={styles.confirmBtnLogin} 111 style={styles.confirmBtnLogin}
93 onPress={() => { 112 onPress={() => {
94 - this.loginRequest(); 113 + this.loginRequest()
95 }} 114 }}
96 > 115 >
97 <Text style={styles.confirmText}>登录</Text> 116 <Text style={styles.confirmText}>登录</Text>
@@ -100,7 +119,7 @@ export default class Login extends Component { @@ -100,7 +119,7 @@ export default class Login extends Component {
100 <TouchableOpacity 119 <TouchableOpacity
101 style={styles.confirmBtnRegister} 120 style={styles.confirmBtnRegister}
102 onPress={() => { 121 onPress={() => {
103 - this.loginRequest(); 122 + this.register();
104 }} 123 }}
105 > 124 >
106 <Text style={styles.confirmText}>注册</Text> 125 <Text style={styles.confirmText}>注册</Text>
@@ -111,14 +130,26 @@ export default class Login extends Component { @@ -111,14 +130,26 @@ export default class Login extends Component {
111 {!this.state.isRegister ? ( 130 {!this.state.isRegister ? (
112 <TouchableOpacity 131 <TouchableOpacity
113 style={styles.registerContaier} 132 style={styles.registerContaier}
114 - onPress={() => this.setState({ isRegister: !this.state.isRegister })} 133 + onPress={() =>
  134 + this.setState({
  135 + isRegister: !this.state.isRegister,
  136 + phone: "",
  137 + password: "",
  138 + nickname: "",
  139 + })}
115 > 140 >
116 <Text style={styles.register}>注册账号</Text> 141 <Text style={styles.register}>注册账号</Text>
117 </TouchableOpacity> 142 </TouchableOpacity>
118 ) : ( 143 ) : (
119 <TouchableOpacity 144 <TouchableOpacity
120 style={styles.registerContaier} 145 style={styles.registerContaier}
121 - onPress={() => this.setState({ isRegister: !this.state.isRegister })} 146 + onPress={() =>
  147 + this.setState({
  148 + isRegister: !this.state.isRegister,
  149 + phone: "",
  150 + password: "",
  151 + nickname: "",
  152 + })}
122 > 153 >
123 <Text style={styles.register}>登录</Text> 154 <Text style={styles.register}>登录</Text>
124 </TouchableOpacity> 155 </TouchableOpacity>
@@ -128,9 +159,10 @@ export default class Login extends Component { @@ -128,9 +159,10 @@ export default class Login extends Component {
128 ); 159 );
129 } 160 }
130 161
  162 + // 登录
131 loginRequest() { 163 loginRequest() {
132 - // if (this.props.loginSuccess) return; 164 + let that = this,
133 - let phone = this.state.phone, 165 + phone = this.state.phone,
134 password = this.state.password; 166 password = this.state.password;
135 // if (!/^1(3|4|5|7|8)\d{9}$/.test(phone)) { 167 // if (!/^1(3|4|5|7|8)\d{9}$/.test(phone)) {
136 // alert("请正确输入手机号码"); 168 // alert("请正确输入手机号码");
@@ -140,48 +172,93 @@ export default class Login extends Component { @@ -140,48 +172,93 @@ export default class Login extends Component {
140 // alert("请输入密码"); 172 // alert("请输入密码");
141 // return; 173 // return;
142 // } 174 // }
143 - // this.props.onLogin(true);  
144 - let that = this;  
145 let formData = new FormData(); 175 let formData = new FormData();
146 - formData.append("phone",phone); 176 + formData.append("phone", phone);
147 - formData.append("password",password); 177 + formData.append("password", password);
148 178
149 - return fetch(`https://devpay.brae.co/test/insurance/login`,{ 179 + return fetch(`https://devpay.brae.co/test/insurance/login`, {
150 method: "POST", 180 method: "POST",
151 headers: { 181 headers: {
152 - 'Accept': 'application/json', 182 + Accept: "application/json",
153 - 'Content-Type': 'application/json', 183 + "Content-Type": "application/json"
154 }, 184 },
155 - body: formData, 185 + body: formData
156 }) 186 })
157 - .then((resp) => { 187 + .then(resp => {
158 - console.log("测试接口",resp);  
159 - // return false;  
160 if (resp.status === 200) { 188 if (resp.status === 200) {
161 return resp.json(); 189 return resp.json();
162 } else { 190 } else {
163 console.error("something went wrong!"); 191 console.error("something went wrong!");
164 } 192 }
165 }) 193 })
166 - .then((respJson) => { 194 + .then(respJson => {
167 - // if (respJson.enmsg != 'ok') { 195 + if (respJson.enmsg != "ok") {
168 - // console.error(err); 196 + alert(respJson.cnmsg);
169 - // alert(respJson.cnmsg); 197 + } else {
170 - // } else {  
171 that.setState({ 198 that.setState({
172 - testData: respJson.data 199 + // testData: respJson.data
173 }); 200 });
174 - console.log('看看登录能获得啥数据',that.state.testData); 201 + AsyncStorage.setItem("IS_LOGIN", "yes", function(err) {
175 - // } 202 + if (err) {
  203 + console.log("存储出错", err);
  204 + return false;
  205 + }
  206 + that.props.navigation.navigate("Mine");
  207 + });
  208 + }
  209 + })
  210 + .catch(err => console.error(err));
  211 + }
  212 + // 注册
  213 + register() {
  214 + let that = this,
  215 + phone = this.state.phone,
  216 + password = this.state.password,
  217 + nickname = this.state.nickname,
  218 + formData = new FormData();
  219 + if (!phone || !password || !nickname) {
  220 + alert("请完整填写信息");
  221 + return false;
  222 + }
  223 + formData.append("phone", phone);
  224 + formData.append("password", password);
  225 + formData.append("nickname", nickname);
  226 +
  227 + return fetch(`https://devpay.brae.co/test/insurance/register`, {
  228 + method: "POST",
  229 + headers: {
  230 + Accept: "application/json",
  231 + "Content-Type": "application/json"
  232 + },
  233 + body: formData
  234 + })
  235 + .then(resp => {
  236 + if (resp.status === 200) {
  237 + return resp.json();
  238 + } else {
  239 + console.error("something went wrong!");
  240 + }
  241 + })
  242 + .then(respJson => {
  243 + if (respJson.enmsg != "ok") {
  244 + alert(respJson.cnmsg);
  245 + } else {
  246 + console.log("注册按钮返回的数据",respJson.data);
  247 + that.setState({
  248 + isRegister: !that.state.isRegister,
  249 + id: respJson.data.id,
  250 + });
  251 + alert("注册成功!");
  252 + }
176 }) 253 })
177 - .catch((err) => console.error(err)) 254 + .catch(err => console.error(err));
178 } 255 }
179 } 256 }
180 257
181 const styles = StyleSheet.create({ 258 const styles = StyleSheet.create({
182 container: { 259 container: {
183 flex: 1, 260 flex: 1,
184 - paddingTop: 125, 261 + paddingTop: 80,
185 justifyContent: "flex-start", 262 justifyContent: "flex-start",
186 alignItems: "stretch", 263 alignItems: "stretch",
187 backgroundColor: "#F5FCFF", 264 backgroundColor: "#F5FCFF",
@@ -28,6 +28,7 @@ export default class Mine extends Component { @@ -28,6 +28,7 @@ export default class Mine extends Component {
28 } 28 }
29 componentWillMount() { 29 componentWillMount() {
30 var _that = this; 30 var _that = this;
  31 + console.log("this",this)
31 AsyncStorage.getItem("IS_LOGIN",(err, result) => { 32 AsyncStorage.getItem("IS_LOGIN",(err, result) => {
32 if (err) { 33 if (err) {
33 console.error(err); 34 console.error(err);
@@ -35,7 +36,7 @@ export default class Mine extends Component { @@ -35,7 +36,7 @@ export default class Mine extends Component {
35 _that.setState({ 36 _that.setState({
36 IS_LOGIN: result 37 IS_LOGIN: result
37 }) 38 })
38 - console.log("能拿到IS_LOGIN吗?",this.state.IS_LOGIN); 39 + console.log("我的页面的IS_LOGIN",this.state.IS_LOGIN);
39 }) 40 })
40 } 41 }
41 componentDidMount() { 42 componentDidMount() {
@@ -52,7 +53,6 @@ export default class Mine extends Component { @@ -52,7 +53,6 @@ export default class Mine extends Component {
52 _that.setState({ 53 _that.setState({
53 IS_LOGIN: "yes" 54 IS_LOGIN: "yes"
54 }) 55 })
55 - console.log("改变AsyncStorage",this.state.IS_LOGIN);  
56 } 56 }
57 signOut() { 57 signOut() {
58 var _that = this 58 var _that = this
@@ -65,7 +65,6 @@ export default class Mine extends Component { @@ -65,7 +65,6 @@ export default class Mine extends Component {
65 _that.setState({ 65 _that.setState({
66 IS_LOGIN: "no" 66 IS_LOGIN: "no"
67 }) 67 })
68 - console.log("改变AsyncStorage",this.state.IS_LOGIN);  
69 } 68 }
70 saveResponse1(d) { 69 saveResponse1(d) {
71 if (this.state.IS_LOGIN == "yes") { 70 if (this.state.IS_LOGIN == "yes") {
@@ -138,17 +137,23 @@ export default class Mine extends Component { @@ -138,17 +137,23 @@ export default class Mine extends Component {
138 </TouchableOpacity> 137 </TouchableOpacity>
139 </View> 138 </View>
140 139
141 - {/* 退出登录 */} 140 + {/* 登录 */}
142 <TouchableOpacity 141 <TouchableOpacity
143 style={styles.loginOut} 142 style={styles.loginOut}
144 onLogin={this.props.onLogin} 143 onLogin={this.props.onLogin}
145 onPress={() => { 144 onPress={() => {
146 - this.signOut(); 145 + {/* this.signOut(); */}
147 - navigate("Login"); 146 + navigate("Login",{IS_LOGIN: this.state.IS_LOGIN});
148 }} 147 }}
149 > 148 >
150 <Text style={styles.loginOutText}>{this.state.IS_LOGIN ? "登录" : "退出登录"}</Text> 149 <Text style={styles.loginOutText}>{this.state.IS_LOGIN ? "登录" : "退出登录"}</Text>
151 </TouchableOpacity> 150 </TouchableOpacity>
  151 + {/* <Button
  152 + title="登出"
  153 + onPress={() => {
  154 + this.signOut();
  155 + }}
  156 + /> */}
152 </View> 157 </View>
153 ); 158 );
154 } 159 }