|
@@ -10,7 +10,9 @@ class Shop extends AddressComponent{
|
|
|
super()
|
|
|
this.addShop = this.addShop.bind(this);
|
|
|
this.getRestaurants = this.getRestaurants.bind(this);
|
|
|
+ this.searchResaturant = this.searchResaturant.bind(this);
|
|
|
}
|
|
|
+ //添加商铺
|
|
|
async addShop(req, res, next){
|
|
|
let restaurant_id;
|
|
|
try{
|
|
@@ -58,6 +60,7 @@ class Shop extends AddressComponent{
|
|
|
is_new: fields.new || false,
|
|
|
latitude: fields.latitude,
|
|
|
longitude: fields.longitude,
|
|
|
+ location: [fields.longitude, fields.latitude],
|
|
|
opening_hours: [opening_hours],
|
|
|
phone: fields.phone,
|
|
|
promotion_info: fields.promotion_info || "欢迎光临,用餐高峰请提前下单,谢谢",
|
|
@@ -89,6 +92,7 @@ class Shop extends AddressComponent{
|
|
|
registered_number: "",
|
|
|
},
|
|
|
}
|
|
|
+ //配送方式
|
|
|
if (fields.delivery_mode) {
|
|
|
Object.assign(newShop, {delivery_mode: {
|
|
|
color: "57A9FF",
|
|
@@ -97,6 +101,7 @@ class Shop extends AddressComponent{
|
|
|
text: "蜂鸟专送"
|
|
|
}})
|
|
|
}
|
|
|
+ //商店支持的活动
|
|
|
fields.activities.forEach((item, index) => {
|
|
|
switch(item.icon_name){
|
|
|
case '减':
|
|
@@ -146,6 +151,7 @@ class Shop extends AddressComponent{
|
|
|
})
|
|
|
}
|
|
|
try{
|
|
|
+ //保存数据,并增加对应食品种类的数量
|
|
|
const shop = new ShopModel(newShop);
|
|
|
await shop.save();
|
|
|
CategoryHandle.addCategory(fields.category)
|
|
@@ -174,9 +180,11 @@ class Shop extends AddressComponent{
|
|
|
restaurant_category_id,
|
|
|
order_by,
|
|
|
extras,
|
|
|
- delivery_mode,
|
|
|
- restaurant_category_ids,
|
|
|
+ delivery_mode = [],
|
|
|
+ support_ids = [],
|
|
|
+ restaurant_category_ids = [],
|
|
|
} = req.query;
|
|
|
+
|
|
|
try{
|
|
|
if (!latitude) {
|
|
|
throw new Error('latitude参数错误')
|
|
@@ -192,18 +200,73 @@ class Shop extends AddressComponent{
|
|
|
})
|
|
|
return
|
|
|
}
|
|
|
- const restaurants = await ShopModel.find({}, '-_id').limit(Number(limit)).skip(Number(offset));
|
|
|
+ let filter = {};
|
|
|
+ //获取对应食品种类
|
|
|
+ if (restaurant_category_ids.length && Number(restaurant_category_ids[0])) {
|
|
|
+ const category = await CategoryHandle.findById(restaurant_category_ids[0]);
|
|
|
+ Object.assign(filter, {category})
|
|
|
+ }
|
|
|
+ //按照距离,评分,销量等排序
|
|
|
+ let sortBy = {}
|
|
|
+ if (Number(order_by)) {
|
|
|
+ switch(Number(order_by)){
|
|
|
+ case 1:
|
|
|
+ Object.assign(sortBy, {float_minimum_order_amount: 1});
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ Object.assign(filter, {location: {$near: [longitude, latitude]}});
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ Object.assign(sortBy, {rating: -1});
|
|
|
+ break;
|
|
|
+ case 5:
|
|
|
+ Object.assign(filter, {location: {$near: [longitude, latitude]}});
|
|
|
+ break;
|
|
|
+ case 6:
|
|
|
+ Object.assign(sortBy, {recent_order_num: -1});
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //查找配送方式
|
|
|
+ if (delivery_mode.length) {
|
|
|
+ delivery_mode.forEach(item => {
|
|
|
+ if (Number(item)) {
|
|
|
+ Object.assign(filter, {'delivery_mode.id': Number(item)})
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ //查找活动支持方式
|
|
|
+ if (support_ids.length) {
|
|
|
+ const filterArr = [];
|
|
|
+ support_ids.forEach(item => {
|
|
|
+ if (Number(item) && (Number(item) !== 8)) {
|
|
|
+ filterArr.push(Number(item))
|
|
|
+ }else if(Number(item) == 8){ //品牌保证特殊处理
|
|
|
+ Object.assign(filter, {is_premium: true})
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if (filterArr.length) {
|
|
|
+ //匹配同时拥有多种活动的数据
|
|
|
+ Object.assign(filter, {'supports.id': {$all: filterArr}})
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const restaurants = await ShopModel.find(filter, '-_id').sort(sortBy).limit(Number(limit)).skip(Number(offset))
|
|
|
const from = latitude + ',' + longitude;
|
|
|
let to = '';
|
|
|
+ //获取百度地图测局所需经度纬度
|
|
|
restaurants.forEach((item, index) => {
|
|
|
const slpitStr = (index == restaurants.length -1) ? '' : '|';
|
|
|
to += item.latitude + ',' + item.longitude + slpitStr;
|
|
|
})
|
|
|
try{
|
|
|
- const positionArr = await this.getDistance(from, to)
|
|
|
- restaurants.map((item, index) => {
|
|
|
- return Object.assign(item, positionArr[index])
|
|
|
- })
|
|
|
+ if (restaurants.length) {
|
|
|
+ //获取距离信息,并合并到数据中
|
|
|
+ const distance_duration = await this.getDistance(from, to)
|
|
|
+ restaurants.map((item, index) => {
|
|
|
+ return Object.assign(item, distance_duration[index])
|
|
|
+ })
|
|
|
+ }
|
|
|
res.send(restaurants)
|
|
|
}catch(err){
|
|
|
console.log('从addressComoponent获取数据后处理失败');
|
|
@@ -214,6 +277,52 @@ class Shop extends AddressComponent{
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
+ //搜索餐馆
|
|
|
+ async searchResaturant(req, res, next){
|
|
|
+ const {geohash, keyword} = req.query;
|
|
|
+ try{
|
|
|
+ if (!geohash || geohash.indexOf(',') == -1) {
|
|
|
+ throw new Error('经纬度参数错误');
|
|
|
+ }else if(!keyword){
|
|
|
+ throw new Error('关键词参数错误');
|
|
|
+ }
|
|
|
+ }catch(err){
|
|
|
+ console.log('搜索商铺参数错误');
|
|
|
+ res.send({
|
|
|
+ status: 0,
|
|
|
+ type: 'ERROR_PARAMS',
|
|
|
+ message: err.message,
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ try{
|
|
|
+ const restaurants = await ShopModel.find({name: eval('/' + keyword + '/gi')}, '-_id').limit(50);
|
|
|
+ if (restaurants.length) {
|
|
|
+ const [latitude, longitude] = geohash.split(',');
|
|
|
+ const from = latitude + ',' + longitude;
|
|
|
+ let to = '';
|
|
|
+ //获取百度地图测局所需经度纬度
|
|
|
+ restaurants.forEach((item, index) => {
|
|
|
+ const slpitStr = (index == restaurants.length -1) ? '' : '|';
|
|
|
+ to += item.latitude + ',' + item.longitude + slpitStr;
|
|
|
+ })
|
|
|
+ //获取距离信息,并合并到数据中
|
|
|
+ const distance_duration = await this.getDistance(from, to)
|
|
|
+ restaurants.map((item, index) => {
|
|
|
+ return Object.assign(item, distance_duration[index])
|
|
|
+ })
|
|
|
+ }
|
|
|
+ res.send(restaurants);
|
|
|
+ }catch(err){
|
|
|
+ console.log('搜索餐馆数据失败');
|
|
|
+ res.send({
|
|
|
+ status: 0,
|
|
|
+ type: 'ERROR_DATA',
|
|
|
+ message: '搜索餐馆数据失败'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default new Shop()
|