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

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