index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // components/picker/index.js
  2. const { formatTime } = require('../../utils/util')
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. // picker类型
  9. type: {
  10. type: String,
  11. value: ''
  12. },
  13. // 未选择时提示
  14. placeholder: {
  15. type: Array,
  16. value: ['请选择']
  17. },
  18. // 数据列表
  19. list: {
  20. type: Array,
  21. value: []
  22. },
  23. // 显示参数key
  24. rangeKey: {
  25. type: String,
  26. value: ''
  27. },
  28. // 选择下标
  29. index: {
  30. value: null
  31. },
  32. // 日期时间选择器
  33. dateTime: {
  34. type: String,
  35. value: null
  36. },
  37. delay: {
  38. type: Number,
  39. value: null
  40. },
  41. isAudit:{
  42. type:Boolean,
  43. value:false
  44. }
  45. },
  46. /**
  47. * 组件的初始数据
  48. */
  49. data: {
  50. index: null, //普通选择器下标
  51. date: null, //日期
  52. time: null, //时间
  53. },
  54. /**
  55. * 组件的方法列表
  56. */
  57. lifetimes: {
  58. attached: function() {
  59. if(this.data.type === 'date-time' && this.data.dateTime === null) {
  60. this.getTime()
  61. }
  62. },
  63. detached: function() {
  64. // 在组件实例被从页面节点树移除时执行
  65. },
  66. },
  67. observers:{
  68. "dateTime": function(dateTime) {
  69. if(this.data.type === 'date-time') {
  70. this.getTime()
  71. }
  72. }
  73. },
  74. methods: {
  75. // 获得传入时间或默认当前时间
  76. getTime() {
  77. var { date, time, dateTime, delay } = this.data;
  78. var newDateTime = null;
  79. if (dateTime) {
  80. newDateTime = dateTime.replaceAll('-', '/');
  81. } else if(delay != null) {
  82. var nowDateTime = (Date.now()+delay);
  83. newDateTime = nowDateTime;
  84. } else {
  85. return
  86. }
  87. date = formatTime(newDateTime, 'YYYY-MM-DD');
  88. time = formatTime(newDateTime, 'HH:NN');
  89. this.setData({ date, time });
  90. if (date && time) {
  91. this.triggerEvent('change', (date+' '+time+':00'))
  92. }
  93. },
  94. // 选择普通项
  95. chooseItem(e) {
  96. var { index } = this.data;
  97. index = e.detail.value
  98. this.setData({ index });
  99. this.triggerEvent('change', this.data.list[index] )
  100. },
  101. // 选择日期
  102. chooseDateTime(e) {
  103. var { date, time } = this.data
  104. switch (e.currentTarget.dataset.type) {
  105. case 'date':
  106. date = e.detail.value
  107. this.setData({ date })
  108. break;
  109. case 'time':
  110. time = e.detail.value
  111. this.setData({ time })
  112. break;
  113. default:
  114. break;
  115. };
  116. if (date && time) {
  117. this.triggerEvent('change', (date+' '+time+':00'))
  118. }
  119. }
  120. }
  121. })