|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+<template>
|
|
|
2
|
+ <view class="page-shop">
|
|
|
3
|
+ <view v-if="pageError" class="page-shop__error">
|
|
|
4
|
+ <u-empty mode="page" :text="pageError" icon-size="80" />
|
|
|
5
|
+ <u-button type="primary" text="重试" size="small" custom-style="margin-top:24rpx" @click="initPage" />
|
|
|
6
|
+ <u-button plain text="返回" size="small" custom-style="margin-top:16rpx" @click="onBack" />
|
|
|
7
|
+ </view>
|
|
|
8
|
+
|
|
|
9
|
+ <template v-else>
|
|
|
10
|
+ <view v-if="profileLoading" class="page-shop__loading">
|
|
|
11
|
+ <u-loading-icon mode="circle" />
|
|
|
12
|
+ </view>
|
|
|
13
|
+
|
|
|
14
|
+ <template v-else-if="profile">
|
|
|
15
|
+ <view class="shop-header">
|
|
|
16
|
+ <image class="shop-header__avatar" :src="profile.shopAvatar" mode="aspectFill" />
|
|
|
17
|
+ <view class="shop-header__main">
|
|
|
18
|
+ <view class="shop-header__title-row">
|
|
|
19
|
+ <text class="shop-header__name">{{ profile.shopName }}</text>
|
|
|
20
|
+ <text v-if="profile.isClosed" class="shop-header__tag">{{ closedLabel }}</text>
|
|
|
21
|
+ </view>
|
|
|
22
|
+ <view v-if="profile.showRating || profile.showFans" class="shop-header__meta">
|
|
|
23
|
+ <text v-if="profile.showRating" class="shop-header__meta-item">评分 {{ profile.ratingText }}</text>
|
|
|
24
|
+ <text v-if="profile.showFans" class="shop-header__meta-item">粉丝 {{ profile.fansCount }}</text>
|
|
|
25
|
+ </view>
|
|
|
26
|
+ <text v-if="profile.shopDesc" class="shop-header__desc">{{ profile.shopDesc }}</text>
|
|
|
27
|
+ </view>
|
|
|
28
|
+ <view
|
|
|
29
|
+ class="shop-header__follow"
|
|
|
30
|
+ :class="{ 'shop-header__follow--on': profile.followed }"
|
|
|
31
|
+ @click="onFollowTap"
|
|
|
32
|
+ >
|
|
|
33
|
+ <text>{{ profile.followed ? '已关注' : '+ 关注' }}</text>
|
|
|
34
|
+ </view>
|
|
|
35
|
+ </view>
|
|
|
36
|
+
|
|
|
37
|
+ <view class="shop-search-bar" @click="onGoShopSearch">
|
|
|
38
|
+ <u-icon name="search" color="#2e7d32" size="18" />
|
|
|
39
|
+ <text class="shop-search-bar__text">搜索本店商品</text>
|
|
|
40
|
+ </view>
|
|
|
41
|
+
|
|
|
42
|
+ <scroll-view class="page-shop__l1" scroll-x show-scrollbar="false">
|
|
|
43
|
+ <view class="tabs-inner">
|
|
|
44
|
+ <view
|
|
|
45
|
+ v-for="(tab, index) in level1Tabs"
|
|
|
46
|
+ :key="tab.categoryId"
|
|
|
47
|
+ class="tab-item"
|
|
|
48
|
+ :class="{ 'tab-item--active': activeLevel1Index === index }"
|
|
|
49
|
+ @click="onLevel1Change(index)"
|
|
|
50
|
+ >
|
|
|
51
|
+ <text>{{ tab.categoryName }}</text>
|
|
|
52
|
+ </view>
|
|
|
53
|
+ </view>
|
|
|
54
|
+ </scroll-view>
|
|
|
55
|
+
|
|
|
56
|
+ <view v-if="!isAllLevel1 && tabsLoading" class="page-shop__hint">
|
|
|
57
|
+ <u-loading-icon mode="circle" size="20" />
|
|
|
58
|
+ </view>
|
|
|
59
|
+ <scroll-view
|
|
|
60
|
+ v-else-if="!isAllLevel1 && level2Tabs.length"
|
|
|
61
|
+ class="page-shop__l2"
|
|
|
62
|
+ scroll-x
|
|
|
63
|
+ show-scrollbar="false"
|
|
|
64
|
+ >
|
|
|
65
|
+ <view class="tabs-inner">
|
|
|
66
|
+ <view
|
|
|
67
|
+ v-for="(tab, index) in level2Tabs"
|
|
|
68
|
+ :key="tab.categoryId"
|
|
|
69
|
+ class="tab-item tab-item--sm"
|
|
|
70
|
+ :class="{ 'tab-item--active': activeLevel2Index === index }"
|
|
|
71
|
+ @click="onLevel2Change(index)"
|
|
|
72
|
+ >
|
|
|
73
|
+ <text>{{ tab.categoryName }}</text>
|
|
|
74
|
+ </view>
|
|
|
75
|
+ </view>
|
|
|
76
|
+ </scroll-view>
|
|
|
77
|
+
|
|
|
78
|
+ <shop-goods-block
|
|
|
79
|
+ :shop-id="shopId"
|
|
|
80
|
+ :shop-category-id="activeShopCategoryId"
|
|
|
81
|
+ :scroll-top-key="goodsScrollKey"
|
|
|
82
|
+ :scroll-height="goodsScrollHeight"
|
|
|
83
|
+ :enabled="goodsBlockEnabled"
|
|
|
84
|
+ :empty-text="goodsEmptyText"
|
|
|
85
|
+ @goods-click="onGoodsClick"
|
|
|
86
|
+ />
|
|
|
87
|
+ </template>
|
|
|
88
|
+ </template>
|
|
|
89
|
+ </view>
|
|
|
90
|
+</template>
|
|
|
91
|
+
|
|
|
92
|
+<script setup>
|
|
|
93
|
+import { ref, computed } from 'vue'
|
|
|
94
|
+import { onLoad } from '@dcloudio/uni-app'
|
|
|
95
|
+import { getShopProfile, getShopCategories, getShopLevel2Tabs, followShop, unfollowShop } from '@/api/shop'
|
|
|
96
|
+import {
|
|
|
97
|
+ mapShopProfile,
|
|
|
98
|
+ mapShopCategoryTree,
|
|
|
99
|
+ mapShopLevel2Tabs,
|
|
|
100
|
+ sortShopTabsWithHot
|
|
|
101
|
+} from '@/utils/shopDisplay'
|
|
|
102
|
+import {
|
|
|
103
|
+ SHOP_ALL_TAB_ID,
|
|
|
104
|
+ SHOP_ALL_TAB_NAME,
|
|
|
105
|
+ SHOP_CLOSED_LABEL
|
|
|
106
|
+} from '@/constants/shop'
|
|
|
107
|
+import { goShopSearch } from '@/utils/shopNav'
|
|
|
108
|
+import { goGoodsDetail } from '@/utils/goodsDetail'
|
|
|
109
|
+import { ensureApiToken } from '@/utils/apiAuth'
|
|
|
110
|
+import { smartNavigateBack } from '@/utils/navBack'
|
|
|
111
|
+import ShopGoodsBlock from '@/components/shop/ShopGoodsBlock.vue'
|
|
|
112
|
+
|
|
|
113
|
+const shopId = ref('')
|
|
|
114
|
+const profile = ref(null)
|
|
|
115
|
+const profileLoading = ref(true)
|
|
|
116
|
+const pageError = ref('')
|
|
|
117
|
+const followLoading = ref(false)
|
|
|
118
|
+const closedLabel = SHOP_CLOSED_LABEL
|
|
|
119
|
+
|
|
|
120
|
+const categoryTree = ref([])
|
|
|
121
|
+const level2Tabs = ref([])
|
|
|
122
|
+const activeLevel1Index = ref(0)
|
|
|
123
|
+const activeLevel2Index = ref(0)
|
|
|
124
|
+const tabsLoading = ref(false)
|
|
|
125
|
+const tabsFailed = ref(false)
|
|
|
126
|
+const goodsScrollKey = ref(0)
|
|
|
127
|
+const goodsScrollHeight = ref('500px')
|
|
|
128
|
+
|
|
|
129
|
+const allTab = { categoryId: SHOP_ALL_TAB_ID, categoryName: SHOP_ALL_TAB_NAME }
|
|
|
130
|
+
|
|
|
131
|
+const level1Tabs = computed(() => [allTab, ...categoryTree.value])
|
|
|
132
|
+
|
|
|
133
|
+const isAllLevel1 = computed(() => {
|
|
|
134
|
+ const tab = level1Tabs.value[activeLevel1Index.value]
|
|
|
135
|
+ return !tab || tab.categoryId === SHOP_ALL_TAB_ID
|
|
|
136
|
+})
|
|
|
137
|
+
|
|
|
138
|
+const activeLevel1Id = computed(() => {
|
|
|
139
|
+ const tab = level1Tabs.value[activeLevel1Index.value]
|
|
|
140
|
+ return tab && tab.categoryId !== SHOP_ALL_TAB_ID ? tab.categoryId : ''
|
|
|
141
|
+})
|
|
|
142
|
+
|
|
|
143
|
+const activeShopCategoryId = computed(() => {
|
|
|
144
|
+ if (isAllLevel1.value) return ''
|
|
|
145
|
+ if (!level2Tabs.value.length) return ''
|
|
|
146
|
+ const tab = level2Tabs.value[activeLevel2Index.value]
|
|
|
147
|
+ return tab ? tab.categoryId : ''
|
|
|
148
|
+})
|
|
|
149
|
+
|
|
|
150
|
+const goodsBlockEnabled = computed(() => {
|
|
|
151
|
+ if (isAllLevel1.value) return true
|
|
|
152
|
+ return level2Tabs.value.length > 0
|
|
|
153
|
+})
|
|
|
154
|
+
|
|
|
155
|
+const goodsEmptyText = computed(() => {
|
|
|
156
|
+ if (!isAllLevel1.value && !level2Tabs.value.length && !tabsLoading.value) {
|
|
|
157
|
+ return '该分类下暂无商品'
|
|
|
158
|
+ }
|
|
|
159
|
+ return '暂无商品'
|
|
|
160
|
+})
|
|
|
161
|
+
|
|
|
162
|
+function calcScrollHeight() {
|
|
|
163
|
+ try {
|
|
|
164
|
+ const sys = uni.getSystemInfoSync()
|
|
|
165
|
+ const h = sys.windowHeight || 600
|
|
|
166
|
+ goodsScrollHeight.value = `${h - 280}px`
|
|
|
167
|
+ } catch (e) {
|
|
|
168
|
+ goodsScrollHeight.value = '500px'
|
|
|
169
|
+ }
|
|
|
170
|
+}
|
|
|
171
|
+
|
|
|
172
|
+function onBack() {
|
|
|
173
|
+ smartNavigateBack()
|
|
|
174
|
+}
|
|
|
175
|
+
|
|
|
176
|
+async function loadProfile() {
|
|
|
177
|
+ profileLoading.value = true
|
|
|
178
|
+ pageError.value = ''
|
|
|
179
|
+ try {
|
|
|
180
|
+ const res = await getShopProfile(shopId.value)
|
|
|
181
|
+ const mapped = mapShopProfile(res.data)
|
|
|
182
|
+ if (!mapped) {
|
|
|
183
|
+ throw new Error('店铺数据异常')
|
|
|
184
|
+ }
|
|
|
185
|
+ profile.value = mapped
|
|
|
186
|
+ const title = mapped.shopName || '店铺'
|
|
|
187
|
+ uni.setNavigationBarTitle({ title })
|
|
|
188
|
+ } catch (e) {
|
|
|
189
|
+ profile.value = null
|
|
|
190
|
+ const msg = (e && e.msg) || (e && e.message) || '店铺不存在或已关闭'
|
|
|
191
|
+ pageError.value = msg
|
|
|
192
|
+ } finally {
|
|
|
193
|
+ profileLoading.value = false
|
|
|
194
|
+ }
|
|
|
195
|
+}
|
|
|
196
|
+
|
|
|
197
|
+async function loadCategories() {
|
|
|
198
|
+ try {
|
|
|
199
|
+ const res = await getShopCategories(shopId.value)
|
|
|
200
|
+ categoryTree.value = sortShopTabsWithHot(mapShopCategoryTree(res.data || []))
|
|
|
201
|
+ } catch (e) {
|
|
|
202
|
+ categoryTree.value = []
|
|
|
203
|
+ }
|
|
|
204
|
+}
|
|
|
205
|
+
|
|
|
206
|
+async function loadLevel2Tabs() {
|
|
|
207
|
+ if (!activeLevel1Id.value) {
|
|
|
208
|
+ level2Tabs.value = []
|
|
|
209
|
+ return
|
|
|
210
|
+ }
|
|
|
211
|
+ tabsLoading.value = true
|
|
|
212
|
+ tabsFailed.value = false
|
|
|
213
|
+ try {
|
|
|
214
|
+ const res = await getShopLevel2Tabs(shopId.value, activeLevel1Id.value)
|
|
|
215
|
+ level2Tabs.value = sortShopTabsWithHot(mapShopLevel2Tabs(res.data || []))
|
|
|
216
|
+ if (activeLevel2Index.value >= level2Tabs.value.length) {
|
|
|
217
|
+ activeLevel2Index.value = 0
|
|
|
218
|
+ }
|
|
|
219
|
+ } catch (e) {
|
|
|
220
|
+ tabsFailed.value = true
|
|
|
221
|
+ level2Tabs.value = []
|
|
|
222
|
+ } finally {
|
|
|
223
|
+ tabsLoading.value = false
|
|
|
224
|
+ }
|
|
|
225
|
+}
|
|
|
226
|
+
|
|
|
227
|
+function bumpGoodsList() {
|
|
|
228
|
+ goodsScrollKey.value += 1
|
|
|
229
|
+}
|
|
|
230
|
+
|
|
|
231
|
+async function onLevel1Change(index) {
|
|
|
232
|
+ if (activeLevel1Index.value === index) return
|
|
|
233
|
+ activeLevel1Index.value = index
|
|
|
234
|
+ activeLevel2Index.value = 0
|
|
|
235
|
+ if (isAllLevel1.value) {
|
|
|
236
|
+ level2Tabs.value = []
|
|
|
237
|
+ bumpGoodsList()
|
|
|
238
|
+ return
|
|
|
239
|
+ }
|
|
|
240
|
+ await loadLevel2Tabs()
|
|
|
241
|
+ bumpGoodsList()
|
|
|
242
|
+}
|
|
|
243
|
+
|
|
|
244
|
+function onLevel2Change(index) {
|
|
|
245
|
+ if (activeLevel2Index.value === index) return
|
|
|
246
|
+ activeLevel2Index.value = index
|
|
|
247
|
+ bumpGoodsList()
|
|
|
248
|
+}
|
|
|
249
|
+
|
|
|
250
|
+async function onFollowTap() {
|
|
|
251
|
+ if (!profile.value || followLoading.value) return
|
|
|
252
|
+ if (!ensureApiToken()) return
|
|
|
253
|
+ followLoading.value = true
|
|
|
254
|
+ try {
|
|
|
255
|
+ if (profile.value.followed) {
|
|
|
256
|
+ await unfollowShop(shopId.value)
|
|
|
257
|
+ profile.value.followed = false
|
|
|
258
|
+ profile.value.fansCount = Math.max(0, profile.value.fansCount - 1)
|
|
|
259
|
+ } else {
|
|
|
260
|
+ await followShop(shopId.value)
|
|
|
261
|
+ profile.value.followed = true
|
|
|
262
|
+ profile.value.fansCount += 1
|
|
|
263
|
+ }
|
|
|
264
|
+ } catch (e) {
|
|
|
265
|
+ // request 已 Toast
|
|
|
266
|
+ } finally {
|
|
|
267
|
+ followLoading.value = false
|
|
|
268
|
+ }
|
|
|
269
|
+}
|
|
|
270
|
+
|
|
|
271
|
+function onGoShopSearch() {
|
|
|
272
|
+ goShopSearch(shopId.value)
|
|
|
273
|
+}
|
|
|
274
|
+
|
|
|
275
|
+function onGoodsClick(item) {
|
|
|
276
|
+ if (item && item.goodsId) {
|
|
|
277
|
+ goGoodsDetail(item.goodsId)
|
|
|
278
|
+ }
|
|
|
279
|
+}
|
|
|
280
|
+
|
|
|
281
|
+async function initPage() {
|
|
|
282
|
+ if (!shopId.value) return
|
|
|
283
|
+ await loadProfile()
|
|
|
284
|
+ if (!profile.value) return
|
|
|
285
|
+ await loadCategories()
|
|
|
286
|
+ if (!isAllLevel1.value) {
|
|
|
287
|
+ await loadLevel2Tabs()
|
|
|
288
|
+ }
|
|
|
289
|
+}
|
|
|
290
|
+
|
|
|
291
|
+onLoad((options) => {
|
|
|
292
|
+ shopId.value = options.shopId || ''
|
|
|
293
|
+ calcScrollHeight()
|
|
|
294
|
+ if (!shopId.value) {
|
|
|
295
|
+ pageError.value = '无法打开店铺'
|
|
|
296
|
+ profileLoading.value = false
|
|
|
297
|
+ setTimeout(() => onBack(), 800)
|
|
|
298
|
+ return
|
|
|
299
|
+ }
|
|
|
300
|
+ initPage()
|
|
|
301
|
+})
|
|
|
302
|
+</script>
|
|
|
303
|
+
|
|
|
304
|
+<style lang="scss" scoped>
|
|
|
305
|
+.page-shop {
|
|
|
306
|
+ display: flex;
|
|
|
307
|
+ flex-direction: column;
|
|
|
308
|
+ min-height: 100vh;
|
|
|
309
|
+ background: #f5f6f8;
|
|
|
310
|
+}
|
|
|
311
|
+.page-shop__error,
|
|
|
312
|
+.page-shop__loading,
|
|
|
313
|
+.page-shop__hint {
|
|
|
314
|
+ padding: 80rpx 24rpx;
|
|
|
315
|
+ display: flex;
|
|
|
316
|
+ flex-direction: column;
|
|
|
317
|
+ align-items: center;
|
|
|
318
|
+}
|
|
|
319
|
+.shop-header {
|
|
|
320
|
+ display: flex;
|
|
|
321
|
+ align-items: flex-start;
|
|
|
322
|
+ padding: 24rpx;
|
|
|
323
|
+ background: #fff;
|
|
|
324
|
+ margin-bottom: 2rpx;
|
|
|
325
|
+}
|
|
|
326
|
+.shop-header__avatar {
|
|
|
327
|
+ width: 120rpx;
|
|
|
328
|
+ height: 120rpx;
|
|
|
329
|
+ border-radius: 12rpx;
|
|
|
330
|
+ background: #eee;
|
|
|
331
|
+ flex-shrink: 0;
|
|
|
332
|
+}
|
|
|
333
|
+.shop-header__main {
|
|
|
334
|
+ flex: 1;
|
|
|
335
|
+ margin: 0 20rpx;
|
|
|
336
|
+ min-width: 0;
|
|
|
337
|
+}
|
|
|
338
|
+.shop-header__title-row {
|
|
|
339
|
+ display: flex;
|
|
|
340
|
+ align-items: center;
|
|
|
341
|
+ flex-wrap: wrap;
|
|
|
342
|
+ gap: 12rpx;
|
|
|
343
|
+}
|
|
|
344
|
+.shop-header__name {
|
|
|
345
|
+ font-size: 32rpx;
|
|
|
346
|
+ font-weight: 600;
|
|
|
347
|
+ color: #333;
|
|
|
348
|
+}
|
|
|
349
|
+.shop-header__tag {
|
|
|
350
|
+ font-size: 22rpx;
|
|
|
351
|
+ color: #e65100;
|
|
|
352
|
+ background: #fff3e0;
|
|
|
353
|
+ padding: 4rpx 12rpx;
|
|
|
354
|
+ border-radius: 6rpx;
|
|
|
355
|
+}
|
|
|
356
|
+.shop-header__meta {
|
|
|
357
|
+ margin-top: 12rpx;
|
|
|
358
|
+ display: flex;
|
|
|
359
|
+ flex-wrap: wrap;
|
|
|
360
|
+ gap: 16rpx;
|
|
|
361
|
+}
|
|
|
362
|
+.shop-header__meta-item {
|
|
|
363
|
+ font-size: 24rpx;
|
|
|
364
|
+ color: #999;
|
|
|
365
|
+}
|
|
|
366
|
+.shop-header__desc {
|
|
|
367
|
+ margin-top: 12rpx;
|
|
|
368
|
+ font-size: 24rpx;
|
|
|
369
|
+ color: #666;
|
|
|
370
|
+ line-height: 1.5;
|
|
|
371
|
+ overflow: hidden;
|
|
|
372
|
+ display: -webkit-box;
|
|
|
373
|
+ -webkit-line-clamp: 2;
|
|
|
374
|
+ -webkit-box-orient: vertical;
|
|
|
375
|
+}
|
|
|
376
|
+.shop-header__follow {
|
|
|
377
|
+ flex-shrink: 0;
|
|
|
378
|
+ padding: 12rpx 24rpx;
|
|
|
379
|
+ border-radius: 32rpx;
|
|
|
380
|
+ border: 2rpx solid #2e7d32;
|
|
|
381
|
+ background: #fff;
|
|
|
382
|
+}
|
|
|
383
|
+.shop-header__follow text {
|
|
|
384
|
+ font-size: 26rpx;
|
|
|
385
|
+ color: #2e7d32;
|
|
|
386
|
+}
|
|
|
387
|
+.shop-header__follow--on {
|
|
|
388
|
+ background: #e8f5e9;
|
|
|
389
|
+ border-color: #a5d6a7;
|
|
|
390
|
+}
|
|
|
391
|
+.shop-header__follow--on text {
|
|
|
392
|
+ color: #666;
|
|
|
393
|
+}
|
|
|
394
|
+.shop-search-bar {
|
|
|
395
|
+ display: flex;
|
|
|
396
|
+ align-items: center;
|
|
|
397
|
+ margin: 16rpx 24rpx;
|
|
|
398
|
+ padding: 0 24rpx;
|
|
|
399
|
+ height: 72rpx;
|
|
|
400
|
+ background: #fff;
|
|
|
401
|
+ border-radius: 36rpx;
|
|
|
402
|
+ border: 2rpx solid #c8e6c9;
|
|
|
403
|
+}
|
|
|
404
|
+.shop-search-bar__text {
|
|
|
405
|
+ margin-left: 12rpx;
|
|
|
406
|
+ font-size: 28rpx;
|
|
|
407
|
+ color: #999;
|
|
|
408
|
+}
|
|
|
409
|
+.page-shop__l1,
|
|
|
410
|
+.page-shop__l2 {
|
|
|
411
|
+ background: #fff;
|
|
|
412
|
+ white-space: nowrap;
|
|
|
413
|
+}
|
|
|
414
|
+.page-shop__l2 {
|
|
|
415
|
+ border-top: 1rpx solid #f0f0f0;
|
|
|
416
|
+}
|
|
|
417
|
+.tabs-inner {
|
|
|
418
|
+ display: inline-flex;
|
|
|
419
|
+ padding: 16rpx 12rpx;
|
|
|
420
|
+}
|
|
|
421
|
+.tab-item {
|
|
|
422
|
+ display: inline-flex;
|
|
|
423
|
+ align-items: center;
|
|
|
424
|
+ justify-content: center;
|
|
|
425
|
+ padding: 12rpx 28rpx;
|
|
|
426
|
+ margin: 0 8rpx;
|
|
|
427
|
+ border-radius: 32rpx;
|
|
|
428
|
+ background: #f5f5f5;
|
|
|
429
|
+}
|
|
|
430
|
+.tab-item--sm {
|
|
|
431
|
+ padding: 8rpx 24rpx;
|
|
|
432
|
+}
|
|
|
433
|
+.tab-item text {
|
|
|
434
|
+ font-size: 26rpx;
|
|
|
435
|
+ color: #666;
|
|
|
436
|
+}
|
|
|
437
|
+.tab-item--active {
|
|
|
438
|
+ background: #e8f5e9;
|
|
|
439
|
+}
|
|
|
440
|
+.tab-item--active text {
|
|
|
441
|
+ color: #2e7d32;
|
|
|
442
|
+ font-weight: 500;
|
|
|
443
|
+}
|
|
|
444
|
+</style>
|