| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- // pages/cartList/cartList.js
- var app = getApp();
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- startX: 0, //开始坐标
- startY: 0,
- userInfo: wx.getStorageSync('userInfo'),
- checkAll: false,
- loading: false,
- load: false,
- checkNum: 0,
- checkPrice: 0.00,
- cartList: []
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {},
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- // let userInfo = wx.getStorageSync('userInfo');
- // if (userInfo && userInfo.userId) {
- // app.globalData.tabbar.list[0].show = false;
- // app.globalData.tabbar.list[1].show = true;
- // } else {
- // app.globalData.tabbar.list[0].show = true;
- // }
- // this.getTabBar().setData({
- // selectIndex: 3,
- // tabbar: app.globalData.tabbar
- // })
- }
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- let userInfo = wx.getStorageSync('userInfo');
- this.setData({
- userInfo,
- })
- this.setData({
- checkAll: false,
- checkNum: 0,
- checkPrice: 0.00,
- })
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- // if (userInfo && userInfo.userId) {
- // app.globalData.tabbar.list[0].show = false;
- // app.globalData.tabbar.list[1].show = true;
- // } else {
- // app.globalData.tabbar.list[0].show = true;
- // }
- // this.getTabBar().setData({
- // selectIndex: 3,
- // tabbar: app.globalData.tabbar
- // })
- }
- wx.setNavigationBarTitle({
- title: app.globalData.tabbar.list[2].text,
- });
- if (userInfo && userInfo.userId)
- this.initData();
- },
- async initData() {
- this.setData({
- loading: !this.data.load
- })
- let {
- data
- } = await app.ajax.get({
- url: `/admin/bxdusercollection/query?userId=${this.data.userInfo.userId}&virtualInventoryId=${wx.getStorageSync('virtualInventoryId')}`,
- });
- let cartList = data.data.map(v => {
- v["picArr"] = v.pic ? v.pic.split(",") : null;
- return v;
- });
- this.setData({
- cartList: cartList,
- loading: false,
- load: true
- });
- this.switchChange({
- currentTarget: {
- dataset: {
- all: true
- }
- }
- });
- },
- switchChange(e) {
- let cartList = this.data.cartList;
- const allCHave = cartList.every(item => item.check == true);
- let checkAll = null;
- if (e.currentTarget.dataset.all) {
- if (!allCHave) {
- cartList.map(v => {
- v.check = true;
- });
- checkAll = true;
- } else {
- cartList.map(v => {
- v.check = false;
- });
- checkAll = true;
- }
- } else {
- cartList[e.currentTarget.dataset.index].check = !cartList[e.currentTarget.dataset.index].check;
- }
- this.setData({
- cartList,
- checkAll: cartList.every(item => item.check == true),
- checkNum: cartList.filter((item => item.check)).length,
- checkPrice: cartList.filter((item => item.check)).map(v => {
- return {
- price: app.currency(v.price).multiply(v.qty).value
- }
- }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
- })
- },
- async jianNum(e) {
- let cartList = this.data.cartList;
- let cart = cartList[e.currentTarget.dataset.index];
- if (cart.qty <= 1) return;
- cart.qty = (cart.qty || 1) - 1;
- try {
- await this.updateSku({
- id: cart.id,
- qty: cart.qty
- });
- // this.setData({
- // cartList,
- // checkPrice: cartList.filter((item => item.check)).map(v => {
- // return {
- // price: app.currency(v.price).multiply(v.qty).value
- // }
- // }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
- // })
- this.initData();
- } catch (e) {}
- },
- async jiaNum(e) {
- let cartList = this.data.cartList;
- let cart = cartList[e.currentTarget.dataset.index];
- if (cart.qty > cart.usableStocks || cart.qty == cart.limitQty) return;
- try {
- cart.qty = (cart.qty || 1) + 1;
- await this.updateSku({
- id: cart.id,
- qty: cart.qty
- });
- // this.setData({
- // cartList,
- // checkPrice: cartList.filter((item => item.check)).map(v => {
- // return {
- // price: app.currency(v.price).multiply(v.qty).value
- // }
- // }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
- // });
- this.initData();
- } catch (e) {}
- },
- nextBtn() {
- let role = wx.getStorageSync('roleList').find(v => v.roleCode == 'User' || v.roleCode == 'SZMAdmin');
- if (!role) {
- wx.showToast({
- title: '您无下单权限!',
- icon: "error"
- })
- return;
- }
- let cartList = this.data.cartList.filter((item => item.check));
- if (cartList.length == 0) {
- wx.showToast({
- title: '请选择商品',
- icon: "error"
- });
- return;
- }
- wx.navigateTo({
- url: '/pages/order/create/create',
- success: function (res) {
- // 通过eventChannel向被打开页面传送数据
- res.eventChannel.emit('acceptDataFromOpenerPage', {
- goodsList: cartList,
- checkPrice: cartList.map(v => {
- return {
- price: app.currency(v.price).multiply(v.qty).value
- }
- }).reduce((price, item1) => app.currency(price).add(item1.price).value, 0),
- })
- }
- })
- },
- async updateSku(data) {
- await app.ajax.post({
- url: "/admin/bxdusercollection/updateQtyById",
- method: "PUT",
- data
- });
- },
- async delBtn() {
- let ids = this.data.cartList.filter(v => v.check).map(v => v.id).join(",");
- if (!ids) {
- return;
- }
- let that = this;
- wx.showModal({
- title: "删除提示",
- content: `确定删除这些商品吗?`,
- async success(res) {
- if (res.confirm) {
- await app.ajax.post({
- url: `/admin/bxdusercollection/removeByIds?ids=${ids}`,
- method: "DELETE"
- });
- wx.showToast({
- title: '删除成功'
- })
- that.setData({
- current: 0,
- cartList: []
- })
- setTimeout(async () => {
- await that.initData();
- }, 1000);
- }
- }
- })
- },
- async delBtn1(e) {
- let ids = e.currentTarget.dataset.item.id;
- if (!ids) {
- return;
- }
- let that = this;
- wx.showModal({
- title: "删除提示",
- content: `确定删除这个商品吗?`,
- async success(res) {
- if (res.confirm) {
- await app.ajax.post({
- url: `/admin/bxdusercollection/removeByIds?ids=${ids}`,
- method: "DELETE"
- });
- wx.showToast({
- title: '删除成功'
- })
- that.setData({
- current: 0,
- cartList: []
- })
- setTimeout(async () => {
- await that.initData();
- }, 1000);
- }
- }
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- // this.setData({
- // checkAll: false,
- // checkNum: 0,
- // checkPrice: 0.00,
- // })
- },
- toLogin() {
- wx.navigateTo({
- url: '/pages/login/login',
- })
- },
- toGoods(e) {
- wx.navigateTo({
- url: `/pages/goods/goods?prodId=${e.currentTarget.dataset.item.prodId}`,
- })
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- touchE: function (e) {
- // //console.log(e);
- var that = this
- if (e.changedTouches.length == 1) {
- //手指移动结束后触摸点位置的X坐标
- var endX = e.changedTouches[0].clientX;
- //触摸开始与结束,手指移动的距离
- var disX = that.data.startX - endX;
- var delBtnWidth = 120;
- //如果距离小于删除按钮的1/2,不显示删除按钮
- var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "rpx" : "left:0rpx";
- //获取手指触摸的是哪一项
- var index = e.currentTarget.dataset.index;
- var list = that.data.cartList;
- list[index].txtStyle = txtStyle;
- //更新列表的状态
- that.setData({
- cartList: list
- });
- }
- },
- //手指触摸动作开始 记录起点X坐标
- touchstart: function (e) {
- //开始触摸时 重置所有删除
- this.data.cartList.forEach(function (v, i) {
- if (v.isTouchMove) //只操作为true的
- v.isTouchMove = false;
- })
- this.setData({
- startX: e.changedTouches[0].clientX,
- startY: e.changedTouches[0].clientY,
- cartList: this.data.cartList
- })
- },
- //滑动事件处理
- touchmove: function (e) {
- var that = this,
- index = e.currentTarget.dataset.index, //当前索引
- startX = that.data.startX, //开始X坐标
- startY = that.data.startY, //开始Y坐标
- touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
- touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
- //获取滑动角度
- angle = that.angle({
- X: startX,
- Y: startY
- }, {
- X: touchMoveX,
- Y: touchMoveY
- });
- that.data.cartList.forEach(function (v, i) {
- v.isTouchMove = false
- //滑动超过30度角 return
- if (Math.abs(angle) > 30) return;
- if (i == index) {
- if (touchMoveX > startX) //右滑
- v.isTouchMove = false
- else //左滑
- v.isTouchMove = true
- }
- })
- //更新数据
- that.setData({
- cartList: that.data.cartList
- })
- },
- /**
- * 计算滑动角度
- * @param {Object} start 起点坐标
- * @param {Object} end 终点坐标
- */
- angle: function (start, end) {
- var _X = end.X - start.X,
- _Y = end.Y - start.Y
- //返回角度 /Math.atan()返回数字的反正切值
- return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
- },
- })
|