import { removeToken } from '@/config/auth.js' import { menuList as getMenuList, getShoppingCount, getSmallCount, } from '@/config/goods' import { getInfo, logout } from '@/config/login.js' const getDefaultState = () => { return { user: '', shopCarLength: 0, smallCarLength: 0, specialAreaAuth: null, } } const state = getDefaultState() const mutations = { SET_USER: (state, user) => { state.user = user }, SET_SMALL_CAR_LENGTH: (state, data) => { state.smallCarLength = data }, SET_SHOP_CAR_LENGTH: (state, data) => { state.shopCarLength = data }, SET_SPECIAL_AREA_AUTH: (state, data) => { state.specialAreaAuth = data }, } const actions = { // 获取用户信息 GetInfo({ commit, state }) { return new Promise((resolve, reject) => { getInfo() .then(res => { if (res) { commit('SET_USER', res.data) uni.setStorageSync('User', res.data) resolve(res) } }) .catch(error => { reject(error) }) }) }, // 退出登录 LogOut({ commit, state }) { return new Promise((resolve, reject) => { logout(state.token) .then(() => { removeToken() resolve() }) .catch(error => { reject(error) }) }) }, getCarLength({ commit }, data) { return new Promise((resolve, reject) => { let pkCountry let userInfo = uni.getStorageSync('User') if (data == 1) { if (uni.getStorageSync('pkCountry')) { pkCountry = uni.getStorageSync('pkCountry') } else { pkCountry = userInfo.pkSettleCountry } } else { pkCountry = userInfo.pkSettleCountry } let obj = { specialArea: data, pkCountry: pkCountry } getShoppingCount(obj).then(res => { if (res.code == 200) { getSmallCount(obj).then(res1 => { if (res.code == 200) { commit('SET_SMALL_CAR_LENGTH', res1.data.smallCount) commit('SET_SHOP_CAR_LENGTH', res.data.cont) resolve(res) } }) } }) }) }, getSpecialAreaAuth({ commit, state }) { return new Promise((resolve, reject) => { if (state.specialAreaAuth) { resolve(state.specialAreaAuth) } else { getMenuList().then(res => { if (res.code !== 200) { return reject(res) } const keyList = res.data.map(item => item.menuKey) commit('SET_SPECIAL_AREA_AUTH', keyList) resolve(keyList) }) } }) }, setSmallCarLength({ commit }, data) { commit('SET_SMALL_CAR_LENGTH', data) }, setShopCarLength({ commit }, data) { commit('SET_SHOP_CAR_LENGTH', data) }, } export default { state, mutations, actions, }