maguohua 8 anni fa
parent
commit
0465dc0c87
11 ha cambiato i file con 206 aggiunte e 30 eliminazioni
  1. 1 1
      README.md
  2. 17 15
      app.js
  3. 121 0
      controller/statis/statis.js
  4. 30 0
      middlewares/statistic.js
  5. 2 0
      models/ids.js
  6. 18 0
      models/statis/statis.js
  7. 1 1
      prototype/baseComponent.js
  8. 0 10
      routes/home.js
  9. 2 2
      routes/index.js
  10. 14 0
      routes/statis.js
  11. 0 1
      routes/v1.js

+ 1 - 1
README.md

@@ -71,7 +71,7 @@ npm run dev (需先开启mongodb)
 - [x] 食品管理
 - [x] 会员管理
 - [x] 订单管理
-- [ ] 流量统计
+- [x] 流量统计
 - [x] 超级管理员
 - [x] 美化路由(history模式)
 - [ ] 部署上线

+ 17 - 15
app.js

@@ -9,32 +9,34 @@ import winston from 'winston';
 import expressWinston from 'express-winston';
 import path from 'path';
 import history from 'connect-history-api-fallback';
+import Statistic from './middlewares/statistic'
 
 const app = express();
 
 app.all('*', (req, res, next) => {
 	res.header("Access-Control-Allow-Origin", req.headers.origin);
 	res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
-	res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
-  res.header("Access-Control-Allow-Credentials", true); //可以带cookies
-	res.header("X-Powered-By",' 3.2.1')
-  if (req.method == 'OPTIONS') {
-    res.send(200);
-  } else {
-    next();
-  }
+	res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
+  	res.header("Access-Control-Allow-Credentials", true); //可以带cookies
+	res.header("X-Powered-By", '3.2.1')
+	if (req.method == 'OPTIONS') {
+	  	res.send(200);
+	} else {
+	    next();
+	}
 });
 
