corgi/src/api/controllers/shop.controller.ts
2021-05-19 10:16:58 +08:00

119 lines
2.9 KiB
TypeScript

import BaseController from '../../common/base.controller'
import { Shop, validShopId } from '../../models/shop/Shop'
import { role, router } from '../../decorators/router'
import { ZError } from '../../common/ZError'
import { ShopExam } from '../../models/shop/ShopExam'
import { ShopActivity } from '../../models/shop/ShopActivity'
class ShopController extends BaseController {
/**
* 获取附近的店铺
* @param req
* lng: 经度
* lat: 纬度
* distance: 多少半径范围内, 单位: 米
*/
@role('anon')
@router('post /weapp/nearme')
async shopNearby(req) {
let { lng, lat, distance } = req.params
let queryParam = {
location: {
$nearSphere: {
$geometry: {
type: 'Point',
coordinates: [lng, lat]
},
$maxDistance: distance
}
},
deleted: false
}
let records = await Shop.find(queryParam)
return records.map(o => {
return {
id: o.id,
name: o.showName,
address: o.address
}
})
}
@role('anon')
@router('post /weapp/shopList')
async shopList(req) {
let { start, limit } = req.params
limit = +limit || 10
start = +start || 0
let { opt, sort } = Shop.parseQueryParam(req.params)
let articles = await Shop.find(opt)
.sort(sort)
.skip(start)
.limit(limit)
let count = await Shop.count(opt)
let records: any = []
for (let record of articles) {
records.push({
id: record.id,
showName: record.showName,
logo: record.logo,
areaStr: record.areaStr
})
}
return {
total: count,
start,
limit,
records
}
}
/**
* TODO:: 获取某店铺实时的活动信息
*/
@role('anon')
@router('get /weapp/act/:sid')
async shopActivity(req) {
let { sid } = req.params
return {}
}
@role('anon')
@router('post /api/:accountid/shop')
async shopInfo(req) {
let { sid } = req.params
if (!sid || !validShopId(sid)) {
throw new ZError(10, '没有店铺id或者店铺id格式不正确, 测试使用: 607ff59d4a4e16687a3b7079')
}
let rspData: any = {}
let shop = await Shop.fetchByID(sid)
if (!shop) {
throw new ZError(11, '未找到对应的店铺')
}
rspData.id = shop.sid
rspData.name = shop.showName
rspData.area = shop.areaStr
rspData.logo = shop.logo
const now = Date.now()
let exams = await ShopExam.find({
shop: shop.id,
beginTime: { $lte: now },
endTime: { $gte: now },
active: true,
deleted: false
})
if (exams && exams.length > 0) {
let datas: any = []
for (let exam of exams) {
datas.push({ id: exam.id, name: exam.name, desc: exam.desc })
}
rspData.exams = datas
}
rspData.activity = await ShopActivity.getActivityOne(shop.id)
return rspData
}
}