food.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. cartList:[],//购物车列表
  15. currentProductList:[],//商品
  16. totalNum:0,//购物车总数
  17. totalPrice:0.00//购物车总价格
  18. },
  19. onShow: function () {
  20. var that = this;
  21. var mobile = wx.getStorageSync('mobile');
  22. console.log(mobile + "/////////" );
  23. if (!mobile) {
  24. that.setData({
  25. check_mobile: false
  26. })
  27. } else {
  28. that.setData({
  29. check_mobile: true
  30. })
  31. }
  32. },
  33. //商品分类列表
  34. productTypeList: async function(){
  35. let self = this
  36. let rs = await productApi.getProductTypeList(1,10000,2)
  37. self.setData({
  38. series: rs.list
  39. })
  40. },
  41. //商品列表
  42. getProductList: async function (refresh = false) {
  43. const self = this
  44. let prolist = await productApi.getProductList(0,1,10000,2);
  45. self.setData({
  46. productList:prolist.list
  47. })
  48. await this.initProduct();
  49. },
  50. // 获取购物车
  51. getCart: async function(){
  52. let self = this;
  53. let data = {
  54. tableId: this.data.tableId,
  55. storeId: this.data.storeId
  56. }
  57. const rs = await productApi.getCart(data)
  58. console.log(rs);
  59. self.setData({
  60. cartId:rs.id,
  61. cartList:rs.list,
  62. totalNum:rs.totalNum?rs.totalNum:0,
  63. totalPrice:rs.totalPrice?rs.totalPrice:0.00,
  64. })
  65. },
  66. // 更新购物车 type类型1增加 2减少
  67. updateCart: async function(productId,type=1,num=1){
  68. let data = {
  69. productId : productId,
  70. num : num,
  71. cartId : this.data.cartId,
  72. type : type
  73. }
  74. await productApi.updateCart(data)
  75. },
  76. updateCartClick :async function(e){
  77. let productId = e.target.dataset.no;
  78. let type = e.target.dataset.type;
  79. // wx.showLoading({
  80. // title: '加入购物车',
  81. // mask: true
  82. // });
  83. await this.updateCart(productId,type);
  84. await this.getCart();
  85. if(this.data.cartList.length ==0){
  86. this.setData({showCart:false})
  87. }
  88. await this.initProduct();
  89. this.cartWwing();
  90. },
  91. onLoad: async function (options) {
  92. this.setData({
  93. tableId:options.table_id,
  94. storeId:options.store_id
  95. })
  96. await this.getCart();
  97. await this.productTypeList();
  98. await this.getProductList();
  99. },
  100. startOrder: function (e) {
  101. if(this.data.cartList.length>0){
  102. this.setData({showCart:false})
  103. wx.navigateTo({
  104. url: '/pages/foodOrder/foodOrder?tableId='+this.data.tableId+"&storeId="+this.data.storeId+"&cartId="+this.data.cartId
  105. })
  106. }else{
  107. wx.showToast({
  108. title: '请选择产品',
  109. icon: 'none',
  110. duration: 1000
  111. })
  112. }
  113. },
  114. handleTabClick: function (e) {
  115. let index = e.currentTarget.dataset.index;
  116. this.activeTab(index);
  117. this.setData({ activeTab: index });
  118. },
  119. handleSwiperChange: function (e) {
  120. var index = e.detail.current;
  121. this.activeTab(index);
  122. this.setData({ activeTab: index });
  123. },
  124. activeTab: function (_activeTab) {
  125. var len = this.data.series.length;
  126. if (len === 0) return;
  127. var currentView = _activeTab - 1;
  128. if (currentView < 0) currentView = 0;
  129. if (currentView > len - 1) currentView = len - 1;
  130. this.setData({ currentView: currentView });
  131. },
  132. initProduct: function(){
  133. let series = this.data.series;
  134. let productList = this.data.productList;
  135. let cartList = this.data.cartList;
  136. cartList.forEach((ite,index) => {
  137. productList.forEach((ele,pindex) => {
  138. ele.hasChoose = false;
  139. if (ele.id == ite.productId){
  140. ele.hasChoose = true;
  141. ele.hasNum = ite.num;
  142. }
  143. });
  144. });
  145. let currentProductList = [];
  146. series.forEach((item,index) => {
  147. currentProductList[index] = [];
  148. let i = 0;
  149. productList.forEach((element,pindex) => {
  150. if (element.productTypeId == item.id){
  151. currentProductList[index][i] = element;
  152. i++;
  153. }
  154. });
  155. });
  156. this.setData({
  157. currentProductList:currentProductList
  158. })
  159. },
  160. openCartDialog: function(e){
  161. let type = e.currentTarget.dataset.type;
  162. if (type === 'open'){
  163. if(this.data.cartList.length==0){
  164. wx.showToast({
  165. title: '请选择产品',
  166. icon: 'none',
  167. duration: 1000
  168. })
  169. }else{
  170. this.setData({showCart:true})
  171. }
  172. }else{
  173. this.setData({showCart:false})
  174. }
  175. },
  176. /**
  177. * 点击商品+号购物车摆动
  178. * @return void
  179. */
  180. cartWwing: function()
  181. {
  182. // 创建动画实例(animation)
  183. var animation = wx.createAnimation({
  184. duration: 100,//动画持续时间
  185. timingFunction: 'ease-in',//动画以低速开始
  186. //具体配置项请查看文档
  187. })
  188. // 通过实例描述对象()
  189. animation.translateX(6).rotate(21).step()
  190. animation.translateX(-6).rotate(-21).step()
  191. animation.translateX(0).rotate(0).step()
  192. // 导出动画
  193. this.setData({
  194. ani: animation.export()
  195. })
  196. },
  197. })