+app.use(Statistic.apiRecord)
 const MongoStore = connectMongo(session);
 app.use(cookieParser());
 app.use(session({
-  name: config.session.name,
-	secret: config.session.secret,
-	resave: true,
-	saveUninitialized: false,
-	cookie: config.session.cookie,
-	store: new MongoStore({
-  	url: config.url
+  	name: config.session.name,
+		secret: config.session.secret,
+		resave: true,
+		saveUninitialized: false,
+		cookie: config.session.cookie,
+		store: new MongoStore({
+	  	url: config.url
 	})
 }))
 

+ 121 - 0
controller/statis/statis.js

@@ -0,0 +1,121 @@
+'use strict'
+
+import StatisModel from '../../models/statis/statis'
+import UserInfoModel from '../../models/v2/userInfo'
+import OrderModel from '../../models/bos/order'
+import dtime from 'time-formater'
+
+class Statis {
+	constructor(){
+
+	}
+	async apiCount(req, res, next){
+		const date = req.params.date;
+		if (!date) {
+			console.log('参数错误')
+			res.send({
+				status: 0,
+				type: 'ERROR_PARAMS',
+				message: '参数错误'
+			})
+			return
+		}
+		try{
+			const count = await StatisModel.find({date}).count()
+			res.send({
+				status: 1,
+				count,
+			})
+		}catch(err){
+			console.log('获取当天API请求次数失败');
+			res.send({
+				status: 0,
+				type: 'ERROR_GET_TODAY_API_COUNT',
+				message: '获取当天API请求次数失败'
+			})
+		}
+	}
+	async apiAllCount(req, res, next){
+		try{
+			const count = await StatisModel.count()
+			res.send({
+				status: 1,
+				count,
+			})
+		}catch(err){
+			console.log('获取所有API请求次数失败');
+			res.send({
+				status: 0,
+				type: 'ERROR_GET_ALL_API_COUNT',
+				message: '获取所有API请求次数失败'
+			})
+		}
+	}
+	async allApiRecord(req, res, next){
+		try{
+			const allRecord = await StatisModel.find({}, '-_id -__v')
+			res.send(allRecord)
+		}catch(err){
+			console.log('获取所有API请求信息失败');
+			res.send({
+				status: 0,
+				type: 'GET_ALL_API_RECORD_DATA_FAILED',
+				message: '获取所有API请求信息失败'
+			})
+		}
+	}
+	async userCount(req, res, next){
+		const date = req.params.date;
+		if (!date) {
+			console.log('参数错误')
+			res.send({
+				status: 0,
+				type: 'ERROR_PARAMS',
+				message: '参数错误'
+			})
+			return
+		}
+		try{
+			const count = await UserInfoModel.find({registe_time: eval('/^' + date + '/gi')}).count()
+			res.send({
+				status: 1,
+				count,
+			})
+		}catch(err){
+			console.log('获取当天注册人数失败');
+			res.send({
+				status: 0,
+				type: 'ERROR_GET_USER_REGISTE_COUNT',
+				message: '获取当天注册人数失败'
+			})
+		}
+	}
+	async orderCount(req, res, next){
+		const date = req.params.date;
+		if (!date) {
+			console.log('参数错误')
+			res.send({
+				status: 0,
+				type: 'ERROR_PARAMS',
+				message: '参数错误'
+			})
+			return
+		}
+		try{
+			const count = await OrderModel.find({formatted_created_at: eval('/^' + date + '/gi')}).count()
+			res.send({
+				status: 1,
+				count,
+			})
+		}catch(err){
+			console.log('获取当天订单数量失败');
+			res.send({
+				status: 0,
+				type: 'ERROR_GET_ORDER_COUNT',
+				message: '获取当天订单数量失败'
+			})
+		}
+	}
+}
+
+export default new Statis()

+ 30 - 0
middlewares/statistic.js

@@ -0,0 +1,30 @@
+'use strict';
+
+import AddressComponent from '../prototype/addressComponent'
+import StatisModel from '../models/statis/statis'
+import dtime from 'time-formater'
+
+class Statistic extends AddressComponent {
+	constructor(){
+		super()
+		this.apiRecord = this.apiRecord.bind(this)
+	}
+	async apiRecord(req, res, next){
+		try{
+			const address = await this.guessPosition(req);
+			const statis_id = await this.getId('statis_id')
+			const apiInfo = {
+				date: dtime().format('YYYY-MM-DD'),
+				origin: req.headers.origin,
+				id: statis_id,
+				address: address.city,
+			}
+			StatisModel.create(apiInfo)
+		}catch(err){
+			console.log('API记录出错', err);
+		}
+		next()
+	}
+}
+
+export default new Statistic()

+ 2 - 0
models/ids.js

@@ -14,6 +14,7 @@ const idsSchema = new mongoose.Schema({
 	item_id: Number,
 	sku_id: Number, 
 	admin_id: Number,
+	statis_id: Number,
 });
 
 const Ids = mongoose.model('Ids', idsSchema);
@@ -32,6 +33,7 @@ Ids.findOne((err, data) => {
 			item_id: 0,
 			sku_id: 0, 
 			admin_id: 0,
+			statis_id: 0,
 		});
 		newIds.save();
 	}

+ 18 - 0
models/statis/statis.js

@@ -0,0 +1,18 @@
+'use strict';
+
+import mongoose from 'mongoose'
+
+const Schema  = mongoose.Schema;
+
+const statisSchema = new Schema({
+	date: String,
+	origin: String,
+	id: Number,
+	address: String
+})
+
+statisSchema.index({id: 1})
+
+const Statis = mongoose.model('Statis', statisSchema)
+
+export default Statis

+ 1 - 1
prototype/baseComponent.js

@@ -10,7 +10,7 @@ qiniu.conf.SECRET_KEY = 'XNIW2dNffPBdaAhvm9dadBlJ-H6yyCTIJLxNM_N6';
 
 export default class BaseComponent {
 	constructor(){
-		this.idList = ['restaurant_id', 'food_id', 'order_id', 'user_id', 'address_id', 'cart_id', 'img_id', 'category_id', 'item_id', 'sku_id', 'admin_id'];
+		this.idList = ['restaurant_id', 'food_id', 'order_id', 'user_id', 'address_id', 'cart_id', 'img_id', 'category_id', 'item_id', 'sku_id', 'admin_id', 'statis_id'];
 		this.imgTypeList = ['shop', 'food', 'avatar','default'];
 		this.uploadImg = this.uploadImg.bind(this)
 		this.qiniu = this.qiniu.bind(this)

+ 0 - 10
routes/home.js

@@ -1,10 +0,0 @@
-'use strict';
-
-import express from 'express';
-const router = express.Router();
-
-router.get('/', async (req, res) => {
-	res.send('node-elm 后台程序正常启动');
-});
-
-export default router;

+ 2 - 2
routes/index.js

@@ -1,6 +1,5 @@
 'use strict';
 
-import home from './home'
 import v1 from './v1'
 import v2 from './v2'
 import v3 from './v3'
@@ -9,6 +8,7 @@ import ugc from './ugc'
 import bos from './bos'
 import eus from './eus'
 import admin from './admin'
+import statis from './statis'
 import member from './member'
 import shopping from './shopping'
 import promotion from './promotion'
@@ -17,7 +17,6 @@ export default app => {
 	// app.get('/', (req, res, next) => {
 	// 	res.redirect('/');
 	// });
-	// app.use('/home', home);
 	app.use('/v1', v1);
 	app.use('/v2', v2);
 	app.use('/v3', v3);
@@ -27,6 +26,7 @@ export default app => {
 	app.use('/eus', eus);
 	app.use('/admin', admin);
 	app.use('/member', member);
+	app.use('/statis', statis);
 	app.use('/shopping', shopping);
 	app.use('/promotion', promotion);
 }

+ 14 - 0
routes/statis.js

@@ -0,0 +1,14 @@
+'use strict'
+
+import express from 'express'
+import Statis from '../controller/statis/statis'
+
+const router = express.Router()
+
+router.get('/api/:date/count', Statis.apiCount)
+router.get('/api/all/count', Statis.apiAllCount)
+router.get('/api/all', Statis.allApiRecord)
+router.get('/user/:date/count', Statis.userCount)
+router.get('/order/:date/count', Statis.orderCount)
+
+export default router

+ 0 - 1
routes/v1.js

@@ -33,6 +33,5 @@ router.delete('/users/:user_id/addresses/:address_id', Address.deleteAddress);
 router.post('/users/:user_id/carts/:cart_id/orders', Order.postOrder);
 router.post('/users/:user_id/hongbao/exchange', Hongbao.exchange);
 
-
  
 export default router