food.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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: async 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.tableId,
  94. storeId:options.storeId
  95. })
  96. const isAuth = await app.isAuth()
  97. if (!isAuth) {
  98. wx.redirectTo({
  99. url: '/pages/prompt/prompt?page=' + this.route+"&tableId="+options.tableId+"&storeId="+options.storeId,
  100. })
  101. return
  102. }
  103. await this.getCart();
  104. await this.productTypeList();
  105. await this.getProductList();
  106. },
  107. startOrder: function (e) {
  108. if(this.data.cartList.length>0){
  109. this.setData({showCart:false})
  110. wx.navigateTo({
  111. url: '/pages/foodOrder/foodOrder?tableId='+this.data.tableId+"&storeId="+this.data.storeId+"&cartId="+this.data.cartId
  112. })
  113. }else{
  114. wx.showToast({
  115. title: '请选择产品',
  116. icon: 'none',
  117. duration: 1000
  118. })
  119. }
  120. },
  121. handleTabClick: function (e) {
  122. let index = e.currentTarget.dataset.index;
  123. this.activeTab(index);
  124. this.setData({ activeTab: index });
  125. },
  126. handleSwiperChange: function (e) {
  127. var index = e.detail.current;
  128. this.activeTab(index);
  129. this.setData({ activeTab: index });
  130. },
  131. activeTab: function (_activeTab) {
  132. var len = this.data.series.length;
  133. if (len === 0) return;
  134. var currentView = _activeTab - 1;
  135. if (currentView < 0) currentView = 0;
  136. if (currentView > len - 1) currentView = len - 1;
  137. this.setData({ currentView: currentView });
  138. },
  139. initProduct: function(){
  140. let series = this.data.series;
  141. let productList = this.data.productList;
  142. let cartList = this.data.cartList;
  143. cartList.forEach((ite,index) => {
  144. productList.forEach((ele,pindex) => {
  145. ele.hasChoose = false;
  146. if (ele.id == ite.productId){
  147. ele.hasChoose = true;
  148. ele.hasNum = ite.num;
  149. }
  150. });
  151. });
  152. let currentProductList = [];
  153. series.forEach((item,index) => {
  154. currentProductList[index] = [];
  155. let i = 0;
  156. productList.forEach((element,pindex) => {
  157. if (element.productTypeId == item.id){
  158. currentProductList[index][i] = element;
  159. i++;
  160. }
  161. });
  162. });
  163. this.setData({
  164. currentProductList:currentProductList
  165. })
  166. },
  167. openCartDialog: function(e){
  168. let type = e.currentTarget.dataset.type;
  169. if (type === 'open'){
  170. if(this.data.cartList.length==0){
  171. wx.showToast({
  172. title: '请选择产品',
  173. icon: 'none',
  174. duration: 1000
  175. })
  176. }else{
  177. this.setData({showCart:true})
  178. }
  179. }else{
  180. this.setData({showCart:false})
  181. }
  182. },
  183. /**
  184. * 点击商品+号购物车摆动
  185. * @return void
  186. */
  187. cartWwing: function()
  188. {
  189. // 创建动画实例(animation)
  190. var animation = wx.createAnimation({
  191. duration: 100,//动画持续时间
  192. timingFunction: 'ease-in',//动画以低速开始
  193. //具体配置项请查看文档
  194. })
  195. // 通过实例描述对象()
  196. animation.translateX(6).rotate(21).step()
  197. animation.translateX(-6).rotate(-21).step()
  198. animation.translateX(0).rotate(0).step()
  199. // 导出动画
  200. this.setData({
  201. ani: animation.export()
  202. })
  203. },
  204. })