u-calendar.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <u-popup
  3. :show="show"
  4. mode="bottom"
  5. closeable
  6. @close="close"
  7. :round="round"
  8. :closeOnClickOverlay="closeOnClickOverlay"
  9. >
  10. <view class="u-calendar">
  11. <uHeader
  12. :title="title"
  13. :subtitle="subtitle"
  14. :showSubtitle="showSubtitle"
  15. :showTitle="showTitle"
  16. ></uHeader>
  17. <scroll-view
  18. :style="{
  19. height: $u.addUnit(listHeight)
  20. }"
  21. scroll-y
  22. @scroll="onScroll"
  23. :scrollIntoView="scrollIntoView"
  24. >
  25. <uMonth
  26. :color="color"
  27. :rowHeight="rowHeight"
  28. :showMark="showMark"
  29. :months="months"
  30. :mode="mode"
  31. :maxCount="maxCount"
  32. :startText="startText"
  33. :endText="endText"
  34. :defaultDate="defaultDate"
  35. :minDate="innerMinDate"
  36. :maxDate="innerMaxDate"
  37. :maxMonth="monthNum"
  38. :readonly="readonly"
  39. :maxRange="maxRange"
  40. :rangePrompt="rangePrompt"
  41. :showRangePrompt="showRangePrompt"
  42. :allowSameDay="allowSameDay"
  43. ref="month"
  44. @monthSelected="monthSelected"
  45. @updateMonthTop="updateMonthTop"
  46. ></uMonth>
  47. </scroll-view>
  48. <slot name="footer" v-if="showConfirm">
  49. <view class="u-calendar__confirm">
  50. <u-button
  51. shape="circle"
  52. :text="
  53. buttonDisabled ? confirmDisabledText : confirmText
  54. "
  55. :color="color"
  56. @click="confirm"
  57. :disabled="buttonDisabled"
  58. ></u-button>
  59. </view>
  60. </slot>
  61. </view>
  62. </u-popup>
  63. </template>
  64. <script>
  65. import uHeader from './header.vue'
  66. import uMonth from './month.vue'
  67. import props from './props.js'
  68. import util from './util.js'
  69. import dayjs from '../../libs/util/dayjs.js'
  70. import Calendar from '../../libs/util/calendar.js'
  71. /**
  72. * Calendar 日历
  73. * @description 此组件用于单个选择日期,范围选择日期等,日历被包裹在底部弹起的容器中.
  74. * @tutorial https://www.uviewui.com/components/calendar.html
  75. *
  76. * @property {String} title 标题内容 (默认 日期选择 )
  77. * @property {Boolean} showTitle 是否显示标题 (默认 true )
  78. * @property {Boolean} showSubtitle 是否显示副标题 (默认 true )
  79. * @property {String} mode 日期类型选择 single-选择单个日期,multiple-可以选择多个日期,range-选择日期范围 ( 默认 'single' )
  80. * @property {String} startText mode=range时,第一个日期底部的提示文字 (默认 '开始' )
  81. * @property {String} endText mode=range时,最后一个日期底部的提示文字 (默认 '结束' )
  82. * @property {Array} customList 自定义列表
  83. * @property {String} color 主题色,对底部按钮和选中日期有效 (默认 ‘#3c9cff' )
  84. * @property {String | Number} minDate 最小的可选日期 (默认 0 )
  85. * @property {String | Number} maxDate 最大可选日期 (默认 0 )
  86. * @property {Array | String| Date} defaultDate 默认选中的日期,mode为multiple或range是必须为数组格式
  87. * @property {String | Number} maxCount mode=multiple时,最多可选多少个日期 (默认 Number.MAX_SAFE_INTEGER )
  88. * @property {String | Number} rowHeight 日期行高 (默认 56 )
  89. * @property {Function} formatter 日期格式化函数
  90. * @property {Boolean} showLunar 是否显示农历 (默认 false )
  91. * @property {Boolean} showMark 是否显示月份背景色 (默认 true )
  92. * @property {String} confirmText 确定按钮的文字 (默认 '确定' )
  93. * @property {String} confirmDisabledText 确认按钮处于禁用状态时的文字 (默认 '确定' )
  94. * @property {Boolean} show 是否显示日历弹窗 (默认 false )
  95. * @property {Boolean} closeOnClickOverlay 是否允许点击遮罩关闭日历 (默认 false )
  96. * @property {Boolean} readonly 是否为只读状态,只读状态下禁止选择日期 (默认 false )
  97. * @property {String | Number} maxRange 日期区间最多可选天数,默认无限制,mode = range时有效
  98. * @property {String} rangePrompt 范围选择超过最多可选天数时的提示文案,mode = range时有效
  99. * @property {Boolean} showRangePrompt 范围选择超过最多可选天数时,是否展示提示文案,mode = range时有效 (默认 true )
  100. * @property {Boolean} allowSameDay 是否允许日期范围的起止时间为同一天,mode = range时有效 (默认 false )
  101. * @property {Number|String} round 圆角值,默认无圆角 (默认 0 )
  102. * @property {Number|String} monthNum 最多展示的月份数量 (默认 3 )
  103. *
  104. * @event {Function()} confirm 点击确定按钮时触发 选择日期相关的返回参数
  105. * @event {Function()} close 日历关闭时触发 可定义页面关闭时的回调事件
  106. * @example <u-calendar :defaultDate="defaultDateMultiple" :show="show" mode="multiple" @confirm="confirm">
  107. </u-calendar>
  108. * */
  109. export default {
  110. name: 'u-calendar',
  111. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  112. components: {
  113. uHeader,
  114. uMonth
  115. },
  116. data() {
  117. return {
  118. // 需要显示的月份的数组
  119. months: [],
  120. // 在月份滚动区域中,当前视图中月份的index索引
  121. monthIndex: 0,
  122. // 月份滚动区域的高度
  123. listHeight: 0,
  124. // month组件中选择的日期数组
  125. selected: [],
  126. scrollIntoView: '',
  127. // 过滤处理方法
  128. innerFormatter: (value) => value
  129. }
  130. },
  131. watch: {
  132. selectedChange: {
  133. immediate: true,
  134. handler(n) {
  135. this.setMonth()
  136. }
  137. },
  138. // 打开弹窗时,设置月份数据
  139. show: {
  140. immediate: true,
  141. handler(n) {
  142. this.setMonth()
  143. }
  144. }
  145. },
  146. computed: {
  147. // 由于maxDate和minDate可以为字符串(2021-10-10),或者数值(时间戳),但是dayjs如果接受字符串形式的时间戳会有问题,这里进行处理
  148. innerMaxDate() {
  149. return uni.$u.test.number(this.maxDate)
  150. ? Number(this.maxDate)
  151. : this.maxDate
  152. },
  153. innerMinDate() {
  154. return uni.$u.test.number(this.minDate)
  155. ? Number(this.minDate)
  156. : this.minDate
  157. },
  158. // 多个条件的变化,会引起选中日期的变化,这里统一管理监听
  159. selectedChange() {
  160. return [this.innerMinDate, this.innerMaxDate, this.defaultDate]
  161. },
  162. subtitle() {
  163. // 初始化时,this.months为空数组,所以需要特别判断处理
  164. if (this.months.length) {
  165. return `${this.months[this.monthIndex].year}年${
  166. this.months[this.monthIndex].month
  167. }月`
  168. } else {
  169. return ''
  170. }
  171. },
  172. buttonDisabled() {
  173. // 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态
  174. if (this.mode === 'range') {
  175. if (this.selected.length <= 1) {
  176. return true
  177. } else {
  178. return false
  179. }
  180. } else {
  181. return false
  182. }
  183. }
  184. },
  185. mounted() {
  186. this.start = Date.now()
  187. this.init()
  188. },
  189. methods: {
  190. // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
  191. setFormatter(e) {
  192. this.innerFormatter = e
  193. },
  194. // month组件内部选择日期后,通过事件通知给父组件
  195. monthSelected(e) {
  196. this.selected = e
  197. if (!this.showConfirm) {
  198. // 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还
  199. if (
  200. this.mode === 'multiple' ||
  201. this.mode === 'single' ||
  202. (this.mode === 'range' && this.selected.length >= 2)
  203. ) {
  204. this.$emit('confirm', this.selected)
  205. }
  206. }
  207. },
  208. init() {
  209. // 校验maxDate,不能小于当前时间
  210. if (
  211. this.innerMaxDate &&
  212. new Date(this.innerMaxDate).getTime() <= Date.now()
  213. ) {
  214. return uni.$u.error('maxDate不能小于当前时间')
  215. }
  216. // 滚动区域的高度
  217. this.listHeight = this.rowHeight * 5 + 30
  218. this.setMonth()
  219. },
  220. close() {
  221. this.$emit('close')
  222. },
  223. // 点击确定按钮
  224. confirm() {
  225. if (!this.buttonDisabled) {
  226. this.$emit('confirm', this.selected)
  227. }
  228. },
  229. // 获得两个日期之间的月份数
  230. getMonths(minDate, maxDate) {
  231. const minYear = dayjs(minDate).year()
  232. const minMonth = dayjs(minDate).month() + 1
  233. const maxYear = dayjs(maxDate).year()
  234. const maxMonth = dayjs(maxDate).month() + 1
  235. return (maxYear - minYear) * 12 + (maxMonth - minMonth)
  236. },
  237. // 设置月份数据
  238. setMonth() {
  239. // 最小日期的毫秒数
  240. const minDate = this.innerMinDate || dayjs().valueOf()
  241. // 如果没有指定最大日期,则往后推3个月
  242. const maxDate =
  243. this.innerMaxDate ||
  244. dayjs(minDate)
  245. .add(this.monthNum - 1, 'month')
  246. .valueOf()
  247. // 最大最小月份之间的共有多少个月份,
  248. const months = uni.$u.range(
  249. 1,
  250. this.monthNum,
  251. this.getMonths(minDate, maxDate)
  252. )
  253. // 先清空数组
  254. this.months = []
  255. for (let i = 0; i < months; i++) {
  256. this.months.push({
  257. date: new Array(
  258. dayjs(minDate).add(i, 'month').daysInMonth()
  259. )
  260. .fill(1)
  261. .map((item, index) => {
  262. // 日期,取值1-31
  263. let day = index + 1
  264. // 星期,0-6,0为周日
  265. const week = dayjs(minDate)
  266. .add(i, 'month')
  267. .date(day)
  268. .day()
  269. const date = dayjs(minDate)
  270. .add(i, 'month')
  271. .date(day)
  272. .format('YYYY-MM-DD')
  273. let bottomInfo = ''
  274. if (this.showLunar) {
  275. // 将日期转为农历格式
  276. const lunar = Calendar.solar2lunar(
  277. dayjs(date).year(),
  278. dayjs(date).month() + 1,
  279. dayjs(date).date()
  280. )
  281. bottomInfo = lunar.IDayCn
  282. }
  283. let config = {
  284. day,
  285. week,
  286. // 小于最小允许的日期,或者大于最大的日期,则设置为disabled状态
  287. disabled:
  288. dayjs(date).isBefore(
  289. dayjs(minDate).format('YYYY-MM-DD')
  290. ) ||
  291. dayjs(date).isAfter(
  292. dayjs(maxDate).format('YYYY-MM-DD')
  293. ),
  294. // 返回一个日期对象,供外部的formatter获取当前日期的年月日等信息,进行加工处理
  295. date: new Date(date),
  296. bottomInfo,
  297. dot: false,
  298. month:
  299. dayjs(minDate).add(i, 'month').month() + 1
  300. }
  301. const formatter =
  302. this.formatter || this.innerFormatter
  303. return formatter(config)
  304. }),
  305. // 当前所属的月份
  306. month: dayjs(minDate).add(i, 'month').month() + 1,
  307. // 当前年份
  308. year: dayjs(minDate).add(i, 'month').year()
  309. })
  310. }
  311. },
  312. // scroll-view滚动监听
  313. onScroll(event) {
  314. // 不允许小于0的滚动值,如果scroll-view到顶了,继续下拉,会出现负数值
  315. const scrollTop = Math.max(0, event.detail.scrollTop)
  316. // 将当前滚动条数值,除以滚动区域的高度,可以得出当前滚动到了哪一个月份的索引
  317. for (let i = 0; i < this.months.length; i++) {
  318. if (scrollTop >= (this.months[i].top || this.listHeight)) {
  319. this.monthIndex = i
  320. }
  321. }
  322. },
  323. // 更新月份的top值
  324. updateMonthTop(topArr = []) {
  325. // 设置对应月份的top值,用于onScroll方法更新月份
  326. topArr.map((item, index) => {
  327. this.months[index].top = item
  328. })
  329. }
  330. }
  331. }
  332. </script>
  333. <style lang="scss" scoped>
  334. @import '../../libs/css/components.scss';
  335. .u-calendar {
  336. &__confirm {
  337. padding: 7px 18px;
  338. }
  339. }
  340. </style>