maguohua 8 лет назад
Родитель
Сommit
353f98d160
3 измененных файлов с 48 добавлено и 19 удалено
  1. 24 3
      controller/v1/cities.js
  2. 22 13
      models/v1/cities.js
  3. 2 3
      routes/v1.js

+ 24 - 3
controller/v1/cities.js

@@ -1,8 +1,29 @@
 'use strict';
 
-class Cities {
+import Cities from '../../models/v1/cities';
+
+class CityHandle {
 	constructor(){
 
 	}
-	
-}
+	async cityGuess(req, res, next){
+		const type = req.query.type;
+		if (!type) {
+			res.json({
+				name: 'ERROR_QUERY_TYPE',
+				message: '参数错误',
+			})
+			return 
+		}
+		let cityInfo;
+		switch (type){
+			case 'guess': 
+				cityInfo = await Cities.cityGuess('SH');
+
+
+		}
+		res.json(cityInfo)
+	}
+}
+
+export default new CityHandle()

+ 22 - 13
models/v1/cities.js

@@ -3,21 +3,30 @@
 import mongoose from 'mongoose';
 
 const citySchema = new mongoose.Schema({
-	data: {
-		type: [{
-			id: Number,
-			name: String,
-			abbr: String,
-			area_code: String,
-			sort: Number,
-			latitude: Number,
-			longitude: Number,
-			is_map: Boolean,
-			pinyin: String,
-		}]
-	}
+	id: Number,
+	name: String,
+	abbr: String,
+	area_code: String,
+	sort: Number,
+	latitude: Number,
+	longitude: Number,
+	is_map: Boolean,
+	pinyin: String,
 });
 
+citySchema.statics.cityGuess = async (name) => {
+	try{
+		let city = await this.find();
+		city.entries.forEach(item => {
+			
+		})
+	}catch(err){
+		console.error(err);
+	}
+
+}
+
+
 const Cities = mongoose.model('Cities', citySchema);
 
 export default Cities

+ 2 - 3
routes/v1.js

@@ -1,10 +1,9 @@
 'use strict';
 
 import express from 'express';
+import CityHandle from '../controller/v1/cities'
 const router = express.Router();
 
-router.get('/cities', (req, res, next) => {
-	res.send('2323132131')
-});
+router.get('/cities', CityHandle.cityGuess);
 
 export default router