food.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. 'use strict';
  2. import FoodModel from '../../models/shopping/food'
  3. import BaseComponent from '../../prototype/baseComponent'
  4. import formidable from 'formidable'
  5. class Food extends BaseComponent{
  6. constructor(){
  7. super();
  8. this.defaultData = [{
  9. name: '热销榜',
  10. description: '大家喜欢吃,才叫真好吃。',
  11. icon_url: "5da3872d782f707b4c82ce4607c73d1ajpeg",
  12. is_selected: true,
  13. type: 1,
  14. }, {
  15. name: '优惠',
  16. description: '美味又实惠, 大家快来抢!',
  17. icon_url: "4735c4342691749b8e1a531149a46117jpeg",
  18. type: 1,
  19. }]
  20. this.initData = this.initData.bind(this);
  21. this.addFood = this.addFood.bind(this);
  22. this.getCategory = this.getCategory.bind(this);
  23. this.addCategory = this.addCategory.bind(this);
  24. }
  25. async initData(restaurant_id){
  26. for (let i = 0; i < this.defaultData.length; i++) {
  27. let category_id;
  28. try{
  29. category_id = await this.getId('category_id');
  30. }catch(err){
  31. console.log('获取category_id失败');
  32. throw new Error(err);
  33. }
  34. const defaultData = this.defaultData[i];
  35. const Category = {...defaultData, id: category_id, restaurant_id};
  36. const newFood = new FoodModel(Category);
  37. try{
  38. await newFood.save();
  39. console.log('初始化食品数据成功');
  40. }catch(err){
  41. console.log('初始化食品数据失败');
  42. throw new Error(err);
  43. }
  44. }
  45. }
  46. async getCategory(req, res, next){
  47. const restaurant_id = req.params.restaurant_id;
  48. try{
  49. const category_list = await FoodModel.find({restaurant_id});
  50. res.send({
  51. status: 1,
  52. category_list,
  53. })
  54. }catch(err){
  55. console.log('获取餐馆食品种类失败');
  56. res.send({
  57. status: 0,
  58. type: 'ERROR_GET_DATA',
  59. message: '获取数据失败'
  60. })
  61. }
  62. }
  63. async addCategory(req, res, next){
  64. const form = new formidable.IncomingForm();
  65. form.parse(req, async (err, fields, files) => {
  66. try{
  67. if (!fields.name) {
  68. throw new Error('必须填写食品类型名称');
  69. }else if(!fields.restaurant_id){
  70. throw new Error('餐馆ID错误');
  71. }
  72. }catch(err){
  73. console.log('前台参数错误');
  74. res.send({
  75. status: 0,
  76. type: 'ERROR_PARAMS',
  77. message: err.message
  78. })
  79. return
  80. }
  81. let category_id;
  82. try{
  83. category_id = await this.getId('category_id');
  84. }catch(err){
  85. console.log('获取category_id失败');
  86. res.send({
  87. type: 'ERROR_DATA',
  88. message: '获取数据失败'
  89. })
  90. return
  91. }
  92. const foodObj = {
  93. name: fields.name,
  94. description: fields.description,
  95. restaurant_id: fields.restaurant_id,
  96. id: category_id,
  97. }
  98. const newFood = new FoodModel(foodObj);
  99. try{
  100. await newFood.save();
  101. res.send({
  102. status: 1,
  103. success: '添加食品种类成功',
  104. })
  105. }catch(err){
  106. console.log('保存数据失败');
  107. res.send({
  108. status: 0,
  109. type: 'ERROR_IN_SAVE_DATA',
  110. message: '保存数据失败',
  111. })
  112. }
  113. })
  114. }
  115. async addFood(req, res, next){
  116. const form = new formidable.IncomingForm();
  117. form.parse(req, async (err, fields, files) => {
  118. console.log(fields)
  119. try{
  120. if (!fields.name) {
  121. throw new Error('必须填写食品名称');
  122. }else if(!fields.image_path){
  123. throw new Error('必须上传食品图片');
  124. }else if(!fields.specs.length){
  125. throw new Error('至少填写一种规格');
  126. }else if(!fields.category_id){
  127. throw new Error('食品类型ID错误');
  128. }else if(!fields.restaurant_id){
  129. throw new Error('餐馆ID错误');
  130. }
  131. }catch(err){
  132. console.log('前台参数错误');
  133. res.send({
  134. status: 0,
  135. type: 'ERROR_PARAMS',
  136. message: err.message
  137. })
  138. return
  139. }
  140. let category;
  141. try{
  142. category = await FoodModel.findOne({id: fields.category_id});
  143. }catch(err){
  144. console.log('获取食品类型失败');
  145. res.send({
  146. status: 0,
  147. type: 'ERROR_DATA',
  148. message: '添加食品失败'
  149. })
  150. return
  151. }
  152. let item_id;
  153. try{
  154. item_id = await this.getId('item_id');
  155. }catch(err){
  156. console.log('获取item_id失败');
  157. res.send({
  158. status: 0,
  159. type: 'ERROR_DATA',
  160. message: '添加食品失败'
  161. })
  162. return
  163. }
  164. const rating_count = Math.ceil(Math.random()*1000);
  165. const month_sales = Math.ceil(Math.random()*1000);
  166. const tips = rating_count + "评价 月售" + month_sales + "份";
  167. const newFood = {
  168. name: fields.name,
  169. description: fields.description,
  170. image_path: fields.image_path,
  171. activity: null,
  172. attributes: [],
  173. restaurant_id: fields.restaurant_id,
  174. category_id: fields.category_id,
  175. satisfy_rate: Math.ceil(Math.random()*100),
  176. satisfy_count: Math.ceil(Math.random()*1000),
  177. item_id,
  178. rating: (Math.random()*5).toFixed(1),
  179. rating_count,
  180. month_sales,
  181. tips,
  182. specfoods: [],
  183. specifications: [],
  184. }
  185. if (fields.activity) {
  186. newFood.activity = {
  187. image_text_color: 'f1884f',
  188. icon_color: 'f07373',
  189. image_text: fields.activity,
  190. }
  191. }
  192. if (fields.attributes.length) {
  193. fields.attributes.forEach(item => {
  194. let attr;
  195. switch(item){
  196. case '新':
  197. attr = {
  198. icon_color: '5ec452',
  199. icon_name: '新'
  200. }
  201. break;
  202. case '招牌':
  203. attr = {
  204. icon_color: 'f07373',
  205. icon_name: '招牌'
  206. }
  207. break;
  208. }
  209. newFood.attributes.push(attr);
  210. })
  211. }
  212. if (fields.specs.length < 2) {
  213. let food_id, sku_id;
  214. try{
  215. sku_id = await this.getId('sku_id');
  216. food_id = await this.getId('food_id');
  217. }catch(err){
  218. console.log('获取sku_id、food_id失败');
  219. res.send({
  220. status: 0,
  221. type: 'ERROR_DATA',
  222. message: '添加食品失败'
  223. })
  224. return
  225. }
  226. newFood.specfoods.push({
  227. packing_fee: fields.specs[0].packing_fee,
  228. price: fields.specs[0].price,
  229. specs: [],
  230. name: fields.name,
  231. item_id,
  232. sku_id,
  233. food_id,
  234. restaurant_id: fields.restaurant_id,
  235. recent_rating: (Math.random()*5).toFixed(1),
  236. recent_popularity: Math.ceil(Math.random()*1000),
  237. })
  238. }else{
  239. newFood.specifications.push({
  240. values: [],
  241. name: "规格"
  242. })
  243. for (let i = 0; i < fields.specs.length; i++) {
  244. let food_id, sku_id;
  245. try{
  246. sku_id = await this.getId('sku_id');
  247. food_id = await this.getId('food_id');
  248. }catch(err){
  249. console.log('获取sku_id、food_id失败');
  250. res.send({
  251. status: 0,
  252. type: 'ERROR_DATA',
  253. message: '添加食品失败'
  254. })
  255. return
  256. }
  257. newFood.specfoods.push({
  258. packing_fee: fields.specs[i].packing_fee,
  259. price: fields.specs[i].price,
  260. specs: [{
  261. name: "规格",
  262. value: fields.specs[i].specs
  263. }],
  264. name: fields.name,
  265. item_id,
  266. sku_id,
  267. food_id,
  268. restaurant_id: fields.restaurant_id,
  269. recent_rating: (Math.random()*5).toFixed(1),
  270. recent_popularity: Math.ceil(Math.random()*1000),
  271. })
  272. newFood.specifications[0].values.push(fields.specs[i].specs);
  273. }
  274. }
  275. try{
  276. category.foods.push(newFood);
  277. category.markModified('foods');
  278. await category.save();
  279. res.send({
  280. status: 1,
  281. success: '添加食品成功',
  282. });
  283. }catch(err){
  284. console.log('保存食品到数据库失败', err);
  285. res.send({
  286. status: 0,
  287. type: 'ERROR_DATA',
  288. message: '添加食品失败'
  289. })
  290. }
  291. })
  292. }
  293. async getMenu(req, res, next){
  294. const restaurant_id = req.query.restaurant_id;
  295. if (!restaurant_id || !Number(restaurant_id)) {
  296. console.log('获取餐馆参数ID错误');
  297. res.send({
  298. status: 0,
  299. type: 'ERROR_PARAMS',
  300. message: '餐馆ID参数错误',
  301. })
  302. return
  303. }
  304. try{
  305. const menu = await FoodModel.find({restaurant_id, $where: function(){return this.foods.length}}, '-_id');
  306. res.send(menu);
  307. }catch(err){
  308. console.log('获取食品数据失败', err);
  309. res.send({
  310. status: 0,
  311. type: 'GET_DATA_ERROR',
  312. message: '获取食品数据失败'
  313. })
  314. }
  315. }
  316. }
  317. export default new Food()