address.js 2.03 KB
import urlAlias from '../url'
import request from '../request'

const { editAddress, addressList, getAddress, getDefaultAddress } = urlAlias

const state = { list: [], detail: {}, defaultInfo: {} }

const mutations = {
  LIST: (state, list) => {
    state.list = list
  },
  DETAILS: (state, details) => {
    state.details = details
  },
  DEFAULT: (state, data) => {
    state.defaultInfo = data
  },
}

const actions = {
  edit(mutations, param, success) {
    return new Promise((resolve) => request({
      url: editAddress,
      data: param,
      success: () => {
        resolve()
      },
      fail: () => {
        uni.showModal({
          content: '编辑失败',
          showCancel: false,
        })
      },
      complete: (res) => {
        console.log('complete status === > ', res)
      },
    }))
  },
  list({ commit },param) {
    request({
      url: addressList,
	  data: param,
      success: (res) => {
        commit('LIST', res.data.data.list)
      },
      fail: () => {
        uni.showModal({
          content: '获取地址列表失败',
          showCancel: false,
        })
      },
    })
  },
  details({ commit }, param) {
    return new Promise((resolve, reject) => request({
      url: getAddress,
      data: param,
      success: (res) => {
        commit('DETAILS', res.data.data)
        resolve({
          code: res.data.code,
          data: res.data.data,
        })
      },
      fail: () => {
        uni.showModal({
          content: '获取地址信息失败',
          showCancel: false,
        })
      },
    }))
  },
  default({ commit }, param) {
    return new Promise((resolve, reject) => request({
      url: getDefaultAddress,
      data: param,
      success: (res) => {
        resolve({
          code: res.data.code,
          data: res.data.data,
        })
      },
      fail: () => {
        uni.showModal({
          content: '获取默认地址信息失败',
          showCancel: false,
        })
      },
    }))
  },
}

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