food.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {
  2. default as productApi
  3. } from "../../api/product"
  4. const app = getApp()
  5. var utils = require('../../utils/util.js');
  6. let globalInterval = {};
  7. Page({
  8. data: {
  9. currentTab: 0,
  10. activeTab:0,
  11. showCart:false,
  12. mask:true,
  13. series:[],
  14. },
  15. onShow: function () {
  16. var that = this;
  17. var mobile = wx.getStorageSync('mobile');
  18. console.log(mobile + "/////////" );
  19. if (!mobile) {
  20. that.setData({
  21. check_mobile: false
  22. })
  23. } else {
  24. that.setData({
  25. check_mobile: true
  26. })
  27. }
  28. },
  29. //商品分类列表
  30. productTypeList: async function(){
  31. let self = this
  32. let rs = await productApi.getProductTypeList(1,10000,2)
  33. self.setData({
  34. series: rs.list
  35. })
  36. },
  37. //商品列表
  38. getProductList: async function (refresh = false) {
  39. const self = this
  40. let prolist = await productApi.getProductList(0,1,10000,2);
  41. self.setData({
  42. productList:prolist.list
  43. })
  44. await this.initProduct();
  45. },
  46. // 获取购物车
  47. getCart: async function(){
  48. let self = this;
  49. let data = {
  50. tableId: this.data.tableId,
  51. storeId: this.data.storeId
  52. }
  53. const rs = await productApi.getCart(data)
  54. console.log(rs);
  55. self.setData({
  56. cartId:rs.id,
  57. cartList:rs.list,
  58. cartListNum:rs.count?rs.count:0,
  59. })
  60. },
  61. // 更新购物车 type类型1增加 2减少
  62. updateCart: async function(productId,type=1,num=1){
  63. let data = {
  64. productId : productId,
  65. num : num,
  66. cartId : this.data.cartId,
  67. type : type
  68. }
  69. await productApi.updateCart(data)
  70. },
  71. updateCartClick :async function(e){
  72. let productId = e.target.dataset.no;
  73. let type = e.target.dataset.type;
  74. // wx.showLoading({
  75. // title: '加入购物车',
  76. // mask: true
  77. // });
  78. await this.updateCart(productId,type);
  79. await this.getCart();
  80. },
  81. onLoad: async function (options) {
  82. this.setData({
  83. tableId:options.table_id,
  84. storeId:options.store_id
  85. })
  86. await this.productTypeList();
  87. await this.getProductList();
  88. await this.getCart();
  89. },
  90. startOrder: function (e) {
  91. if(this.data.cartList.length>0){
  92. wx.navigateTo({
  93. url: '/pages/foodOrder/foodOrder?tableId='+this.data.tableId+"&storeId="+this.data.storeId+"&cartId="+this.data.cartId
  94. })
  95. }else{
  96. wx.showModal({
  97. content: '请选择产品',
  98. })
  99. }
  100. },
  101. activeTab: function (_activeTab) {
  102. var len = this.data.series.length;
  103. if (len === 0) return;
  104. var currentView = _activeTab - 1;
  105. if (currentView < 0) currentView = 0;
  106. if (currentView > len - 1) currentView = len - 1;
  107. this.setData({ currentView: currentView });
  108. },
  109. initProduct: function(){
  110. let series = this.data.series;
  111. let productList = this.data.productList;
  112. let currentProductList = [];
  113. series.forEach((item,index) => {
  114. currentProductList[index] = [];
  115. let i = 0;
  116. productList.forEach((element,pindex) => {
  117. if (element.productTypeId == item.id){
  118. currentProductList[index][i] = element;
  119. i++;
  120. }
  121. });
  122. });
  123. this.setData({
  124. currentProductList:currentProductList
  125. })
  126. },
  127. openCartDialog: function(e){
  128. let type = e.currentTarget.dataset.type;
  129. if (type === 'open'){
  130. this.setData({showCart:true})
  131. }else{
  132. this.setData({showCart:false})
  133. }
  134. }
  135. })