web
2025-07-11 397d00d95a766ab838986edf6df4ea754474f0be
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
<template>
    <view class="device">
        <Navbar title="设备列表"></Navbar>
        <view class="content">
            <view class="list">
                <block v-for="(item,index) in facitilyList" :key="index">
                    <view class="item">
                        <view class="item-img">
                            <image v-if="item.facilityUrl" :src="BASE_URL + '/upload' + item.facilityUrl" alt='' mode="aspectFit"></image>
                            <image v-else src="../../static/images/facilityImg.svg" alt='' mode="aspectFit"></image>
                        </view>
                        <view class="item-info">
                            <view class="info-name">
                                <view class="name">{{item.facilityName}}</view>
                                <view class="btn" v-if="item.state === 2" @click="handleQushui(item.id)">取水</view>
                                <view v-else>设备使用中...</view>
                            </view>
                            <view class="info-addr" @click="openMap(item.lat,item.lon)">
                                <text class="addr-text">地址:{{item.address}}</text>
                                <view class="addr-icon">
                                    <image src="../../static/images/icon-point.svg" alt=''></image>
                                    <text>{{item.distanceValue}}km</text>
                                </view>
                            </view>
                        </view>
                    </view>
                </block>
            </view>
        </view>
        <uni-popup ref="qushuiDialog" type="dialog">
            <uni-popup-dialog type="info" cancelText="取消" confirmText="确认" title="请输入取水量" 
                :before-close="true"
                @confirm="dialogConfirm"
                @close="dialogClose"
            >
                <view>
                    <input v-model="qushuiNum" placeholder="请输入取水量(m³)" />
                </view>
            </uni-popup-dialog>
        </uni-popup>
    </view>
</template>
 
<script setup>
    import{ ref, onMounted } from 'vue'
    import Navbar from '../../components/navbar/navbar.vue'
    import { getFacitily, getWater } from '@/api/index.js'
    import { BASE_URL } from '../../config/index.js'
    
    const userLocation = ref()
    const facitilyList = ref([])
    const qushuiDialog = ref()
    const facilityId = ref()
    const qushuiNum = ref()
    
    let timer = null
    
    const getFacitilyList = () => {
        const data = {
            longitude: userLocation.value?.lon,
            latitude: userLocation.value?.lat,
            limit: 100,
            page: 1
        }
        getFacitily(data).then(res => {
            if(res.code === 200) {
                facitilyList.value = res.data.list
            }
        })
        
        // 重新挂在定时器
        clearInterval(timer)
        timer = setInterval(() => {
            getFacitilyList()
        }, 10000)
    }
    
    // 取水
    const handleQushui = (id) => {
        facilityId.value = id
        qushuiDialog.value.open()
    }
    
    // 取消
    const dialogClose = () => {
        facilityId.value = ''
        qushuiNum.value = ''
        qushuiDialog.value.close()
    }
    
    // 确认
    const dialogConfirm = () => {
        let reg = /^(0|([1-9][0-9]*))(\.[\d]+)?$/
        if(!reg.test(qushuiNum.value)) {
            uni.showToast({
                title:'取水量为大于零的数字',
                icon:'none'
            })
            return
        }
        const data = {
            facilityId: facilityId.value,
            amount: Number(qushuiNum.value)
        }
        getWater(data).then(res => {
            if(res.code === 200) {
                uni.showToast({
                    title:res.data,
                    icon:'success'
                })
                dialogClose()
                getFacitilyList()
            }else{
                uni.showToast({
                    title:res.msg,
                    icon:'error'
                })
            }
        }).catch(err => {
            uni.showToast({
                title:'指令发送失败,请稍后重试',
                icon:'error'
            })
        })
    }
    
    //打开地图导航
    const openMap = (lat,lon) => {
        uni.openLocation({
            latitude: parseFloat(lat),
            longitude: parseFloat(lon),
            scale:18
        })
    }
    
    //获取用户位置并存储
    const storageLocation = () => {
        uni.getLocation({
            type:'gcj02',
            isHighAccuracy:true,
            success:(res) =>{
                userLocation.value = {
                    lat: res.latitude,
                    lon: res.longitude
                }
                uni.setStorageSync('userLocation',JSON.stringify(userLocation.value))
                getFacitilyList()
            },
            fail:(err) =>{
                locationToast()
            }
        })
    }
    //用户拒绝授权
    const locationToast = () => {
        uni.showModal({
            title: "请求授权当前位置",
            content: "请求获取您的位置,加载附近取水设备信息!",
            confirmText: "前往设置",
            success: (res) => {
                if (res.confirm) {
                    uni.openSetting({
                         success:(res1) =>{ //打开设置成功
                            if (res1.authSetting['scope.userLocation']){
                                setTimeout(() =>{
                                    storageLocation()
                                },1000)
                            }
                        },
                    })
                }else{
                    uni.showToast({
                        title: '请先授权!',
                        duration: 2000,
                        icon:'none'
                    });
                }
            },
        });
    }
    
    onMounted(() => {
        const location = uni.getStorageSync('userLocation')
        if(location) {
            userLocation.value = JSON.parse(location)
            getFacitilyList()
        }else{
            locationToast()
        }
    })
</script>
 
<style lang="scss" scoped>
    .content{
        width: 100%;
        height:calc(100vh - 176rpx);
        background:linear-gradient(to top,#FFFFFF,#E8EFFF);
        padding:32rpx 32rpx 100rpx;
        box-sizing: border-box;
        .list{
            width:100%;
            height:100%;
            overflow: scroll;
            // background-color: #f1ffef;
            .item{
                height:200rpx;
                width:100%;
                background-color: #FFFFFF;
                border-radius: 24rpx;
                padding:0 20rpx;
                margin-bottom:20rpx;
                box-sizing: border-box;
                display: flex;
                align-items:center;
                .item-img{
                    image{
                        width:130rpx;
                        height: 130rpx;
                    }
                }
                .item-info{
                    display: flex;
                    flex-direction: column;
                    margin-left:20rpx;
                    justify-content: space-between;
                    .info-name{
                        color:#222c35;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        .btn{
                            padding: 10rpx 20rpx;
                            background-color: $uni-primary;
                            border-radius: 10rpx;
                            color: #fff;
                        }
                    }
                    .info-addr{
                        display: flex;
                        justify-content: space-between;
                        align-items:center;
                        .addr-text{
                            width:340rpx;
                            flex-shrink: 0;
                            height:80rpx;
                            color: #6a6e75;
                            font-size:26rpx;
                        }
                        .addr-icon{
                            width: 150rpx;
                            flex-shrink: 0;
                            display: flex;
                            align-items:center;
                            justify-content: flex-end;
                            image{
                                width: 40rpx;
                                height: 40rpx;
                            }
                            text{
                                color: #6a6e75;
                                font-size:22rpx;
                            }
                        }
                    }
                }
            }
        }
    }
</style>