木邻有你前端代码仓库
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.

400 lines
9.8 KiB

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