wangzhibo
2025-10-23 cad64c1de2c45856b0774933c5801b05b64984e8
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
<template>
    <view class="page-wrapper">
        <div class="bg-top"></div>
    
        <!--<view class="text-2xl font-bold mb-4 text-center">🏨 酒店介绍</view>    -->
    
        <div class="content-wrapper">
            <!-- 遍历推荐餐饮数据 -->
            <view v-for="(item, index) in restaurants" :key="index"
                class="flex rounded-lg overflow-hidden shadow-md mt-10 h-40 items-center">
                <!-- 左侧图片,占30% -->
                <image :src="item.imgPath" mode="aspectFill" class="w-1-3 h-9-10 rounded-lg object-cover mr-2 ml-2">
                </image>
 
                <!-- 右侧信息,占70% -->
                <view class="flex-1 p-4 flex flex-col justify-between">
                    <div class="space-y-1">
                        <div class="text-lg font-bold text-gray-800 break-words">{{ item.name }}</div>
                        <div class="text-gray-600 break-words">位置:{{ item.location }}</div>
                        <div class="text-gray-600 break-words">类别:{{ item.type }}</div>
                        <div class="text-gray-600 break-words">距离:{{ item.distance }}</div>
                    </div>
                </view>
            </view>
        </div>
        <div class="bg-bottom"></div>
    </view>
</template>
 
<script setup>
    import {
        ref,
        onMounted
    } from 'vue';
    import { restaurants as rawRestaurants } from './foods';
    
    const restaurants = ref(rawRestaurants);
 
 
    function calculateDistance(lat1, lon1, lat2, lon2) {
        const R = 6371000; // 地球半径,单位:米
        const toRad = angle => angle * Math.PI / 180;
    
        const dLat = toRad(lat2 - lat1);
        const dLon = toRad(lon2 - lon1);
    
        const radLat1 = toRad(lat1);
        const radLat2 = toRad(lat2);
    
        const a = Math.sin(dLat / 2) ** 2 +
                  Math.cos(radLat1) * Math.cos(radLat2) *
                  Math.sin(dLon / 2) ** 2;
    
        const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    
        return Math.round(R * c); // 返回单位:米
    }
 
    // 页面加载后执行
    onMounted(() => {
        console.log('页面加载完成,执行初始化逻辑');
        uni.getLocation({
            type: 'gcj02',
            isHighAccuracy: true,
            success: (data) => {
                console.log(data);
                restaurants.value.forEach(item => {
                    if (item.longitude != null && item.latitude != null) {
                      const distance = calculateDistance(
                        data.latitude,
                        data.longitude,
                        item.latitude,
                        item.longitude
                      );
                      console.log(distance)
                      item.distance = distance.toLocaleString() +"米"; // 更新 distance 为字符串
                    }
            });
        },
        })
    });
</script>
 
<style>
    .items-center {
        align-items: center;
    }
 
    .h-9-10 {
        height: 90%;
    }
 
    /* 自定义类,90% */
    .rounded-lg {
        border-radius: 0.5rem;
    }
 
    .mr-2 {
        margin-right: 0.5rem;
    }
 
    .ml-2 {
        margin-left: 0.5rem
    }
 
    .mt-10 {
        margin-top: 10rpx;
    }
 
    .h-40 {
        height: 130px;
    }
 
    /* 外层容器 */
    .p-4 {
        padding: 1rem;
    }
 
    .bg-gray-100 {
        background-color: #f3f4f6;
    }
 
    .min-h-screen {
        min-height: 100vh;
    }
 
    .space-y-4 {
        margin-top: 1rem;
    }
 
    /* 卡片 */
    .flex {
        display: flex;
    }
 
    .bg-white {
        background-color: #ffffff;
    }
 
    .rounded-lg {
        border-radius: 0.5rem;
    }
 
    .overflow-hidden {
        overflow: hidden;
    }
 
    .shadow-md {
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }
 
    /* 左侧图片 */
    .w-1-3 {
        width: 30%;
    }
 
    .h-full {
        height: 100%;
    }
 
    .object-cover {
        object-fit: cover;
    }
 
    /* 右侧内容 */
    .flex-1 {
        flex: 1 1 0%;
    }
 
    .p-4 {
        padding: 1rem;
    }
 
    .flex-col {
        display: flex;
        flex-direction: column;
    }
 
    .justify-between {
        justify-content: space-between;
    }
 
    /* 文字 */
    .space-y-1 {
        margin-top: 0.25rem;
    }
 
    .text-lg {
        font-size: 1.125rem;
        line-height: 1.75rem;
    }
 
    .font-bold {
        font-weight: 700;
    }
 
    .text-gray-800 {
        color: #1f2937;
    }
 
    .text-gray-600 {
        color: #4b5563;
    }
 
    .break-words {
        word-break: break-word;
        white-space: normal;
    }
    .page-wrapper {
      position: relative;
      min-height: 100vh; /* 确保小内容时填满视口 */
      overflow: hidden;
    }
    
    /* 顶部背景 */
    .bg-top {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 500rpx; /* 可用固定像素或图片高度 */
      background: url('https://farm.evcc.top/Uploads/yuanboyuan/background/bg_top.jpg') no-repeat top center;
      background-size: cover;
      z-index: 0;
    }
    
    /* 底部背景 */
    .bg-bottom {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 500rpx; /* 可用固定像素或图片高度 */
      background: url('https://farm.evcc.top/Uploads/yuanboyuan/background/bg_bottom.jpg') no-repeat bottom center;
      background-size: cover;
      z-index: 0;
    }
    
    /* 内容层 */
    .content-wrapper {
      position: relative;
      z-index: 1;
      padding: 2rem 1rem;
    }
</style>