guhongwei 2 年 前
コミット
d88757d4ae

+ 1 - 4
app.json

@@ -11,10 +11,7 @@
         "pages/setting/basic",
         "pages/setting/contact",
         "pages/test/index",
-        "pages/logs/logs",
-        "commpents/pagesMatchs/achieve/achieve-1",
-        "commpents/pagesMatchs/achieve/achieve-2",
-        "commpents/pagesMatchs/achieve/achieve-3"
+        "pages/logs/logs"
     ],
     "subPackages": [
         {

+ 61 - 59
commpents/datetime-picker/index.js

@@ -1,86 +1,88 @@
-// commpents/datetime-picker/index.js
-const app = getApp()
+const App = getApp();
+const dateTimePicker = require('../../utils/datePicker');
 const moment = require("../../utils/moment.min");
 Component({
-    /**
-     * 组件的属性列表
-     */
-    options: { multipleSlots: true },
+    options: { addGlobalClass: true },
     properties: {
         datetime: { type: String, value: '' },
         name: { type: String, value: '' }
     },
-    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
-    attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
-    ready: function () { },
+    data: {
+        // 时间列表
+        dateTimeArray: null,
+        // 默认显示时间
+        valueDateTime: null,
+        // 开始时间
+        startDateTime: '',
+        // 结束时间
+        endDateTime: '',
+    },
     pageLifetimes: {
-        // 组件所在页面的生命周期函数
         show: function () {
             const that = this;
             that.search();
+
+            
         },
         hide: function () { },
-        resize: function () { },
-    },
-    /**
-     * 组件的初始数据
-     */
-    data: {
-        dateTimeArray: [],
-        // 计算日期
-        year: '',
-        moneh: '',
+        resize: function (size) { }
     },
     /**
      * 组件的方法列表
      */
     methods: {
-        // 查询时间
         search: function () {
             const that = this;
-            // 计算年份
-            let year = [];
-            let startYear = moment().format('YYYY');
-            let endYear = Number(startYear) + 100;
-            for (var i = startYear; i <= endYear; i++) { year.push(String(i)) }
-            // 数组数据, 年, 月, 日, 时, 分, 秒;
-            let dateTimeArray = [
-                [...year],
-                ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
-                ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'],
-                ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'],
-                ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
-                ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'],
-            ]
-            that.setData({ dateTimeArray })
+            // 计算时间区间
+            let startDateTime = moment().format('YYYY-MM-DD HH:mm');
+            let endDateTime = moment().add(100, 'years').format('YYYY-MM-DD HH:mm');
+            that.setData({ startDateTime: startDateTime, endDateTime: endDateTime })
+            // 获取完整的年月日 时分秒,以及默认显示的数组
+            that.data.unit = ['年', '月', '日', '时', '分']
+            that.data.dateTimePicker = dateTimePicker.newDateTimePicker(startDateTime, endDateTime)
+            let obj = that.data.dateTimePicker.render();
+            let lastArray = obj.dateTimeArray;
+            let lastTime = obj.dateTime;
+            for (let i = 0; i < lastArray.length; i++) {
+                lastArray[i] = lastArray[i].map(m => m + this.data.unit[i])
+            }
+            that.setData({ dateTimeArray: lastArray, valueDateTime: lastTime })
         },
         // 确认选择
-        change: function (e) {
+        change(e) {
             const that = this;
-            let dateTimeArray = that.data.dateTimeArray;
-            let value = e.detail.value;
-            let datetime = dateTimeArray[0][value[0]] + '-' + dateTimeArray[1][value[1]] + '-' + dateTimeArray[2][value[2]] + ' ' + dateTimeArray[3][value[3]] + ':' + dateTimeArray[4][value[4]] + ':' + dateTimeArray[5][value[5]];
-            that.triggerEvent('datetimeChange', { datetime: datetime, name: that.properties.name })
+            const valueDateTime = that.data.valueDateTime;
+            const year = that.data.dateTimeArray[0][valueDateTime[0]].replace(/年/, '')
+            const month = that.data.dateTimeArray[1][valueDateTime[1]].replace(/月/, '')
+            const day = that.data.dateTimeArray[2][valueDateTime[2]].replace(/日/, '')
+            const hour = that.data.dateTimeArray[3][valueDateTime[3]].replace(/时/, '')
+            const minute = that.data.dateTimeArray[4][valueDateTime[4]].replace(/分/, '')
+            let dateTimeWhole = `${year}-${month}-${day} ${hour}:${minute}`;
+            that.triggerEvent('datetimeChange', { datetime: dateTimeWhole, name: that.properties.name })
         },
-        // 选择月份
+        // 选择时间
         columnChange: function (e) {
             const that = this;
-            const { column, value } = e.detail;
-            let dateTimeArray = that.data.dateTimeArray;
-            let year = '';
-            let month = '';
-            year = that.data.year || dateTimeArray[0][0];
-            month = that.data.month || dateTimeArray[1][0];
-            if (column == '0') year = dateTimeArray[0][value];
-            if (column == '1') month = dateTimeArray[1][value];
-            that.setData({ year: year, month: month });
-            // 计算当前选择年月所要显示的日期
-            let ym = that.data.year + '-' + that.data.month;
-            let test = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
-            let monthDay = moment(ym).daysInMonth();
-            let thr = test.splice(0, monthDay);
-            that.setData({ 'dateTimeArray[2]': thr })
+            // 时间列表
+            const dateTimeArray = that.data.dateTimeArray;
+            const { column, value } = e.detail
+            let dateTimeTemp = 'valueDateTime[' + column + ']'
+            that.setData({ [dateTimeTemp]: value })
+            // 当前选择的值
+            const valueDateTime = that.data.valueDateTime;
+            that.data.dateTimePicker.setValue({ dateTimeArray: dateTimeArray, dateTime: valueDateTime })
+            for (let i = 1; i < valueDateTime.length; i++) {
+                if (column == i - 1) {
+                    for (let j = i; j < valueDateTime.length; j++) {
+                        let temp = 'valueDateTime[' + j + ']'
+                        that.setData({ [temp]: 0 })
+                    }
+                }
+                let arr = that.data.dateTimePicker.dispatch(i).map(m => m + that.data.unit[i])
+                let temp1 = 'dateTimeArray[' + i + ']';
+                that.setData({ [temp1]: arr })
+            }
+            that.setData({ dateTimeArray: dateTimeArray, valueDateTime: valueDateTime })
         },
     }
-})
-
+})

+ 2 - 2
commpents/datetime-picker/index.wxml

@@ -1,3 +1,3 @@
-<picker mode="multiSelector" value="{{datetime}}" bindchange="change" bindcolumnchange="columnChange" range="{{dateTimeArray}}">
-    <view class="text">{{datetime||'选择日期时间'}}</view>
+<picker mode="multiSelector" value="{{valueDateTime}}" bindchange="change" bindcolumnchange="columnChange" range="{{dateTimeArray}}">
+    <view>{{datetime ||"请选择时间"}}</view>
 </picker>

+ 1 - 1
pages/test/index.js

@@ -13,7 +13,7 @@ Page({
         img_url: [],
         // dialog弹框
         dialog: { title: '弹框标题', show: false, type: '1' },
-        form: {}
+        form: {},
     },
     initValidate() {
         const rules = { start_time: { required: true } }

+ 262 - 0
utils/datePicker.js

@@ -0,0 +1,262 @@
+class BaseInfo {
+    constructor() { this.newDate = new Date(); }
+    withData(param) {
+        return parseInt(param) < 10 ? '0' + param : '' + param;
+    }
+    getLoopArray(start, end) {
+        var start = start || 0;
+        var end = end || 0;
+        var array = [];
+        for (var i = start; i <= end; i++) {
+            array.push(this.withData(i));
+        }
+        return array;
+    }
+    formatArr(dateString) {
+        return [...dateString.split(' ')[0].split('-'), ...dateString.split(' ')[1].split(':')]
+    }
+    beforeDateArr(disYear) {
+        /*
+         * 处理前
+         * 获取当前时间
+         */
+        let year = this.newDate.getFullYear() - (disYear || 0)
+        let month = this.newDate.getMonth() + 1
+        let day = this.newDate.getDate()
+        let hour = this.newDate.getHours()
+        let minute = this.newDate.getMinutes()
+        return [year, month, day, hour, minute]
+    }
+    afterDateArr() {
+        /*
+         * 处理后
+         * 获取当前时间
+         */
+        let year = this.withData(this.newDate.getFullYear())
+        let mont = this.withData(this.newDate.getMonth() + 1)
+        let date = this.withData(this.newDate.getDate())
+        let hour = this.withData(this.newDate.getHours())
+        let minu = this.withData(this.newDate.getMinutes())
+        return [year, mont, date, hour, minu];
+    }
+}
+
+// 实现
+class dateTimePicker extends BaseInfo {
+    constructor(startDate, endDate, defaultDate) {
+        super();
+        this.dateTimeArray = null
+        this.dateTime = null
+        this.startDate = super.formatArr(startDate); // 开始时间
+        this.endDate = endDate ? super.formatArr(endDate) : super.afterDateArr(); // 结束时间
+        this.defaultDate = defaultDate ? super.formatArr(defaultDate) : this.startDate;
+    }
+    setValue(obj) {
+        for (let key in obj) {
+            this[key] = obj[key]
+        }
+    }
+    /* 获取当前切换选择的日期值*/
+    getCurDateInfo() {
+        return this.dateTime && this.dateTimeArray ? {
+            year: this.dateTimeArray[0][this.dateTime[0]],
+            month: this.dateTimeArray[1][this.dateTime[1]],
+            day: this.dateTimeArray[2][this.dateTime[2]],
+            hour: this.dateTimeArray[3][this.dateTime[3]],
+            second: this.dateTimeArray[4][this.dateTime[4]],
+        } : {}
+    }
+    /* 获取月数组*/
+    getMonths() {
+        let array = []
+        const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
+        if (this.startDate[0] == this.endDate[0]) {
+            /* 
+             * 开始的年和结束的年相同
+             * 就取(开始月,结束月)
+             */
+            array = super.getLoopArray(parseInt(this.startDate[1]), parseInt(this.endDate[1]))
+        } else {
+            switch (year) {
+                case this.startDate[0]:
+                    /* 开始年 */
+                    array = super.getLoopArray(parseInt(this.startDate[1]), 12)
+                    break;
+                case this.endDate[0]:
+                    /* 结束年 */
+                    array = super.getLoopArray(1, parseInt(this.endDate[1]))
+                    break;
+                default:
+                    array = super.getLoopArray(1, 12)
+                    break;
+            }
+        }
+
+        return array;
+    }
+    /* 获取日数组*/
+    getDays() {
+        let array = []
+        let lastDay = null
+        const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
+        const month = (this.getCurDateInfo().month || this.defaultDate[1]).replace(/月/, '');
+        const flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
+        switch (month) {
+            case '01':
+            case '03':
+            case '05':
+            case '07':
+            case '08':
+            case '10':
+            case '12':
+                lastDay = 31
+                break;
+            case '04':
+            case '06':
+            case '09':
+            case '11':
+                lastDay = 30
+                break;
+            case '02':
+                lastDay = flag ? 29 : 28
+                break;
+            default:
+                array = '月份格式不正确,请重新输入!'
+        }
+        const afterDateArr = super.afterDateArr()
+        const _start = year == this.startDate[0] && month == this.startDate[1]
+        const _end = year == this.endDate[0] && month == this.endDate[1]
+        if (this.startDate[0] == this.endDate[0] && this.startDate[1] == this.endDate[1]) {
+            /*
+             * 开始的年和结束的年相同,开始月和结束月相同
+             * 就取(开始日,结束日)
+             */
+            array = super.getLoopArray(parseInt(this.startDate[2]), parseInt(this.endDate[2]))
+        } else {
+            if (_start) { // 开始年月
+                array = super.getLoopArray(parseInt(this.startDate[2]), lastDay)
+            } else if (_end) { // 结束年月
+                array = super.getLoopArray(1, parseInt(this.endDate[2]))
+            } else {
+                array = super.getLoopArray(1, lastDay)
+            }
+        }
+
+        return array;
+    }
+
+    /* 获取小时数组*/
+    getHours() {
+        let array = []
+        const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
+        const month = (this.getCurDateInfo().month || this.defaultDate[1]).replace(/月/, '');
+        const day = (this.getCurDateInfo().day || this.defaultDate[2]).replace(/日/, '');
+        const _start = year == this.startDate[0] && month == this.startDate[1] && day == this.startDate[2]
+        const _end = year == this.endDate[0] && month == this.endDate[1] && day == this.endDate[2]
+        const _equal = this.startDate[0] == this.endDate[0] && this.startDate[1] == this.endDate[1] && this.startDate[
+            2] == this.endDate[2]
+        if (_equal) {
+            /*
+             * 开始的年月日和结束的年月日都相同
+             * 就取(开始小时,结束小时)
+             */
+            array = super.getLoopArray(parseInt(this.startDate[3]), parseInt(this.endDate[3]))
+        } else {
+            if (_start) { // 开始年月日
+                array = super.getLoopArray(parseInt(this.startDate[3]), 23)
+            } else if (_end) { // 结尾年月日
+                array = super.getLoopArray(0, parseInt(this.endDate[3]))
+            } else {
+                array = super.getLoopArray(0, 23)
+            }
+        }
+        return array;
+    }
+    /* 获取分钟数组*/
+    getMinutes(years, months, days, hours) {
+        let array = []
+        const year = (this.getCurDateInfo().year || this.defaultDate[0]).replace(/年/, '');
+        const month = (this.getCurDateInfo().month || this.defaultDate[1]).replace(/月/, '');
+        const day = (this.getCurDateInfo().day || this.defaultDate[2]).replace(/日/, '');
+        const hour = (this.getCurDateInfo().hour || this.defaultDate[3]).replace(/时/, '');
+        const _start = year == this.startDate[0] && month == this.startDate[1] && day == this.startDate[2] && hour == this
+            .startDate[3]
+        const _end = year == this.endDate[0] && month == this.endDate[1] && day == this.endDate[2] && hour == this
+            .endDate[3]
+        const _equal = this.startDate[0] == this.endDate[0] && this.startDate[1] == this.endDate[1] && this.startDate[
+            2] == this.endDate[2] && this.startDate[3] == this.endDate[3]
+        if (_equal) {
+            /*
+             * 开始的年月日时和结束的年月日时都相同
+             * 就取(开始小时,结束小时)
+             */
+            array = super.getLoopArray(parseInt(this.startDate[4]), parseInt(this.endDate[4]))
+        } else {
+            if (_start) { // 开始年月日
+                array = super.getLoopArray(parseInt(this.startDate[4]), 59)
+            } else if (_end) { // 结尾年月日
+                array = super.getLoopArray(0, parseInt(this.endDate[4]))
+            } else {
+                array = super.getLoopArray(0, 59)
+            }
+        }
+        return array;
+    }
+    /*  */
+    dispatch(index) {
+        let arr = []
+        switch (index) {
+            case 0:
+                arr = super.getLoopArray(this.startDate[0], this.endDate[0]);
+                break;
+            case 1:
+                arr = this.getMonths();
+                break;
+            case 2:
+                arr = this.getDays();
+                break;
+            case 3:
+                arr = this.getHours();
+                break;
+            case 4:
+                arr = this.getMinutes();
+                break;
+            default:
+                break;
+        }
+        return arr
+    }
+
+    /* 初始默认数据 */
+    render() {
+        const dateTime = []
+        const dateTimeArray = [
+            [],
+            [],
+            [],
+            [],
+            []
+        ];
+        /*年月日 时分秒*/
+        for (let i = 0; i < dateTimeArray.length; i++) {
+            dateTimeArray[i] = this.dispatch(i)
+        }
+        dateTimeArray.forEach((current, index) => {
+            const _index = current.indexOf(this.defaultDate[index])
+            dateTime.push(_index == -1 ? 0 : _index);
+        });
+
+        return {
+            dateTimeArray,
+            dateTime
+        }
+    }
+}
+
+function newDateTimePicker(startDateTime, endDateTime, pText) {
+    let newDateTimePicker = new dateTimePicker(startDateTime, endDateTime, pText)
+    return newDateTimePicker
+}
+module.exports = {
+    newDateTimePicker: newDateTimePicker,
+}

+ 0 - 80
utils/dateTimePicker.js

@@ -1,80 +0,0 @@
-function withData(param) {
-    return param < 10 ? '0' + param : '' + param;
-}
-function getLoopArray(start, end) {
-    var start = start || 0;
-    var end = end || 1;
-    var array = [];
-    for (var i = start; i <= end; i++) {
-        array.push(withData(i));
-    }
-    return array;
-}
-function getMonthDay(year, month) {
-    var flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0), array = null;
-
-    switch (month) {
-        case '01':
-        case '03':
-        case '05':
-        case '07':
-        case '08':
-        case '10':
-        case '12':
-            array = getLoopArray(1, 31)
-            break;
-        case '04':
-        case '06':
-        case '09':
-        case '11':
-            array = getLoopArray(1, 30)
-            break;
-        case '02':
-            array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28)
-            break;
-        default:
-            array = '月份格式不正确,请重新输入!'
-    }
-    return array;
-}
-function getNewDateArry() {
-    // 当前时间的处理
-    var newDate = new Date();
-    var year = withData(newDate.getFullYear()),
-        mont = withData(newDate.getMonth() + 1),
-        date = withData(newDate.getDate()),
-        hour = withData(newDate.getHours()),
-        minu = withData(newDate.getMinutes()),
-        seco = withData(newDate.getSeconds());
-
-    return [year, mont, date, hour, minu, seco];
-}
-function dateTimePicker(startYear, endYear, date) {
-    // 返回默认显示的数组和联动数组的声明
-    var dateTime = [], dateTimeArray = [[], [], [], [], [], []];
-    var start = startYear || 1978;
-    var end = endYear || 2100;
-    // 默认开始显示数据
-    var defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry();
-    // 处理联动列表数据
-    /*年月日 时分秒*/
-    dateTimeArray[0] = getLoopArray(start, end);
-    dateTimeArray[1] = getLoopArray(1, 12);
-    dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
-    dateTimeArray[3] = getLoopArray(0, 23);
-    dateTimeArray[4] = getLoopArray(0, 59);
-    dateTimeArray[5] = getLoopArray(0, 59);
-
-    dateTimeArray.forEach((current, index) => {
-        dateTime.push(current.indexOf(defaultDate[index]));
-    });
-
-    return {
-        dateTimeArray: dateTimeArray,
-        dateTime: dateTime
-    }
-}
-module.exports = {
-    dateTimePicker: dateTimePicker,
-    getMonthDay: getMonthDay
-}