food.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. currentProductList:[],//商品
  15. totalNum:0,//购物车总数
  16. totalPrice:0.00//购物车总价格
  17. },
  18. onShow: function () {
  19. var that = this;
  20. var mobile = wx.getStorageSync('mobile');
  21. console.log(mobile + "/////////" );
  22. if (!mobile) {
  23. that.setData({
  24. check_mobile: false
  25. })
  26. } else {
  27. that.setData({
  28. check_mobile: true
  29. })
  30. }
  31. },
  32. //商品分类列表
  33. productTypeList: async function(){
  34. let self = this
  35. let rs = await productApi.getProductTypeList(1,10000,2)
  36. self.setData({
  37. series: rs.list
  38. })
  39. },
  40. //商品列表
  41. getProductList: async function (refresh = false) {
  42. const self = this
  43. let prolist = await productApi.getProductList(0,1,10000,2);
  44. self.setData({
  45. productList:prolist.list
  46. })
  47. await this.initProduct();
  48. },
  49. // 获取购物车
  50. getCart: async function(){
  51. let self = this;
  52. let data = {
  53. tableId: this.data.tableId,
  54. storeId: this.data.storeId
  55. }
  56. const rs = await productApi.getCart(data)
  57. console.log(rs);
  58. self.setData({
  59. cartId:rs.id,
  60. cartList:rs.list,
  61. totalNum:rs.totalNum?rs.totalNum:0,
  62. totalPrice:rs.totalPrice?rs.totalPrice:0.00,
  63. })
  64. },
  65. // 更新购物车 type类型1增加 2减少
  66. updateCart: async function(productId,type=1,num=1){
  67. let data = {
  68. productId : productId,
  69. num : num,
  70. cartId : this.data.cartId,
  71. type : type
  72. }
  73. await productApi.updateCart(data)
  74. },
  75. updateCartClick :async function(e){
  76. let productId = e.target.dataset.no;
  77. let type = e.target.dataset.type;
  78. // wx.showLoading({
  79. // title: '加入购物车',
  80. // mask: true
  81. // });
  82. await this.updateCart(productId,type);
  83. await this.getCart();
  84. if(this.data.cartList.length ==0){
  85. this.setData({showCart:false})
  86. }
  87. },
  88. onLoad: async function (options) {
  89. this.setData({
  90. tableId:options.table_id,
  91. storeId:options.store_id
  92. })
  93. await this.productTypeList();
  94. await this.getProductList();
  95. await this.getCart();
  96. },
  97. startOrder: function (e) {
  98. if(this.data.cartList.length>0){
  99. this.setData({showCart:false})
  100. wx.navigateTo({
  101. url: '/pages/foodOrder/foodOrder?tableId='+this.data.tableId+"&storeId="+this.data.storeId+"&cartId="+this.data.cartId
  102. })
  103. }else{
  104. wx.showToast({
  105. title: '请选择产品',
  106. icon: 'none',
  107. duration: 1000
  108. })
  109. }
  110. },
  111. handleTabClick: function (e) {
  112. let index = e.currentTarget.dataset.index;
  113. this.activeTab(index);
  114. this.setData({ activeTab: index });
  115. },
  116. handleSwiperChange: function (e) {
  117. var index = e.detail.current;
  118. this.activeTab(index);
  119. this.setData({ activeTab: index });
  120. },
  121. activeTab: function (_activeTab) {
  122. var len = this.data.series.length;
  123. if (len === 0) return;
  124. var currentView = _activeTab - 1;
  125. if (currentView < 0) currentView = 0;
  126. if (currentView > len - 1) currentView = len - 1;
  127. this.setData({ currentView: currentView });
  128. },
  129. initProduct: function(){
  130. let series = this.data.series;
  131. let productList = this.data.productList;
  132. let currentProductList = [];
  133. series.forEach((item,index) => {
  134. currentProductList[index] = [];
  135. let i = 0;
  136. productList.forEach((element,pindex) => {
  137. if (element.productTypeId == item.id){
  138. currentProductList[index][i] = element;
  139. i++;
  140. }
  141. });
  142. });
  143. this.setData({
  144. currentProductList:currentProductList
  145. })
  146. },
  147. openCartDialog: function(e){
  148. let type = e.currentTarget.dataset.type;
  149. if (type === 'open'){
  150. if(this.data.cartList.length==0){
  151. wx.showToast({
  152. title: '请选择产品',
  153. icon: 'none',
  154. duration: 1000
  155. })
  156. }else{
  157. this.setData({showCart:true})
  158. }
  159. }else{
  160. this.setData({showCart:false})
  161. }
  162. }
  163. })