shoppingCarLogic.js 2.25 KB
import {observable,computed} from 'mobx'
import {_getCarList, _carUpdate, _carDelete} from "../integralMall/utils/AppService";
import {AsyncStorage} from 'react-native';
import {systemSite} from "../../index";
import carCount from '../logic/carCountLogic'
import { xnToast } from '../../utils/utils';

class shoppingCarLogic {
	@observable
	itemList = []
	@observable
	edit = false
	@observable
	loading = false
	@observable
	chooseArray = []
	@observable
	carLength=0
	@observable
	isLogin=false
	@computed
	get check(){
		if(this.chooseArray.length>0){
			return true
		}
		return false
	}
	@computed
	get isAllChoose(){
		console.log('count===>',this.chooseCount,carCount.count);
		
		if(this.chooseCount===carCount.count){
			return true
		}
		return false
	}
	@computed
	get chooseCount(){
		let count=0
		if(this.chooseArray.length>0){
			for(let v of this.chooseArray){
				count=v.quantity+count
			}
		}
		return count
	}
	async getList(memberId,navigation) {
		this.loading = true
		try {
			const {operatingUnitId, storeId} = systemSite
			const data = await _getCarList({memberId, operatingUnitId, storeId})
			const {errors, firstErrorMessage, supplierCartList,code} = data
			if(code){
				await AsyncStorage.removeItem("memberId")
				navigation.navigate("Login")
				return
			}
			if (errors.length > 0) {
                xnToast(firstErrorMessage)
			} else {
				this.itemList.replace(supplierCartList)
				this.carLength=0
				for(let v of this.itemList){
				this.carLength=this.carLength+v.cartList.length
				}
			}
		} catch (e) {
			console.log(e)
		}
		this.loading = false
	}

	async carUpdate(id, quantity) {
		try {
			const data = await _carUpdate({
				id,
				quantity,
			})
			const {errors, firstErrorMessage} = data
			if (errors.length > 0) {
                xnToast(firstErrorMessage)
				return false
			} else {
				return true
			}
		} catch (e) {
			console.log(e)
		}
	}

	async del() {
		try {
			const ids = this.chooseArray.map(v => v.id)
			const data = await _carDelete({ids})
			this.chooseArray.clear()
			const {errors, firstErrorMessage} = data
			if (errors.length > 0) {
                xnToast(firstErrorMessage)
				return false
			} else {
				return true
			}
		} catch (e) {
			console.log(e)
		}
	}
}

export default new shoppingCarLogic()