malls.js 11.1 KB
import dateArr from '../dateData.js';
import { MALLS } from '../mutation-types.js' 

// initial state
const state = {
	list: [],	// 门店列表
	checkedMallId: null,	// 单选门店
	multiCheckedMallId: [],	// 多选门店
	gateList: [],	// 出入口列表
	checkedGateId: null,	// 单选出入口
	multiCheckedGateId: [],	// 多选出入口
	customTimeData: [],	// 自定义时间数据
	selectedCustomStartDate: '',	// 自定义开始时间字符串
	selectedCustomEndDate: '',	// 自定义结束时间字符串
	pickerCustomStartDate: [],	// 自定义开始时间picker下标
	pickerCustomEndDate: [],	// 自定义结束时间picker下标
	dayDateData: [],	// 天级时间数据
	selectedDayTime: '',	// 选中时间字符串
	pickerDayDate: []	,// 选中天时间picker下标,
	startDayTime: '',	// 开始日期
	endDayTime: '',	// 结束日期
	pickerStartDayDate: [],	// 结束日期
	pickerEndDayDate: [],	// 结束日期
}

// getter
const getter = {}

// actions
const actions = {
	getMallList({ commit }, { data, header, isMultiple = false }) {
		uni.request({
			url: window.url + "/malls",
			data: data,
			header: header,
			method: 'GET',
			success: (res) => {
				console.log('store res', res)
				let result = res.data;
				let buildData = result.data.map(item => {
					item._selected = false;
					return item;
				})
				// 默认选中一个/多个
				let mallId = null
					, multiMallId = [];
					multiMallId = buildData.map((item, index) => {
						if(index == 0) {
							mallId = {
								id: item.id,
								name: item.name
							};
						}
						return {
							id: item.id,
							name: item.name
						};
					});
				// console.log('store actions', isMultiple, mallId, multiMallId)
				commit(MALLS.CHECKED_MALL_ID, mallId)
				if(isMultiple) {
					commit(MALLS.CHECKED_MULTI_MALL_ID, multiMallId)
				}
				commit(MALLS.SET_MALLS, buildData)
			}
		})
	},
	getGateList({ commit }, { data, header, isMultiple = false }) {
		uni.request({
			url: window.url + "/gates",
			data: data,
			header: header,
			method: 'GET',
			success: (res) => {
				console.log('store gates res', res)
				let result = res.data;
				let buildGateData = result.data.map(item => {
					item._selected = false;
					return item;
				})
				// 默认选中一个/多个
				let gateId = null
					, multiGateId = [];
					multiGateId = buildGateData.map((item, index) => {
						if(index == 0) {
							gateId = {
								id: item.id,
								name: item.name
							};
						}
						return {
							id: item.id,
							name: item.name
						};
					});
				// console.log('store gate actions', isMultiple, gateId, multiGateId)
				commit(MALLS.CHECKED_GATE_ID, gateId)
				if(isMultiple) {
					commit(MALLS.CHECKED_MULTI_GATE_ID, multiGateId)
				}
				commit(MALLS.SET_GATES, buildGateData)
			}
		})
	},
	getDateData({ commit }, frontDay) {
		dateArr.getCustomTime(customTimeData => {
			let startDate = null
				, endDate = null
				, startTime = {}
				, endTime = {}
				, times = []
				, customStartTimes = {}
				, customEndTimes = {}
				, customPickerStartIdx = []
				, customPickerEndIdx = [];
				endDate = new Date();
				startDate = frontTime(endDate, frontDay);
				endTime = formatterCustomTime(endDate, true);
				startTime = formatterCustomTime(startDate, true);
				customPickerStartIdx = computedPickerIdx(startTime);
				customPickerEndIdx = computedPickerIdx(endTime);
				customStartTimes = `${startTime['Y']}-${startTime['M']}-${startTime['D']} ${startTime['h']}:${startTime['m']}:${startTime['s']}`;
				customEndTimes = `${endTime['Y']}-${endTime['M']}-${endTime['D']} ${endTime['h']}:${endTime['m']}:${endTime['s']}`;
				
			// console.log('customTimeData', customTimeData)
			// console.log('store times', customStartTimes, customEndTimes, customTimeData, customPickerStartIdx, customPickerEndIdx);
			commit(MALLS.SET_TIMEDATA, customTimeData);	// 自定义时间数据
// 			commit(MALLS.SET_SELECTED_CUSTOM_START_TIME, customStartTimes);	// 自定义开始时间
// 			commit(MALLS.SET_SELECTED_CUSTOM_END_TIME, customEndTimes);	// 自定义结束时间
// 			commit(MALLS.SET_PICKER_CUSTOM_START_IDX, customPickerStartIdx);	// 自定义时间组件下标
// 			commit(MALLS.SET_PICKER_CUSTOM_END_IDX, customPickerEndIdx);	// 自定义时间组件下标
		})
		// 日级
		dateArr.getDayTime(dayData => {
			let dayTime = {}
				, dayTimesText = ''
				, pickerIdx = []
				dayTime = formatterDayTime(new Date());
				pickerIdx = computedPickerIdx(dayTime);
				dayTimesText = `${dayTime['Y']}-${dayTime['M']}-${dayTime['D']}`;
			commit(MALLS.SET_DAY_DATA, dayData);	// 日级时间数据
			commit(MALLS.SET_SELECTED_DAY_TIME, dayTimesText);	// 日级时间文本
			commit(MALLS.SET_PICKER_DAY_IDX, pickerIdx)	// 日级时间组件下标
			// pickerStartDayDate pickerEndDayDate
		})
		// 开始结束日期
		dateArr.getStartEndTime(dayData => {
			let startDate = null
				, endDate = null
				, startTime = ''
				, endTime = ''
				, st = ''
				, et = ''
				, pickerSt = []
				, pickerEt = [];
				endDate = new Date();
				startDate = frontTime(endDate, 7);
				endTime = formatterDayTime(endDate);
				startTime = formatterDayTime(startDate);
				pickerSt = computedPickerIdx(startTime);
				pickerEt = computedPickerIdx(endTime);
				console.log('开始结束日期:', pickerSt, pickerEt)
				st = `${startTime['Y']}-${startTime['M']}-${startTime['D']}`;
				et = `${endTime['Y']}-${endTime['M']}-${endTime['D']}`;
			commit(MALLS.SET_SELECTED_CUSTOM_START_TIME, st);	// 自定义开始时间文本
			commit(MALLS.SET_SELECTED_CUSTOM_END_TIME, et);
			commit(MALLS.SET_PICKER_START_IDX, pickerSt);
			commit(MALLS.SET_PICKER_END_IDX, pickerEt)
		})
	},
	// 存储新值
	updateDayDate({ commit }, { dayTimeText, dayPickerIdx }) {
		commit(MALLS.SET_SELECTED_DAY_TIME, dayTimeText);	// 日级时间文本
		commit(MALLS.SET_PICKER_DAY_IDX, dayPickerIdx)	// 日级时间组件下标
	},
	updateCustomDate({ commit }, { customTimeStartText, customTimeEndText, customPickerStartIdx, customPickerEndIdx }) {
		customTimeStartText && commit(MALLS.SET_SELECTED_CUSTOM_START_TIME, customTimeStartText);	// 自定义开始时间文本
		customTimeEndText && commit(MALLS.SET_SELECTED_CUSTOM_END_TIME, customTimeEndText);	// 自定义结束时间文本
		customPickerStartIdx && commit(MALLS.SET_PICKER_CUSTOM_START_IDX, customPickerStartIdx)	// 自定义开始时间组件下标
		customPickerEndIdx && commit(MALLS.SET_PICKER_CUSTOM_END_IDX, customPickerEndIdx)	// 自定义结束时间组件下标
	},
	updateCheckedMall({ commit }, mallId) {
		commit(MALLS.CHECKED_MALL_ID, mallId)
	},
	updateMultiCheckedMall({ commit }, multiMallId) {
		commit(MALLS.CHECKED_MULTI_MALL_ID, multiMallId)
	},
	updateCheckedGate({ commit }, gateId) {
		commit(MALLS.CHECKED_GATE_ID, gateId)
	},
	updateMultiCheckedGate({ commit }, multiGateId) {
		commit(MALLS.CHECKED_MULTI_GATE_ID, multiMallId)
	},
	updateStartEndDate({ commit }, { startDate, endDate, startPickerIdx, endPickerIdx }) {
		startDate && commit(MALLS.SET_START_DATE, startDate)
		endDate && commit(MALLS.SET_END_DATE, endDate)
		startPickerIdx && commit(MALLS.SET_PICKER_START_IDX, startPickerIdx)
		endPickerIdx && commit(MALLS.SET_PICKER_END_IDX, endPickerIdx)
	}
}

