maguohua 8 лет назад
Родитель
Сommit
b62805fc82
5 измененных файлов с 38 добавлено и 35 удалено
  1. 1 2
      README.md
  2. 1 2
      app.js
  3. 3 3
      controller/admin/admin.js
  4. 1 1
      middlewares/check.js
  5. 32 27
      prototype/addressComponent.js

+ 1 - 2
README.md

@@ -4,10 +4,9 @@
 
 因为前端项目是根据饿了么官网接口写的,所以后台系统也保持和官网一致的API接口。
 
-因为接下来的项目 react-native 构建原生APP,也是以这个后台系统作为基础,而且APP需要更加细粒度的功能,也就需要更多的接口。
-
 __注:此项目只作为nodejs的练习,不用于任何商业用途。__
 
+
 # 说明
 
 >  如果对您对此项目有兴趣,可以点 "Star" 支持一下 谢谢! ^_^

+ 1 - 2
app.js

@@ -9,7 +9,6 @@ import winston from 'winston';
 import expressWinston from 'express-winston';
 import path from 'path';
 import history from 'connect-history-api-fallback';
-// import history from './middlewares/history';
 import Statistic from './middlewares/statistic'
 
 const app = express();
@@ -31,7 +30,7 @@ app.use(Statistic.apiRecord)
 const MongoStore = connectMongo(session);
 app.use(cookieParser());
 app.use(session({
-  	name: config.session.name,
+	  	name: config.session.name,
 		secret: config.session.secret,
 		resave: true,
 		saveUninitialized: false,

+ 3 - 3
controller/admin/admin.js

@@ -28,9 +28,9 @@ class Admin extends AddressComponent {
 			const {user_name, password, status = 1} = fields;
 			try{
 				if (!user_name) {
-					throw new Error('用户名错误')
+					throw new Error('用户名参数错误')
 				}else if(!password){
-					throw new Error('密码错误')
+					throw new Error('密码参数错误')
 				}
 			}catch(err){
 				console.log(err.message, err);
@@ -68,7 +68,7 @@ class Admin extends AddressComponent {
 					res.send({
 						status: 0,
 						type: 'ERROR_PASSWORD',
-						message: '密码输入错误',
+						message: '该用户已存在,密码输入错误',
 					})
 				}else{
 					req.session.admin_id = admin.id;

+ 1 - 1
middlewares/check.js

@@ -43,7 +43,7 @@ class Check {
 				res.send({
 					status: 0,
 					type: 'HAS_NO_ACCESS',
-					message: '权限不足,请联系管理员提升权限',
+					message: '权限不足',
 				})
 				return
 			}

+ 32 - 27
prototype/addressComponent.js

@@ -13,34 +13,39 @@ class AddressComponent extends BaseComponent {
 	}
 	//获取定位地址
 	async guessPosition(req){
-		let ip = req.headers['x-forwarded-for'] || 
- 		req.connection.remoteAddress || 
- 		req.socket.remoteAddress ||
- 		req.connection.socket.remoteAddress;
- 		const ipArr = ip.split(':');
- 		ip = ipArr[ipArr.length -1];
- 		if (process.env.NODE_ENV == 'development') {
- 			ip = '116.231.55.195';
- 		}
- 		try{
-	 		const result = await this.fetch('http://apis.map.qq.com/ws/location/v1/ip', {
-	 			ip,
-	 			key: this.tencentkey,
-	 		})
-	 		if (result.status == 0) {
-	 			const cityInfo = {
-	 				lat: result.result.location.lat,
-	 				lng: result.result.location.lng,
-	 				city: result.result.ad_info.city,
-	 			}
-	 			cityInfo.city = cityInfo.city.replace(/市$/, '');
-	 			return cityInfo
-	 		}else{
-	 			throw new Error('定位失败');
+		return new Promise(async (resolve, reject) => {
+			let ip = req.headers['x-forwarded-for'] || 
+	 		req.connection.remoteAddress || 
+	 		req.socket.remoteAddress ||
+	 		req.connection.socket.remoteAddress;
+	 		const ipArr = ip.split(':');
+	 		ip = ipArr[ipArr.length -1];
+	 		if (ip.indexOf('.') == -1) {
+	 			resolve({
+	 				city: '本地访问'
+	 			})
+	 			return
 	 		}
- 		}catch(err){
- 			throw new Error(err);
- 		}
+	 		try{
+		 		const result = await this.fetch('http://apis.map.qq.com/ws/location/v1/ip', {
+		 			ip,
+		 			key: this.tencentkey,
+		 		})
+		 		if (result.status == 0) {
+		 			const cityInfo = {
+		 				lat: result.result.location.lat,
+		 				lng: result.result.location.lng,
+		 				city: result.result.ad_info.city,
+		 			}
+		 			cityInfo.city = cityInfo.city.replace(/市$/, '');
+		 			resolve(cityInfo)
+		 		}else{
+		 			reject('定位失败');
+		 		}
+	 		}catch(err){
+	 			reject(err);
+	 		}
+		})
 	}
 	//搜索地址
 	async searchPlace(keyword, cityName, type = 'search'){