index.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const App = getApp();
  2. const dateTimePicker = require('../../utils/datePicker');
  3. const moment = require("../../utils/moment.min");
  4. Component({
  5. options: { addGlobalClass: true },
  6. properties: {
  7. datetime: { type: String, value: '' },
  8. name: { type: String, value: '' }
  9. },
  10. data: {
  11. // 时间列表
  12. dateTimeArray: null,
  13. // 默认显示时间
  14. valueDateTime: null,
  15. // 开始时间
  16. startDateTime: '',
  17. // 结束时间
  18. endDateTime: '',
  19. },
  20. pageLifetimes: {
  21. show: function () {
  22. const that = this;
  23. that.search();
  24. },
  25. hide: function () { },
  26. resize: function (size) { }
  27. },
  28. /**
  29. * 组件的方法列表
  30. */
  31. methods: {
  32. search: function () {
  33. const that = this;
  34. // 计算时间区间
  35. let startDateTime = moment().format('YYYY-MM-DD HH:mm');
  36. let endDateTime = moment().add(100, 'years').format('YYYY-MM-DD HH:mm');
  37. that.setData({ startDateTime: startDateTime, endDateTime: endDateTime })
  38. // 获取完整的年月日 时分秒,以及默认显示的数组
  39. that.data.unit = ['年', '月', '日', '时', '分']
  40. that.data.dateTimePicker = dateTimePicker.newDateTimePicker(startDateTime, endDateTime)
  41. let obj = that.data.dateTimePicker.render();
  42. let lastArray = obj.dateTimeArray;
  43. let lastTime = obj.dateTime;
  44. for (let i = 0; i < lastArray.length; i++) {
  45. lastArray[i] = lastArray[i].map(m => m + this.data.unit[i])
  46. }
  47. that.setData({ dateTimeArray: lastArray, valueDateTime: lastTime })
  48. },
  49. // 确认选择
  50. change(e) {
  51. const that = this;
  52. const valueDateTime = that.data.valueDateTime;
  53. const year = that.data.dateTimeArray[0][valueDateTime[0]].replace(/年/, '')
  54. const month = that.data.dateTimeArray[1][valueDateTime[1]].replace(/月/, '')
  55. const day = that.data.dateTimeArray[2][valueDateTime[2]].replace(/日/, '')
  56. const hour = that.data.dateTimeArray[3][valueDateTime[3]].replace(/时/, '')
  57. const minute = that.data.dateTimeArray[4][valueDateTime[4]].replace(/分/, '')
  58. let dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`;
  59. that.triggerEvent('datetimeChange', { datetime: dateTimeWhole, name: that.properties.name })
  60. },
  61. // 选择时间
  62. columnChange: function (e) {
  63. const that = this;
  64. // 时间列表
  65. const dateTimeArray = that.data.dateTimeArray;
  66. const { column, value } = e.detail
  67. let dateTimeTemp = 'valueDateTime[' + column + ']'
  68. that.setData({ [dateTimeTemp]: value })
  69. // 当前选择的值
  70. const valueDateTime = that.data.valueDateTime;
  71. that.data.dateTimePicker.setValue({ dateTimeArray: dateTimeArray, dateTime: valueDateTime })
  72. for (let i = 1; i < valueDateTime.length; i++) {
  73. if (column == i - 1) {
  74. for (let j = i; j < valueDateTime.length; j++) {
  75. let temp = 'valueDateTime[' + j + ']'
  76. that.setData({ [temp]: 0 })
  77. }
  78. }
  79. let arr = that.data.dateTimePicker.dispatch(i).map(m => m + that.data.unit[i])
  80. let temp1 = 'dateTimeArray[' + i + ']';
  81. that.setData({ [temp1]: arr })
  82. }
  83. that.setData({ dateTimeArray: dateTimeArray, valueDateTime: valueDateTime })
  84. },
  85. }
  86. })