shop.js 8.8 KB

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