123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- Component({
- properties: {
- //1-日期+时间(年月日+时分) 2-日期(年月日) 3-日期(年月) 4-时间(时分)
- type: {
- type: Number,
- value: 1,
- observer(val) {
- this.propsChange();
- }
- },
- //年份区间
- startYear: {
- type: Number,
- value: 1980,
- observer(val) {
- this.propsChange();
- }
- },
- //年份区间
- endYear: {
- type: Number,
- value: 2050,
- observer(val) {
- this.propsChange();
- }
- },
- //"取消"字体颜色
- cancelColor: {
- type: String,
- value: "#888"
- },
- //"确定"字体颜色
- color: {
- type: String,
- value: "#5677fc"
- },
- //设置默认显示日期 2019-08-01 || 2019-08-01 17:01 || 2019/08/01
- setDateTime: {
- type: String,
- value: "",
- observer(val) {
- this.propsChange();
- }
- }
- },
- lifetimes: {
- ready: function () {
- this.initData()
- }
- },
- data: {
- isShow: false,
- years: [],
- months: [],
- days: [],
- hours: [],
- minutes: [],
- year: 0,
- month: 0,
- day: 0,
- hour: 0,
- minute: 0,
- startDate: "",
- endDate: "",
- value: [0, 0, 0, 0, 0],
- reset: false
- },
- observers:{
- 'year, month': function (year, month) {
- this.setDays();
- }
- },
- methods: {
- stop() {},
- propsChange() {
- this.setData({
- reset: true
- })
- setTimeout(() => {
- this.initData()
- }, 10);
- },
- formatNum: function(num) {
- return num < 10 ? "0" + num : num + "";
- },
- generateArray: function(start, end) {
- return Array.from(new Array(end + 1).keys()).slice(start)
- },
- getIndex: function(arr, val) {
- let index = arr.indexOf(val);
- return ~index ? index : 0
- },
- //日期时间处理
- initSelectValue() {
- let fdate = this.data.setDateTime.replace(/\-/g, '/');
- fdate = fdate && fdate.indexOf("/") == -1 ? `2020/01/01 ${fdate}` : fdate
- let time = null;
- if (fdate)
- time = new Date(fdate);
- else
- time = new Date();
- this.setData({
- year: time.getFullYear(),
- month: time.getMonth() + 1,
- day: time.getDate(),
- hour: time.getHours(),
- minute: time.getMinutes()
- })
- },
- initData() {
- this.initSelectValue()
- this.setData({
- reset: false
- })
- switch (this.data.type) {
- case 1:
- this.setData({
- value: [0, 0, 0, 0, 0]
- })
- this.setYears();
- this.setMonths();
- this.setDays();
- this.setHours();
- this.setMinutes();
- break;
- case 2:
- this.setData({
- value: [0, 0, 0]
- })
- this.setYears();
- this.setMonths();
- this.setDays();
- break;
- case 3:
- this.setData({
- value: [0, 0]
- })
- this.setYears();
- this.setMonths();
- break;
- case 4:
- this.setData({
- value: [0, 0]
- })
- this.setHours();
- this.setMinutes();
- break;
- default:
- break;
- }
- },
- setYears() {
- this.setData({
- years: this.generateArray(this.data.startYear, this.data.endYear)
- })
- setTimeout(() => {
- let value = "value[0]";
- this.setData({
- [value]: this.getIndex(this.data.years, this.data.year)
- })
- }, 8);
- },
- setMonths() {
- this.setData({
- months: this.generateArray(1, 12)
- })
- setTimeout(() => {
- let value = "value[1]";
- this.setData({
- [value]: this.getIndex(this.data.months, this.data.month)
- })
- }, 8);
- },
- setDays() {
- if (this.data.type == 3 || this.data.type == 4) return;
- let totalDays = new Date(this.data.year, this.data.month, 0).getDate();
- this.setData({
- days: this.generateArray(1, totalDays)
- })
- setTimeout(() => {
- let value = "value[2]";
- this.setData({
- [value]: this.getIndex(this.data.days, this.data.day)
- })
- }, 8);
- },
- setHours() {
- this.setData({
- hours: this.generateArray(0, 23)
- })
- setTimeout(() => {
- let value = `value[${this.data.value.length - 2}]`;
- this.setData({
- [value]: this.getIndex(this.data.hours, this.data.hour)
- })
- }, 8);
- },
- setMinutes() {
- this.setData({
- minutes: this.generateArray(0, 59)
- })
- setTimeout(() => {
- let value = `value[${this.data.value.length - 1}]`;
- this.setData({
- [value]: this.getIndex(this.data.minutes, this.data.minute)
- })
- }, 8);
- },
- show() {
- setTimeout(() => {
- this.setData({
- isShow: true
- })
- }, 50);
- },
- hide() {
- this.setData({
- isShow: false
- })
- },
- change(e) {
- this.setData({
- value: e.detail.value
- })
- switch (this.data.type) {
- case 1:
- this.setData({
- year: this.data.years[this.data.value[0]],
- month: this.data.months[this.data.value[1]],
- day: this.data.days[this.data.value[2]],
- hour: this.data.hours[this.data.value[3]],
- minute: this.data.minutes[this.data.value[4]]
- })
- break;
- case 2:
- this.setData({
- year: this.data.years[this.data.value[0]],
- month: this.data.months[this.data.value[1]],
- day: this.data.days[this.data.value[2]]
- })
- break;
- case 3:
- this.setData({
- year: this.data.years[this.data.value[0]],
- month: this.data.months[this.data.value[1]]
- })
- break;
- case 4:
- this.setData({
- hour: this.data.hours[this.data.value[0]],
- minute: this.data.minutes[this.data.value[0]]
- })
- break;
- default:
- break;
- }
- },
- btnFix() {
- let result = {};
- let year = this.data.year;
- let month = this.formatNum(this.data.month || 0);
- let day = this.formatNum(this.data.day || 0);
- let hour = this.formatNum(this.data.hour || 0);
- let minute = this.formatNum(this.data.minute || 0);
- switch (this.data.type) {
- case 1:
- result = {
- year: year,
- month: month,
- day: day,
- hour: hour,
- minute: minute,
- result: `${year}-${month}-${day} ${hour}:${minute}`
- }
- break;
- case 2:
- result = {
- year: year,
- month: month,
- day: day,
- result: `${year}-${month}-${day}`
- }
- break;
- case 3:
- result = {
- year: year,
- month: month,
- result: `${year}-${month}`
- }
- break;
- case 4:
- result = {
- hour: hour,
- minute: minute,
- result: `${hour}:${minute}`
- }
- break;
- default:
- break;
- }
- this.triggerEvent('confirm', result)
- this.hide();
- }
- }
- })
|