123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import {
- default as productApi
- } from "../../api/product"
- const app = getApp()
- var utils = require('../../utils/util.js');
- Page({
- data: {
- currentTab: 0,
- activeTab:0,
- showCart:false,
- mask:true,
- series:[],//商品分类
- cartList:[],//购物车列表
- currentProductList:[],//商品
- totalNum:0,//购物车总数
- totalPrice:0.00//购物车总价格
- },
- onShow: async function () {
- // console.log(app.globalData.userInfo)
- // var that = this;
- // let checkMobile = false;
- // if(app.globalData.userInfo && app.globalData.userInfo.mobile){
- // checkMobile = true;
- // }
- // that.setData({
- // check_mobile: checkMobile
- // })
-
-
- },
- //商品分类列表
- productTypeList: async function(){
- let self = this
- let rs = await productApi.getProductTypeList(1,10000,2)
- self.setData({
- series: rs.list
- })
- },
- //商品列表
- getProductList: async function (refresh = false) {
- const self = this
- let prolist = await productApi.getProductList(0,1,10000,2);
- self.setData({
- productList:prolist.list
- })
- await this.initProduct();
- },
- // 获取购物车
- getCart: async function(){
- let self = this;
- let data = {
- tableId: this.data.tableId,
- storeId: this.data.storeId
- }
- const rs = await productApi.getCart(data)
- console.log(rs);
- self.setData({
- cartId:rs.id,
- cartList:rs.list,
- totalNum:rs.totalNum?rs.totalNum:0,
- totalPrice:rs.totalPrice?rs.totalPrice:0.00,
- })
- },
- // 更新购物车 type类型1增加 2减少
- updateCart: async function(productId,type=1,num=1){
- let data = {
- productId : productId,
- num : num,
- cartId : this.data.cartId,
- type : type
- }
- await productApi.updateCart(data)
- },
- updateCartClick :async function(e){
- let productId = e.target.dataset.no;
- let type = e.target.dataset.type;
- // wx.showLoading({
- // title: '加入购物车',
- // });
- await this.updateCart(productId,type);
- await this.getCart();
- if(this.data.cartList.length ==0){
- this.setData({showCart:false})
- }
- await this.initProduct();
- //动画
- this.cartWwing();
- },
- onLoad: async function (options) {
-
- this.setData({
- tableId:options.tableId,
- storeId:options.storeId
- })
- const isAuth = await app.isAuth()
- if (!isAuth) {
- wx.redirectTo({
- url: '/pages/prompt/prompt?page=' + this.route+"&tableId="+options.tableId+"&storeId="+options.storeId,
- })
- return
- }
- await this.getCart();
- await this.productTypeList();
- await this.getProductList();
-
- },
-
- startOrder: function (e) {
- if(this.data.cartList.length>0){
- this.setData({showCart:false})
- wx.navigateTo({
- url: '/pages/foodOrder/foodOrder?tableId='+this.data.tableId+"&storeId="+this.data.storeId+"&cartId="+this.data.cartId
- })
- }else{
- wx.showToast({
- title: '请选择产品',
- icon: 'none',
- duration: 1000
- })
-
- }
-
- },
- handleTabClick: function (e) {
- let index = e.currentTarget.dataset.index;
- this.activeTab(index);
- this.setData({ activeTab: index });
- },
- handleSwiperChange: function (e) {
- var index = e.detail.current;
- this.activeTab(index);
- this.setData({ activeTab: index });
- },
- activeTab: function (_activeTab) {
- var len = this.data.series.length;
- if (len === 0) return;
- var currentView = _activeTab - 1;
- if (currentView < 0) currentView = 0;
- if (currentView > len - 1) currentView = len - 1;
- this.setData({ currentView: currentView });
- },
- initProduct: function(){
- let series = this.data.series;
- let productList = this.data.productList;
- let cartList = this.data.cartList;
- productList.forEach((ele,pindex) => {
- ele.hasChoose = false;
- ele.hasNum = 0;
- cartList.forEach((ite,index) => {
- if (ele.id == ite.productId){
- ele.hasChoose = true;
- ele.hasNum = ite.num;
- }
-
- });
- });
-
- let currentProductList = [];
- series.forEach((item,index) => {
- currentProductList[index] = [];
- let i = 0;
- productList.forEach((element,pindex) => {
- if (element.productTypeId == item.id){
- currentProductList[index][i] = element;
- i++;
- }
- });
- });
- this.setData({
- currentProductList:currentProductList
- })
- },
- openCartDialog: function(e){
- let type = e.currentTarget.dataset.type;
- if (type === 'open'){
- if(this.data.cartList.length==0){
- wx.showToast({
- title: '请选择产品',
- icon: 'none',
- duration: 1000
- })
- }else{
- this.setData({showCart:true})
- }
-
-
- }else{
- this.setData({showCart:false})
- }
- },
- /**
- * 点击商品+号购物车摆动
- * @return void
- */
- cartWwing: function()
- {
-
- // 创建动画实例(animation)
- var animation = wx.createAnimation({
- duration: 100,//动画持续时间
- timingFunction: 'ease-in',//动画以低速开始
- //具体配置项请查看文档
- })
-
- // 通过实例描述对象()
- animation.translateX(6).rotate(21).step()
- animation.translateX(-6).rotate(-21).step()
- animation.translateX(0).rotate(0).step()
-
- // 导出动画
- this.setData({
- ani: animation.export()
- })
-
- },
- })
|