food.js 7.5 KB

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