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

const { editAddress, addressList, getAddress, getDefaultAddress, delAddress } = 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 }) {
    request({
      url: addressList,
      success: (res) => {
        commit('LIST', res.data.data)
      },
      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,
        })
      },
    }))
  },
  delete(_, param) {
    return new Promise((resolve) => request({
      url: delAddress,
      data: param,
      success: (res) => {
        resolve()
      },
      fail: () => {
        uni.showModal({
          content: '删除地址失败',
          showCancel: false,
        })
      },
    }))
  },
}

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