123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // customized/pages/made/car/car.js
- let app = getApp();
- let host = app.globalData.servsers; // 请求的url
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- carList:[],// 购物车列表数组
- host: app.globalData.servsers,
- amount:0, //总价
- checkAll:false, // 全选
- checkStatic:false, //默认一个都没有选择
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onShow: function () {
- let that = this;
- that.getCarlist(that)
- },
- /**
- * 生命周期函数--监听页面显示
- */
- subtraction(e){ // 减少
- let that = this;
- console.log(e)
- let index = e.target.dataset.index,carList = that.data.carList;
- if (carList[index].customGoodsNum > 1){
- // 表示最小数量,不可减少
- carList[index].customGoodsNum--;
- that.setData({
- carList: carList
- })
- }
- that.totalPrice(that)
- },
- add(e) { // 减少
- let that = this;
- console.log(e)
- let index = e.target.dataset.index, carList = that.data.carList;
- carList[index].customGoodsNum++;
- that.setData({
- carList: carList
- })
- that.totalPrice(that)
- },
- checkboxChange(e){
- let index = e.target.dataset.index, that = this, carList = that.data.carList;
- if (e.detail.value[0]){ // 表示价格增加
- that.data.carList[index].checked = true; // 表示选中
- } else {
- that.data.carList[index].checked = false; // 表示选中
- }
- that.setData({
- carList: carList
- })
- that.totalPrice(that)
- },
- checkboxChangeAll(e){ // 全选
- console.log(e)
- let that = this, checkAll= that.data.checkAll , carList=that.data.carList;
- if (e.detail.value[0]) {
- checkAll = true; // 表示选中
- } else {
- checkAll = false;
- }
- carList.forEach(res => {
- res.checked = checkAll;
- })
- that.setData({
- carList:carList
- })
- that.totalPrice(that)
- },
- getCarlist(that){
- wx.showLoading({
- title: '加载中...',
- })
- wx.request({
- url: host +'/queryShopCar',
- method:'post',
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- data:{
- userId: app.globalData.user_id //app.globalData.user_id //'oovpNwjlsY6xx8ceCebFa1dOLd9E' //app.globalData.user_id,
- },
- success:function(res){
- console.log('购物车列表数据',res)
- let carList = [],imgArray=[];
- res.data.forEach((el,index)=>{
- imgArray =el.customGoodsImg.split(',');
- carList.push(el);
- carList[index]['imgArray'] = imgArray;
- carList[index]['checked'] = false;
- })
- that.setData({
- carList: carList
- })
- console.log(that.data.carList,"carListcarList")
- },
- complete(){
- wx.hideLoading()
- }
- })
- },
- carSubmit(){ // 提交订单
- let that = this, carList=[];
- if (that.data.checkStatic){
- that.data.carList.forEach(el=>{
- if(el.checked){
- carList.push(el)
- }
- })
- wx.setStorageSync('goodsList', carList);
- wx.setStorageSync('amount', that.data.amount);
- wx.navigateTo({
- url: '/customized/pages/accounts/accounts',
- })
- } else {
- wx.showToast({
- title: '您还没有选择商品',
- icon:'none'
- })
- }
- },
- totalPrice(that){
- let amount = 0, checkAll = true, checkStatic= false;
- that.data.carList.forEach(res=>{
- if(res.checked){
- amount += res.customgoodsPriceNow * res.customGoodsNum;
- checkStatic = true
-
- }
- if (!res.checked) {
- checkAll = false
- }
- })
- that.setData({
- amount:amount.toFixed(2),
- checkAll: checkAll,
- checkStatic: checkStatic
- })
- },
- previewImage(e){ // 点击图片放大
- let urls = [],that = this;
- console.log(e)
- e.target.dataset.srcarray.forEach(el=>{
- urls.push(that.data.host + el)
- })
- wx.previewImage({
- urls: urls,
- current:e.target.dataset.src,
- success(res){
- console.log(res,"放大成功")
- }
- })
- }
- })
|