Skip to content

Commit f97d96c

Browse files
committed
0.1.0
可测试版本
1 parent fbc652a commit f97d96c

20 files changed

+641
-109
lines changed

README.md

+19-18
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@ React-test
33

44
## 介绍
55

6-
这是一个react测试项目,旨在通过webpack打包生成用于restful API的one page react项目
6+
这是一个react测试项目,旨在通过webpack打包生成用于restful API的one page react项目.
7+
该demo展示了异步登录,页面检测登录自动跳转,不同页面渲染等相关操作.
8+
须安装[webpack](https://github.com/webpack/webpack),[redux-devtools-extension](https://github.com/zalmoxisus/redux-devtools-extension)
79

8-
## 技术栈
10+
## 目录结构
911
```
10-
*babel
11-
*webpack
12-
*react
13-
*redux
14-
*react-redux
15-
*react-router
16-
*react-router-redux
12+
webpack.config.js //webpack配置文件
13+
app.js //express应用程序
14+
-views //express模板
15+
-public //静态文件夹
16+
-client //客户端项目文件夹
17+
--app.js //入口文件
18+
--config.js //配置文件
19+
---actions //redux action列表
20+
---reducers //redux reducer列表
21+
---components //react组件
22+
---pages //页面组件
1723
```
1824

19-
## 目录结构
25+
##如何运行
2026
```
21-
-client //客户端项目库
22-
--app.js //入口文件
23-
--router.js //路由
24-
---actions //redux action列表
25-
---reducers //redux reducer列表
26-
---components //react组件
27-
---pages //页面组件
28-
-public //发布目录
27+
npm install
28+
webpack
29+
npm start
2930
```

app.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//加载依赖
2+
var express = require('express');
3+
var path = require('path');
4+
5+
var app = express();
6+
7+
// view engine setup
8+
app.set('views', path.join(__dirname, 'views'));
9+
app.set('view engine', 'html');
10+
app.engine('html', require('ejs-mate'));
11+
12+
app.use(express.static(path.join(__dirname, 'public')));
13+
14+
//路由设置
15+
app.get('*',function(req,res){
16+
res.render('index',{});
17+
});
18+
19+
20+
// catch 404 and forward to error handler
21+
app.use(function(req, res, next) {
22+
var err = {status:404};
23+
next(err);
24+
});
25+
26+
// error handlers
27+
28+
// production error handler
29+
// no stacktraces leaked to user
30+
app.use(function(err, req, res, next) {
31+
console.log(err);
32+
if(!err.status){
33+
err.status=500;
34+
}
35+
switch(err.status){
36+
case 403:
37+
err.message = '没有权限访问';
38+
break;
39+
case 404:
40+
err.message = '找不到这个页面';
41+
break;
42+
case 500:
43+
err.message='系统出错了';
44+
break;
45+
}
46+
res.status(err.status);
47+
res.render('error', {
48+
title: '出错拉',
49+
error:err
50+
});
51+
});
52+
53+
module.exports = app;

bin/www

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var http = require('http');
9+
10+
/**
11+
* Get port from environment and store in Express.
12+
*/
13+
14+
var port = normalizePort(process.env.PORT || 8080);
15+
app.set('port', port);
16+
17+
/**
18+
* Create HTTP server.
19+
*/
20+
21+
var server = http.createServer(app);
22+
23+
/**
24+
* Listen on provided port, on all network interfaces.
25+
*/
26+
27+
server.listen(port);
28+
server.on('error', onError);
29+
server.on('listening', onListening);
30+
31+
/**
32+
* Normalize a port into a number, string, or false.
33+
*/
34+
35+
function normalizePort(val) {
36+
var port = parseInt(val, 10);
37+
38+
if (isNaN(port)) {
39+
// named pipe
40+
return val;
41+
}
42+
43+
if (port >= 0) {
44+
// port number
45+
return port;
46+
}
47+
48+
return false;
49+
}
50+
51+
/**
52+
* Event listener for HTTP server "error" event.
53+
*/
54+
55+
function onError(error) {
56+
if (error.syscall !== 'listen') {
57+
throw error;
58+
}
59+
60+
var bind = typeof port === 'string'
61+
? 'Pipe ' + port
62+
: 'Port ' + port
63+
64+
// handle specific listen errors with friendly messages
65+
switch (error.code) {
66+
case 'EACCES':
67+
console.error(bind + ' requires elevated privileges');
68+
process.exit(1);
69+
break;
70+
case 'EADDRINUSE':
71+
console.error(bind + ' is already in use');
72+
process.exit(1);
73+
break;
74+
default:
75+
throw error;
76+
}
77+
}
78+
79+
/**
80+
* Event listener for HTTP server "listening" event.
81+
*/
82+
83+
function onListening() {
84+
var addr = server.address();
85+
var bind = typeof addr === 'string'
86+
? 'pipe ' + addr
87+
: 'port ' + addr.port;
88+
console.log('Listening on ' + bind);
89+
}

client/actions/auth.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Action - auth
3+
* 验证方法
4+
*/
5+
//加载依赖
6+
import { push } from 'react-router-redux'; //router跳转方法
7+
8+
//导入常量
9+
import { LOGIN_STATUS_IN } from '../actions/login';
10+
11+
/*
12+
* action 创建函数
13+
*/
14+
15+
/**
16+
* [authLoginStatus description]
17+
* @param {string} login_status 登录状态
18+
* @param {boolen} check_status 须检测的状态,true为检测是否登录(当未登录时,跳转到登录页),false为检测是否未登录(当登录时,跳转到dashboard页)
19+
* @return {function} [description]
20+
*/
21+
export function authLoginStatus(login_status,check_status){
22+
return function(dispatch) {
23+
//检测登录,当未登录时,跳转到登录页
24+
if(check_status&&login_status!==LOGIN_STATUS_IN){
25+
dispatch(push('/'));
26+
}
27+
//检测未登录,当登录时,跳转到dashboard页
28+
if(!check_status&&login_status===LOGIN_STATUS_IN){
29+
dispatch(push('/dashboard/'));
30+
}
31+
}
32+
}

client/actions/login.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Action - login
3+
* 登录,控制登录操作
4+
*/
5+
//加载依赖
6+
import fetch from 'isomorphic-fetch';
7+
import {
8+
API_LOGIN, API_LOGOUT
9+
}
10+
from '../config';
11+
import {
12+
push
13+
}
14+
from 'react-router-redux'; //router跳转方法
15+
16+
/*
17+
* action 类型
18+
*/
19+
//登入请求
20+
export const LOGIN_REQUEST = 'LOGIN_REQUEST';
21+
export const LOGIN_RECEIVE = 'LOGIN_RECEIVE';
22+
export const LOGIN_ERROR = 'LOGIN_ERROR';
23+
//登出请求
24+
export const LOGOUT_REQUEST = 'LOGOUT_REQUEST';
25+
export const LOGOUT_RECEIVE = 'LOGOUT_RECEIVE';
26+
export const LOGOUT_ERROR = 'LOGOUT_ERROR';
27+
//状态
28+
export const LOGIN_STATUS_OUT = 'out'; //登出
29+
export const LOGIN_STATUS_IN = 'in'; //登入
30+
export const LOGIN_STATUS_LOAD = 'load'; //加载
31+
export const LOGIN_STATUS_ERROR = 'error'; //出错
32+
/*
33+
* action 创建函数
34+
*/
35+
//登录
36+
export function loginRequest() {
37+
return {
38+
type: LOGIN_REQUEST
39+
}
40+
}
41+
42+
export function loginReceive(data) {
43+
return {
44+
type: LOGIN_RECEIVE,
45+
info: data
46+
}
47+
}
48+
49+
export function loginError(data) {
50+
return {
51+
type: LOGIN_ERROR,
52+
msg: data
53+
}
54+
}
55+
56+
export function loginPost(data) {
57+
return function(dispatch) {
58+
if (!data.name || !data.password) {
59+
dispatch(loginError('没有输入用户名或密码'));
60+
return false;
61+
}
62+
dispatch(loginRequest());
63+
let headers = new Headers(data);
64+
let request = new Request(API_LOGIN, {
65+
method: 'GET',
66+
headers: headers
67+
})
68+
fetch(request).then(response => response.json())
69+
.then(json => {
70+
if (json.head.status === 200) {
71+
dispatch(loginReceive(json.body));
72+
//跳转到登录后首页
73+
dispatch(push('/dashboard/'));
74+
} else {
75+
dispatch(loginError(json.head.msg));
76+
}
77+
}).catch(error => {
78+
console.log(error);
79+
dispatch(loginError('网络错误,请重试'));
80+
})
81+
}
82+
}
83+
84+
//登出
85+
export function logoutRequest() {
86+
return {
87+
type: LOGOUT_REQUEST
88+
}
89+
}
90+
91+
export function logoutReceive(data) {
92+
return {
93+
type: LOGOUT_RECEIVE,
94+
info: data
95+
}
96+
}
97+
98+
export function logoutError(data) {
99+
return {
100+
type: LOGOUT_ERROR,
101+
msg: data
102+
}
103+
}
104+
105+
export function logoutPost() {
106+
return function(dispatch) {
107+
dispatch(logoutRequest());
108+
let request = new Request(API_LOGOUT, {
109+
method: 'GET'
110+
});
111+
fetch(request).then(response => response.json())
112+
.then(json => {
113+
if (json.head.status === 200) {
114+
dispatch(logoutReceive(json.body));
115+
//跳转到登录后首页
116+
dispatch(push('/'));
117+
} else {
118+
dispatch(logoutError(json.head.msg));
119+
}
120+
}).catch(error =>
121+
dispatch(loginError('网络错误,请重试'))
122+
)
123+
}
124+
}

0 commit comments

Comments
 (0)