qingyiay
2023-09-12 393cdcbef072e52f3a046e884c8349c903c4081f
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
<template>
    <view class="message-body">
        <u-empty mode="data"
            icon="http://cdn.uviewui.com/uview/empty/data.png"
            v-if="messageList.length == 0"
            width="400"
            height="400"
            textSize="18"
            text="暂无数据"></u-empty>
        <view class="message-list">
            <u-list @scrolltolower="scrolltolower"
                :height="1400">
                <u-list-item>
                    <u-swipe-action>
                        <u-swipe-action-item :options="options1"
                            :threshold="50"
                            v-for="(item, index) in messageList"
                            :key="item.id"
                            @click="deleteMsg(item, index)">
                            <u-badge :isDot="true"
                                type="error"
                                v-if="item.status !== 1"></u-badge>
                            <u-cell size="large"
                                :title="item.title"
                                @click="messageDetails(item, index)">
                                <u-parse :content="item.content"
                                    slot="label"></u-parse>
 
                                <view class="prepose"
                                    slot="icon"><u-icon name="volume"
                                        color="#035CFB"
                                        size="40"></u-icon></view>
                            </u-cell>
                        </u-swipe-action-item>
                    </u-swipe-action>
                </u-list-item>
                <!-- <view class="more-text"
                    v-if="showMoreData && messageList.length !== 0">没有数据了...</view> -->
            </u-list>
        </view>
        <u-modal :show="show"
            :content="content"
            :showCancelButton="true"
            @confirm="confirm"
            @cancel="cancel"></u-modal>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                pageNum: 1,
                pageSize: 10,
                total: '',
                messageList: [],
                showMoreData: false,
                options1: [{
                    text: '删除'
                }],
                show: false,
                content: '是否确认删除',
                id: '',
                index: ''
            };
        },
        onShow() {
            this.messageReq();
        },
        onHide() {
            this.messageList = [];
            this.pageNum = 1;
        },
        methods: {
            // 触底加载
            scrolltolower() {
                if (this.pageNum * this.pageSize >= this.total) return (this.showMoreData = true);
                this.pageNum++;
                this.messageReq();
            },
            // 获取消息列表
            messageReq() {
                uni.showLoading({
                    title: '加载中'
                });
                this.$reqGet('getMessageByUser', { current: this.pageNum, size: this.pageSize }).then(res => {
                    if (res.code == 0) {
                        uni.hideLoading();
                        this.total = res.data.total;
 
                        function removeTags(str) {
                            return str.replace(/<\/?[^>]+>/gi, '');
                        }
                        if (this.pageNum > 1) {
                            this.messageList = this.messageList.concat(res.data.records);
                            this.messageList = this.messageList.map(v => {
                                return {
                                    ...v,
                                    title: v.title.slice(0, 8) + '...',
                                    content: removeTags(v.content).trim().slice(0, 8) + '...'
                                };
                            });
                        } else if (this.pageNum == 1) {
                            this.messageList = res.data.records;
                            this.messageList = this.messageList.map(v => {
                                return {
                                    ...v,
                                    title: v.title.slice(0, 8) + '...',
                                    content: removeTags(v.content).trim().slice(0, 8) + '...'
                                };
                            });
                        }
                    } else {
                        this.$u.toast('加载失败');
                    }
                });
            },
            // 已读消息
            messageDetails(value, index) {
                if (value.status == 0) {
                    this.$reqAllJson('readMessage', {
                        id: value.id,
                        status: value
                            .status
                    }, { method: 'PUT', 'Content-Type': 'application/json' }).then(res => {
                        if (res.code == 0) {
                            uni.navigateTo({
                                url: `/pages/public-page/messageDetails/messageDetails?messageId=${value.messageId}&id=${value.id}`
                            });
                        }
                    });
                } else {
                    uni.navigateTo({
                        url: `/pages/public-page/messageDetails/messageDetails?messageId=${value.messageId}&id=${value.id}`
                    });
                }
            },
            // 删除消息
            deleteMsg(value, index) {
                this.show = true;
                this.id = value.id;
            },
            // 确认删除
            confirm() {
                this.show = false;
                this.$reqGet('delteMessage', { id: this.id }).then(res => {
                    if (res.code == 0) {
                        this.$u.toast('删除成功');
                        setTimeout(() => {
                            uni.showLoading({
                                title: '加载中'
                            });
                            this.$reqGet('getMessageByUser', { current: 1, size: this.pageSize }).then(
                                res => {
                                    uni.hideLoading();
                                    this.total = res.data.total;
                                    this.messageList = [];
                                    this.pageNum = 1
                                    this.messageList = res.data.records;
                                });
                        }, 800);
                    } else {
                        this.$u.toast('删除失败');
                    }
                });
            },
            cancel() {
                this.show = false;
            }
        }
    };
</script>
 
<style lang="scss"
    scoped>
    @mixin flex {
        display: flex;
        justify-content: center;
        align-items: center;
    }
 
    ::v-deep .u-list-item- {
        position: relative;
 
        .u-badge {
            position: absolute;
            left: vww(45);
            top: vww(13);
        }
    }
 
    ::v-deep .u-cell__title-text {
        height: 32rpx;
        font-size: 34rpx !important;
        font-weight: bold;
        color: #2f2f2f !important;
        margin-bottom: vww(18);
    }
 
    .message-body {
        width: 100%;
        height: 100%;
 
        .message-list {
            margin: vww(5) 0;
        }
 
        .prepose {
            width: 89rpx;
            height: 89rpx;
            background: rgba(237, 240, 245, 0.75);
            border-radius: 50%;
            @include flex;
            margin-right: vww(32);
        }
 
        .more-text {
            color: #999;
            font-size: 24rpx;
            text-align: center;
        }
    }
</style>