index.js 1.44 KB
import {
  fetchUserBaseInfo,
  fetchUserBaseInfoByPhone,
} from '@/api/base/customer';
import { GET_CUSTOMER_BASEINFO } from './mutation-types';

const state = () => ({
  areaName: '',
  createAt: '',
  customerHead: '',
  customerName: '',
  customerPhone: '',
  customerSex: '',
  customerType: '',
  id: null,
  isFirstRecharge: null,
  state: '',
});

const getters = {
  customerBaseInfo: state => state,
};

const actions = {
  fetchUserBaseInfo({ commit }, entity) {
    fetchUserBaseInfo(entity)
      .then(res => {
        const userInfo = res.data;
        if (userInfo) {
          commit(GET_CUSTOMER_BASEINFO, userInfo);
        } else {
          commit(GET_CUSTOMER_BASEINFO, state());
        }
      })
      .catch(err => {
        console.error(err);
        commit(GET_CUSTOMER_BASEINFO, state());
      });
  },
  fetchUserBaseInfoByPhone({ commit }, entity) {
    fetchUserBaseInfoByPhone(entity)
      .then(res => {
        const userInfo = res.data;
        if (userInfo) {
          commit(GET_CUSTOMER_BASEINFO, userInfo);
        } else {
          commit(GET_CUSTOMER_BASEINFO, state());
        }
      })
      .catch(err => {
        console.error(err);
        commit(GET_CUSTOMER_BASEINFO, state());
      });
  },
};

const mutations = {
  [GET_CUSTOMER_BASEINFO](state, userInfo) {
    Object.keys(userInfo).map(key => {
      state[key] = userInfo[key];
    });
  },
};

export default {
  state,
  getters,
  actions,
  mutations,
};