Browse Source

add food api

maguohua 8 years ago
parent
commit
cb86880be7
8 changed files with 316 additions and 25 deletions
  1. 1 0
      README.md
  2. 1 1
      config/default.js
  3. 172 0
      controller/shopping/food.js
  4. 24 7
      controller/shopping/shop.js
  5. 18 12
      models/ids.js
  6. 79 0
      models/shopping/food.js
  7. 17 5
      prototype/baseComponent.js
  8. 4 0
      routes/shopping.js

+ 1 - 0
README.md

@@ -51,6 +51,7 @@ npm run dev
 - [x] 添加商铺
 - [ ] 添加食品
 - [ ] 搜索美食,餐馆
+- [ ] 测距
 - [ ] 餐馆排序
 - [ ] 购物车功能
 - [ ] 评价

+ 1 - 1
config/default.js

@@ -2,7 +2,7 @@
 
 module.exports = {
 	port: 8001,
-	url: 'mongodb://localhost:27017/elm',
+	url: 'mongodb://cangdu.org:27017/elm',
 	session: {
 		name: 'elm',
 		secret: 'elm',

+ 172 - 0
controller/shopping/food.js

@@ -0,0 +1,172 @@
+'use strict';
+
+import FoodModel from '../../models/shopping/food'
+import BaseComponent from '../../prototype/baseComponent'
+import formidable from 'formidable'
+
+class Food extends BaseComponent{
+	constructor(){
+		super()
+		this.addFood = this.addFood.bind(this);
+		this.getCategory = this.getCategory.bind(this);
+		this.addCategory = this.addCategory.bind(this);
+		this.uploadFoodImg = this.uploadFoodImg.bind(this);
+	}
+	async getCategory(req, res, next){
+		const restaurant_id = req.params.restaurant_id;
+		try{
+			const category_list = await FoodModel.find({restaurant_id});
+			res.send({
+				status: 1,
+				category_list,
+			})
+
+		}catch(err){
+			console.log('获取餐馆食品种类失败');
+			res.send({
+				status: 0,
+				type: 'ERROR_GET_DATA',
+				message: '获取数据失败'
+			})
+		}
+	}
+	async addCategory(req, res, next){
+		const form = new formidable.IncomingForm();
+		let category_id;
+		try{
+			category_id = await this.getId('category_id');
+		}catch(err){
+			console.log('获取category_id失败');
+			res.send({
+				type: 'ERROR_DATA',
+				message: '获取数据失败'
+			})
+			return
+		}
+		form.parse(req, async (err, fields, files) => {
+			const foodObj = {
+				name: fields.name,
+				description: fields.description, 
+				restaurant_id: fields.restaurant_id, 
+				id: category_id,
+			}
+			const newFood = new FoodModel(foodObj);
+			try{
+				await newFood.save();
+				res.send({
+					status: 1,
+					message: '添加食品种类成功',
+				})
+			}catch(err){
+				console.log('保存数据失败');
+				res.send({
+					status: 0,
+					type: 'ERROR_IN_SAVE_DATA',
+					message: '保存数据失败',
+				})
+			}
+		})
+	}
+	async addFood(req, res, next){
+		res.send({a: 2})
+		const newFood = 
+		{
+			description: "大家喜欢吃,才叫真好吃。",
+			is_selected: true,
+			icon_url: "5da3872d782f707b4c82ce4607c73d1ajpeg",
+			name: "热销榜",
+			id: 1,
+			restaurant_id: 3,
+			foods: [
+				{
+					rating: 4.3,
+					restaurant_id: 154078098,
+					description: "",
+					month_sales: 262,
+					rating_count: 86,
+					tips: "86评价 月售262份",
+					image_path: "8c4faa8342498a301c464711b6d8a8bcjpeg",
+					item_id: "165472716078",
+					name: "招牌豪大大鸡排(特大)",
+					satisfy_count: 80,
+					satisfy_rate: 93,
+					category_id: 513873481,
+					activity: null,
+					attributes: [ ],
+					specfoods: [
+						{
+							
+							sku_id: "191809493294",
+							name: "招牌豪大大鸡排(特大)",
+							restaurant_id: 154078098,
+							food_id: 577587396,
+							packing_fee: 0,
+							recent_rating: 4.3,
+							price: 16,
+							item_id: "165472716078",
+							checkout_mode: 1,
+							stock: 9862,
+							specs: [
+								{
+									name: "规格",
+									value: "招牌豪大大鸡排(特大)"
+								}
+							],
+							is_essential: false,
+							recent_popularity: 131,
+							sold_out: false,
+							promotion_stock: -1,
+							original_price: null,
+							pinyin_name: "zhaopaihaodadajipai(teda)",
+						},
+					],
+					attrs: [
+						{
+							values: [
+								"原味",
+								"甘梅",
+								"咖喱",
+								"孜然",
+								"番茄酱"
+							],
+							name: "口味"
+						},
+					],
+					specifications: [
+						{
+							values: [
+								"招牌豪大大鸡排(特大)",
+								"默认"
+							],
+							name: "规格"
+						}
+					],
+					is_essential: false,
+					server_utc: 1494479869,
+					is_featured: 0,
+					pinyin_name: "zhaopaihaodadajipai(teda)",
+					limitation: { },
+					display_times: [ ],
+				}
+			]
+		}
+	}
+
+	async uploadFoodImg(req, res, next){
+		try{
+			let path = await this.uploadImg(req, 'food');
+			res.send({
+				status: 1,
+				image_path: path
+			})
+		}catch(err){
+			res.send({
+				type: 'ERROR_PATH',
+				message: '上传头像失败',
+				status: 0,
+			})
+		}
+	}
+}
+
+export default new Food()

+ 24 - 7
controller/shopping/shop.js

@@ -1,6 +1,6 @@
 'use strict';
 
-import ShopModel from '../../models/shopping/shop';
+import ShopModel from '../../models/shopping/shop'
 import AddressComponent from '../../prototype/addressComponent'
 import formidable from 'formidable'
 
@@ -11,10 +11,11 @@ class Shop extends AddressComponent{
 		this.uploadShopImg = this.uploadShopImg.bind(this);
 	}
 	async addShop(req, res, next){
-		let shopId;
+		let restaurant_id;
 		try{
-			shopId = await this.getId('shopId');
+			restaurant_id = await this.getId('restaurant_id');
 		}catch(err){
+			console.log('获取商店id失败');
 			res.send({
 				type: 'ERROR_DATA',
 				message: '获取数据失败'
@@ -34,7 +35,9 @@ class Shop extends AddressComponent{
 					throw new Error('商店位置信息错误');
 				}
 			}catch(err){
+				console.log('前台参数出错');
 				res.send({
+					status: 0,
 					type: 'ERROR_PARAMS',
 					message: err.message
 				})
@@ -47,7 +50,7 @@ class Shop extends AddressComponent{
 				description: fields.description || '',
 				float_delivery_fee: fields.float_delivery_fee || 0,
 				float_minimum_order_amount: fields.float_minimum_order_amount || 0,
-				id: shopId,
+				id: restaurant_id,
 				is_premium: fields.is_premium || false,
 				is_new: fields.new || false,
 				latitude: 31.056997,
@@ -138,18 +141,32 @@ class Shop extends AddressComponent{
 					name: "开发票"
 				})
 			}
-			res.send(newShop)
-			return
+			try{
+				const shop = new ShopModel(newShop);
+				await shop.save();
+				res.send({
+					status: 1,
+					shopDetail: newShop
+				})
+			}catch(err){
+				console.log('商铺写入数据库失败');
+				res.send({
+					status: 0,
+					type: 'ERROR_SERVER',
+					message: '添加商铺失败',
+				})
+			}
 		})
 	}
 	async uploadShopImg(req, res, next){
 		try{
-			let path = await this.uploadImg(req, 'shop');
+			const path = await this.uploadImg(req, 'shop');
 			res.send({
 				status: 1,
 				image_path: path
 			})
 		}catch(err){
+			console.log('后台写入图片出错');
 			res.send({
 				type: 'ERROR_PATH',
 				message: '上传头像失败',

+ 18 - 12
models/ids.js

@@ -3,13 +3,16 @@
 import mongoose from 'mongoose'
 
 const idsSchema = new mongoose.Schema({
-	shopId: Number,
-	foodId: Number,
+	restaurant_id: Number,
+	food_id: Number,
 	orderId: Number,
-	userId: Number,
-	addressId: Number,
-	cartId: Number,
-	imgId: Number,
+	user_id: Number,
+	address_id: Number,
+	cart_id: Number,
+	img_id: Number,
+	category_id: Number,
+	item_id: Number,
+	sku_id: Number, 
 });
 
 const Ids = mongoose.model('Ids', idsSchema);
@@ -17,13 +20,16 @@ const Ids = mongoose.model('Ids', idsSchema);
 Ids.findOne((err, data) => {
 	if (!data) {
 		const newIds = new Ids({
-			shopId: 0,
-			foodId: 0,
+			restaurant_id: 0,
+			food_id: 0,
 			orderId: 0,
-			userId: 0,
-			addressId: 0,
-			cartId: 0,
-			imgId: 0,
+			user_id: 0,
+			address_id: 0,
+			cart_id: 0,
+			img_id: 0,
+			category_id: 0,
+			item_id: 0,
+			sku_id: 0, 
 		});
 		newIds.save();
 	}

+ 79 - 0
models/shopping/food.js

@@ -0,0 +1,79 @@
+'use strict';
+
+import mongoose from 'mongoose'
+const Schema = mongoose.Schema;
+
+const foodSchema = new Schema({
+	description: String,
+	is_selected: {type: Boolean, default: true},
+	icon_url: {type: String, default: ''},
+	name: {type: String, isRequired: true},
+	id:  {type: Number, isRequired: true},
+	restaurant_id: {type: Number, isRequired: true},
+	foods: [
+		{
+			rating: {type: Number, default: 0},
+			is_featured: {type: Number, default: 0},
+			restaurant_id: {type: Number, isRequired: true},
+			pinyin_name: String,
+			display_times: {type: Array, default: []},
+			category_id: {type: Number, isRequired: true},
+			attrs: [
+				{
+					values: [String],
+					name: String
+				}
+			],
+			description: {type: String, default: ""},
+			month_sales: {type: Number, default: 0},
+			rating_count: {type: Number, default: 0},
+			tips: String,
+			image_path: String,
+			specifications: [
+				{
+					values: [String],
+					name: String
+				}
+			],
+			server_utc: {type: Number, default: 0},
+			is_essential: {type: Boolean, default: false},
+			attributes: {type: Array, default: []},
+			item_id: {type: Number, isRequired: true},
+			limitation: {type: Schema.Types.Mixed, default: {}},
+			name: {type: String, isRequired: true},
+			satisfy_count: {type: Number, default: 0},
+			activity: String,
+			satisfy_rate: {type: Number, default: 0},
+			specfoods: [{
+				original_price: {type: Number, default: 0},
+				sku_id: {type: Number, isRequired: true},
+				name: {type: String, isRequired: true},
+				pinyin_name: String,
+				restaurant_id: {type: Number, isRequired: true},
+				food_id: {type: Number, isRequired: true},
+				packing_fee: {type: Number, default: 0},
+				recent_rating: {type: Number, default: 0},
+				promotion_stock: {type: Number, default: -1},
+				price: {type: Number, default: 0},
+				sold_out: {type: Boolean, default: false},
+				recent_popularity: {type: Number, default: 0},
+				is_essential: {type: Boolean, default: false},
+				item_id: {type: Number, isRequired: true},
+				checkout_mode: {type: Number, default: 1},
+				stock: {type: Number, default: 1000},
+				specs: [
+					{
+						name: String,
+						value: String
+					}
+				]
+			}]
+		}
+	]
+});
+
+foodSchema.index({ id: 1 });
+
+const Food = mongoose.model('Food', foodSchema);
+
+export default Food

+ 17 - 5
prototype/baseComponent.js

@@ -46,14 +46,25 @@ export default class BaseComponent {
 				responseJson = await response.json();
 			}
 		} catch (error) {
-			console.error(error)
+			console.log('获取http数据失败');
 			throw new Error(error)
 		}
 		return responseJson
 	}
 	//获取id列表
 	async getId(type){
-		const typeList = ['orderId', 'userId', 'addressId', 'cartId', 'imgId'];
+		const typeList = [
+			'restaurant_id',
+			'food_id',
+			'orderId',
+			'user_id',
+			'address_id',
+			'cart_id',
+			'img_id',
+			'category_id',
+			'item_id',
+			'sku_id',
+		];
 		if (!typeList.includes(type)) {
 			console.log('id类型错误');
 			throw new Error('id类型错误');
@@ -65,6 +76,7 @@ export default class BaseComponent {
 			await idData.save();
 			return idData[type]
 		}catch(err){
+			console.log('获取ID数据失败');
 			throw new Error(err)
 		}
 	}
@@ -74,15 +86,15 @@ export default class BaseComponent {
 			const form = formidable.IncomingForm();
 			form.uploadDir = './public/img/' + type;
 			form.parse(req, async (err, fields, files) => {
-				let imgId;
+				let img_id;
 				try{
-					imgId = await this.getId('imgId');
+					img_id = await this.getId('img_id');
 				}catch(err){
 					console.log('获取图片id失败');
 					fs.unlink(files.file.path)
 					reject(err);
 				}
-				const imgUrl = new Date().getTime().toString() + imgId;
+				const imgUrl = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
 				const extname = path.extname(files.file.name);
 				const repath = './public/img/' + type + '/' + imgUrl + extname;
 				try{

+ 4 - 0
routes/shopping.js

@@ -2,9 +2,13 @@
 
 import express from 'express';
 import Shop from '../controller/shopping/shop'
+import Food from '../controller/shopping/food'
 const router = express.Router();
 
 router.post('/addshop', Shop.addShop);
 router.post('/addimg', Shop.uploadShopImg);
+router.get('/addfood', Food.addFood);
+router.get('/getcategory/:restaurant_id', Food.getCategory);
+router.post('/addcategory', Food.addCategory);
 
 export default router