uni-popup手撸了一个菜鸟上门取件时间选择器

引言

 近期做的项目有个需求是做一个类似菜鸟的取件时间选择器,去找了很久没找到合适的,没办法只能自己收撸,经过好几个小版本修改之后也算是定型了,这里总结一篇文档备忘,把源码贴出来后续方便后续copy

技术

uniapp + vue2 + uni-popup

兼容

因为目前我的项目只用到这三端,其他的都还没测,所以兼容不保证

  • 支付宝小程序开发者工具popup弹出来会直接滚到最顶部,显示异常,但真机上面没问题,可以不用管
环境 兼容
支付宝小程序
微信小程序
H5

菜鸟上门时间选择器

uni-popup手撸了一个菜鸟上门取件时间选择器_第1张图片

需求分析:

1、弹窗从底部弹出

  • 点击蒙层不可关闭
  • 弹窗header左侧title , 右侧关闭按钮

2、左侧日期选择器

  • 显示近3天日期
  • 显示(今天、明天、周一、周二等)

3、右侧时间选择器

  • 可选时间可配置
  • 过期时间显示 “已过期”
  • 选中效果
  • 当前已无可选时间,应该删除今天日期,只可以选未来日期

代码实现:

1.popup弹窗

先做一下基础布局,简单的分成上左右三大块,并做一些基础的配置

uni-popup手撸了一个菜鸟上门取件时间选择器_第2张图片




2.日期+时间选择器

按照需求我重新设计了一下功能及交互

日期选择器

  • 日期可配置,支持显示最近n天日期
  • 显示今天、明天、后台及工作日
  • 默认选中当日(今天)

时间选择器

基础功能

  • 删除过期时间
  • 今日所有可选日期都过期之后删除日期选框(今天)选项
  • 选中时间后面打钩,并关闭弹窗

可选功能

  • 显示已过期时间 (逻辑几个版本之前已经删除了,现在只剩类名,需要的同学可以大概看下代码把它加上或者评论区留个言我把给你找找代码 , 功能样式就类似菜鸟)
  • 直接删除已过期时间

先看效果

核心逻辑:

1、生成左侧日期列表

// 生成时间选择器 最近n天的时间
/**
*@n {Number} : 生成的天数
*
*/
setRecentData(n) {
	const oneDaySeconds = 60 * 1000 * 60 * 24;
	const today = +new Date();
	let list = [];
	for (let i = 0; i < n; i++) {
		let formatTime = this.formatTime_zh(today + oneDaySeconds * i);
		list.push({
			...formatTime,
			week: i == 0 ? '今天' : i == 1 ? '明天' : formatTime.week
		});
	}
        //设置一下默认选中日期
	this.selectedDate = list[0];
	return list;
},
// 时间处理函数
formatTime_zh(date){
	date = new Date(date);
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const weekDay = date.getDay();
	const formatNumber = (n) => {
		n = n.toString();
		return n[1] ? n : '0' + n;
	};
        const numToTxt = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
	return {
		date_zh: `${formatNumber(month)}月${formatNumber(day)}日`,
		date_en: `${year}/${formatNumber(month)}/${formatNumber(day)}`,
		week: numToTxt[weekDay]
	};
},

最终数据格式如图:

uni-popup手撸了一个菜鸟上门取件时间选择器_第3张图片

2、判断时间有没有过期

因为考虑到取件没有那么快,至少要提前半小时下单,所以就有了下面的逻辑(我这里是90分钟)

  • 传入 09:00-10:00 格式时间区间
  • 截取过期时间, 和当前时间做对比
  • 判断已过期 、即将过期 、未过期
/**
* @return {Number} 1:已过期 , 2:即将过期 , 3:未过期
* @time   {String} 09:00-10:00
*/
checkRemainingMinute(time) {
	if (!time) return;
        //过期时间
	const outTime = time.toString().split('-')[1];
        // 这里兼容一下iphone,iphone不支持yyyy-mm-dd hh:mm 格式时间 ,分隔符换为 /
	const fullYearDate = formatMinute(new Date(), '/');
	const now = new Date(fullYearDate);
	const dateTime = this.currentDate + ' ' + outTime;
	const check = new Date(dateTime);
	const difference = check - now;
	const minutes = difference / (1000 * 60);
	// minutes <= 0  : 已过期    --> 1
	// minutes <= 90 : 即将过期  --> 2
	// minutes >  0  : 未过期     --> 3
	return minutes <= 0 ? 1 : minutes <= 90 ? 2 : 3;
}
  /**
   * @description yyyy-mm-dd hh:mm
   * @author wangxinu
   * @export
   * @param {*} cent
   * @returns
   */
  formatMinute: (date, separator = '-') => {
    date = new Date(date);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    const formatNumber = (n) => {
      n = n.toString();
      return n[1] ? n : '0' + n;
    };
    return `${formatNumber(year)}${separator}${formatNumber(month)}${separator}${formatNumber(
      day,
    )} ${formatNumber(hour)}:${formatNumber(minute)}`;
  },

3、通过计算属性获取有效时间(即右侧列表展示即将过期的和未过期的时间)

data(){
    return {
    	appointment: [
		'08:00-09:00',
		'09:00-10:00',
		'10:00-11:00',
		'11:00-12:00',
		'12:00-13:00',
		'13:00-14:00',
		'14:00-15:00',
		'15:00-16:00',
		'16:00-17:00',
		'17:00-18:00',
		'18:00-19:00',
		'19:00-20:00'
	]
    }
},
computed: {
	// 有效取件时间
   effectAppointmentTime() {
        //取件时间列表
	const appointment = this.appointment;
	// 未来日期返回全部
	if (this.selectedDate.date_en != this.currentDate) {
		return appointment;
	}
        // 当日只返回有效时间
	let list = appointment.filter((item) => this.checkRemainingMinute(item) != 1);
	// 当天取件时间长度>0 添加立即上门
	if (list.length > 0) {
		list.unshift('立即上门');
	}
	return list;
   }
},

4、通过计算属性获取有效日期

computed: {
	// 有效日期
   effectRecentDate() {
        //查看有效时间列表
	const effectAppointmentTime = this.effectAppointmentTime;
	// 当日取件时间全部失效
            if (effectAppointmentTime.length == 0) {
                //删除(今日)
		this.recentDateList.splice(0, 1);
                //修改默认选中日期
		this.selectedDate = this.recentDateList[0];
		return this.recentDateList;
            } else {
		return this.recentDateList;
            }
	},
},

5、日期或时间选中函数

	// 时间选择器修改函数
	timeChange(date, type) {
		const dateList = this.recentDateList;
		if (type === 'date') {
			// 选择日期
			this.selectedDate = date;
			this.selectedTime = '';
		} else {
			// 选择时间
			this.selectedTime = date;
			if (this.selectedDate.date_zh == '') {
				this.selectedDate = dateList[0];
			}
                        this.handleClose();
                       	this.$emit('selectTime', this.selectedDate, this.selectedTime);
		}
	},

源码及使用

使用:



源码:




TODO:

  • 时间区域打开显示对应选中时间位置
  • 右侧时间列表改后台返回

以上就是uni-popup手撸了一个菜鸟上门取件时间选择器的详细内容,更多关于uni popup取件时间选择器的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(uni-popup手撸了一个菜鸟上门取件时间选择器)