import { observable , ObservableMap , action , runInAction , computed , decorate } from 'mobx' ; import { ILiProductInfo , IProductDetail } from '@/interfaces/product' ; import shopService from '@/services/shopService' ; import mockPromise from '@/utils/mockPromise' ; /**
 * 产品数据
 **/ export interface IProductStore { /** 产品数据源 */ productData : Map < number , ILiProductInfo > ; /** 列表页面数据 */ productListData : { ids : number [ ] ; page : number ; num : number ; count : number ; } ; /** 列表数据是否已经请求完 */ listIsEnd : boolean ; /** 产品详情 */ productDatailData : IProductDetail | { product_id : number ; } ; /** 获取产品数据, 默认请求第一页20条数据 */ fetchProductData : ( params ? : { page : number ; num ? : number ; } ) => Promise < any > ; /** 获取产品详情 */ fetchProductDetail : ( product_id : number ) => Promise < void > ; /** 设置商品上下架 */ setProductStatus : ( productId : number , status : 1 | 2 ) => Promise < any > ; /** 设置商品库存 */ setProductStock : ( productId : number , sku : string , stock : number ) => Promise < void > ; } class ProductStore implements IProductStore { productData : ObservableMap < number , ILiProductInfo > = observable . map ( { } , { deep : false } ) ; @ observable productListData = { ids : [ ] as number [ ] , page : 1 , num : 0 , count : 0 } ; @ computed get listIsEnd ( ) { if ( this . productListData . ids . length <= 0 ) { return false ; } return this . productListData . ids . length < this . productListData . page * 20 ; } @ action async fetchProductData ( params = { page : 1 } ) { const reqParams = Object . assign ( { num : 20 } , params ) ; const resData = await mockPromise ( { lists : [ { price : 2500 , status : 1 , sub_title : 'Milk Tea Series: Green Milk Tea' , thumb : 'https://www.leidenglai.com/image/weapp/server/product.jpg' , title : '小山绿奶茶' , product_id : 1 } , { price : 28000 , vip_price : 20000 , status : 2 , sub_title : 'Mellow Latte: Royal No.9 Latte' , thumb : 'https://www.leidenglai.com/image/weapp/server/product.jpg' , title : '皇家九号拿铁' , product_id : 2 } ] } ) ; const idList = resData . lists . map ( item => item . product_id ) ; runInAction ( ( ) => { this . productData . merge ( resData . lists . map ( item => { decorate ( item , { status : observable } ) ; return [ item . product_id , item ] ; } ) ) ; this . productListData . page = reqParams . page ; this . productListData . num = reqParams . num ; if ( reqParams . page === 1 ) { this . productListData . ids = idList ; } else { this . productListData . ids . push ( ... idList ) ; } } ) ; } @ observable . ref productDatailData : any = { product_id : 0 } ; @ action async fetchProductDetail ( productId ) { const resData = await shopService . fetchProductDetail ( { product_id : productId } ) ; const resContentData = await shopService . fetchProductDesc ( { product_id : productId } ) ; runInAction ( ( ) => { this . productDatailData = decorate ( Object . assign ( resData , resContentData ) , { stock_lists : observable } ) ; } ) ; } @ action async setProductStatus ( productId , status ) { try { await shopService . setProductStatus ( { product_id : productId , status } ) ; runInAction ( ( ) => { this . productData . get ( productId ) ! . status = status ; } ) ; } catch ( err ) { console . log ( '修改失败：' , err ) ; } } @ action async setProductStock ( productId , sku , stock ) { await shopService . fetchUpdateSkuStock ( { product_id : productId , product_sku : sku , number : stock } ) ; const stock_lists = this . productDatailData . stock_lists as IProductDetail [ 'stock_lists' ] ; const index = stock_lists . findIndex ( item => item . product_sku === sku ) ; runInAction ( ( ) => { this . productDatailData . stock_lists [ index ] . number = stock ; } ) ; } } export default ProductStore ;