123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- const getReq = require('./../../config.js').getReq;
- import Bundle from '../../utils/Bundle'
- Page({
- data: {
- free_info: [],
- summary: [],
- cart_list: [],
- n_cart_list: [],
- all_checked: false,
- allPrice: 0,
- allGoodsPrice: 0,
- hasmore: false,
- curpage: 0,
- special_datas: {},
- prop_special: [],
- summery: [],
- },
- onLoad: function () {
- },
- onShow: function () {
- this.setData({
- free_info: [],
- summary: [],
- cart_list: [],
- n_cart_list: [],
- all_checked: false,
- allPrice: 0,
- allGoodsPrice: 0,
- hasmore: false,
- curpage: 0,
- special_datas: {},
- prop_special: [],
- summery: []
- });
- this.req_datas();
- // this.getDatas()
- this.getFavorite()
- },
- getFavorite() {
- let self = this
- let session_id = wx.getStorageSync('session_id')
- wx.request({
- url: "https://passport.lrlz.com/mobile/index.php",
- data: {
- act: 'member_bonus',
- op: 'match_goodsex',
- curpage: 1,
- client_type: 'ios'
- },
- header: {
- 'content-type': 'application/json', // 默认值
- 'Cookie': 'MPHPSESSID=' + session_id
- },
- success(res) {
- console.log(res);
- }})
- // getReq({
- // act: 'member_bonus',
- // op: 'match_goodsex',
- // curpage: 1
- // }, function (res) {
- // console.log(res);
- // if (res.code == 200) {
- // console.log(res)
- // let special_datas = res.datas
- // let prop_special = res.datas.special_list
- // let summery = res.datas.summary
- // self.setData({
- // special_datas,
- // prop_special,
- // summery
- // })
- // }
- // })
- },
- getDatas() {
- let self = this;
- wx.showLoading({
- title: '加载中',
- })
- getReq({
- act: 'cart',
- op: 'list',
- minest_cartid: self.data.curpage
- }, function (res) {
- if (res.code == 200) {
- wx.hideLoading()
- let { cart_list, summary, bundling } = res.datas
- let n_cart_list = self.getCartList(cart_list, summary, bundling)
- self.setData({
- n_cart_list
- })
- }
- })
- },
- getCartList(cart_list, summary, bundling) {
- let summaryMap = new Map()
- let bundlingMap = new Map()
- summary.map(item => {
- summaryMap.set(item['goods_id'], item)
- })
- if (bundling.length) {
- bundling.map(item => {
- bundlingMap.set(item['bl_id'], item)
- })
- }
- let n_cart_list = new Bundle(cart_list, summaryMap, bundlingMap).createOrders()
- return n_cart_list
- },
- req_datas() {
- let self = this;
- wx.showLoading({
- title: '加载中',
- });
- getReq({
- act: 'cart',
- op: 'list',
- minest_cartid: self.data.curpage
- }, function (res) {
- if (res.code == 200) {
- let hasmore = res.datas.mobile_page.hasmore;
- let free_info = res.datas.free_info;
- let summary_datas = res.datas.summary;
- let cart_list = res.datas.cart_list;
- let bundling = res.datas.bundling
- // let { cart_list, summary, bundling } = res.datas
- // let n_cart_list = self.getCartList(cart_list, summary, bundling)
- let summary = [];
- if (cart_list.length <= 0) {
- self.setData({
- free_info: free_info || self.data.free_info
- });
- wx.hideLoading();
- return;
- }
- for (let item of cart_list) {
- item.checked = false;
- }
- for (let i = 0; i < cart_list.length; i++) {
- for (let j = 0; j < summary_datas.length; j++) {
- if (cart_list[i].goods_id == summary_datas[j].goods_id) {
- summary.push(summary_datas[j]);
- break;
- }
- }
- }
- if (hasmore) {
- let lastCartListId = cart_list[cart_list.length - 1].cart_id;
- self.setData({
- hasmore: true,
- curpage: lastCartListId
- })
- }
- else {
- self.setData({
- hasmore: false
- })
- }
- cart_list = self.getCartList(cart_list, summary_datas, bundling);
- let localSummary = self.data.summary;
- let localCartList = self.data.cart_list;
- self.setData({
- free_info: free_info || self.data.free_info,
- summary: localSummary.concat(summary),
- cart_list: localCartList.concat(cart_list)
- });
- wx.hideLoading();
- }
- })
- },
- goods_checked(e) {
- let goods_id = e.currentTarget.dataset.goodsid;
- let cart_list = this.data.cart_list;
- let all_checked_state = 0;
- for (let item of cart_list) {
- if (item.goods_id == goods_id) {
- item.checked = !item.checked;
- }
- }
- for (let item of cart_list) {
- if (item.checked) {
- all_checked_state = all_checked_state + 1;
- }
- }
- if (all_checked_state == cart_list.length) {
- all_checked_state = true;
- }
- else {
- all_checked_state = false;
- }
- this.setData({
- cart_list,
- all_checked: all_checked_state
- });
- this.total();
- },
- all_checked() {
- let cart_list = this.data.cart_list;
- let all_checked_state = this.data.all_checked;
- for (let item of cart_list) {
- item.checked = !all_checked_state;
- }
- this.setData({
- cart_list,
- all_checked: !all_checked_state
- });
- this.total();
- },
- req_goods_num(cart_id, quantity, callback) {
- let self = this;
- getReq({
- act: 'cart',
- op: 'edit',
- cart_id,
- quantity
- }, function (res) {
- if (res.code == 200) {
- let goods_num = res.datas.goods_num;
- let cart_list = self.data.cart_list;
- for (let item of cart_list) {
- if (item.cart_id == cart_id) {
- item.goods_num = goods_num;
- }
- }
- self.setData({
- cart_list
- });
- callback;
- for (let i = 0; i < self.data.cart_list.length; i++) {
- if (self.data.cart_list[i].checked) {
- self.total();
- break;
- }
- }
- }
- else {
- wx.showToast({
- title: res.message,
- icon: "none"
- })
- }
- })
- },
- goods_num_handle(e) {
- let self = this;
- let cart_id = e.target.dataset.cartid;
- let num = e.target.dataset.num;
- let compute_function = e.target.dataset.function;
- let goods_id = e.target.dataset.goodsid;
- let quantity = 1;
- if (compute_function == 'add') {
- quantity = num + quantity;
- this.req_goods_num(cart_id, quantity);
- }
- else {
- quantity = num - quantity;
- if (quantity <= 0) {
- this.cancal_cart(e, goods_id, cart_id);
- }
- else {
- self.req_goods_num(cart_id, quantity);
- }
- }
- },
- del_cart(cart_id, goods_id) {
- let summary = this.data.summary;
- let cart_list = this.data.cart_list;
- for (let i = 0; i < summary.length; i++) {
- if (summary[i].goods_id == goods_id) {
- summary.splice(i, 1);
- }
- }
- for (let i = 0; i < cart_list.length; i++) {
- if (cart_list[i].goods_id == goods_id) {
- cart_list.splice(i, 1);
- }
- }
- this.setData({
- summary,
- cart_list
- })
- },
- cancal_cart(e, goodsid, cartid) {
- let self = this;
- let goods_id = e.target.dataset.goodsid || goodsid;
- let cart_id = e.target.dataset.cartid || cartid;
- wx.showModal({
- title: "提示",
- content: "确定要删除吗?",
- success: function (res) {
- if (res.confirm) {
- self.req_goods_num(cart_id, 0, self.del_cart(cart_id, goods_id));
- } else if (res.cancel) {
- return;
- }
- }
- })
- },
- total() {
- let self = this;
- let cart_item = [];
- for (let item of this.data.cart_list) {
- if (item.checked) {
- cart_item.push(item.cart_id);
- }
- }
- if (cart_item.length <= 0) {
- self.setData({
- allPrice: 0,
- allGoodsPrice: 0
- })
- }
- let carts = cart_item.join(",");
- getReq({
- act: 'member_buy',
- op: 'calc_cash',
- ifcart: 1,
- cart_id: carts
- }, function (res) {
- if (res.code == 200) {
- let datas = res.datas.payinfo;
- let allPrice = parseFloat((datas.goods_amount + datas.freight - datas.available_pred - datas.full_discount - datas.opgoods_discount).toFixed(2));
- let allGoodsPrice = parseFloat((datas.goods_amount).toFixed(2));
- self.setData({
- allPrice,
- allGoodsPrice
- })
- }
- })
- },
- onReachBottom() {
- let self = this;
- let hasmore = this.data.hasmore;
- if (hasmore) {
- let req_curpage = this.data.curpage;
- getReq({
- act: 'cart',
- op: 'list',
- minest_cartid: req_curpage
- }, function (res) {
- self.req_datas();
- })
- }
- },
- settlement() {
- const cart_id_list = [];
- for (let cart_item of this.data.cart_list) {
- if (cart_item.checked) {
- cart_id_list.push(cart_item.cart_id);
- }
- }
- if (cart_id_list.length <= 0) {
- wx.showToast({
- title: "请最少选择一件商品",
- icon: "none"
- });
- return;
- }
- let cart_list = cart_id_list.join(',');
- wx.navigateTo({
- url: `/pages/confirmOrder/confirmOrder?iscart=1&cart_id=${cart_list}`
- })
- },
- skip_index() {
- wx.switchTab({
- url: "/pages/index/index"
- })
- },
- toDetails(e) {
- let goods_id = e.currentTarget.dataset.goodsid
- wx.navigateTo({
- url: `/pages/details/details?goods_id=${goods_id}&title=商品详情`
- })
- }
- })
|