123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- var util = require('../../../utils/util.js');
- var mall = require('../../../api/mall.js');
- var user = require('../../../utils/user.js');
- var app = getApp();
- Page({
- data: {
- cartGoods: [],
- cartTotal: {
- "goodsCount": 0,
- "goodsAmount": 0.00,
- "checkedGoodsCount": 0,
- "checkedGoodsAmount": 0.00
- },
- isEditCart: false,
- checkedAllStatus: true,
- editCartList: [],
- hasLogin: false
- },
- onLoad: function (options) {
- // 页面初始化 options为页面跳转所带来的参数
- },
- onReady: function () {
- // 页面渲染完成
- },
- onPullDownRefresh() {
- wx.showNavigationBarLoading() //在标题栏中显示加载
- this.getCartList();
- wx.hideNavigationBarLoading() //完成停止加载
- wx.stopPullDownRefresh() //停止下拉刷新
- },
- goIndex: function () {
- wx.switchTab({
- url: '/pages/index/index',
- })
- },
- onShow: function () {
- console.info(app.globalData.hasLogin)
- let that = this;
- that.getCartList();
- // if (app.globalData.hasLogin) {
- // that.setData({
- // hasLogin: app.globalData.hasLogin
- // });
- // that.getCartList();
- // } else {
- // app.checkLoginReadyCallback = res => {
- // this.setData({
- // hasLogin: true
- // });
- // that.getCartList();
- // }
- // }
- },
- onHide: function () {
- // 页面隐藏
- },
- onUnload: function () {
- // 页面关闭
- },
- getPhoneNumber: function (e) {
- let that = this;
- var ivObj = e.detail.iv
- var telObj = e.detail.encryptedData
- let r_userid = wx.getStorageSync('r_userid');
- if (r_userid == null || r_userid == "") r_userid = 0;
- //------执行Login---------
- wx.login({
- success: res => {
- util.request(mall.AuthRegister, {
- code: res.code,
- encryptedData: telObj,
- iv: ivObj,
- r_userid: r_userid
- }).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- hasLogin: true
- });
- //存储用户信息
- app.globalData.hasLogin = true;
- wx.setStorageSync('userInfo', res.data.userInfo);
- wx.setStorageSync('token', res.data.token);
- wx.showToast({
- title: '授权注册成功',
- icon: 'success',
- duration: 2000
- })
- }
- });
- }
- });
- //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
- if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
- wx.showModal({
- title: '警告',
- content: '您点击了拒绝授权,部分功能无法使用!!!',
- showCancel: false,
- confirmText: '确定',
- success: function (res) {
- // 用户没有授权成功,不需要改变 isHide 的值
- if (res.confirm) {
- wx.switchTab({
- url: '/pages/index/index',
- })
- };
- }
- })
- }
- },
- getCartList: function () {
- let that = this;
- util.request(mall.CartList).then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoods: res.data.cartList,
- cartTotal: res.data.cartTotal
- });
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- //显示购物车角标
- if (res.data.cartTotal != null) {
- wx.setStorageSync('cartcount', res.data.cartTotal.goodsCount);
- // user.cartshow();
- }
- }
- });
- },
- isCheckedAll: function () {
- //判断购物车商品已全选
- return this.data.cartGoods.every(function (element, index, array) {
- if (element.checked == true) {
- return true;
- } else {
- return false;
- }
- });
- },
- doCheckedAll: function () {
- let checkedAll = this.isCheckedAll()
- this.setData({
- checkedAllStatus: this.isCheckedAll()
- });
- },
- checkedItem: function (event) {
- let itemIndex = event.target.dataset.itemIndex;
- let that = this;
- let productIds = [];
- productIds.push(that.data.cartGoods[itemIndex].commoditySkuId);
- if (!this.data.isEditCart) {
- util.request(mall.CartChecked, {
- productIds: productIds,
- isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1
- }, 'POST').then(function (res) {
- if (res.errno === 0) {
- that.setData({
- cartGoods: res.data.cartList,
- cartTotal: res.data.cartTotal
- });
- }
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- } else {
- //编辑状态
- let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
- if (index == itemIndex) {
- element.checked = !element.checked;
- }
- return element;
- });
- that.setData({
- cartGoods: tmpCartData,
- checkedAllStatus: that.isCheckedAll(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- }
- },
- getCheckedGoodsCount: function () {
- let checkedGoodsCount = 0;
- this.data.cartGoods.forEach(function (v) {
- if (v.checked === true) {
- checkedGoodsCount += v.number;
- }
- });
- console.log(checkedGoodsCount);
- return checkedGoodsCount;
- },
- getCheckedGoodsAmount: function () {
- let checkedGoodsAmount = 0;
- this.data.cartGoods.forEach(function (v) {
- if (v.checked === true) {
- checkedGoodsAmount += v.number * v.price;
- }
- });
- return checkedGoodsAmount.toFixed(2);
- },
- checkedAll: function () {
- let that = this;
- if (!this.data.isEditCart) {
- var productIds = this.data.cartGoods.map(function (v) {
- return v.commoditySkuId;
- });
- util.request(mall.CartChecked, {
- productIds: productIds,
- isChecked: that.isCheckedAll() ? 0 : 1
- }, 'POST').then(function (res) {
- if (res.errno === 0) {
- console.log(res.data);
- that.setData({
- cartGoods: res.data.cartList,
- cartTotal: res.data.cartTotal
- });
- }
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- } else {
- //编辑状态
- let checkedAllStatus = that.isCheckedAll();
- let tmpCartData = this.data.cartGoods.map(function (v) {
- v.checked = !checkedAllStatus;
- return v;
- });
- that.setData({
- cartGoods: tmpCartData,
- checkedAllStatus: that.isCheckedAll(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- }
- },
- editCart: function () {
- var that = this;
- if (this.data.isEditCart) {
- this.getCartList();
- this.setData({
- isEditCart: !this.data.isEditCart
- });
- } else {
- //编辑状态
- let tmpCartList = this.data.cartGoods.map(function (v) {
- v.checked = false;
- return v;
- });
- this.setData({
- editCartList: this.data.cartGoods,
- cartGoods: tmpCartList,
- isEditCart: !this.data.isEditCart,
- checkedAllStatus: that.isCheckedAll(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- }
- },
- updateCart: function (cartItem,index,number,count) {
- let that = this;
- util.request(mall.CartUpdate, {
- id:cartItem.id,
- commodityId:cartItem.commodityId,
- commoditySkuId:cartItem.commoditySkuId,
- number:number
- }, 'POST').then(res=> {
- console.log(res);
- if(res.errno==0){
- // cartItem.number = number;
- let cartItem = "cartGoods[" + index + "].number";
- this.setData({
- [cartItem]:number,
- });
- console.log(this.cartGoods);
- this.setData({
-
- 'cartTotal.checkedGoodsAmount': that.getCheckedGoodsAmount(),
- 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- });
- user.cartadd(count);
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- }else{
- util.showErrorToast(res.errmsg);
- }
-
- }).catch(err=>{
- console.log(err);
- util.showErrorToast(err.errno);
- });
- },
- cutNumber: function (event) {
- let that = this;
- let itemIndex = event.target.dataset.itemIndex;
- let cartItem = this.data.cartGoods[itemIndex];
- //删除操作
- if (cartItem.number == 1) {
- wx.showModal({
- title: "提醒",
- content: "确定从购物车删除?",
- success(res) {
- if (res.confirm)
- that.deleteCartGoods(cartItem);
- }
- })
-
- } else {
- let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
- // cartItem.number = number;
- // this.setData({
- // cartGoods: this.data.cartGoods,
- // 'cartTotal.checkedGoodsAmount': that.getCheckedGoodsAmount(),
- // 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- // });
- this.updateCart(cartItem,itemIndex,number,-1);
- //显示tab购物车
- // user.cartadd(-1);
- }
- },
- addNumber: function (event) {
- let that = this;
- let itemIndex = event.target.dataset.itemIndex;
- let cartItem = this.data.cartGoods[itemIndex];
- console.log(cartItem);
- if(cartItem.goodsSn=='1036016'){
- wx.showToast({
- title: '此款商品只能购买一个',
- icon: 'none',
- duration: 2000
- });
- var number=1;
- }else{
- var number = cartItem.number + 1;
- //显示tab购物车
- // user.cartadd(1);
- }
- // cartItem.number = number;
- // this.setData({
- // cartGoods: this.data.cartGoods,
- // 'cartTotal.checkedGoodsAmount': that.getCheckedGoodsAmount(),
- // 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
- // });
- this.updateCart(cartItem,itemIndex,number,1);
-
- },
- checkoutOrder: function () { //下单
- //获取已选择的商品
- let that = this;
- var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
- if (element.checked == true) {
- return true;
- } else {
- return false;
- }
- });
- if (checkedGoods.length <= 0) {
- util.showErrorToast('请先选择商品')
- return false;
- }
- // storage中设置了cartId,则是购物车购买
- try {
- wx.setStorageSync('cartId', 0);
- wx.navigateTo({
- url: '/pages/mall/checkout/checkout'
- })
- } catch (e) {}
- },
- deleteCart: function () {
- //获取已选择的商品
- let that = this;
- let productIds = this.data.cartGoods.filter(function (element, index, array) {
- if (element.checked == true) {
- return true;
- } else {
- return false;
- }
- });
- if (productIds.length <= 0) {
- return false;
- }
- productIds = productIds.map(function (element, index, array) {
- if (element.checked == true) {
- return element.productId;
- }
- });
- let arr=[];
- arr.push(productIds);
- console.log(arr);
- util.request(mall.CartDelete, {
- productIds: arr
- }, 'POST').then(function (res) {
- console.log(res);
- if (res.errno === 0) {
- let cartList = res.data.cartList.map(v => {
- v.checked = false;
- return v;
- });
- that.setData({
- cartGoods: cartList,
- cartTotal: res.data.cartTotal
- });
- }
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- },
- deleteCartGoods: function (cartItem) {
- //获取已选择的商品
- let that = this;
- console.log(cartItem);
- util.request(mall.CartDelete, {
- productIds:[cartItem.commoditySkuId],
- }, 'POST').then(function (res) {
- console.log(res.data);
- if (res.errno === 0) {
- let cartList = res.data.cartList.map(v => {
- v.checked = false;
- return v;
- });
- that.setData({
- cartGoods: cartList,
- cartTotal: res.data.cartTotal
- });
- user.cartadd(-1);
- user.cartshow();
- }
- that.setData({
- checkedAllStatus: that.isCheckedAll()
- });
- });
- }
- })
|