shop.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  12. async addShop(req, res, next){
  13. let restaurant_id;
  14. try{
  15. restaurant_id = await this.getId('restaurant_id');
  16. }catch(err){
  17. console.log('获取商店id失败');
  18. res.send({
  19. type: 'ERROR_DATA',
  20. message: '获取数据失败'
  21. })
  22. return
  23. }
  24. const form = new formidable.IncomingForm();
  25. form.parse(req, async (err, fields, files) => {
  26. try{
  27. if (!fields.name) {
  28. throw new Error('必须填写商店名称');
  29. }else if(!fields.address){
  30. throw new Error('必须填写商店地址');
  31. }else if(!fields.phone){
  32. throw new Error('必须填写联系电话');
  33. }else if(!fields.latitude || !fields.longitude){
  34. throw new Error('商店位置信息错误');
  35. }else if(!fields.image_path){
  36. throw new Error('必须上传商铺图片');
  37. }
  38. }catch(err){
  39. console.log('前台参数出错');
  40. res.send({
  41. status: 0,
  42. type: 'ERROR_PARAMS',
  43. message: err.message
  44. })
  45. return
  46. }
  47. const opening_hours = fields.startTime&&fields.endTime? fields.startTime + '/' + fields.endTime : "8:30/20:30";
  48. const newShop = {
  49. name: fields.name,
  50. address: fields.address,
  51. description: fields.description || '',
  52. float_delivery_fee: fields.float_delivery_fee || 0,
  53. float_minimum_order_amount: fields.float_minimum_order_amount || 0,
  54. id: restaurant_id,
  55. is_premium: fields.is_premium || false,
  56. is_new: fields.new || false,
  57. latitude: fields.latitude,
  58. longitude: fields.longitude,
  59. opening_hours: [opening_hours],
  60. phone: fields.phone,
  61. promotion_info: fields.promotion_info || "欢迎光临,用餐高峰请提前下单,谢谢",
  62. rating: (Math.random()*5).toFixed(1),
  63. rating_count: Math.ceil(Math.random()*1000),
  64. recent_order_num: Math.ceil(Math.random()*1000),
  65. status: Math.round(Math.random()),
  66. image_path: fields.image_path,
  67. category: fields.category,
  68. piecewise_agent_fee: {
  69. tips: "配送费约¥" + (fields.float_delivery_fee || 0),
  70. },
  71. activities: [],
  72. supports: [],
  73. license: {
  74. business_license_image: fields.business_license_image || '',
  75. catering_service_license_image: fields.catering_service_license_image || '',
  76. },
  77. identification: {
  78. company_name: "",
  79. identificate_agency: "",
  80. identificate_date: "",
  81. legal_person: "",
  82. licenses_date: "",
  83. licenses_number: "",
  84. licenses_scope: "",
  85. operation_period: "",
  86. registered_address: "",
  87. registered_number: "",
  88. },
  89. }
  90. if (fields.delivery_mode) {
  91. Object.assign(newShop, {delivery_mode: {
  92. color: "57A9FF",
  93. id: 1,
  94. is_solid: true,
  95. text: "蜂鸟专送"
  96. }})
  97. }
  98. fields.activities.forEach((item, index) => {
  99. switch(item.icon_name){
  100. case '减':
  101. item.icon_color = 'f07373';
  102. item.id = index + 1;
  103. break;
  104. case '特':
  105. item.icon_color = 'EDC123';
  106. item.id = index + 1;
  107. break;
  108. case '新':
  109. item.icon_color = '70bc46';
  110. item.id = index + 1;
  111. break;
  112. case '领':
  113. item.icon_color = 'E3EE0D';
  114. item.id = index + 1;
  115. break;
  116. }
  117. newShop.activities.push(item);
  118. })
  119. if (fields.bao) {
  120. newShop.supports.push({
  121. description: "已加入“外卖保”计划,食品安全有保障",
  122. icon_color: "999999",
  123. icon_name: "保",
  124. id: 7,
  125. name: "外卖保"
  126. })
  127. }
  128. if (fields.zhun) {
  129. newShop.supports.push({
  130. description: "准时必达,超时秒赔",
  131. icon_color: "57A9FF",
  132. icon_name: "准",
  133. id: 9,
  134. name: "准时达"
  135. })
  136. }
  137. if (fields.piao) {
  138. newShop.supports.push({
  139. description: "该商家支持开发票,请在下单时填写好发票抬头",
  140. icon_color: "999999",
  141. icon_name: "票",
  142. id: 4,
  143. name: "开发票"
  144. })
  145. }
  146. try{
  147. const shop = new ShopModel(newShop);
  148. await shop.save();
  149. CategoryHandle.addCategory(fields.category)
  150. res.send({
  151. status: 1,
  152. shopDetail: newShop
  153. })
  154. }catch(err){
  155. console.log('商铺写入数据库失败');
  156. res.send({
  157. status: 0,
  158. type: 'ERROR_SERVER',
  159. message: '添加商铺失败',
  160. })
  161. }
  162. })
  163. }
  164. //获取餐馆列表
  165. async getRestaurants(req, res, next){
  166. const {
  167. latitude,
  168. longitude,
  169. offset = 0,
  170. limit = 20,
  171. keyword,
  172. restaurant_category_id,
  173. order_by,
  174. extras,
  175. delivery_mode,
  176. restaurant_category_ids,
  177. } = req.query;
  178. try{
  179. if (!latitude) {
  180. throw new Error('latitude参数错误')
  181. }else if(!longitude){
  182. throw new Error('longitude参数错误');
  183. }
  184. }catch(err){
  185. console.log('latitude,longitude参数错误');
  186. res.send({
  187. status: 0,
  188. type: 'ERROR_PARAMS',
  189. message: err.message
  190. })
  191. return
  192. }
  193. const restaurants = await ShopModel.find({}, '-_id').limit(Number(limit)).skip(Number(offset));
  194. const from = latitude + ',' + longitude;
  195. let to = '';
  196. restaurants.forEach((item, index) => {
  197. const slpitStr = (index == restaurants.length -1) ? '' : '|';
  198. to += item.latitude + ',' + item.longitude + slpitStr;
  199. })
  200. try{
  201. const positionArr = await this.getDistance(from, to)
  202. restaurants.map((item, index) => {
  203. return Object.assign(item, positionArr[index])
  204. })
  205. res.send(restaurants)
  206. }catch(err){
  207. console.log('从addressComoponent获取数据后处理失败');
  208. res.send({
  209. status: 0,
  210. type: 'ERROR_DATA',
  211. message: '获取数据失败'
  212. })
  213. }
  214. }
  215. }
  216. export default new Shop()