zhaojs
2023-09-15 39e2103e28830337c3be4dbd3dc7ad0a829ee78b
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!-- 轮播滚动组件 -->
 
<template>
    <view class="flex row bg-white">
        <view v-if="textShow" class="family700 size16 line20 bg-clip-text col-transparent"
            :style="{backgroundImage: gradients ? 'linear-gradient(to bottom right, ' + textColor + ', '+ colorRgba(textColor,0.2) +')' : '',color:gradients ? '' : textColor}">
            {{ text }}
        </view>
        <swiper circular autoplay vertical current="1" :interval="interval" duration="1000" :display-multiple-items="2"
            class="swiper marginLR15" :style="{color: titleColor}">
            <swiper-item v-if="list.length > 3" v-for="(item,index) in list" :key="index" @click="$emit('active', item)"
                class="flex jbetween">
                <view class="ellipsis">{{item[title]}}</view>
                <view v-if="timeShow" class="white-space size12 color9 paddingL5">
                    {{ timeFrom(item[time]) }}
                </view>
            </swiper-item>
            <swiper-item v-if="list.length == 3" v-for="(item,index) in 4" :key="index"
                @click="$emit('active', list[item-1])">
                <view v-if="item < 4" class="flex jbetween">
                    <view class="ellipsis">{{list[item-1][title]}}</view>
                    <view v-if="timeShow" class="white-space size12 color9 paddingL5">
                        {{ timeFrom(list[item-1][time]) }}
                    </view>
                </view>
                <view v-else class="flex jbetween">
                    <view class="ellipsis">{{list[1][title]}}</view>
                    <view v-if="timeShow" class="white-space size12 color9 paddingL5">
                        {{ timeFrom(list[1][time]) }}
                    </view>
                </view>
            </swiper-item>
            <swiper-item v-if="list.length == 0" v-for="item in 3" :key="item" class="flex jbetween">
                <view class="ellipsis">暂无 {{ text }}</view>
                <view v-if="timeShow" class="white-space size12 color9 paddingL5">刚刚</view>
            </swiper-item>
            <swiper-item v-if="list.length == 1" v-for="item in 3" :key="item" @click="$emit('active', list[0])"
                class="flex jbetween">
                <view class="ellipsis">{{list[0][title]}}</view>
                <view v-if="timeShow" class="white-space size12 color9 paddingL5">
                    {{ timeFrom(list[0][time]) }}
                </view>
            </swiper-item>
            <swiper-item v-if="list.length == 2" v-for="(item,index) in 4" :key="item"
                @click="$emit('active', list[index%list.length])" class="flex jbetween">
                <!-- #ifdef MP-WEIXIN -->
                <view class="ellipsis" v-if="item == 0">{{list[0][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 0">
                    {{ timeFrom(list[0][time]) }}
                </view>
                <view class="ellipsis" v-if="item == 1">{{list[1][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 1">
                    {{ timeFrom(list[1][time]) }}
                </view>
                <view class="ellipsis" v-if="item == 2">{{list[0][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 2">
                    {{ timeFrom(list[0][time]) }}
                </view>
                <view class="ellipsis" v-if="item == 3">{{list[1][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 3">
                    {{ timeFrom(list[1][time]) }}
                </view>
                <!-- #endif -->
                <!-- #ifndef MP-WEIXIN -->
                <view class="ellipsis" v-if="item == 1">{{list[0][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 1">
                    {{ timeFrom(list[0][time]) }}
                </view>
                <view class="ellipsis" v-if="item == 2">{{list[1][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 2">
                    {{ timeFrom(list[1][time]) }}
                </view>
                <view class="ellipsis" v-if="item == 3">{{list[0][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 3">
                    {{ timeFrom(list[0][time]) }}
                </view>
                <view class="ellipsis" v-if="item == 4">{{list[1][title]}}</view>
                <view class="white-space size12 color9 paddingL5" v-if="timeShow && item == 4">
                    {{ timeFrom(list[1][time]) }}
                </view>
                <!-- #endif -->
            </swiper-item>
        </swiper>
    </view>
</template>
 
<script>
    /**
     * @description 轮播滚动
     * @example <h-carousel-roll :list="list"></h-carousel-roll>
     * 
     * @property {String}            text                标题文字(默认 通知公告 )
     * @property {Boolean}            textShow            是否展示标题文字(默认 true )
     * @property {String}            textColor            标题文字颜色(默认 #f15723 )
     * @property {Boolean}            gradients            字体是否开启渐变色(默认 true )
     * @property {Array}            list                滚动数据
     * @property {String}            title                滚动数据标题字段(默认 title)
     * @property {String}            titleColor            滚动数据标题颜色(默认 #000000)
     * @property {String}            time                滚动数据时间字段(默认 time)
     * @property {Boolean}            timeShow            是否展示时间(默认 true )
     * @property {Number}            interval            轮播自动切换时间间隔(默认 5000 )
     * 
     * @event {Function} active            点击数据时触发
     */
    export default {
        // 小程序样式穿透
        options: {
            styleIsolation: 'shared'
        },
        name: "h-carousel-roll",
        props: {
            // 标题文字
            text: {
                type: String,
                default: '通知公告'
            },
            // 是否展示标题文字
            textShow: {
                type: Boolean,
                default: true
            },
            // 标题文字颜色
            textColor: {
                type: String,
                default: '#f15723'
            },
            // 字体是否开启渐变色
            gradients: {
                type: Boolean,
                default: true
            },
            // 滚动数据
            list: {
                type: Array,
                default: () => []
            },
            // 滚动数据标题字段
            title: {
                type: String,
                default: 'title'
            },
            // 滚动数据标题颜色
            titleColor: {
                type: String,
                default: '#000000'
            },
            // 滚动数据时间字段
            time: {
                type: String,
                default: 'time'
            },
            // 是否展示时间
            timeShow: {
                type: Boolean,
                default: true
            },
            // 轮播自动切换时间间隔
            interval: {
                type: Number,
                default: 5000
            },
        },
        data() {
            return {}
        },
        methods: {
            // 转换时间
            timeFrom(time) {
                if (time) {
                    let arrs = time.split(/[- :]/)
                    let times = new Date(arrs[0], arrs[1] - 1, arrs[2], arrs[3], arrs[4], arrs[5]);
                    return uni.$u.timeFrom(new Date(times).getTime(), false)
                }
            },
            // 颜色转换
            colorRgba(sHex, alpha = 1) {
                // 十六进制颜色值的正则表达式
                var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
                /* 16进制颜色转为RGB格式 */
                let sColor = sHex.toLowerCase()
                if (sColor && reg.test(sColor)) {
                    if (sColor.length === 4) {
                        var sColorNew = '#'
                        for (let i = 1; i < 4; i += 1) {
                            sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
                        }
                        sColor = sColorNew
                    }
                    //  处理六位的颜色值
                    var sColorChange = []
                    for (let i = 1; i < 7; i += 2) {
                        sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
                    }
                    if (alpha == 1) {
                        return sColorChange.join(',')
                    } else {
                        return 'rgba(' + sColorChange.join(',') + ',' + alpha + ')'
                    }
                } else {
                    return sColor
                }
            }
        }
    }
</script>
<style lang="scss" scoped>
    page {
        background: #f9f9f9;
        font-family: '微软雅黑', sans-serif;
        font-size: 28rpx;
        min-height: 100%;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }
 
    .bg-white {
        background-color: #FFFFFF !important;
    }
 
    .row {
        flex-direction: row;
    }
 
    .flex {
        display: flex;
    }
 
    .family700 {
        font-weight: 700;
    }
 
    .line20 {
        line-height: 40rpx;
    }
 
    .col-transparent {
        color: transparent;
    }
 
    .marginLR15 {
        margin: 0 30rpx;
    }
 
    .jbetween {
        justify-content: space-between;
    }
 
    /* 不换行显示省略号 */
    .ellipsis {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
 
    /* 不换行 */
    .white-space {
        white-space: nowrap;
    }
 
    .size12 {
        font-size: 24rpx;
    }
 
    .size16 {
        font-size: 32rpx;
    }
 
    .color9 {
        color: #999999;
    }
 
    .paddingL5 {
        padding-left: 10rpx;
    }
 
    .swiper {
        width: 100%;
        height: 108rpx;
        padding: 16rpx 0;
    }
 
    .bg-clip-text {
        padding: 30rpx 0 30rpx 30rpx;
        -webkit-background-clip: text;
        background-clip: text;
        min-width: 70rpx;
        width: 70rpx;
    }
 
    /deep/ .uni-swiper-slide-frame {
        line-height: 54rpx;
    }
</style>