productLogic.js 5.76 KB
import {observable,computed} from 'mobx'
import {
	_allCategoryShow,
	_categorySearhCondition,
	_findCommodityExtend,
	_addToCarByCenter,
	_addPk,
	_pk
} from "../integralMall/utils/AppService";
import {systemSite} from "../../index";
import {AsyncStorage} from 'react-native'
import { xnToast } from '../../utils/utils';
export default class productLogic {
	@observable
	activeCategory = 0
	@observable
	priceUp = true
	@observable
	saleUp = true
	@observable
	activeItem = null
	@observable
	activeBar = 0
	@observable
	comparedArray = []
	@observable
	filterActive = [0, 0]
	@observable
	dropActive = 0
	@observable
	showDrop = false
	@observable
	category = []
	@observable
	condition = []
	@observable
	list = []
	@observable
	loading = false
	@observable
	brandId = []
	@observable
	attributes = []
	@observable
	price = []
	@observable
	type = ""
	@observable
	sort = ""
	@observable
	pkList=[]
	@observable
	text=""
	@observable
	activeCategoryIndex=0
	@observable
	filterDisabled=false
	@observable
	conditionLoading=true
	@observable
	pageNumber=1
	@observable
	totalCount=0 //数据总数
	@observable
	supplierId=''
	async getCategory() {
		try {
			const data = await _allCategoryShow({type: "STANDARD"})
			data.result=[{id:"_null_",categoryName:"全部",categoryCode:"_null_"},...data.result]
			this.category.replace(data.result)
			if(!!!this.activeCategory){
				this.activeCategory = data.result[0].id
			}
			// const {id,tenantId,categoryName,pictureId,pictureUrl}=result
			if (data.error.length > 0) {
                xnToast(data.firstErrorMessage)
			}
		} catch (e) {

		}
	}

	async getList(flag) {
		
		if (this.loading || ((!!this.totalCount && this.pageNumber * 30 >= this.totalCount-0)&&flag)) {
			return
		}
		if (!!this.totalCount && (30 * this.pageNumber < this.totalCount-0)) {
			this.pageNumber = this.pageNumber + 1
		}
		if(!flag){
			//不是滚动页面
			this.pageNumber=1;
		}
		// debugger;
		this.loading = true
		const options = {
			operatingUnitId: systemSite.operatingUnitId,
			pageSize:30,
			pageNumber:this.pageNumber
		}
		const memberId=await AsyncStorage.getItem("memberId")
		if(memberId){
			options.memberId=memberId
		}
		if(this.activeCategory!=="_null_"){
			options.categoryIds= [this.activeCategory]
		}
		if (!!this.type) {
			console.log(this.type)
			options.sortColumn = this.type
			options.sortType = this.sort
		}
		if (this.brandId.length > 0) {
			options.brandIdList = this.brandId.peek()
		}
		if (this.attributes.length > 0) {
			options.categoryAttributeAndValuesList = this.attributes.peek()
		}
		if (this.price.length > 0) {
			options.priceRangeList = this.price.peek()
		}
		if(!!this.text){
			options.keyword=this.text
		}
		if(!!this.supplierId){
			options.supplierId=this.supplierId
		}
		try {
			console.log('options????????????????:',options);
			const data = await _findCommodityExtend(options)
			const {result,totalCount} = data
			console.log('result--',result);
			this.totalCount=totalCount
			// debugger;
			let array=[];
			console.log('flag===========',flag);
			if(flag){
				
				//分页
				if(result.length){
					array=this.list.slice().concat(result);
				}else{
					array=this.list.slice();
				}
			}else{
				array=result;
			}
			
			this.list.replace(array)
			// this.list.concat(result)
			console.log('this.list--',this.list);
		} catch (e) {

		}
		this.loading = false
	}

	async getCondition() {
		this.conditionLoading=true
		try {
			if(this.activeCategory==="_null_"){
				this.condition.clear()
				this.condition.push({name: "品牌", items: []})
				this.condition.push({name: "属性", items: []})
				this.condition.push({name: "价格", items: []})
				this.filterDisabled=true
				this.conditionLoading=false
				return
			}
			const result = await _categorySearhCondition({categoryId: this.activeCategory})
			if (result.errors.length > 0) {
                xnToast(result.firstErrorMessage)
			} else {
				const {brandsList, category, categoryPriceList, categoryAttributesList} = result
				this.filterDisabled=false
				if(brandsList.length===0&&categoryAttributesList.length===0&&categoryPriceList.length===0){
					this.filterDisabled=true
									}

				this.condition.clear()
				this.condition.push({name: "品牌", items: []})
				this.condition.push({name: "属性", items: []})
				this.condition.push({name: "价格", items: []})
				brandsList.map(v => this.condition[0].items.push(v))
				categoryAttributesList.map(v => this.condition[1].items.push(v))
				categoryPriceList.map(v => this.condition[2].items.push(v))
			}
		} catch (e) {
			console.log(e)
		}
		this.conditionLoading=false
	}

	async addCar(id, memberId,navigation) {
		try {
			const data = await _addToCarByCenter({
				commodityId: id,
				operatingUnitId: systemSite.operatingUnitId,
				storeId: systemSite.storeId,
				memberId: memberId
			})
			const {errors, firstErrorMessage,code} = data
			if(code){
				await AsyncStorage.removeItem("memberId")
				navigation.navigate("Login")
				return false
			}
			if (errors.length > 0) {
                xnToast(firstErrorMessage)
				return false
			} else {
                xnToast("添加成功")
				return true
			}
		} catch (e) {
			console.log(e)
		}
	}

	async addPk(id) {
		try {
			const data = await _addPk({
				commodityId: id,
				operatingUnitId: systemSite.operatingUnitId,
			})
			const {errors, firstErrorMessage,itemId} = data
			if (errors.length > 0) {
                xnToast(firstErrorMessage)
			} else {
				return itemId
			}
		} catch (e) {
			console.log(e)
		}
	}
	async pk(ids){
		try{
			const data=await _pk({
				operatingUnitId: systemSite.operatingUnitId,
				itemIdList:ids
			})
			const {errors, firstErrorMessage,result} = data
			if (errors.length > 0) {
                xnToast(firstErrorMessage)
			} else {
				this.pkList.replace(result)
			}
		}catch(e){
			console.log(e)
		}
	}
}