木邻有你前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
10 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="shop-content">
  3. <!-- 搜索框 -->
  4. <view class="search-container">
  5. <uv-search
  6. v-model="title"
  7. placeholder="搜索商品名"
  8. :show-action="false"
  9. bg-color="#f3f7f8"
  10. inputAlign="center"
  11. height="40"
  12. margin="10rpx"
  13. @search="onSearch"
  14. @clickIcon="onSearch"
  15. @clear="onSearch"
  16. ></uv-search>
  17. </view>
  18. <!-- Tab栏 -->
  19. <view class="tab-container">
  20. <scroll-view scroll-x="true" class="tab-scroll">
  21. <view class="tab-list">
  22. <!-- 固定的前三个Tab -->
  23. <view
  24. class="tab-item"
  25. :class="{ active: currentTab === 0 }"
  26. @click="onTabClick(0, '全部')"
  27. >
  28. <text class="tab-text">全部</text>
  29. </view>
  30. <view
  31. class="tab-item sort-tab"
  32. :class="{ active: currentTab === 1 }"
  33. @click="onTabClick(1, '兑换积分')"
  34. >
  35. <text class="tab-text">兑换积分</text>
  36. <view class="sort-arrows">
  37. <view class="arrow up" :class="{ active: sortType === 'points_asc' }"></view>
  38. <view class="arrow down" :class="{ active: sortType === 'points_desc' }"></view>
  39. </view>
  40. </view>
  41. <view
  42. class="tab-item sort-tab"
  43. :class="{ active: currentTab === 2 }"
  44. @click="onTabClick(2, '兑换量')"
  45. >
  46. <text class="tab-text">兑换量</text>
  47. <view class="sort-arrows">
  48. <view class="arrow up" :class="{ active: sortType === 'exchange_asc' }"></view>
  49. <view class="arrow down" :class="{ active: sortType === 'exchange_desc' }"></view>
  50. </view>
  51. </view>
  52. <!-- 从store获取的商品分类Tab -->
  53. <view
  54. v-for="(category, index) in categoryGoodsList"
  55. :key="category.id"
  56. class="tab-item"
  57. :class="{ active: currentTab === index + 3 }"
  58. @click="onTabClick(index + 3, category.title, category.id)"
  59. >
  60. <text class="tab-text">{{ category.title }}</text>
  61. </view>
  62. </view>
  63. </scroll-view>
  64. </view>
  65. <!-- 商品列表 -->
  66. <view class="goods-container">
  67. <view class="goods-grid" v-if="goodsList.length > 0">
  68. <view
  69. class="goods-item"
  70. v-for="(item, index) in goodsList"
  71. :key="index"
  72. @click="onGoodsClick(item)"
  73. >
  74. <view class="goods-image">
  75. <image :src="item.image" mode="aspectFit" class="image"></image>
  76. </view>
  77. <view class="goods-info">
  78. <text class="goods-name">{{ item.title }}</text>
  79. <view class="goods-bottom">
  80. <view class="points-info">
  81. <image src="/static/积分图标.png" class="points-icon" mode="aspectFit"></image>
  82. <text class="points-text">{{ item.price }}积分</text>
  83. </view>
  84. <uv-button
  85. type="primary"
  86. size="mini"
  87. text="立即兑换"
  88. :custom-style="buttonStyle"
  89. @click.stop="onExchange(item)"
  90. ></uv-button>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. <uv-empty
  96. v-else
  97. icon="/static/暂无搜索结果.png"
  98. text="暂无商品数据"
  99. ></uv-empty>
  100. </view>
  101. </view>
  102. </template>
  103. <script>
  104. export default {
  105. name: 'ShopContent',
  106. data() {
  107. return {
  108. // searchValue: '',
  109. currentTab: 0,
  110. pageNo: 1,
  111. pageSize: 10,
  112. title: '',
  113. sortType: '', // 排序类型:points_asc, points_desc, exchange_asc, exchange_desc
  114. goodsList: [],
  115. buttonStyle: {
  116. width: '128rpx',
  117. height: '44rpx',
  118. borderRadius: '28rpx',
  119. fontSize: '22rpx'
  120. },
  121. // 额外的传参
  122. extraParams : {}
  123. }
  124. },
  125. computed: {
  126. // 从store获取商品分类列表
  127. categoryGoodsList() {
  128. return this.$store.state.categoryGoodsList || []
  129. }
  130. },
  131. methods: {
  132. async onSearch(value) {
  133. if (value) this.title = value
  134. this.initData()
  135. await this.getGoodsList()
  136. },
  137. async onTabClick(index, tabName, categoryId = null) {
  138. this.currentTab = index
  139. this.extraParams = {} // 不带任何额外参数
  140. if (index === 0) {
  141. // 全部Tab
  142. console.log('点击了全部Tab')
  143. } else if (index === 1) {
  144. // 兑换积分Tab - 处理排序
  145. if (this.sortType === 'points_asc') {
  146. this.sortType = 'points_desc' // 积分降序
  147. this.extraParams['price'] = 0
  148. } else {
  149. this.sortType = 'points_asc' // 积分升序
  150. this.extraParams['price'] = 1
  151. }
  152. console.log('点击了兑换积分Tab,排序类型:', this.sortType)
  153. } else if (index === 2) {
  154. // 兑换量Tab - 处理排序
  155. if (this.sortType === 'exchange_asc') {
  156. this.sortType = 'exchange_desc' // 兑换量降序
  157. this.extraParams['sales'] = 0
  158. } else {
  159. this.sortType = 'exchange_asc' // 兑换量升序
  160. this.extraParams['sales'] = 1
  161. }
  162. console.log('点击了兑换量Tab,排序类型:', this.sortType)
  163. } else {
  164. // 商品分类Tab
  165. console.log('点击了商品分类Tab:', tabName, '分类ID:', categoryId)
  166. this.extraParams['categoryId'] = categoryId
  167. }
  168. this.initData()
  169. await this.getGoodsList()
  170. },
  171. onGoodsClick(item) {
  172. // 跳转到商品详情页
  173. uni.navigateTo({
  174. url: `/subPages/shop/goodsDetail?id=${item.id}`
  175. })
  176. },
  177. onExchange(item) {
  178. uni.showModal({
  179. title: '确认兑换',
  180. content: `确定要用${item.points}积分兑换${item.name}吗?`,
  181. success: (res) => {
  182. if (res.confirm) {
  183. // 执行兑换逻辑
  184. uni.showToast({
  185. title: '兑换成功',
  186. icon: 'success'
  187. })
  188. }
  189. }
  190. })
  191. },
  192. async getGoodsList() {
  193. // 实际项目中这里应该调用API
  194. const res = await this.$api.shop.queryGoodsList({
  195. pageNo: this.pageNo,
  196. pageSize: this.pageSize,
  197. title: this.title,
  198. ...this.extraParams
  199. })
  200. if (res.result.records.length) {
  201. this.goodsList.push(...res.result.records)
  202. this.pageNo++
  203. }else {
  204. uni.showToast({
  205. title: '暂无商品',
  206. icon: 'none'
  207. })
  208. }
  209. },
  210. // 初始化请求参数
  211. initData() {
  212. this.pageNo = 1
  213. this.goodsList = []
  214. }
  215. },
  216. async mounted() {
  217. // 确保store中的商品分类数据已加载
  218. if (this.categoryGoodsList.length === 0) {
  219. await this.$store.dispatch('getCategoryGoodsList')
  220. }
  221. // 初始化商品列表
  222. // this.getGoodsList()
  223. }
  224. }
  225. </script>
  226. <style lang="scss" scoped>
  227. .shop-content {
  228. background: #f8f8f8;
  229. min-height: calc(100vh - 400rpx);
  230. }
  231. .search-container {
  232. position: sticky;
  233. z-index: 999;
  234. top: 10rpx;
  235. padding: 15rpx 20rpx;
  236. background: #ffffff;
  237. }
  238. .tab-container {
  239. position: sticky;
  240. z-index: 999;
  241. top: 90rpx;
  242. background: #ffffff;
  243. border-bottom: 1rpx solid #f0f0f0;
  244. padding-bottom: 20rpx;
  245. .tab-scroll {
  246. white-space: nowrap;
  247. .tab-list {
  248. display: flex;
  249. padding: 0 30rpx;
  250. .tab-item {
  251. flex-shrink: 0;
  252. display: flex;
  253. align-items: center;
  254. padding: 24rpx 32rpx;
  255. margin-right: 16rpx;
  256. border-radius: 32rpx;
  257. background: #f8f9fa;
  258. transition: all 0.3s ease;
  259. .tab-text {
  260. font-size: 28rpx;
  261. color: #666666;
  262. font-weight: 500;
  263. }
  264. &.active {
  265. background: #218CDD;
  266. .tab-text {
  267. color: #ffffff;
  268. }
  269. .sort-arrows .arrow.active {
  270. color: #ffffff;
  271. }
  272. }
  273. &.sort-tab {
  274. .sort-arrows {
  275. margin-left: 8rpx;
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. .arrow {
  280. font-size: 16rpx;
  281. color: #cccccc;
  282. line-height: 1;
  283. transition: color 0.3s ease;
  284. &.up {
  285. margin-bottom: 2rpx;
  286. }
  287. &.active {
  288. color: rgb(64, 64, 64);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. }
  297. .goods-container {
  298. padding: 20rpx 30rpx;
  299. background: #f8f8f8;
  300. }
  301. .goods-grid {
  302. display: grid;
  303. grid-template-columns: 1fr 1fr;
  304. gap: 20rpx;
  305. }
  306. .goods-item {
  307. display: flex;
  308. flex-direction: column;
  309. background: #ffffff;
  310. border-radius: 12rpx;
  311. padding: 20rpx;
  312. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  313. border: 1rpx solid #f5f5f5;
  314. .goods-image {
  315. width: 100%;
  316. height: 230rpx;
  317. border-radius: 8rpx;
  318. overflow: hidden;
  319. margin-bottom: 16rpx;
  320. border: 2rpx dashed #e0e0e0;
  321. .image {
  322. width: 100%;
  323. height: 100%;
  324. object-fit: cover;
  325. }
  326. }
  327. .goods-info {
  328. flex: 1;
  329. display: flex;
  330. flex-direction: column;
  331. .goods-name {
  332. font-size: 28rpx;
  333. color: #333333;
  334. line-height: 1.4;
  335. margin-bottom: 16rpx;
  336. font-weight: 500;
  337. display: -webkit-box;
  338. -webkit-box-orient: vertical;
  339. -webkit-line-clamp: 2;
  340. overflow: hidden;
  341. min-height: 72rpx;
  342. }
  343. .goods-bottom {
  344. display: flex;
  345. // flex-direction: column;
  346. gap: 22rpx;
  347. margin-top: auto;
  348. .points-info {
  349. display: flex;
  350. align-items: center;
  351. .points-icon {
  352. width: 24rpx;
  353. height: 24rpx;
  354. margin-right: 6rpx;
  355. }
  356. .points-text {
  357. font-size: 28rpx;
  358. color: #218CDD;
  359. font-weight: 700;
  360. }
  361. }
  362. }
  363. }
  364. }
  365. </style>