LocationPicker.vue
5.5 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
<template>
<view class="loc-root">
<view class="loc-trigger" @click.stop="openPicker">
<text class="loc-text">{{ displayLocationName || '—' }}</text>
<AppIcon name="chevronDown" size="sm" color="white" />
</view>
<view v-if="showPicker" class="picker-mask" @click="showPicker = false">
<view class="picker-body" @click.stop>
<view class="picker-header">
<text class="picker-title">Switch Location</text>
<view class="picker-close" @click="showPicker = false">
<AppIcon name="plus" size="sm" color="gray" />
</view>
</view>
<view class="picker-list">
<view
v-for="s in stores"
:key="s.id"
class="picker-item"
:class="{ active: currentId === s.id }"
@click="handleSelect(s)"
>
<view class="picker-icon" :class="{ active: currentId === s.id }">
<AppIcon name="mapPin" size="sm" :color="currentId === s.id ? 'white' : 'gray'" />
</view>
<view class="picker-info">
<text class="picker-name">{{ s.locationName }}</text>
<text class="picker-addr">{{ s.fullAddress }}</text>
</view>
<view v-if="currentId === s.id" class="picker-check">
<AppIcon name="check" size="sm" color="white" />
</view>
</view>
</view>
</view>
</view>
</view>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import AppIcon from './AppIcon.vue'
import {
getCurrentStoreId,
switchStore,
getBoundLocations,
getCurrentLocationCode,
} from '../utils/stores'
import type { UsAppBoundLocationDto } from '../types/usAppBound'
const { t } = useI18n()
const stores = ref<UsAppBoundLocationDto[]>([])
const currentId = ref(getCurrentStoreId())
const showPicker = ref(false)
function refreshList() {
stores.value = getBoundLocations().filter((s) => s.state !== false)
}
/** 药丸展示门店名称(与弹窗内选中项一致);无名称时再退回编码 */
const displayLocationName = computed(() => {
const fromStorage = uni.getStorageSync('storeName')
if (typeof fromStorage === 'string' && fromStorage.trim()) return fromStorage.trim()
if (currentId.value) {
const row = stores.value.find((x) => x.id === currentId.value)
if (row?.locationName?.trim()) return row.locationName.trim()
}
const id = getCurrentStoreId()
if (id) {
const row = getBoundLocations().find((x) => x.id === id)
if (row?.locationName?.trim()) return row.locationName.trim()
}
const code = getCurrentLocationCode()
return code || ''
})
function openPicker() {
refreshList()
currentId.value = getCurrentStoreId()
showPicker.value = true
}
watch(showPicker, (v) => {
if (v) refreshList()
})
const handleSelect = (s: UsAppBoundLocationDto) => {
if (s.id === currentId.value) {
showPicker.value = false
return
}
switchStore(s.id, s.locationName, s.locationCode)
currentId.value = s.id
showPicker.value = false
uni.showToast({ title: t('location.storeSwitched'), icon: 'success' })
setTimeout(() => {
const pages = getCurrentPages()
const cur = pages[pages.length - 1] as { route?: string }
if (cur?.route) {
uni.redirectTo({ url: '/' + cur.route })
}
}, 800)
}
</script>
<style scoped>
.loc-trigger {
display: flex;
align-items: center;
gap: 4rpx;
padding: 4rpx 16rpx 4rpx 20rpx;
background: rgba(255, 255, 255, 0.15);
border-radius: 999rpx;
margin-top: 6rpx;
}
.loc-text {
font-size: 22rpx;
color: rgba(255, 255, 255, 0.9);
font-weight: 500;
max-width: 320rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.loc-trigger .icon-wrap {
opacity: 0.7;
}
/* Picker modal */
.picker-mask {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 2000;
display: flex;
align-items: flex-end;
justify-content: center;
}
.picker-body {
width: 100%;
background: #fff;
border-radius: 32rpx 32rpx 0 0;
padding: 32rpx;
max-height: 70vh;
overflow-y: auto;
}
.picker-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 24rpx;
}
.picker-title {
font-size: 36rpx;
font-weight: 700;
color: #111827;
}
.picker-close {
width: 56rpx;
height: 56rpx;
border-radius: 50%;
background: #f3f4f6;
display: flex;
align-items: center;
justify-content: center;
transform: rotate(45deg);
}
.picker-list {
display: flex;
flex-direction: column;
gap: 12rpx;
}
.picker-item {
display: flex;
align-items: center;
gap: 20rpx;
padding: 28rpx 24rpx;
border-radius: 16rpx;
border: 2rpx solid #e5e7eb;
background: #fff;
}
.picker-item.active {
border-color: var(--theme-primary);
background: var(--theme-primary-light);
}
.picker-icon {
width: 56rpx;
height: 56rpx;
border-radius: 14rpx;
background: #f3f4f6;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.picker-icon.active {
background: var(--theme-primary);
}
.picker-info {
flex: 1;
min-width: 0;
}
.picker-name {
font-size: 30rpx;
font-weight: 600;
color: #111827;
display: block;
margin-bottom: 4rpx;
}
.picker-addr {
font-size: 24rpx;
color: #6b7280;
display: block;
}
.picker-check {
width: 44rpx;
height: 44rpx;
border-radius: 50%;
background: var(--theme-primary);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
</style>