Procházet zdrojové kódy

根据id查找城市

maguohua před 8 roky
rodič
revize
eec1cbd2cb
3 změnil soubory, kde provedl 78 přidání a 23 odebrání
  1. 37 18
      controller/v1/cities.js
  2. 39 4
      models/v1/cities.js
  3. 2 1
      routes/v1.js

+ 37 - 18
controller/v1/cities.js

@@ -9,31 +9,50 @@ import BaseComponent from '../../prototype/baseComponent'
 class CityHandle extends BaseComponent{
 	constructor(){
 		super()
-		this.cityGuess = this.cityGuess.bind(this);
+		this.getCity = this.getCity.bind(this);
 	}
-	async cityGuess(req, res, next){
+	async getCity(req, res, next){
 		const type = req.query.type;
-		if (!type) {
+		let cityInfo;
+		try{
+			switch (type){
+				case 'guess': 
+					const city = await this.getCityName(req);
+					cityInfo = await Cities.cityGuess(city);
+					break;
+				case 'hot': 
+					cityInfo = await Cities.cityHot();
+					break;
+				case 'group': 
+					cityInfo = await Cities.cityGroup();
+					break;
+				default: 
+					res.json({
+						name: 'ERROR_QUERY_TYPE',
+						message: '参数错误',
+					})
+					return
+			}
+			res.send(cityInfo);
+		}catch(err){
+			res.send(err);
+		}
+	}
+	async getCityById(req, res, next){
+		const cityid = req.params.id;
+		if (isNaN(cityid)) {
 			res.json({
-				name: 'ERROR_QUERY_TYPE',
+				name: 'ERROR_PARAM_TYPE',
 				message: '参数错误',
 			})
-			return 
+			return
 		}
-		let cityInfo;
-		switch (type){
-			case 'guess': 
-				const city = await this.getCityName(req);
-				cityInfo = await Cities.cityGuess(city);
-				break;
-			case 'hot': 
-				cityInfo = await Cities.cityHot();
-				break;
-			case 'group': 
-				cityInfo = await Cities.cityGroup();
-				break;
+		try{
+			const cityInfo = await Cities.getCityById(cityid);
+			res.send(cityInfo);
+		}catch(err){
+			res.send(err);
 		}
-		res.send(cityInfo)
 	}
 	getCityName(req){
 		return new Promise(async (resolve, reject) => {

+ 39 - 4
models/v1/cities.js

@@ -29,7 +29,11 @@ citySchema.statics.cityGuess = function(name){
 				}
 			})
 		}catch(err){
-			console.error(err);
+			reject({
+				name: 'ERROR_DATA',
+				message: '查找数据失败',
+			});
+			console.log(err);
 		}
 	})
 }
@@ -40,7 +44,11 @@ citySchema.statics.cityHot = function (){
 			const city = await this.findOne();
 			resolve(city._doc.hotCities)
 		}catch(err){
-			console.error(err);
+			reject({
+				name: 'ERROR_DATA',
+				message: '查找数据失败',
+			});
+			console.log(err);
 		}
 	})
 }
@@ -49,12 +57,39 @@ citySchema.statics.cityGroup = function (){
 	return new Promise(async (resolve, reject) => {
 		try{
 			const city = await this.findOne();
-			let cityObj = city._doc;
+			const cityObj = city._doc;
 			delete(cityObj._id)
 			delete(cityObj.hotCities)
 			resolve(cityObj)
 		}catch(err){
-			console.error(err);
+			reject({
+				name: 'ERROR_DATA',
+				message: '查找数据失败',
+			});
+			console.log(err);
+		}
+	})
+}
+
+citySchema.statics.getCityById = function(id){
+	return new Promise(async (resolve, reject) => {
+		try{
+			const city = await this.findOne();
+			Object.entries(city._doc).forEach(item => {
+				if(item[0] !== '_id' && item[0] !== 'hotCities'){
+					item[1].forEach(cityItem => {
+						if (cityItem.id == id) {
+							resolve(cityItem)
+						}
+					})
+				}
+			})
+		}catch(err){
+			reject({
+				name: 'ERROR_DATA',
+				message: '查找数据失败',
+			});
+			console.log(err);
 		}
 	})
 }

+ 2 - 1
routes/v1.js

@@ -4,6 +4,7 @@ import express from 'express';
 import CityHandle from '../controller/v1/cities'
 const router = express.Router();
 
-router.get('/cities', CityHandle.cityGuess);
+router.get('/cities', CityHandle.getCity);
+router.get('/cities/:id', CityHandle.getCityById);
 
 export default router