// components/picker/index.js const { formatTime } = require("../../utils/util"); Component({ options: { observers: true, lifetimes: true }, /** * 组件的属性列表 */ properties: { // picker类型 type: { type: String, value: "" }, // 未选择时提示 placeholder: { type: Array, value: ["请选择"] }, // 数据列表 list: { type: Array, value: [] }, // 显示参数key rangeKey: { type: String, value: "" }, // 选择下标 index: { value: null }, // 日期时间选择器 dateTime: { type: String, value: null }, delay: { type: Number, value: null }, isAudit: { type: Boolean, value: false } }, /** * 组件的初始数据 */ data: { index: null, //普通选择器下标 date: null, //日期 time: null //时间 }, /** * 组件的方法列表 */ lifetimes: { attached: function () { if (this.data.type === "date-time" && this.data.dateTime === null) { this.getTime(); } }, detached: function () { // 在组件实例被从页面节点树移除时执行 } }, observers: { dateTime: function (dateTime) { if (this.data.type === "date-time") { this.getTime(); } } }, methods: { // 获得传入时间或默认当前时间 getTime() { var { date, time, dateTime, delay } = this.data; var newDateTime = null; if (dateTime) { newDateTime = dateTime.replaceAll("-", "/"); } else if (delay != null) { var nowDateTime = Date.now() + delay; newDateTime = nowDateTime; } else { return; } date = formatTime(newDateTime, "YYYY-MM-DD"); time = formatTime(newDateTime, "HH:NN"); this.setData({ date, time }); if (date && time) { this.triggerEvent("change", date + " " + time + ":00"); } }, // 选择普通项 chooseItem(e) { var { index } = this.data; index = e.detail.value; this.setData({ index }); this.triggerEvent("change", this.data.list[index]); }, // 选择日期 chooseDateTime(e) { var { date, time } = this.data; switch (e.currentTarget.dataset.type) { case "date": date = e.detail.value; this.setData({ date }); break; case "time": time = e.detail.value; this.setData({ time }); break; default: break; } if (date && time) { this.triggerEvent("change", date + " " + time + ":00"); } } } });