Skip to content

Commit bd89716

Browse files
author
xujie3949
committed
修改登录操作
1 parent f5c027f commit bd89716

File tree

5 files changed

+32
-52
lines changed

5 files changed

+32
-52
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
"run-Android": "react-native run-android"
88
},
99
"dependencies": {
10-
"babel-polyfill": "^6.9.1",
11-
"isomorphic-fetch": "^2.2.1",
1210
"react": "15.2.1",
1311
"react-native": "0.30.0",
1412
"react-native-button": "^1.6.0",

src/actions/loginActions.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,18 @@
33
*/
44
import { createAction } from 'redux-actions';
55
import loginActionEnum from '../constants/loginActionEnum'
6-
import {api} from '../apis/api'
6+
import {api, callApi} from '../apis/api'
77
const loginActions = {
88
login: createAction(loginActionEnum.LOGIN_REQUEST),
9-
loginRequest: (data)=> {
9+
loginRequest: (parameter)=> {
1010
return dispatch=> {
11-
api.login(data)
12-
.then(response=> {
13-
if (response.ok) {
14-
return response;
15-
} else {
16-
console.warn("出错了");
17-
}
18-
})
19-
.then(response=>response.json())
20-
.then(jsonResult=> {
21-
if (jsonResult.errcode === 0) {
22-
console.warn(jsonResult.data);
23-
} else {
24-
console.warn(jsonResult.errmsg);
25-
}
26-
})
27-
.catch(error=> {
28-
console.warn(error);
29-
});
11+
callApi(
12+
api.login(parameter),
13+
(data)=>console.warn(data),
14+
(err)=>console.warn(err)
15+
);
3016
}
31-
},
17+
}
3218
};
3319

3420
export default loginActions;

src/apis/api.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,35 @@ import { fetchMethod, createFetch } from '../fetch/fetch'
22
import appConfig from '../constants/appConfig'
33

44
const api = {
5-
test: (parameter)=> createFetch(
6-
`${appConfig.serviceRoot}service/man/userInfo/login`,
7-
fetchMethod.Get,
8-
{parameter: parameter}
9-
),
105
login: (parameter)=> createFetch(
116
`${appConfig.serviceRoot}app/login`,
127
fetchMethod.Get,
138
{parameter: parameter}
149
)
1510
};
1611

17-
function callApi(api, success, fail){
18-
api.then(response=> {
19-
if (response.ok) {
20-
return response;
21-
} else {
22-
throw new Error(`网络错误:${response.status}`);
23-
}
24-
})
25-
.then(response=>response.json())
26-
.then(jsonResult=> {
27-
if (jsonResult.errcode === 0) {
28-
if (success) {
29-
const data = jsonResult.data;
30-
success(data);
31-
}
32-
} else {
33-
throw new Error(`服务器错误:${jsonResult.errmsg}`);
34-
}
35-
})
36-
.catch(error=> {
37-
if (fail) {
38-
fail(error.message);
12+
async function callApi(api, success, fail) {
13+
try {
14+
const response = await api;
15+
if (!response.ok) {
16+
throw new Error(`网络错误:${response.status}`);
17+
}
18+
19+
const jsonResult = await response.json();
20+
21+
if (jsonResult.errcode === 0) {
22+
if (success) {
23+
const data = jsonResult.data;
24+
success(data);
3925
}
40-
})
26+
} else {
27+
throw new Error(`服务器错误:${jsonResult.errmsg}`);
28+
}
29+
} catch (error) {
30+
if (fail) {
31+
fail(error.message);
32+
}
33+
}
4134
}
4235

4336
export {

src/containers/android/Login.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class Login extends Component {
7474
this.login = this.login.bind(this);
7575
}
7676
login() {
77+
console.log("test");
7778
const username = this.refs.username._lastNativeText;
7879
const password = this.refs.password._lastNativeText;
7980
const data = {

src/fetch/fetch.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function createFetch(url, method, jsonObj) {
3030
const options = {
3131
method: fetchMethod.Get
3232
};
33+
console.log(urlWithQueryStr);
3334
return fetch(urlWithQueryStr, options);
3435
}
3536
break;
@@ -40,6 +41,7 @@ function createFetch(url, method, jsonObj) {
4041
method: fetchMethod.Post,
4142
body: queryStr
4243
};
44+
console.log(url);
4345
return fetch(url, options);
4446
}
4547
break;

0 commit comments

Comments
 (0)