zhangxiaoxu123456
2021-12-17 4d4a844ef624dde8667b314d353599297f6cb8d9
1
2
3
4
5
6
7
8
9
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
60
61
62
<template>
    <view class="u-demo">
        <view class="u-demo-wrap">
            <view class="u-demo-title">演示效果</view>
            <view class="u-demo-area">
                <u-toast ref="uToast"></u-toast>
                <view class="u-no-demo-here" style="text-align: left;">
                    根据当前时间,返回类似"刚刚,5分钟前,8小时前,3天前"等字样
                </view>
                <view class="u-demo-result-line">
                    {{result}}
                </view>
            </view>
        </view>
        <view class="u-config-wrap">
            <view class="u-config-title u-border-bottom">
                参数配置
            </view>
            <view class="u-config-item">
                <view class="u-item-title">时间</view>
                <u-subsection :list="timeArr1" @change="timeArr1Change"></u-subsection>
                <u-gap></u-gap>
                <u-subsection style="margin-top: 40rpx;" :list="timeArr2" @change="timeArr2Change"></u-subsection>
            </view>
        </view>
    </view>
</template>
 
<script>
    export default {
        data() {
            // 微信小程序无法动态修改u-subsection的list参数,导致onLoad中赋值timeArr1,timeArr2无效,故在data中直接赋值
            let nowTime = Number(+ new Date());
            let threeDayAgo = nowTime - 2 * 86400000;
            let arr1 = [0, 0], arr2 = [0, 0];
            [0, 0].map((val, index) => {
                arr1[index] = this.$u.timeFormat(this.$u.random(threeDayAgo, nowTime), 'yyyy/mm/dd hh:MM:ss');
                arr2[index] = this.$u.timeFormat(this.$u.random(threeDayAgo, nowTime), 'yyyy/mm/dd hh:MM:ss');
            })
            return {
                timeArr1: arr1,
                timeArr2: arr2,
                result: null
            }
        },
        onLoad() {
            this.timeArr1Change(0);
        },
        methods: {
            timeArr1Change(index) {
                this.result = this.$u.timeFrom((new Date(this.timeArr1[index])).getTime());
            },
            timeArr2Change(index) {
                this.result = this.$u.timeFrom((new Date(this.timeArr2[index])).getTime());
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .u-demo {}
</style>