cart.js 2.05 KB
import urlAlias from '../url'
import request from '../request'

const { cartList, cartModi, cartDel, cartAdd } = urlAlias

const state = {
  cartList: [],
  checkedCartLst:[],
  buyItem:Object
}

const mutations = {
  INIT: (state, cartList) => {
    state.cartList = cartList
  },
  DEL: (state, index) => {
    state.cartList.splice(index, 1)
  },
  MODI: (state, args) => {
    state.cartList[args.index].num = args.num
  },

}

const actions = {
  // 获取购物车列表
    getCartList({ commit }, param) {
    return new Promise((resolve) =>  request({
      url: cartList,
      data: param,
      success: (res) => {
		let test = {
			isChecked: false,
			num:1,
			price:0
		}
        commit('INIT', res.data.data.list)
		resolve(res.data.data)
      },
    }))
  },
  // 修改购物车数量
  modiCart({ commit }, param) {
    const arg = Object.assign({ num: param.num }, param.args)
    delete param.args
    return new Promise((resolve) =>  request({
      url: cartModi,
      data: param,
      success: (res) => {
        commit('MODI', arg)
      },
      fail: (res) => {
      },
      complete: (res) => {
      },
    }))
  },
  // 删除购物车商品
  delCart({ commit }, param) {
    const arg = param.arg
    delete param.arg
    request({
      url: cartDel,
      data: param,
      success: (res) => {
        commit('DEL', arg)
      },
      fail: (res) => {
      },
      complete: (res) => {
      },
    })
  },
  // 添加到购物车
  addCart({ commit }, param) {
    console.log('请求接口开始')
    return new Promise((resolve) =>  request({
      url: cartAdd,
      data: param,
      success: (res) => {
        console.log('add-parm', param)
        console.log('addcart===>res.data===>', res.data)
        // commit('INIT', res.data.data)
		resolve(res.data)
      },
      fail: (res) => {
        console.log('添加购物车失败 === > ', res)
      },
      complete: (res) => {
        console.log('添加购物车完成 === > ', res)
      },
    }))
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}