罗广聪

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

... ... @@ -72,11 +72,11 @@ export default class InsuranceCircle extends Component {
comments: []
}
],
testData: [],
testData: null,
};
}
componentWillMount() {
componentDidMount() {
var that = this;
return fetch(`https://mpay.brae.co/test/insurance/article`,{
method: "POST",
... ... @@ -86,7 +86,7 @@ export default class InsuranceCircle extends Component {
},
})
.then((resp) => {
console.log("测试接口",resp);
// console.log("测试接口",resp);
// return false;
if (resp.status === 200) {
return resp.json();
... ...
... ... @@ -8,7 +8,8 @@ import {
TouchableOpacity,
ScrollView,
TextInput,
KeyboardAvoidingView
KeyboardAvoidingView,
AsyncStorage
} from "react-native";
export default class Login extends Component {
... ... @@ -18,11 +19,24 @@ export default class Login extends Component {
phone: "",
password: "",
nickname: "",
id: "",
// 处于登录还是注册页面
isRegister: false,
testData: null,
IS_LOGIN: ""
};
}
componentWillMount() {
var _that = this;
AsyncStorage.getItem("IS_LOGIN", (err, result) => {
if (err) {
console.error(err);
}
_that.setState({
IS_LOGIN: result
});
console.log("Login页面的AsyncStorage", result);
});
}
componentDidMount() {
console.log("thisprops", this.props);
}
... ... @@ -32,6 +46,7 @@ export default class Login extends Component {
<View style={styles.container}>
<KeyboardAvoidingView behavior="padding" style={styles.KAVContainer}>
<Text style={styles.appName}>保护神</Text>
{/* 手机号 */}
<View style={styles.inputField}>
<Image
style={styles.icon}
... ... @@ -50,6 +65,7 @@ export default class Login extends Component {
keyboardType="default"
/>
</View>
{/* 密码 */}
<View style={styles.inputField}>
<Image
style={styles.icon}
... ... @@ -69,7 +85,8 @@ export default class Login extends Component {
/>
</View>
{/* 昵称 */}
{/* <View style={styles.inputField}>
{this.state.isRegister ? (
<View style={styles.inputField}>
<Image
style={styles.icon}
source={require("../assets/login/ic_password2.png")}
... ... @@ -85,13 +102,15 @@ export default class Login extends Component {
clearButtonMode="always"
keyboardType="default"
/>
</View> */}
</View>
) : null}
{/* 登录/注册 按钮 */}
{!this.state.isRegister ? (
<TouchableOpacity
style={styles.confirmBtnLogin}
onPress={() => {
this.loginRequest();
this.loginRequest()
}}
>
<Text style={styles.confirmText}>登录</Text>
... ... @@ -100,7 +119,7 @@ export default class Login extends Component {
<TouchableOpacity
style={styles.confirmBtnRegister}
onPress={() => {
this.loginRequest();
this.register();
}}
>
<Text style={styles.confirmText}>注册</Text>
... ... @@ -111,14 +130,26 @@ export default class Login extends Component {
{!this.state.isRegister ? (
<TouchableOpacity
style={styles.registerContaier}
onPress={() => this.setState({ isRegister: !this.state.isRegister })}
onPress={() =>
this.setState({
isRegister: !this.state.isRegister,
phone: "",
password: "",
nickname: "",
})}
>
<Text style={styles.register}>注册账号</Text>
</TouchableOpacity>
) : (
<TouchableOpacity
style={styles.registerContaier}
onPress={() => this.setState({ isRegister: !this.state.isRegister })}
onPress={() =>
this.setState({
isRegister: !this.state.isRegister,
phone: "",
password: "",
nickname: "",
})}
>
<Text style={styles.register}>登录</Text>
</TouchableOpacity>
... ... @@ -128,9 +159,10 @@ export default class Login extends Component {
);
}
// 登录
loginRequest() {
// if (this.props.loginSuccess) return;
let phone = this.state.phone,
let that = this,
phone = this.state.phone,
password = this.state.password;
// if (!/^1(3|4|5|7|8)\d{9}$/.test(phone)) {
// alert("请正确输入手机号码");
... ... @@ -140,48 +172,93 @@ export default class Login extends Component {
// alert("请输入密码");
// return;
// }
// this.props.onLogin(true);
let that = this;
let formData = new FormData();
formData.append("phone",phone);
formData.append("password",password);
formData.append("phone", phone);
formData.append("password", password);
return fetch(`https://devpay.brae.co/test/insurance/login`,{
return fetch(`https://devpay.brae.co/test/insurance/login`, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Accept: "application/json",
"Content-Type": "application/json"
},
body: formData,
body: formData
})
.then((resp) => {
console.log("测试接口",resp);
// return false;
.then(resp => {
if (resp.status === 200) {
return resp.json();
} else {
console.error("something went wrong!");
}
})
.then((respJson) => {
// if (respJson.enmsg != 'ok') {
// console.error(err);
// alert(respJson.cnmsg);
// } else {
.then(respJson => {
if (respJson.enmsg != "ok") {
alert(respJson.cnmsg);
} else {
that.setState({
testData: respJson.data
// testData: respJson.data
});
console.log('看看登录能获得啥数据',that.state.testData);
// }
AsyncStorage.setItem("IS_LOGIN", "yes", function(err) {
if (err) {
console.log("存储出错", err);
return false;
}
that.props.navigation.navigate("Mine");
});
}
})
.catch(err => console.error(err));
}
// 注册
register() {
let that = this,
phone = this.state.phone,
password = this.state.password,
nickname = this.state.nickname,
formData = new FormData();
if (!phone || !password || !nickname) {
alert("请完整填写信息");
return false;
}
formData.append("phone", phone);
formData.append("password", password);
formData.append("nickname", nickname);
return fetch(`https://devpay.brae.co/test/insurance/register`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: formData
})
.then(resp => {
if (resp.status === 200) {
return resp.json();
} else {
console.error("something went wrong!");
}
})
.then(respJson => {
if (respJson.enmsg != "ok") {
alert(respJson.cnmsg);
} else {
console.log("注册按钮返回的数据",respJson.data);
that.setState({
isRegister: !that.state.isRegister,
id: respJson.data.id,
});
alert("注册成功!");
}
})
.catch((err) => console.error(err))
.catch(err => console.error(err));
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 125,
paddingTop: 80,
justifyContent: "flex-start",
alignItems: "stretch",
backgroundColor: "#F5FCFF",
... ...
... ... @@ -28,6 +28,7 @@ export default class Mine extends Component {
}
componentWillMount() {
var _that = this;
console.log("this",this)
AsyncStorage.getItem("IS_LOGIN",(err, result) => {
if (err) {
console.error(err);
... ... @@ -35,7 +36,7 @@ export default class Mine extends Component {
_that.setState({
IS_LOGIN: result
})
console.log("能拿到IS_LOGIN吗?",this.state.IS_LOGIN);
console.log("我的页面的IS_LOGIN",this.state.IS_LOGIN);
})
}
componentDidMount() {
... ... @@ -52,7 +53,6 @@ export default class Mine extends Component {
_that.setState({
IS_LOGIN: "yes"
})
console.log("改变AsyncStorage",this.state.IS_LOGIN);
}
signOut() {
var _that = this
... ... @@ -65,7 +65,6 @@ export default class Mine extends Component {
_that.setState({
IS_LOGIN: "no"
})
console.log("改变AsyncStorage",this.state.IS_LOGIN);
}
saveResponse1(d) {
if (this.state.IS_LOGIN == "yes") {
... ... @@ -138,17 +137,23 @@ export default class Mine extends Component {
</TouchableOpacity>
</View>
{/* 退出登录 */}
{/* 登录 */}
<TouchableOpacity
style={styles.loginOut}
onLogin={this.props.onLogin}
onPress={() => {
this.signOut();
navigate("Login");
{/* this.signOut(); */}
navigate("Login",{IS_LOGIN: this.state.IS_LOGIN});
}}
>
<Text style={styles.loginOutText}>{this.state.IS_LOGIN ? "登录" : "退出登录"}</Text>
</TouchableOpacity>
{/* <Button
title="登出"
onPress={() => {
this.signOut();
}}
/> */}
</View>
);
}
... ...