// 默认15天
function frontTime(currentTime, frontDay = 15) {
	return new Date(currentTime.getTime() - frontDay * 24 * 60 * 60 * 1000);
}

// 格式化日期
function formatterCustomTime(date) {
	let baseFormatter = {
		Y: date.getFullYear(),
		M: date.getMonth() + 1,
		D: date.getDate(),
		h: date.getHours(),
		m: date.getMinutes(),
		s: date.getSeconds()
	}
	let resFormatterTime = {};
	Object.keys(baseFormatter).forEach(k => {
		if(baseFormatter[k]) {
			resFormatterTime[k] = (baseFormatter[k] + '').replace(/(?=\b\d\b)/g, '0')
		}
	})
	return resFormatterTime;
}

function formatterDayTime(date) {
	let baseFormatter = {
		Y: date.getFullYear(),
		M: date.getMonth() + 1,
		D: date.getDate()
	}
	let resFormatterTime = {};
	Object.keys(baseFormatter).forEach(k => {
		resFormatterTime[k] = (baseFormatter[k] + '').replace(/(?=\b\d\b)/g, '0')
	})
	return resFormatterTime;
}

// 处理日期组件保存的下标值 2019-04-04 10:10:20 格式
function dealTimeIdx(timeStr) {
	return timeStr.replace(/\-|\s|\:/g, ',').split(',')
}

