| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- // import bm from '../../utils/baseMath.js'
- // // pages/buycar/buycar.js
- var app = getApp();
- // // var wx = require('../../utils/wx.js');
- Page({
- data: {
- isAttrInfo:'attrInfo',
- itemAttrInfo : '',
- foothidden:false,
- url: app.globalData.urlImages,
- countmoney:"",
- cartNum:"",
- isAllSelect:false,
- minusStatus: 'disabled',
- cartList:[],
- cartInvalid:[],
- cartIdsStr:''
- },
- setNumber: function (e) {
- var that = this;
- var index = e.currentTarget.dataset.item;
- var cartList = that.data.cartList;
- var num = parseInt(e.detail.value);
- var cart_num = num ? num : 1;
- if (cartList[index].productInfo.attrInfo.stock){
- if (cart_num > cartList[index].productInfo.stock) cart_num = cartList[index].productInfo.attrInfo.stock
- }else{
- if (cart_num > cartList[index].productInfo.attrInfo.stock) cart_num = cartList[index].productInfo.stock
- }
- cartList[index].cart_num = cart_num;
- that.setData({
- cartList: cartList
- })
- wx.request({
- url: app.globalData.url + '/routine/auth_api/set_buy_cart_num?uid=' + app.globalData.uid,
- method: 'GET',
- data:{
- cartId: cartList[index].id,
- cartNum: cart_num
- },
- success: function (res) {
- if (res.data.code == 400) {
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 1000,
- })
- }
- }
- })
- },
- onLoad: function (options) {
- app.setBarColor();
- app.setUserInfo();
- this.carnum();
- this.countmoney();
- this.getList();
- },
- btntap: function (e) {
- this.data.foothidden = !this.data.foothidden;
- this.setData({
- foothidden: this.data.foothidden
- })
- },
- getList:function(){
- var that = this;
- var header = {
- 'content-type': 'application/x-www-form-urlencoded',
- };
- wx.request({
- url: app.globalData.url + '/routine/auth_api/get_cart_list?uid=' + app.globalData.uid,
- method: 'POST',
- header: header,
- success: function (res) {
- if(res.data.code == 200){
- that.setData({
- cartList: res.data.data.valid,
- cartInvalid: res.data.data.invalid
- })
- }
- }
- })
- },
- //加
- numAddClick: function (event) {
- var index = event.currentTarget.dataset.index;
- this.data.cartList[index].cart_num = +this.data.cartList[index].cart_num + 1;
- var minusStatus = this.data.cartList[index].cart_num <= 1 ? 'disabled' : 'normal';
- this.setData({ cartList: this.data.cartList, minusStatus: minusStatus});
- this.carnum();
- this.countmoney();
- this.addCartNum(this.data.cartList[index].cart_num, this.data.cartList[index].id);
- },
- //减
- numDescClick: function (event) {
- var index = event.currentTarget.dataset.index;
- this.data.cartList[index].cart_num = this.data.cartList[index].cart_num <= 1 ? 1 : +this.data.cartList[index].cart_num - 1;
- var minusStatus = this.data.cartList[index].cart_num <= 1 ? 'disabled' : 'normal';
- this.setData({ cartList: this.data.cartList, minusStatus: minusStatus});
- this.carnum();
- this.countmoney();
- this.addCartNum(this.data.cartList[index].cart_num, this.data.cartList[index].id);
- },
- //单选;
- switchSelect:function(e){
- var index = e.currentTarget.dataset.index;
- this.data.cartList[index].checked = !this.data.cartList[index].checked;
- var len = this.data.cartList.length;
- var selectnum = [];
- for(var i = 0; i < len ;i++){
- if (this.data.cartList[i].checked==true){
- selectnum.push(true);
- }
- }
- if (selectnum.length==len){
- this.data.isAllSelect = true;
- }else{
- this.data.isAllSelect = false;
- }
- this.setData({
- cartList: this.data.cartList,
- isAllSelect: this.data.isAllSelect
- });
- this.carnum();
- this.countmoney();
- this.confirmOrder();
- },
- //全选
- allChecked:function(e){
- var selectAllStatus = this.data.isAllSelect;
- selectAllStatus = !selectAllStatus;
- var array = this.data.cartList;
- for (var i = 0; i < array.length;i++){
- array[i].checked = selectAllStatus;
- };
- this.setData({
- cartList:this.data.cartList,
- isAllSelect: selectAllStatus
- })
- this.carnum();
- this.countmoney();
- this.confirmOrder();
- },
- //数量
- carnum(){
- var carnum = 0;
- var array = this.data.cartList;
- for(var i = 0;i<array.length;i++){
- if (array[i].checked==true){
- carnum += parseInt(array[i].cart_num);
- }
- }
- this.setData({
- cartNum: carnum
- })
- },
- //总共价钱;
- countmoney(){
- var carmoney = 0;
- var array = this.data.cartList;
- for (var i = 0; i < array.length; i++) {
- if (array[i].checked == true) {
- if (array[i].productInfo.attrInfo){
- carmoney += parseFloat(array[i].cart_num * array[i].productInfo.attrInfo.price);
- }else{
- carmoney += parseFloat(array[i].cart_num * array[i].productInfo.price);
- }
- }
- }
- this.setData({
- countmoney: carmoney.toFixed(2)
- })
- },
- confirmOrder:function(){
- var array = this.data.cartList;
- var cartIds = [];
- for (var i = 0; i < array.length; i++) {
- if (array[i].checked == true) {
- cartIds.push(array[i].id);
- }
- }
- if (cartIds.length > 0) {
- this.goConfirm(cartIds);
- }else{
- this.setData({
- cartIdsStr: ''
- })
- }
- },
- goConfirm : function(cartIds){
- if (cartIds.length > 0) {
- this.setData({
- cartIdsStr: cartIds.join(',')
- })
- }
- // console.log(this);
- },
- addCartNum: function (cartNum, cartId){
- var header = {
- 'content-type': 'application/x-www-form-urlencoded',
- };
- wx.request({
- url: app.globalData.url + '/routine/auth_api/change_cart_num?uid=' + app.globalData.uid,
- method: 'GET',
- data: {
- cartNum: cartNum,
- cartId: cartId
- },
- header: header,
- success: function (res) {
- if (res.data.code == 200) {
- wx.showToast({
- title: '成功',
- icon: 'success',
- duration: 2000
- })
- } else {
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- })
- },
- collectAll: function () {
- var array = this.data.cartList;
- var productIds = [];
- var that = this;
- for (var i = 0; i < array.length; i++) {
- if (array[i].checked == true) {
- productIds.push(array[i].product_id);
- }
- }
- if (productIds.length > 0) {
- var header = {
- 'content-type': 'application/x-www-form-urlencoded',
- };
- wx.request({
- url: app.globalData.url + '/routine/auth_api/collect_product_all?uid=' + app.globalData.uid,
- method: 'GET',
- data: {
- productId: productIds.join(',')
- },
- header: header,
- success: function (res) {
- if (res.data.code == 200) {
- wx.showToast({
- title: '收藏成功',
- icon: 'success',
- duration: 2000
- })
- } else {
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- })
- }
- console.log(productIds);
- },
- cartDelAll:function(){
- var array = this.data.cartList;
- var ids = [];
- var that = this;
- for (var i = 0; i < array.length; i++) {
- if (array[i].checked == true) {
- ids.push(array[i].id);
- }
- }
- if (ids.length > 0){
- var header = {
- 'content-type': 'application/x-www-form-urlencoded',
- };
- wx.request({
- url: app.globalData.url + '/routine/auth_api/remove_cart?uid=' + app.globalData.uid,
- method: 'GET',
- data: {
- ids: ids.join(',')
- },
- header: header,
- success: function (res) {
- if (res.data.code == 200) {
- wx.showToast({
- title: '删除成功',
- icon: 'success',
- duration: 2000
- })
- for (var i = 0; i < ids.length; i++) {
- for (var j = 0; j < array.length; j++) {
- if (ids[i] == array[j].id) {
- array.splice(j, 1);
- }
- }
- }
- that.setData({
- cartList: array
- })
- } else {
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- })
- }
- },
- cartDel:function(e){
- var that = this;
- if (e.currentTarget.dataset.id){
- var header = {
- 'content-type': 'application/x-www-form-urlencoded',
- };
- wx.request({
- url: app.globalData.url + '/routine/auth_api/remove_cart?uid=' + app.globalData.uid,
- method: 'GET',
- data: {
- ids: e.currentTarget.dataset.id
- },
- header: header,
- success: function (res) {
- if(res.data.code == 200){
- wx.showToast({
- title: '删除成功',
- icon: 'success',
- duration: 2000
- })
- var cartInvalid = that.data.cartInvalid;
- for (var i = 0; i < cartInvalid.length ; i++){
- if (e.currentTarget.dataset.id == cartInvalid[i].id){
- cartInvalid.splice(i,1);
- that.setData({
- cartInvalid: cartInvalid
- })
- }
- }
- }else{
- wx.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 2000
- })
- }
- }
- })
- }
- },
- /**
- * 生命周期函数--监听页面加载
- */
-
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
-
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- this.carnum();
- this.countmoney();
- this.getList();
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
-
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
-
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
-
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
-
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
-
- }
- })
|