Skip to content
goods-cart.ts 6.29 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component, ViewChild} from '@angular/core';
import {InfiniteScrollContent, IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
wangqinghua's avatar
wangqinghua committed
import {ServeService} from "../../serve.service";
import {GoodsOrderPage} from "../goods-order/goods-order";
import {SpcesComponent} from "../../../../components/spces/spces";
import {CommonService} from "../../../../provide/common.service";
wangqinghua's avatar
wangqinghua committed
import {GoodsVerifyPage} from "../goods-verify/goods-verify";
wangqinghua's avatar
wangqinghua committed
import {AppMainService} from "../../../../app/app.service";
wangqinghua's avatar
wangqinghua committed
import {AppGlobal} from "../../../../service/http.service";
wangqinghua's avatar
wangqinghua committed
import {timer} from "rxjs/observable/timer";
wangqinghua's avatar
wangqinghua committed


@Component({
    selector: 'page-goods-cart',
    templateUrl: 'goods-cart.html',
})
export class GoodsCartPage {
wangqinghua's avatar
wangqinghua committed
    @ViewChild('infiniteScrollContent') infiniteScrollContent: InfiniteScrollContent;
wangqinghua's avatar
wangqinghua committed
    cartList = [];
    footerView = false;
    chooseObj = [];

    checkAll;
wangqinghua's avatar
wangqinghua committed
    role = [];
wangqinghua's avatar
wangqinghua committed
    isLoad = true;
wangqinghua's avatar
wangqinghua committed
    picture = AppGlobal.picture;
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    page = {
        P_pageNumber: 1,
        P_pageSize: AppGlobal.pageCount,
        loadMore: false,
        total: null
    };

wangqinghua's avatar
wangqinghua committed
    constructor(public navCtrl: NavController, public navParams: NavParams,
                private serveSer: ServeService, private modalCtrl: ModalController,
wangqinghua's avatar
wangqinghua committed
                private commonSer: CommonService, private appMainSer: AppMainService) {
wangqinghua's avatar
wangqinghua committed
    }

    ionViewDidEnter() {
        this.footerView = true;
        this.getCart();
wangqinghua's avatar
wangqinghua committed
        //获取权限
        this.appMainSer.role.subscribe(value => {
                this.role = value;
            }
        );
wangqinghua's avatar
wangqinghua committed
    }

    ionViewWillLeave() {
        this.footerView = false;
    }

    changeCheck(goods) {
        const data = {
            id: goods.id,
            amount: goods.amount,
            brand: goods.brand,
            officeId: goods.officeId,
            model: goods.model,
            specs1: goods.specs1,
            specs2: goods.specs2,
            specs3: goods.specs3,
            specs4: goods.specs4,
            specs5: goods.specs5,
        };
        let get = 0;
        let number;
        this.chooseObj.forEach((e, index) => {
            if (e.id == goods.id) {
                get = 1;
                number = index;
            }
        });
        if (get == 0) this.chooseObj.push(data);
        if (get == 1) this.chooseObj.splice(number, 1);
wangqinghua's avatar
wangqinghua committed
        this.checkAll = this.chooseObj.length === this.cartList.length;
wangqinghua's avatar
wangqinghua committed
    }

    //获取购物车列表
    getCart() {
        const data = {
wangqinghua's avatar
wangqinghua committed
            P_pageNumber: 1,
            P_pageSize: this.page.P_pageSize,
wangqinghua's avatar
wangqinghua committed
            status: 1
        };
        this.serveSer.shoppingCar(data).subscribe(
            (res) => {
                this.cartList = res.list;
wangqinghua's avatar
wangqinghua committed
                this.isLoad = false;
wangqinghua's avatar
wangqinghua committed
                this.cartList.forEach(e => {
                    e.checkbox = false;
                })
            }
        )
    }

wangqinghua's avatar
wangqinghua committed
    //下拉刷新
    doRefresh(e) {
        this.page.loadMore = true;
        this.infiniteScrollContent.inf.enable(true);
        const data = {
            P_pageNumber: 1,
            P_pageSize: this.page.P_pageSize,
            status: 1
        };
        this.serveSer.shoppingCar(data).subscribe(
            (res) => {
                this.cartList = res.list;
                this.page.total = res.data.total;
                timer(800).subscribe(() => {
                    this.commonSer.toast('刷新成功');
                    e.complete()
                });
            }
        )
    }

    doInfinite(e) {
        if (this.page.total == this.cartList.length) {
            console.log("没有更多了");
            this.page.loadMore = false;
            e.enable(false);
            return false;
        }
        this.page.P_pageNumber++;
        const data = {
            P_pageNumber: this.page.P_pageNumber,
            P_pageSize: this.page.P_pageSize,
            status: 1
        };
        this.serveSer.searchCheckList(data).subscribe(
            (res) => {
                this.page.total = res.data.total;
                res.list.forEach(e => {
                    this.cartList.push(e);
                });
                timer(800).subscribe(() => e.complete());
            }
        )
    }

wangqinghua's avatar
wangqinghua committed
    //选择规格 数量
    choose(item) {
wangqinghua's avatar
wangqinghua committed
        const data = {
wangqinghua's avatar
wangqinghua committed
            amount: item.amount,
            specs1: item.specs1,
            specs2: item.specs2,
            specs3: item.specs3,
            specs4: item.specs4,
            specs5: item.specs5,
wangqinghua's avatar
wangqinghua committed
        }
wangqinghua's avatar
wangqinghua committed
        let modal = this.modalCtrl.create(SpcesComponent, {goodsId: item.officeId, data: data});
wangqinghua's avatar
wangqinghua committed
        modal.onDidDismiss(res => {
            if (res) {
wangqinghua's avatar
wangqinghua committed
                const d = res;
wangqinghua's avatar
wangqinghua committed
                d.id = item.id;
wangqinghua's avatar
wangqinghua committed
                this.updateCart(d);
wangqinghua's avatar
wangqinghua committed
            }
        });
        modal.present();
    }

wangqinghua's avatar
wangqinghua committed
    //更新购物车
wangqinghua's avatar
wangqinghua committed
    updateCart(data) {
wangqinghua's avatar
wangqinghua committed
        this.serveSer.saveOrderSuppy(data).subscribe(
wangqinghua's avatar
wangqinghua committed
            (res) => {
wangqinghua's avatar
wangqinghua committed
                this.getCart();
            }
        )
    }

wangqinghua's avatar
wangqinghua committed
    //删除物品
wangqinghua's avatar
wangqinghua committed
    deletI(goods) {
wangqinghua's avatar
wangqinghua committed
        this.serveSer.deleteOrderSuppy(goods.id).subscribe(
wangqinghua's avatar
wangqinghua committed
            (res) => {
wangqinghua's avatar
wangqinghua committed
                this.getCart();
            }
        )
    }

wangqinghua's avatar
wangqinghua committed
    //全选
    selectAll(e) {
        this.chooseObj = [];
        if (e == false) {
            this.cartList.forEach(e => {
                e.checkbox = false;
            })
        } else {
            this.cartList.forEach(e => {
                e.checkbox = true;
                this.chooseObj.push(e);
            })
        }
    }

    //提交订单
    submit() {
        if (this.chooseObj.length == 0) {
            this.commonSer.toast('请选择申请物品')
            return false;
        }
        const data = {
            "status": 2,
            "orderSuppies": this.chooseObj
        };
        this.commonSer.alert('确定申请所选物品?', () => {
            this.serveSer.saveOrder(data).subscribe(
                (res) => {
                    if (res.errcode == 1000) {
                        this.commonSer.toast("提交申请成功");
                        this.getCart();
                    } else {
                        this.commonSer.toast(res.errmsg);
                    }
                }
            )
        });
    }

    //前往订单
    goOrder() {
        this.navCtrl.push(GoodsOrderPage);
    }

wangqinghua's avatar
wangqinghua committed
    //审核提交
    goVerify() {
        this.navCtrl.push(GoodsVerifyPage);
    }

wangqinghua's avatar
wangqinghua committed
}