// 计算 awesome-picker 下标
function computedPickerIdx(dateObject) {
	let temp = [];
	Object.keys(dateObject).forEach(key => {
		switch(key) {
			case 'Y':
				temp.push({
					index: dateObject[key] - 1999,
					value: dateObject[key] + '年'	// 拼接年月日时分秒
				})
				break;
			// case ''
			default:
				temp.push({
					index: dateObject[key] - 1,
					value: dateObject[key] + (key === 'M' ? '月' : key === 'D' ? '日' : '' )
				})
				break;
		}
	})
	return temp;
}

function getGate(data, header, isMultiple) {
	uni.request({
		url: window.url + "/gates",
		data: data,
		header: header,
		method: 'GET',
		success: (res) => {
			console.log('store gates res', res)
			let result = res.data;
			// 默认选中一个/多个
			let gateId = null
				, multiGateId = [];
				multiGateId = result.data.map((item, index) => {
					if(index == 0) {
						gateId = {
							id: item.id,
							name: item.name
						};
						
					}
					return item.id;
				});
			console.log('store actions', isMultiple, gateId, multiGateId)
			commit(MALLS.CHECKED_GATE_ID, { gateId })
			if(isMultiple) {
				commit(MALLS.CHECKED_MULTI_GATE_ID, multiMallId)
			}
			commit(MALLS.SET_GATES, result.data)
		}
	})
}

// mutations
const mutations = {
	// 门店
	[MALLS.SET_MALLS](state, malls) {
		state.list = malls
	},
	[MALLS.CHECKED_MALL_ID] (state, mallId) {
		state.checkedMallId = mallId;
	},
	[MALLS.CHECKED_MULTI_MALL_ID] (state, multiMallId) {
		state.multiCheckedMallId = multiMallId;
	},
	// 出入口
	[MALLS.SET_GATES](state, gates) {
		state.gateList = gates
	},
	[MALLS.CHECKED_GATE_ID] (state, gateId) {
		state.checkedGateId = gateId;
	},
	[MALLS.CHECKED_MULTI_GATE_ID] (state, multiGateId) {
		state.multiCheckedGateId = multiGateId;
	},
	// 日期
	[MALLS.SET_TIMEDATA] (state, timeData) {
		state.customTimeData = timeData;
	},
	[MALLS.SET_SELECTED_CUSTOM_START_TIME] (state, time) {
		// state.selectedCustomStartDate = time
		state.startDayTime = time
	},
	[MALLS.SET_SELECTED_CUSTOM_END_TIME] (state, time) {
		// state.selectedCustomEndDate = time
		state.endDayTime = time
	},
	[MALLS.SET_PICKER_CUSTOM_START_IDX] (state, pickerCustomStartIdx) {
		state.pickerCustomStartDate = pickerCustomStartIdx
	},
	[MALLS.SET_PICKER_CUSTOM_END_IDX] (state, pickerCustomEndIdx) {
		state.pickerCustomEndDate = pickerCustomEndIdx
	},
	[MALLS.SET_SELECTED_DAY_TIME] (state, dayTime) {
		state.selectedDayTime = dayTime
	},
	[MALLS.SET_PICKER_DAY_IDX] (state, pickerIdx) {
		state.pickerDayDate = pickerIdx
	},
	[MALLS.SET_DAY_DATA] (state, dayData) {
		state.dayDateData = dayData
	},
	// 开始结束日期
	[MALLS.SET_START_DATE] (state, startDate) {
		state.startDayTime = startDate
	},
	[MALLS.SET_END_DATE] (state, endDate) {
		state.endDayTime = endDate
	},
	[MALLS.SET_PICKER_START_IDX] (state, pickerStartIdx) {
		state.pickerStartDayDate = pickerStartIdx
	},
	[MALLS.SET_PICKER_END_IDX] (state, pickerEndIdx) {
		state.pickerEndDayDate = pickerEndIdx
	}
	
}

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