list.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <!-- 商品列表页面 -->
  2. <template>
  3. <view class="list-page">
  4. <view class="list-head" :style="{ paddingTop: statusBarH + 'px' }">
  5. <view class="head-bar">
  6. <view class="head-left" @tap="goBack">
  7. <text v-if="pagesNum > 1" class="wlIcon wlIcon-fanhui1 back-icon"></text>
  8. <text v-else class="wlIcon wlIcon-zhuye home-icon"></text>
  9. </view>
  10. <view class="head-search" @tap="goSearch">
  11. <text class="wlIcon wlIcon-sousuo1 search-icon"></text>
  12. <view class="search-tag" v-if="category">
  13. <text>{{ $t('mall.category.label') }}:{{ category }}</text>
  14. </view>
  15. <view class="search-tag" v-else-if="params.search">
  16. <text>{{ params.search }}</text>
  17. </view>
  18. <text v-else class="search-placeholder">{{ $t('mall.searchPlaceholder') }}</text>
  19. </view>
  20. <view class="head-right" @tap="toggleStyle">
  21. <text v-if="listStyle === 'col-2-20'" class="wlIcon wlIcon-liebiao2 style-icon"></text>
  22. <text v-else class="wlIcon wlIcon-liebiao style-icon"></text>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="list-head-placeholder" :style="{ height: (statusBarH + 44) + 'px' }"></view>
  27. <view class="sort-bar">
  28. <view class="sort-item" :class="{ active: filterIndex === 0 }" @tap="tabClick(0)"><text>{{ $t('mall.sort.comprehensive') }}</text></view>
  29. <view class="sort-item" :class="{ active: filterIndex === 1 }" @tap="tabClick(1)"><text>{{ $t('mall.sort.sales') }}</text></view>
  30. <view class="sort-item" :class="{ active: filterIndex === 2 }" @tap="tabClick(2)"><text>{{ $t('mall.sort.new') }}</text></view>
  31. <view class="sort-item" :class="{ active: filterIndex === 3 }" @tap="tabClick(3)">
  32. <text>{{ $t('mall.sort.price') }}</text>
  33. <view class="price-arrow">
  34. <text class="arrow-up" :class="{ active: priceOrder === 1 && filterIndex === 3 }">▲</text>
  35. <text class="arrow-down" :class="{ active: priceOrder === 2 && filterIndex === 3 }">▼</text>
  36. </view>
  37. </view>
  38. <view class="sort-item" @tap="showDrawer"><text>{{ $t('mall.sort.filter') }}</text><text class="wlIcon wlIcon-shaixuan filter-icon"></text>
  39. </view>
  40. </view>
  41. <view class="list-body" v-if="goodsData.length > 0">
  42. <mall-product :dataList="goodsData" :dataStyle="listStyle" />
  43. </view>
  44. <view class="list-empty" v-if="goodsData.length === 0 && !loading">
  45. <text>{{ $t('product.noResults') }}</text>
  46. </view>
  47. <view class="list-like" v-if="goodsData.length === 0 && likeData.length > 0">
  48. <view class="like-divider">
  49. <view class="divider-line"></view>
  50. <text class="divider-text">{{ $t('product.guessYouLike') }}</text>
  51. <view class="divider-line"></view>
  52. </view>
  53. <mall-product :dataList="likeData" dataStyle="col-2-20" />
  54. </view>
  55. <view class="load-more" v-if="goodsData.length > 0">
  56. <text v-if="loading">{{ $t('common.loading') }}</text>
  57. <text v-else-if="params.page >= lastPage">{{ $t('common.noMore') }}</text>
  58. </view>
  59. <!-- 筛选抽屉 -->
  60. <view class="drawer-mask" v-if="showRight" @tap="closeDrawer"></view>
  61. <view class="drawer" :class="{ show: showRight }">
  62. <scroll-view scroll-y class="drawer-scroll" :style="{ height: drawerHeight + 'px', paddingTop: statusBarH + 'px' }">
  63. <view class="drawer-section" v-if="drawerData.brand.length > 0">
  64. <view class="drawer-title">{{ $t('product.brand') }}</view>
  65. <view class="drawer-tags">
  66. <text v-for="(bd, index) in drawerData.brand" :key="bd.id" :class="{ active: bd.choice }"
  67. @tap="onDrawerBrand(index)">{{ bd.name }}</text>
  68. </view>
  69. </view>
  70. <view class="drawer-section">
  71. <view class="drawer-title">{{ $t('product.priceRange') }}</view>
  72. <view class="drawer-price">
  73. <input type="number" :placeholder="$t('product.minPrice')" v-model="drawerData.price.low" />
  74. <text>—</text>
  75. <input type="number" :placeholder="$t('product.maxPrice')" v-model="drawerData.price.high" />
  76. </view>
  77. </view>
  78. <view class="drawer-section" v-for="(att, index) in drawerData.attribute" :key="att.name">
  79. <view class="drawer-title" @tap="toggleAttribute(index)">
  80. <text>{{ att.name }}</text>
  81. <text>{{ att.fold ? '▲' : '▼' }}</text>
  82. </view>
  83. <view class="drawer-tags" v-if="att.fold">
  84. <text v-for="(val, key) in att.value" :key="val.name" :class="{ active: val.choice }"
  85. @tap="onDrawerAttribute(index, key)">{{ val.name }}</text>
  86. </view>
  87. </view>
  88. </scroll-view>
  89. <view class="drawer-foot">
  90. <view class="drawer-btn reset" @tap="onDrawerReset">{{ $t('common.reset') }}</view>
  91. <view class="drawer-btn confirm" @tap="onDrawerTo">{{ $t('common.confirm') }}</view>
  92. </view>
  93. </view>
  94. </view>
  95. </template>
  96. <script>
  97. import { pullMallProductPage, pullMallFilterDrawer } from '@/features/mall/mall-gateway'
  98. export default {
  99. data() {
  100. return {
  101. statusBarH: 0,
  102. pagesNum: 0,
  103. filterIndex: 0,
  104. priceOrder: 0,
  105. listStyle: 'col-2-20',
  106. goodsData: [],
  107. likeData: [],
  108. loading: false,
  109. category: '',
  110. lastPage: 0,
  111. showRight: false,
  112. drawerLoaded: false,
  113. drawerHeight: 500,
  114. params: {
  115. search: '',
  116. sort: 'weigh',
  117. order: 'asc',
  118. page: 1,
  119. filter: {},
  120. op: {}
  121. },
  122. drawerData: {
  123. price: { low: '', high: '' },
  124. brand: [],
  125. attribute: []
  126. },
  127. drawerParams: {}
  128. }
  129. },
  130. onLoad(option) {
  131. this.statusBarH = uni.getSystemInfoSync().statusBarHeight || 0
  132. this.drawerHeight = uni.getSystemInfoSync().windowHeight - 100
  133. this.pagesNum = getCurrentPages().length
  134. if (option.search) {
  135. this.params.search = option.search
  136. this.drawerParams.search = option.search
  137. } else if (option.data) {
  138. try {
  139. const data = JSON.parse(decodeURIComponent(option.data))
  140. this.drawerParams.category_id = data.category_id
  141. this.category = data.category_name
  142. this.params.filter.category_id = data.category_id
  143. this.params.op.category_id = 'in'
  144. } catch (e) {
  145. console.error('解析分类数据失败:', e)
  146. }
  147. }
  148. this.$nextTick(() => {
  149. this.loadData()
  150. })
  151. },
  152. onReachBottom() {
  153. if (this.params.page >= this.lastPage) return
  154. this.params.page++
  155. this.loading = true
  156. this.loadData(true)
  157. },
  158. methods: {
  159. async loadData(append = false) {
  160. this.loading = true
  161. try {
  162. const res = await pullMallProductPage({
  163. type: 'goods',
  164. page: this.params.page,
  165. search: this.params.search,
  166. sort: this.params.sort,
  167. order: this.params.order,
  168. filter: this.params.filter,
  169. op: this.params.op
  170. })
  171. this.goodsData = append ? this.goodsData.concat(res.rows) : res.rows
  172. this.lastPage = res.lastPage
  173. if (this.goodsData.length === 0) {
  174. this.loadLikeData()
  175. }
  176. } catch (e) {
  177. console.error('加载商品失败:', e)
  178. } finally {
  179. this.loading = false
  180. }
  181. },
  182. async loadLikeData() {
  183. try {
  184. const res = await pullMallProductPage({ type: 'goods', page: 1 })
  185. this.likeData = res.rows
  186. } catch (e) {
  187. console.error('加载推荐失败:', e)
  188. }
  189. },
  190. async loadDrawer() {
  191. try {
  192. this.drawerData = await pullMallFilterDrawer({
  193. type: 'goods',
  194. search: this.drawerParams.search,
  195. category_id: this.drawerParams.category_id
  196. })
  197. } catch (e) {
  198. console.error('加载筛选条件失败:', e)
  199. }
  200. },
  201. tabClick(index) {
  202. if (this.filterIndex === index && index !== 3) return
  203. this.filterIndex = index
  204. if (index === 3) {
  205. this.priceOrder = this.priceOrder === 1 ? 2 : 1
  206. } else {
  207. this.priceOrder = 0
  208. }
  209. if (index === 0) { this.params.sort = 'weigh'; this.params.order = 'desc' }
  210. if (index === 1) { this.params.sort = 'sales'; this.params.order = 'desc' }
  211. if (index === 2) { this.params.sort = 'createtime'; this.params.order = 'desc' }
  212. if (index === 3 && this.priceOrder === 1) { this.params.sort = 'price'; this.params.order = 'desc' }
  213. if (index === 3 && this.priceOrder === 2) { this.params.sort = 'price'; this.params.order = 'asc' }
  214. this.params.page = 1
  215. this.loadData()
  216. uni.pageScrollTo({ duration: 300, scrollTop: 0 })
  217. },
  218. showDrawer() {
  219. this.showRight = true
  220. if (!this.drawerLoaded) {
  221. this.loadDrawer()
  222. this.drawerLoaded = true
  223. }
  224. },
  225. closeDrawer() {
  226. this.showRight = false
  227. },
  228. onDrawerBrand(index) {
  229. this.drawerData.brand.forEach((item, i) => {
  230. if (i !== index) item.choice = false
  231. })
  232. this.drawerData.brand[index].choice = !this.drawerData.brand[index].choice
  233. },
  234. onDrawerAttribute(attrIdx, valIdx) {
  235. this.drawerData.attribute[attrIdx].value.forEach((item, i) => {
  236. if (i !== valIdx) item.choice = false
  237. })
  238. this.drawerData.attribute[attrIdx].value[valIdx].choice = !this.drawerData.attribute[attrIdx].value[valIdx].choice
  239. },
  240. toggleAttribute(index) {
  241. this.drawerData.attribute[index].fold = !this.drawerData.attribute[index].fold
  242. },
  243. onDrawerTo() {
  244. if (this.drawerData.price.low !== '' && this.drawerData.price.high !== '') {
  245. this.params.filter.price = this.drawerData.price.low + ',' + this.drawerData.price.high
  246. this.params.op.price = 'BETWEEN'
  247. } else {
  248. delete this.params.filter.price
  249. delete this.params.op.price
  250. }
  251. var brandData = ''
  252. this.drawerData.brand.forEach(item => {
  253. if (item.choice) brandData = item.id
  254. })
  255. if (brandData) {
  256. this.params.filter.brand_id = brandData
  257. this.params.op.brand_id = '='
  258. } else {
  259. delete this.params.filter.brand_id
  260. delete this.params.op.brand_id
  261. }
  262. var attributeData = []
  263. this.drawerData.attribute.forEach(item => {
  264. item.value.forEach(vo => {
  265. if (vo.choice) attributeData.push('%' + vo.name + '%')
  266. })
  267. })
  268. if (attributeData.length > 0) {
  269. this.params.filter.category_attribute = attributeData.join(',')
  270. this.params.op.category_attribute = 'LIKE'
  271. } else {
  272. delete this.params.filter.category_attribute
  273. delete this.params.op.category_attribute
  274. }
  275. this.goodsData = []
  276. this.params.page = 1
  277. this.loadData()
  278. this.closeDrawer()
  279. },
  280. onDrawerReset() {
  281. this.loadDrawer()
  282. },
  283. goSearch() {
  284. uni.navigateTo({ url: '/pages/page/search?type=goods' })
  285. },
  286. toggleStyle() {
  287. this.listStyle = this.listStyle === 'col-2-20' ? 'col-1-20' : 'col-2-20'
  288. },
  289. goBack() {
  290. if (this.pagesNum > 1) {
  291. uni.navigateBack({ delta: 1 })
  292. } else {
  293. uni.switchTab({ url: '/pages/index/index' })
  294. }
  295. }
  296. }
  297. }
  298. </script>
  299. <style scoped>
  300. .list-page {
  301. min-height: 100vh;
  302. background: #f5f5f5;
  303. }
  304. .list-head {
  305. position: fixed;
  306. top: 0;
  307. left: 0;
  308. right: 0;
  309. background: #fff;
  310. z-index: 999;
  311. }
  312. .head-bar {
  313. display: flex;
  314. align-items: center;
  315. height: 44px;
  316. padding: 0 20rpx;
  317. }
  318. .head-left {
  319. width: 60rpx;
  320. }
  321. .back-icon {
  322. font-size: 40rpx;
  323. color: #333;
  324. }
  325. .home-icon {
  326. font-size: 40rpx;
  327. color: #333;
  328. }
  329. .head-search {
  330. flex: 1;
  331. display: flex;
  332. align-items: center;
  333. height: 64rpx;
  334. background: #fff;
  335. border: 2rpx solid #fe6600;
  336. border-radius: 32rpx;
  337. padding: 0 20rpx;
  338. }
  339. .search-icon {
  340. font-size: 32rpx;
  341. margin-right: 10rpx;
  342. color: #999;
  343. }
  344. .search-tag {
  345. font-size: 24rpx;
  346. color: #333;
  347. }
  348. .search-placeholder {
  349. font-size: 26rpx;
  350. color: #999;
  351. }
  352. .head-right {
  353. width: 60rpx;
  354. text-align: center;
  355. }
  356. .style-icon {
  357. font-size: 40rpx;
  358. color: #333;
  359. }
  360. .list-head-placeholder {
  361. width: 100%;
  362. }
  363. .sort-bar {
  364. display: flex;
  365. align-items: center;
  366. height: 80rpx;
  367. background: #fff;
  368. border-bottom: 1rpx solid #f0f0f0;
  369. }
  370. .sort-item {
  371. flex: 1;
  372. display: flex;
  373. align-items: center;
  374. justify-content: center;
  375. font-size: 26rpx;
  376. color: #666;
  377. }
  378. .sort-item.active {
  379. color: #e54d42;
  380. font-weight: bold;
  381. }
  382. .price-arrow {
  383. display: flex;
  384. flex-direction: column;
  385. margin-left: 4rpx;
  386. }
  387. .arrow-up,
  388. .arrow-down {
  389. font-size: 16rpx;
  390. line-height: 16rpx;
  391. color: #ccc;
  392. }
  393. .arrow-up.active,
  394. .arrow-down.active {
  395. color: #e54d42;
  396. }
  397. .filter-icon {
  398. font-size: 28rpx;
  399. margin-left: 4rpx;
  400. color: #999;
  401. }
  402. .list-body {
  403. padding: 16rpx;
  404. }
  405. .list-empty {
  406. text-align: center;
  407. padding: 100rpx 0;
  408. color: #999;
  409. font-size: 28rpx;
  410. }
  411. .list-like {
  412. padding: 0 16rpx;
  413. }
  414. .like-divider {
  415. display: flex;
  416. align-items: center;
  417. justify-content: center;
  418. padding: 30rpx 0 10rpx;
  419. }
  420. .divider-line {
  421. width: 60rpx;
  422. height: 2rpx;
  423. background: #ddd;
  424. }
  425. .divider-text {
  426. margin: 0 24rpx;
  427. font-size: 26rpx;
  428. color: #999;
  429. }
  430. .load-more {
  431. text-align: center;
  432. padding: 20rpx 0;
  433. font-size: 24rpx;
  434. color: #999;
  435. }
  436. .drawer-mask {
  437. position: fixed;
  438. top: 0;
  439. left: 0;
  440. right: 0;
  441. bottom: 0;
  442. background: rgba(0, 0, 0, 0.5);
  443. z-index: 1000;
  444. }
  445. .drawer {
  446. position: fixed;
  447. top: 0;
  448. right: 0;
  449. bottom: 0;
  450. width: 600rpx;
  451. background: #fff;
  452. z-index: 1001;
  453. transform: translateX(100%);
  454. transition: transform 0.3s;
  455. }
  456. .drawer.show {
  457. transform: translateX(0);
  458. }
  459. .drawer-scroll {
  460. padding: 20rpx 30rpx;
  461. }
  462. .drawer-section {
  463. padding: 20rpx 0;
  464. border-bottom: 1rpx solid #f0f0f0;
  465. }
  466. .drawer-title {
  467. display: flex;
  468. justify-content: space-between;
  469. font-size: 28rpx;
  470. font-weight: bold;
  471. color: #333;
  472. margin-bottom: 16rpx;
  473. }
  474. .drawer-tags {
  475. display: flex;
  476. flex-wrap: wrap;
  477. gap: 12rpx;
  478. }
  479. .drawer-tags text {
  480. padding: 8rpx 20rpx;
  481. background: #f5f5f5;
  482. border-radius: 8rpx;
  483. font-size: 24rpx;
  484. color: #666;
  485. }
  486. .drawer-tags text.active {
  487. background: #fff2f0;
  488. color: #e54d42;
  489. border: 1rpx solid #e54d42;
  490. }
  491. .drawer-price {
  492. display: flex;
  493. align-items: center;
  494. gap: 16rpx;
  495. }
  496. .drawer-price input {
  497. flex: 1;
  498. height: 64rpx;
  499. border: 1rpx solid #ddd;
  500. border-radius: 8rpx;
  501. padding: 0 16rpx;
  502. font-size: 24rpx;
  503. }
  504. .drawer-price text {
  505. color: #999;
  506. }
  507. .drawer-foot {
  508. display: flex;
  509. padding: 20rpx 30rpx;
  510. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  511. border-top: 1rpx solid #f0f0f0;
  512. }
  513. .drawer-btn {
  514. flex: 1;
  515. text-align: center;
  516. height: 80rpx;
  517. line-height: 80rpx;
  518. font-size: 28rpx;
  519. font-weight: bold;
  520. border-radius: 40rpx;
  521. }
  522. .drawer-btn.reset {
  523. background: linear-gradient(to right, #f0c819, #f37b1d);
  524. color: #fff;
  525. margin-right: 16rpx;
  526. }
  527. .drawer-btn.confirm {
  528. background: linear-gradient(to right, #f37b1d, #e54d42);
  529. color: #fff;
  530. }
  531. </style>