index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. Component({
  2. options: {
  3. addGlobalClass: true
  4. },
  5. properties: {
  6. iconName: {
  7. type: String,
  8. value: "icon-rili1"
  9. },
  10. title: {
  11. type: String,
  12. },
  13. onChange: {
  14. type: Function,
  15. },
  16. fields: {
  17. type: String,
  18. value: "day", //有效值 year,month,day,表示选择器的粒度
  19. },
  20. end: {
  21. type: [String, Number]
  22. },
  23. start: {
  24. type: [String, Number]
  25. },
  26. type: {
  27. type: String,
  28. value: ""
  29. },
  30. startMonth: {
  31. type: [String, Number],
  32. value: 1
  33. },
  34. endMonth: {
  35. type: [String, Number],
  36. value: 12
  37. }
  38. },
  39. lifetimes: {
  40. attached: function () {
  41. const fields = this.properties.fields;
  42. let value = new Date();
  43. if (fields === "year") {
  44. value = value.getFullYear().toString();
  45. } else if (fields === "month") {
  46. const year = value.getFullYear();
  47. const month = (value.getMonth() + 1).toString().padStart(2, '0');
  48. value = `${year}-${month}`;
  49. } else if (fields === "day") {
  50. value = value.toISOString().split('T')[0];
  51. }
  52. this.setData({
  53. value
  54. });
  55. if (this.data.type == 'sameYear') {
  56. this.setData({
  57. startMonth1: this.data.startMonth,
  58. endMonth1: this.data.endMonth,
  59. year: value
  60. })
  61. }
  62. }
  63. },
  64. data: {
  65. value: new Date().toISOString().split('T')[0], // 默认值为今天,格式为 YYYY-MM-DD
  66. showSameYear: false,
  67. },
  68. methods: {
  69. onChange(e) {
  70. if (e.detail.value == this.data.value) return;
  71. this.setData({
  72. value: e.detail.value
  73. })
  74. this.triggerEvent("onChange", e.detail.value)
  75. },
  76. openSameYear() {
  77. this.setData({
  78. showSameYear: true
  79. })
  80. },
  81. onClose() {
  82. this.setData({
  83. showSameYear: false
  84. })
  85. },
  86. onMonthTap(e) {
  87. const {
  88. month
  89. } = e.currentTarget.dataset;
  90. const {
  91. startMonth1,
  92. endMonth1
  93. } = this.data;
  94. // 情况1:当前没有任何选中
  95. if (startMonth1 === null && endMonth1 === null) {
  96. this.setData({
  97. startMonth1: month,
  98. endMonth1: month
  99. });
  100. }
  101. // 情况2:当前只选中了一个月份(单点选择状态)
  102. else if (startMonth1 === endMonth1) {
  103. // 如果点击同一个月份,取消选中
  104. if (month === startMonth1) {
  105. this.setData({
  106. startMonth1: null,
  107. endMonth1: null
  108. });
  109. }
  110. // 如果点击不同月份,形成区间
  111. else {
  112. const newStart = Math.min(startMonth1, month);
  113. const newEnd = Math.max(startMonth1, month);
  114. this.setData({
  115. startMonth1: newStart,
  116. endMonth1: newEnd
  117. });
  118. }
  119. }
  120. // 情况3:当前已经是一个区间
  121. else {
  122. // 点击任意月份,重置为单点选择
  123. this.setData({
  124. startMonth1: month,
  125. endMonth1: month
  126. });
  127. }
  128. },
  129. onYear(e) {
  130. this.setData({
  131. year: e.detail.value
  132. })
  133. },
  134. onConfirm() {
  135. const {
  136. startMonth1,
  137. endMonth1,
  138. year
  139. } = this.data;
  140. if (!year) {
  141. wx.showModal({
  142. title: '提示',
  143. content: '请选择年份',
  144. showCancel: false
  145. })
  146. return;
  147. } else if (!startMonth1 && !endMonth1) {
  148. wx.showModal({
  149. title: '提示',
  150. content: '请选择月份范围',
  151. showCancel: false
  152. })
  153. return;
  154. }
  155. this.setData({
  156. value: year
  157. })
  158. this.triggerEvent("onChange", {
  159. startMonth: startMonth1 || endMonth1,
  160. endMonth: endMonth1 || startMonth1,
  161. year
  162. })
  163. }
  164. }
  165. })