index.js 2.7 KB

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