首页/新闻资讯/正文详情

小程序时间切换器(uin-appX、uvue、uts、vue3)

发布时间:2026/11/30 1:09:54 来源:云帆数科 栏目:资讯中心
小程序时间切换器(uin-appX、uvue、uts、vue3)
template view classtime-switch-wrap !-- 顶部分段标签 activeKey 为u-tabs标准绑定字段 -- u-tabs :classtabsClass :listtabList :activeStylemergedActiveStyle :inactiveStylemergedInactiveStyle changeonChange / !-- 导航 -- view classdate-nav-1 u-icon v-ifactiveType ! custom classnav-left nameplay-left-fill size16 clickprevDate / view classdate-text clickopenDatePopup{{ displayDate }}/view u-icon v-ifactiveType ! custom classnav-right nameplay-right-fill size16 clicknextDate / /view !-- 选择日期 -- !-- 底部弹出层 -- view v-ifpopupShowactiveType day classpopup-calendar u-calendar :minDateminCalendarDate monthNum121 :disabledFunallDateCanSelect :showshow confirmonDaySelect / /view !-- 周选择弹窗区间选择 -- view v-ifpopupShowactiveType week classpopup-week u-calendar v-modelselectDay modesingle :showshow title选择任意一天自动匹配完整周一~周日 confirmonWeekSelect closeclosebtn / /view !-- 年月选择弹窗 -- view v-ifpopupShowactiveType month classpopup-month u-picker :showshow v-modelmonthPickerValue :columnsmonthColumns confirmonMonthSelect / /view !-- 年份选择弹窗 -- view v-ifpopupShowactiveType year classpopup-year u-picker :showshow v-modelyearPickerValue :columnsyearColumns confirmonYearSelect / /view !-- 自定义时间可自行扩展区间日历 -- view v-ifpopupShowactiveType custom classpopup-custom u-calendar closeclosebtn :minDateminCalendarDate monthNum121 v-modelweekRange allowSameDay :showshow moderange confirmonCustomSelect / /view /view /template script setup languts import { ref, computed, defineEmits, watch, withDefaults } from vue import { dayuts } from /uni_modules/lime-dayuts; export type TimeDimension day | week | month | year | custom const tabList [ { name: 日, value: day as TimeDimension }, { name: 周, value: week as TimeDimension }, { name: 月, value: month as TimeDimension }, { name: 年, value: year as TimeDimension }, { name: 自定义, value: custom as TimeDimension }, // { name: 自定义wwww, value: custom as TimeDimension }, ] const show ref(false) // 只定义一次Props删除下方重复interface Props interface Props { modelValue : TimeDimension currentDate : string | Date activeStyle ?: Recordstring, string inactiveStyle ?: Recordstring, string enableTabsClass ?: boolean } const props withDefaults(definePropsProps(), { // 【内置默认激活样式】 activeStyle: () ({ color: #1F2329, fontWeight: bold, background: #eff2f7, }), // 【内置默认未激活样式】 inactiveStyle: () ({ fontWeight: 400, color: #1F2329, }), enableTabsClass: false }) const emit defineEmits{ update:modelValue : [val: TimeDimension] update:currentDate : [date: string] change : [ type: TimeDimension, baseDate: string, range?: { start ?: string end ?: string yearMonth ?: string year ?: number } ] }() const tabsClass computed(() { return props.enableTabsClass ? u-tabs-f : }) const baseDate ref(dayuts(props.currentDate)) const popupShow ref(false) const activeType refTimeDimension(props.modelValue) const minCalendarDate computed(() { // 复制一份日期避免修改原baseDate const minDay baseDate.value.subtract(120, month) return minDay.format(YYYY-MM-DD) }) const mergedActiveStyle computed(() ({ ...props.activeStyle })) const mergedInactiveStyle computed(() ({ ...props.inactiveStyle })) const closebtn () { popupShow.value false } const onChange (e) { baseDate.value dayuts(props.currentDate) console.log(e, e) activeType.value e.value // 同步父组件选中tab emit(update:modelValue, e.value) const baseStr baseDate.value.format(YYYY-MM-DD) emit(update:currentDate, baseStr) if (activeType.value week) { const start getWeekStart(baseDate.value).format(YYYY-MM-DD) const end getWeekEnd(baseDate.value).format(YYYY-MM-DD) emit(change, activeType.value, baseStr, { start, end }) } else if (activeType.value month) { const yearMonth baseDate.value.format(YYYY-MM) emit(change, activeType.value, baseStr, { yearMonth }) } else if (activeType.value year) { const year baseDate.value.year() emit(change, activeType.value, baseStr, { year }) } else if (activeType.value custom) { // 自定义默认当天开始、当天结束 const start baseStr const end baseStr weekRange.value [baseStr, baseStr] emit(change, activeType.value, baseStr, { start, end }) } else { // day 无额外参数 emit(change, activeType.value, baseStr) } } const handleWeekDisabled (date : Date) : boolean { const target dayuts(date) const selectStart weekRange.value[0] ? dayuts(weekRange.value[0]) : null // 未选开始日期全部可点 if (!selectStart) return false const diffDays target.diff(selectStart, day) // 只能往后选最多6天凑满7天 if (diffDays 0 || diffDays 6) return true const startWeekDay selectStart.day() // 0周日 1周一 ...6周六 const endWeekDay target.day() // 规则起始必须周一结束必须周日且间隔正好6天共7天 const isFullWeek startWeekDay 1 endWeekDay 0 diffDays 6 return !isFullWeek } // 各选择器绑定值 const selectDay refstring(baseDate.value.format(YYYY-MM-DD)) // 周/自定义共用区间数组 const weekRange refstring[]([ getWeekStart(baseDate.value).format(YYYY-MM-DD), getWeekEnd(baseDate.value).format(YYYY-MM-DD) ]) const monthPickerValue refstring([ String(baseDate.value.year()), String(baseDate.value.month() 1) ]) const yearPickerValue refstring[]([String(baseDate.value.year())]) type DayutsInstance ReturnTypetypeof dayuts // 计算本周周一 function getWeekStart(d : DayutsInstance) : DayutsInstance { const dayNum d.day() const offset dayNum 0 ? -6 : 1 - dayNum return d.add(offset, day) } // 计算本周周日 function getWeekEnd(d : DayutsInstance) : DayutsInstance { return getWeekStart(d).add(6, day) } // 月份picker列 const monthColumns computed(() { const curYear baseDate.value.year() const years : string[] [] for (let i curYear - 10; i curYear 10; i) { years.push(String(i)) } const months [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] return [years, months] }) // 年份picker列 const yearColumns computed(() { const curYear baseDate.value.year() const years : string[] [] for (let i curYear - 10; i curYear 10; i) { years.push(String(i)) } return [years] }) // 监听外部参数 watch(() props.modelValue, (newVal : TimeDimension) { activeType.value newVal }) watch(() props.currentDate, (val) { baseDate.value dayuts(val) resetPickerValue() syncEmit() }) // 重置选择器绑定值自定义默认当天-当天 const resetPickerValue () { const safeDate baseDate.value.isValid() ? baseDate.value : dayuts() const today safeDate.format(YYYY-MM-DD) selectDay.value today if (activeType.value custom) { weekRange.value [today, today] } else if (activeType.value week) { // 打开周日历默认当前整周 const mon getWeekStart(safeDate) const sun getWeekEnd(safeDate) weekRange.value [mon.format(YYYY-MM-DD), sun.format(YYYY-MM-DD)] } else { weekRange.value [ getWeekStart(safeDate).format(YYYY-MM-DD), getWeekEnd(safeDate).format(YYYY-MM-DD) ] } monthPickerValue.value [ String(safeDate.year()), String(safeDate.month() 1) ] yearPickerValue.value [String(safeDate.year())] } // 打开底部弹窗 const openDatePopup () { resetPickerValue() popupShow.value true show.value true } // 页面展示日期文本新增custom分支展示区间 const displayDate computed(() { const d baseDate.value switch (activeType.value) { case day: return d.format(YYYY/M/D) case week: const s getWeekStart(d) const e getWeekEnd(d) return ${s.format(YYYY/M/D)} - ${e.format(YYYY/M/D)} case month: return d.format(YYYY/M) case year: return String(d.year()) case custom: // 自定义展示当前基准日-当前基准日 const start weekRange.value[0] const end weekRange.value[weekRange.value.length - 1] console.log(weekRange.value, weekRange.value) return ${dayuts(start).format(YYYY/M/D)} - ${dayuts(end).format(YYYY/M/D)} default: return d.format(YYYY/M/D) } }) // 上一周期 const prevDate () { switch (activeType.value) { case day: baseDate.value baseDate.value.subtract(1, day) break case week: baseDate.value baseDate.value.subtract(7, day) break case month: baseDate.value baseDate.value.subtract(1, month) break case year: baseDate.value baseDate.value.subtract(1, year) break case custom: baseDate.value baseDate.value.subtract(1, day) break } syncEmit() } // 下一周期 const nextDate () { switch (activeType.value) { case day: baseDate.value baseDate.value.add(1, day) break case week: baseDate.value baseDate.value.add(7, day) break case month: baseDate.value baseDate.value.add(1, month) break case year: baseDate.value baseDate.value.add(1, year) break case custom: baseDate.value baseDate.value.add(1, day) break } syncEmit() } // 同步通知父组件补全custom分支 const syncEmit () { const baseStr baseDate.value.format(YYYY-MM-DD) emit(update:currentDate, baseStr) if (activeType.value week) { const start getWeekStart(baseDate.value).format(YYYY-MM-DD) const end getWeekEnd(baseDate.value).format(YYYY-MM-DD) emit(change, activeType.value, baseStr, { start, end }) } else if (activeType.value month) { const yearMonth baseDate.value.format(YYYY-MM) console.log(yearMonth, yearMonth) emit(change, activeType.value, baseStr, { yearMonth }) } else if (activeType.value year) { const year baseDate.value.year() emit(change, activeType.value, baseStr, { year }) } else if (activeType.value custom) { const start weekRange.value[0] const end weekRange.value[weekRange.value.length - 1] emit(change, activeType.value, baseStr, { start, end }) } else { emit(change, activeType.value, baseStr) } } // 单日选择确认 const onDaySelect (date : string[]) { baseDate.value dayuts(date[0]) popupShow.value false show.value false syncEmit() } // 周区间选择确认 const onWeekSelect (dateArr : string[]) { const pickDay dayuts(dateArr[0]) // 获取选中日对应的周一、周日 const mon getWeekStart(pickDay) const sun getWeekEnd(pickDay) // 更新区间为完整一周 weekRange.value [mon.format(YYYY-MM-DD), sun.format(YYYY-MM-DD)] // 基准日期设为本周周一 baseDate.value mon popupShow.value false show.value false syncEmit() } // 月份选择确认 const onMonthSelect (res : string[]) { console.log(res, res) const year Number(res.value[0]) const month Number(res.value[1]) - 1 // 必须减1 console.log(转换后year, year, month, month, 最终month, month) baseDate.value dayuts().year(year).month(month).date(1) console.log(baseDate.value, baseDate.value ) popupShow.value false show.value false syncEmit() } // 年份选择确认 const onYearSelect (res : string[]) { const year Number(res.value[0]) baseDate.value dayuts().year(year).month(0).date(1) popupShow.value false show.value false syncEmit() } // 自定义区间选择确认 const onCustomSelect (res : string[]) { console.log(自定义选中区间, res) // 更新基准日期为选中起始日 baseDate.value dayuts(res[0]) // 更新全局区间数组 weekRange.value res // 关闭弹窗 popupShow.value false show.value false // 统一走syncEmit派发事件内部自动处理custom逻辑 syncEmit() } /script style langscss scoped .time-switch-wrap { width: 100%; .date-nav-1 { margin-top: 33rpx; display: flex; flex-direction: row; justify-content: center; align-items: center; .nav-left { margin-right: 53rpx; } .nav-right { margin-left: 53rpx; } .date-text { font-weight: 600; font-size: 29rpx; color: #1F2329; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } } } // 平板适配min-width:768px单位px media screen and (min-width: 768px) { .time-switch-wrap { // u-tabs 标签整体微调大屏不拥挤 :deep(.u-tabs) { height: 40rpx !important; } :deep(.u-tabs__wrapper__scroll-view__item) { padding: 0 40rpx; } :deep(.u-tabs__wrapper__scroll-view__item-text) { font-size: 21rpx !important; } .date-nav-1 { margin-top: 24rpx; .nav-left { margin-right: 36rpx; } .nav-right { margin-left: 36rpx; } .date-text { font-size: 22rpx; } } } } /styleimport { dayuts } from /uni_modules/lime-dayuts const timeTyperef(day) 初始类型 const selectDate ref(dayuts().format(YYYY-MM-DD)) 初始时间也就是当前时间 TimeTabSwitch v-modeltimeType :current-dateselectDate changehandleTimeChange / const handleTimeChange (type : TimeDimension, baseDate : string, range ?: { start ?: string; end ?: string; yearMonth ?: string; year ?: string }) { if (type week range) { console.log(周开始, range.start) console.log(周结束, range.end) itemizeModel.startTime range.start itemizeModel.endTime range.end // 接口请求传 start / end } else if (type month range) { console.log(选择年月, range.yearMonth) itemizeModel.startTime range.yearMonth itemizeModel.endTime range.yearMonth } else if (type year range) { console.log(选择年份, range.year) itemizeModel.startTime range.year } else if (type custom range) { itemizeModel.startTime range.start itemizeModel.endTime range.end } else { console.log(当前维度, type, 基准日期, baseDate) itemizeModel.startTime baseDate itemizeModel.endTime baseDate } // 接口请求逻辑 itemizeModel.time type findEnergyConsumptionSubItem()//调用自己方法 }

相关推荐

nVisual 如何解决跨楼层机房的线缆问题?
nVisual 如何解决跨楼层机房的线缆问题?

大家好,这里是 nVisual 频道。本频道将持续更新 nVisual 的使用技巧与常见问题的解决方法,欢迎大家关注。 在机房规划中,有时会遇到跨楼层机房的设计场景:上层机房与下层机房通过竖井连接。这类场景下的仿真设计往往让不少用户感到… · 2026/11/30 1:09:54

卷积扰动认证训练:提升CNN鲁棒性的可验证防御方法
卷积扰动认证训练:提升CNN鲁棒性的可验证防御方法

这次我们来看一个名为"Certified Training for Convolutional Perturbations"的项目,这是一个专注于提升卷积神经网络鲁棒性的认证训练方法。该项目针对深度学习模型在面对卷积扰动时的脆弱性问题,提供了一套可验证的防御方案。从项目名称就能… · 2026/9/24 19:15:29

零门槛解锁Wand专业版:告别付费订阅的游戏修改新体验
零门槛解锁Wand专业版:告别付费订阅的游戏修改新体验

零门槛解锁Wand专业版:告别付费订阅的游戏修改新体验 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer 还在为Wand(原WeMod&a… · 2026/9/24 19:15:18

电工证属于什么费用 内部消息透露:工地太忙根本没时间复习考试
电工证属于什么费用 内部消息透露:工地太忙根本没时间复习考试

电工证属于什么费用 内部消息透露:工地太忙根本没时间复习考试 工地太忙根本没时间复习考试,你是不是也经常这样想?特别是像周口水利工程这样的项目,工期紧、任务重,很多人连吃饭都得抢时间。但你有没有想过, 电工证属于什么费用… · 2026/11/30 1:09:05

宜宾考电工证办理地址通过率揭秘:学历不高怕报不上名?实操考试全攻略来了
宜宾考电工证办理地址通过率揭秘:学历不高怕报不上名?实操考试全攻略来了

宜宾考电工证办理地址通过率揭秘:学历不高怕报不上名?实操考试全攻略来了 你是不是还在为“学历不高怕报不上名”而焦虑?别急,今天我就从一个从业十年的老手角度,帮你梳理宜宾考电工证办理地址、考试内容、通过率揭秘、备考方法等关键信息,尤其是针对南… · 2026/11/30 1:09:05

电工证考哪个比较有用在哪考最靠谱
电工证考哪个比较有用在哪考最靠谱

电工证考哪个比较有用在哪考最靠谱 之前考的证在外省能不能转过来?我跟你说, 电工证考哪个比较有用 这个问题,直接影响你未来能不能上岗、能不能接项目,更别提 在哪考… · 2026/11/30 1:09:05

宜宾考电工证办理地址值不值得考?学历不高也能报!
宜宾考电工证办理地址值不值得考?学历不高也能报!

宜宾考电工证办理地址值不值得考?学历不高也能报! 你是不是也担心 学历不高怕报不上名 ?特别是像你这样在南阳卧龙区从事房建工程的朋友,想通过考取电工证来提升职业竞争力,但又怕自己学历不够、经验不足,根本没资格报考?别急,… · 2026/11/30 1:08:05

义乌水电工证怎么考 低学历也能拿高含金量证书
义乌水电工证怎么考 低学历也能拿高含金量证书

义乌水电工证怎么考 低学历也能拿高含金量证书 你是不是总担心自己学历不高,考不了电工证?其实,义乌水电工证的报考要求远没有你想的那么复杂,只要符合基本条件,哪怕是初中毕业也能顺利拿到证。这个证书含金量高,是从事水电工作的“入场券”,在很多工… · 2026/11/30 1:08:05

低压电工证怎么退税率网上查询别走弯路
低压电工证怎么退税率网上查询别走弯路

低压电工证怎么退税率网上查询别走弯路 实操考试心里没底怕挂科,你不是一个人。低压电工证的考试内容不仅涵盖理论,更重要的是实操部分,很多人因为没掌握好流程、没准备好工具而被淘汰。而如今,网上查询越来越便捷,但很多人却不知道怎么操作,更别提低压… · 2026/11/30 1:08:05

工地太忙根本没时间复习考试?考电工证昆明千万别踩坑
工地太忙根本没时间复习考试?考电工证昆明千万别踩坑

工地太忙根本没时间复习考试?考电工证昆明千万别踩坑 工地太忙根本没时间复习考试,这是很多一线施工人员的真实写照。尤其在昆明这样的工程大市,特种作业操作证的需求量不断攀升,但很多工人因为工作节奏快、任务紧,连最基本的考试复习时间都挤不出来,结… · 2026/11/30 0:23:59

电工证线下报名地点怎么找?全国通用证书怎么考?
电工证线下报名地点怎么找?全国通用证书怎么考?

电工证线下报名地点怎么找?全国通用证书怎么考? 你是不是正在找电工证线下报名地点,想着赶紧考个证上岗?别急,现在就告诉你怎么操作。电工证是特种作业操作证的一种, 全国通用… · 2026/11/30 0:20:14

电工证 联网如何操作?官方报名入口全攻略
电工证 联网如何操作?官方报名入口全攻略

电工证 联网如何操作?官方报名入口全攻略 证书过期了不知道怎么复审延期?很多电工朋友在工作中遇到过这样的尴尬,尤其是在一线操作岗位,证件一旦过期,就面临停岗、扣工资甚至被清退的风险。而“电工证… · 2026/11/30 0:10:21

商丘考电工证年龄全流程办理指南多久拿证
商丘考电工证年龄全流程办理指南多久拿证

商丘考电工证年龄全流程办理指南多久拿证 实操考试心里没底怕挂科,商丘的电工证考试真的让人又爱又怕。尤其是对于刚从其他岗位转岗的你来说,年龄可能成了一个顾虑,但别担心,只要了解流程,准备充分, 多久拿证 其实并不难。… · 2026/11/30 0:00:41

电工证需不需要检?郑州报考避坑指南来了,学历不高也能报上名
电工证需不需要检?郑州报考避坑指南来了,学历不高也能报上名

电工证需不需要检?郑州报考避坑指南来了,学历不高也能报上名 你是不是也担心自己学历不够,怕报不了电工证?其实, 电工证不需要检… · 2026/11/30 0:00:41

沭阳在哪里报考电工证多少钱最划算
沭阳在哪里报考电工证多少钱最划算

沭阳在哪里报考电工证多少钱最划算 怕考不过白交培训费?沭阳电工证报考渠道全解析,看看多少钱才值 在沭阳报考电工证,不少学员最担心的就是培训费花出去,考试却不过,最后钱打水漂。这不仅是学员的担忧,更是很多培训机构避而不谈的痛点。其实,只要选对… · 2026/11/30 0:01:41

双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试
双极步进电机控制实战:DRV8818PWPR与PIC18F86K22驱动调试

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/29 5:45:25

STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析
STM32F207ZG + DRV8818PWPR 步进电机驱动实战解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/29 23:33:16

DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案
DRV8818PWPR+dsPIC30F4013工业级双极步进电机控制方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views … · 2026/11/29 23:07:06

了解更多?预约专属演示

我们的顾问将为您一对一讲解产品与方案

企业微信二维码