shop.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. 'use strict';
  2. import ShopModel from '../../models/shopping/shop'
  3. import AddressComponent from '../../prototype/addressComponent'
  4. import formidable from 'formidable'
  5. import CategoryHandle from './category'
  6. class Shop extends AddressComponent{
  7. constructor(){
  8. super()
  9. this.addShop = this.addShop.bind(this);
  10. this.getRestaurants = this.getRestaurants.bind(this);
  11. this.searchResaturant = this.searchResaturant.bind(this);
  12. }
  13. //添加商铺
  14. async addShop(req, res, next){
  15. let restaurant_id;
  16. try{
  17. restaurant_id = await this.getId('restaurant_id');
  18. }catch(err){
  19. console.log('获取商店id失败');
  20. res.send({
  21. type: 'ERROR_DATA',
  22. message: '获取数据失败'
  23. })
  24. return
  25. }
  26. const form = new formidable.IncomingForm();
  27. form.parse(req, async (err, fields, files) => {
  28. try{
  29. if (!fields.name) {
  30. throw new Error('必须填写商店名称');
  31. }else if(!fields.address){
  32. throw new Error('必须填写商店地址');
  33. }else if(!fields.phone){
  34. throw new Error('必须填写联系电话');
  35. }else if(!fields.latitude || !fields.longitude){
  36. throw new Error('商店位置信息错误');
  37. }else if(!fields.image_path){
  38. throw new Error('必须上传商铺图片');
  39. }else if(!fields.category){
  40. throw new Error('必须上传食品种类');
  41. }
  42. }catch(err){
  43. console.log('前台参数出错');
  44. res.send({
  45. status: 0,
  46. type: 'ERROR_PARAMS',
  47. message: err.message
  48. })
  49. return
  50. }
  51. const opening_hours = fields.startTime&&fields.endTime? fields.startTime + '/' + fields.endTime : "8:30/20:30";
  52. const newShop = {
  53. name: fields.name,
  54. address: fields.address,
  55. description: fields.description || '',
  56. float_delivery_fee: fields.float_delivery_fee || 0,
  57. float_minimum_order_amount: fields.float_minimum_order_amount || 0,
  58. id: restaurant_id,
  59. is_premium: fields.is_premium || false,
  60. is_new: fields.new || false,
  61. latitude: fields.latitude,
  62. longitude: fields.longitude,
  63. location: [fields.longitude, fields.latitude],
  64. opening_hours: [opening_hours],
  65. phone: fields.phone,
  66. promotion_info: fields.promotion_info || "欢迎光临,用餐高峰请提前下单,谢谢",
  67. rating: (Math.random()*5).toFixed(1),
  68. rating_count: Math.ceil(Math.random()*1000),
  69. recent_order_num: Math.ceil(Math.random()*1000),
  70. status: Math.round(Math.random()),
  71. image_path: fields.image_path,
  72. category: fields.category,
  73. piecewise_agent_fee: {
  74. tips: "配送费约¥" + (fields.float_delivery_fee || 0),
  75. },
  76. activities: [],
  77. supports: [],
  78. license: {
  79. business_license_image: fields.business_license_image || '',
  80. catering_service_license_image: fields.catering_service_license_image || '',
  81. },
  82. identification: {
  83. company_name: "",
  84. identificate_agency: "",
  85. identificate_date: "",
  86. legal_person: "",
  87. licenses_date: "",
  88. licenses_number: "",
  89. licenses_scope: "",
  90. operation_period: "",
  91. registered_address: "",
  92. registered_number: "",
  93. },
  94. }
  95. //配送方式
  96. if (fields.delivery_mode) {
  97. Object.assign(newShop, {delivery_mode: {
  98. color: "57A9FF",
  99. id: 1,
  100. is_solid: true,
  101. text: "蜂鸟专送"
  102. }})
  103. }
  104. //商店支持的活动
  105. fields.activities.forEach((item, index) => {
  106. switch(item.icon_name){
  107. case '减':
  108. item.icon_color = 'f07373';
  109. item.id = index + 1;
  110. break;
  111. case '特':
  112. item.icon_color = 'EDC123';
  113. item.id = index + 1;
  114. break;
  115. case '新':
  116. item.icon_color = '70bc46';
  117. item.id = index + 1;
  118. break;
  119. case '领':
  120. item.icon_color = 'E3EE0D';
  121. item.id = index + 1;
  122. break;
  123. }
  124. newShop.activities.push(item);
  125. })
  126. if (fields.bao) {
  127. newShop.supports.push({
  128. description: "已加入“外卖保”计划,食品安全有保障",
  129. icon_color: "999999",
  130. icon_name: "保",
  131. id: 7,
  132. name: "外卖保"
  133. })
  134. }
  135. if (fields.zhun) {
  136. newShop.supports.push({
  137. description: "准时必达,超时秒赔",
  138. icon_color: "57A9FF",
  139. icon_name: "准",
  140. id: 9,
  141. name: "准时达"
  142. })
  143. }
  144. if (fields.piao) {
  145. newShop.supports.push({
  146. description: "该商家支持开发票,请在下单时填写好发票抬头",
  147. icon_color: "999999",
  148. icon_name: "票",
  149. id: 4,
  150. name: "开发票"
  151. })
  152. }
  153. try{
  154. //保存数据,并增加对应食品种类的数量
  155. const shop = new ShopModel(newShop);
  156. await shop.save();
  157. CategoryHandle.addCategory(fields.category)
  158. res.send({
  159. status: 1,
  160. sussess: '添加餐馆成功',
  161. shopDetail: newShop
  162. })
  163. }catch(err){
  164. console.log('商铺写入数据库失败');
  165. res.send({
  166. status: 0,
  167. type: 'ERROR_SERVER',
  168. message: '添加商铺失败',
  169. })
  170. }
  171. })
  172. }
  173. //获取餐馆列表
  174. async getRestaurants(req, res, next){
  175. const {
  176. latitude,
  177. longitude,
  178. offset = 0,
  179. limit = 20,
  180. keyword,
  181. restaurant_category_id,
  182. order_by,
  183. extras,
  184. delivery_mode = [],
  185. support_ids = [],
  186. restaurant_category_ids = [],
  187. } = req.query;
  188. try{
  189. if (!latitude) {
  190. throw new Error('latitude参数错误')
  191. }else if(!longitude){
  192. throw new Error('longitude参数错误');
  193. }
  194. }catch(err){
  195. console.log('latitude,longitude参数错误');
  196. res.send({
  197. status: 0,
  198. type: 'ERROR_PARAMS',
  199. message: err.message
  200. })
  201. return
  202. }
  203. let filter = {};
  204. //获取对应食品种类
  205. if (restaurant_category_ids.length && Number(restaurant_category_ids[0])) {
  206. const category = await CategoryHandle.findById(restaurant_category_ids[0]);
  207. Object.assign(filter, {category})
  208. }
  209. //按照距离,评分,销量等排序
  210. let sortBy = {}
  211. if (Number(order_by)) {
  212. switch(Number(order_by)){
  213. case 1:
  214. Object.assign(sortBy, {float_minimum_order_amount: 1});
  215. break;
  216. case 2:
  217. Object.assign(filter, {location: {$near: [longitude, latitude]}});
  218. break;
  219. case 3:
  220. Object.assign(sortBy, {rating: -1});
  221. break;
  222. case 5:
  223. Object.assign(filter, {location: {$near: [longitude, latitude]}});
  224. break;
  225. case 6:
  226. Object.assign(sortBy, {recent_order_num: -1});
  227. break;
  228. }
  229. }
  230. //查找配送方式
  231. if (delivery_mode.length) {
  232. delivery_mode.forEach(item => {
  233. if (Number(item)) {
  234. Object.assign(filter, {'delivery_mode.id': Number(item)})
  235. }
  236. })
  237. }
  238. //查找活动支持方式
  239. if (support_ids.length) {
  240. const filterArr = [];
  241. support_ids.forEach(item => {
  242. if (Number(item) && (Number(item) !== 8)) {
  243. filterArr.push(Number(item))
  244. }else if(Number(item) == 8){ //品牌保证特殊处理
  245. Object.assign(filter, {is_premium: true})
  246. }
  247. })
  248. if (filterArr.length) {
  249. //匹配同时拥有多种活动的数据
  250. Object.assign(filter, {'supports.id': {$all: filterArr}})
  251. }
  252. }
  253. const restaurants = await ShopModel.find(filter, '-_id').sort(sortBy).limit(Number(limit)).skip(Number(offset))
  254. const from = latitude + ',' + longitude;
  255. let to = '';
  256. //获取百度地图测局所需经度纬度
  257. restaurants.forEach((item, index) => {
  258. const slpitStr = (index == restaurants.length -1) ? '' : '|';
  259. to += item.latitude + ',' + item.longitude + slpitStr;
  260. })
  261. try{
  262. if (restaurants.length) {
  263. //获取距离信息,并合并到数据中
  264. const distance_duration = await this.getDistance(from, to)
  265. restaurants.map((item, index) => {
  266. return Object.assign(item, distance_duration[index])
  267. })
  268. }
  269. res.send(restaurants)
  270. }catch(err){
  271. console.log('从addressComoponent获取数据后处理失败');
  272. res.send({
  273. status: 0,
  274. type: 'ERROR_DATA',
  275. message: '获取数据失败'
  276. })
  277. }
  278. }
  279. //搜索餐馆
  280. async searchResaturant(req, res, next){
  281. const {geohash, keyword} = req.query;
  282. try{
  283. if (!geohash || geohash.indexOf(',') == -1) {
  284. throw new Error('经纬度参数错误');
  285. }else if(!keyword){
  286. throw new Error('关键词参数错误');
  287. }
  288. }catch(err){
  289. console.log('搜索商铺参数错误');
  290. res.send({
  291. status: 0,
  292. type: 'ERROR_PARAMS',
  293. message: err.message,
  294. })
  295. return
  296. }
  297. try{
  298. const restaurants = await ShopModel.find({name: eval('/' + keyword + '/gi')}, '-_id').limit(50);
  299. if (restaurants.length) {
  300. const [latitude, longitude] = geohash.split(',');
  301. const from = latitude + ',' + longitude;
  302. let to = '';
  303. //获取百度地图测局所需经度纬度
  304. restaurants.forEach((item, index) => {
  305. const slpitStr = (index == restaurants.length -1) ? '' : '|';
  306. to += item.latitude + ',' + item.longitude + slpitStr;
  307. })
  308. //获取距离信息,并合并到数据中
  309. const distance_duration = await this.getDistance(from, to)
  310. restaurants.map((item, index) => {
  311. return Object.assign(item, distance_duration[index])
  312. })
  313. }
  314. res.send(restaurants);
  315. }catch(err){
  316. console.log('搜索餐馆数据失败');
  317. res.send({
  318. status: 0,
  319. type: 'ERROR_DATA',
  320. message: '搜索餐馆数据失败'
  321. })
  322. }
  323. }
  324. }
  325. export default new Shop()