index.vue
9.88 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<template>
<view class="page">
<view class="message-list-fixed">
<view class="search-row">
<uni-search-bar v-model="searchKeyword" radius="6" placeholder="请输入标题" clearButton="auto"
cancelButton="none" bgColor="#F3F3F3" textColor="rgba(0,0,0,0.4)" @confirm="search"
@input="onSearchInput" />
<view class="tool-icons">
<image class="tool-icon" src="/static/images/dev_manage/filter_icon.png" @click="openFilter" />
</view>
</view>
</view>
<view class="list-box">
<card-list ref="cardRef" :fetch-fn="fetchList" :query="query" :extra="extraParams" row-key="id"
:enable-refresh="true" :enable-load-more="true" @loaded="onCardLoaded" @error="onCardError">
<template v-slot="{ item }">
<view class="card" @click.stop="onCardClick(item)">
<view class="card-header">
<text class="title omit2">{{ item.title || '-' }}</text>
</view>
<view class="info-row">
<text>是否已读</text><text>{{ item.readed ? '是' : '否' }}</text>
</view>
<view class="info-row">
<text>创建时间</text><text>{{ item.createTime || '-' }}</text>
</view>
</view>
</template>
</card-list>
</view>
<filter-modal :visible.sync="filterVisible" :value.sync="filterForm" title="筛选" @reset="onFilterReset"
@confirm="onFilterConfirm">
<template v-slot="{ model }">
<view class="filter-form">
<view class="form-item">
<view class="label">是否已读</view>
<uni-data-checkbox mode="tag" :multiple="false" :value-field="'value'" :text-field="'text'"
v-model="model.readed" :localdata="readOptions" />
</view>
<view class="form-item">
<view class="label">创建时间</view>
<uni-datetime-picker type="daterange" v-model="model.dateRange" start="2023-01-01" />
</view>
</view>
</template>
</filter-modal>
</view>
</template>
<script>
import CardList from '@/components/card/index.vue'
import FilterModal from '@/components/filter/index.vue'
import { queryApi as messageQueryApi } from '@/api/message.js'
export default {
name: 'MessageList',
components: { CardList, FilterModal },
data() {
return {
searchKeyword: '',
searchKeywordDebounced: '',
searchDebounceTimer: null,
// 给到 card 的筛选值
query: { readed: '', dateRange: [] },
extraParams: {},
// 筛选弹框
filterVisible: false,
filterForm: { readed: false, dateRange: [] },
readOptions: [
{ value: true, text: '是' },
{ value: false, text: '否' }
],
currentItems: []
}
},
computed: {
extraCombined() {
return {
title: this.searchKeywordDebounced || undefined
}
}
},
watch: {
extraCombined: {
deep: true,
handler(v) {
this.extraParams = v
},
immediate: true
}
},
created() {
const [start, end] = this.getDefaultDateRange()
this.filterForm.dateRange = [start, end]
this.query = { readed: this.filterForm.readed, dateRange: [start, end] }
},
onReachBottom() {
if (this.$refs && this.$refs.cardRef && this.$refs.cardRef.onLoadMore) {
this.$refs.cardRef.onLoadMore()
}
},
beforeDestroy() {
if (this.searchDebounceTimer) {
clearTimeout(this.searchDebounceTimer)
this.searchDebounceTimer = null
}
},
methods: {
// 默认日期范围:当前日期前一个月到今日
getDefaultDateRange() {
const fmt = d => {
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const dd = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${dd}`
}
const end = new Date()
const start = new Date(end)
start.setMonth(start.getMonth() - 1)
return [fmt(start), fmt(end)]
},
onCardLoaded({ items }) {
this.currentItems = items
},
onCardError() {
uni.showToast({ title: '列表加载失败', icon: 'none' })
},
onCardClick(item) {
const id = (item && (item.id || item.code)) || ''
if (!id) return
const query = '?id=' + encodeURIComponent(id)
uni.navigateTo({ url: '/pages/message/detail' + query })
},
// 输入实时搜索:1200ms 防抖
onSearchInput(val) {
if (this.searchDebounceTimer) clearTimeout(this.searchDebounceTimer)
this.searchDebounceTimer = setTimeout(() => {
this.searchKeywordDebounced = this.searchKeyword
this.searchDebounceTimer = null
}, 1200)
},
// 确认搜索
search(e) {
const val = e && e.value != null ? e.value : this.searchKeyword
this.searchKeyword = val
this.searchKeywordDebounced = val
},
openFilter() {
this.filterVisible = true
},
onFilterReset(payload) {
this.filterForm = payload
},
onFilterConfirm(payload) {
if ((payload.readed === '' || payload.readed == null) && this.filterForm.readed !== '') {
payload.readed = this.filterForm.readed
}
this.query = { ...payload }
},
// 列表接口
fetchList({ pageIndex, pageSize, query, extra }) {
const params = {
pageIndex,
pageSize,
...extra,
...query
}
if (Array.isArray(params.dateRange) && params.dateRange.length === 2) {
params.createTimeStart = params.dateRange[0] + ' 00:00:00'
params.createTimeEnd = params.dateRange[1] + ' 23:59:59'
delete params.dateRange
}
if (this.searchKeywordDebounced) {
params.title = this.searchKeywordDebounced
}
return messageQueryApi(params)
.then(res => {
const _data = res.data || {}
const records = _data.datas || []
const totalCount = _data.totalCount || 0
const hasNext = _data.hasNext || false
return { records, totalCount, hasNext }
})
.catch(err => {
console.error('fetchList error', err)
this.onCardError()
return { records: [], totalCount: 0, hasNext: false }
})
}
}
}
</script>
<style lang="scss" scoped>
.page {
display: flex;
flex-direction: column;
height: 100vh;
}
.message-list-fixed {
position: fixed;
top: 96rpx;
left: 0;
right: 0;
z-index: 2;
background: #fff;
.search-row {
display: flex;
align-items: center;
padding: 16rpx 32rpx;
.uni-searchbar {
padding: 0;
flex: 1;
}
.tool-icons {
display: flex;
.tool-icon {
width: 48rpx;
height: 48rpx;
display: block;
margin-left: 32rpx;
}
}
}
}
/* 仅当前页覆盖 uni-search-bar 盒子高度 */
::v-deep .uni-searchbar__box {
height: 80rpx !important;
justify-content: start;
.uni-searchbar__box-search-input {
font-size: 32rpx !important;
}
}
.list-box {
flex: 1;
padding-top: 132rpx;
.card {
position: relative;
}
.card-header {
margin-bottom: 28rpx;
.title {
font-size: 36rpx;
font-weight: 600;
line-height: 50rpx;
color: rgba(0, 0, 0, 0.9);
}
}
.info-row {
display: flex;
align-items: center;
color: rgba(0, 0, 0, 0.6);
font-size: 28rpx;
margin-bottom: 24rpx;
&:last-child {
margin-bottom: 0;
}
text {
width: 60%;
line-height: 32rpx;
&:last-child {
color: rgba(0, 0, 0, 0.9);
width: 40%;
}
}
}
}
.filter-form {
.form-item {
margin-bottom: 24rpx;
}
.label {
margin-bottom: 20rpx;
color: rgba(0, 0, 0, 0.9);
height: 44rpx;
line-height: 44rpx;
font-size: 30rpx;
}
.uni-easyinput {
border: 1rpx solid #f3f3f3;
}
}
/* 深度覆盖 uni-data-checkbox(mode=tag)内部的 tag 展示与间距 */
::v-deep .filter-form .uni-data-checklist .checklist-group {
.checklist-box {
&.is--tag {
width: 212rpx;
margin-top: 0;
margin-bottom: 24rpx;
margin-right: 24rpx;
height: 80rpx;
padding: 0;
border-radius: 12rpx;
background-color: #f3f3f3;
border-color: #f3f3f3;
&:nth-child(3n) {
margin-right: 0;
}
.checklist-content {
display: flex;
justify-content: center;
}
.checklist-text {
color: rgba(0, 0, 0, 0.9);
font-size: 28rpx;
}
}
&.is-checked {
background-color: $theme-primary-plain-bg !important;
border-color: $theme-primary-plain-bg !important;
.checklist-text {
color: $theme-primary !important;
}
}
}
}
</style>