food.js 5.3 KB

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