points-mall.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <template>
  2. <view class="points-mall">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="back-icon" @click="handleBack"></view>
  6. <text class="header-title">积分商城</text>
  7. <view class="header-right">
  8. <view class="search-icon"></view>
  9. <view class="cart-icon">
  10. <text class="cart-badge">{{ cartCount }}</text>
  11. </view>
  12. </view>
  13. </view>
  14. <!-- 积分显示区域 -->
  15. <view class="points-section">
  16. <view class="points-header">
  17. <text class="points-label">我的积分</text>
  18. <text class="points-value">{{ userPoints }}</text>
  19. <text class="points-icon">💰</text>
  20. </view>
  21. <view class="points-actions">
  22. <view class="action-btn" @click="handlePointsDetail">积分明细</view>
  23. <view class="action-btn" @click="handleEarnPoints">赚取积分</view>
  24. </view>
  25. </view>
  26. <!-- 分类标签 -->
  27. <view class="category-tabs">
  28. <view
  29. v-for="(category, index) in categories"
  30. :key="index"
  31. class="category-tab"
  32. :class="{ active: activeCategory === index }"
  33. @click="activeCategory = index"
  34. >
  35. {{ category }}
  36. </view>
  37. </view>
  38. <!-- 商品区域 -->
  39. <scroll-view scroll-y class="products-container">
  40. <!-- 商品标题 -->
  41. <view class="section-header">
  42. <text class="section-title">{{ activeCategory === 0 ? '全部商品' : categories[activeCategory] }}</text>
  43. <text class="product-count">共 {{ displayProducts.length }} 件</text>
  44. </view>
  45. <!-- 空状态 -->
  46. <view v-if="displayProducts.length === 0 && !loading" class="empty-state">
  47. <text>暂无商品</text>
  48. </view>
  49. <!-- 商品网格 -->
  50. <view class="products-grid">
  51. <view
  52. v-for="(product, index) in displayProducts"
  53. :key="product.id"
  54. class="product-card"
  55. @click="handleProductClick(product)"
  56. >
  57. <view class="product-image">
  58. <image :src="product.image || defaultImage" mode="aspectFit" @error="handleImageError($event, index)"></image>
  59. </view>
  60. <view class="product-info">
  61. <text class="product-name">{{ product.name }}</text>
  62. <view class="product-price">
  63. <text class="price-value">{{ product.points }}</text>
  64. <text class="price-unit">💰</text>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. <!-- 底部空白 -->
  70. <view class="bottom-spacer"></view>
  71. </scroll-view>
  72. </view>
  73. </template>
  74. <script>
  75. import api from '@/utils/api.js'
  76. export default {
  77. name: 'points-mall',
  78. data() {
  79. return {
  80. userPoints: 0,
  81. cartCount: 0,
  82. activeCategory: 0,
  83. categories: ['全部', '实物商品', '虚拟商品'],
  84. recommendedProducts: [],
  85. allProducts: [],
  86. makerId: null,
  87. loading: false,
  88. defaultImage: 'https://img.zcool.cn/community/01b8d95d3f8a8fa801211d53e8c6b0.jpg@1280w_1l_2o_100sh.jpg'
  89. }
  90. },
  91. computed: {
  92. // 根据分类筛选商品
  93. displayProducts() {
  94. if (this.activeCategory === 0) {
  95. return this.allProducts
  96. }
  97. return this.allProducts.filter(p => p.category === this.activeCategory)
  98. }
  99. },
  100. onLoad() {
  101. this.initData()
  102. },
  103. onShow() {
  104. // 每次显示页面时刷新积分余额
  105. if (this.makerId) {
  106. this.loadBalance()
  107. }
  108. },
  109. methods: {
  110. // 初始化数据
  111. async initData() {
  112. // 获取红娘ID
  113. const userInfo = uni.getStorageSync('userInfo')
  114. if (userInfo && userInfo.matchmakerId) {
  115. this.makerId = userInfo.matchmakerId
  116. } else if (userInfo && userInfo.userId) {
  117. // 如果没有matchmakerId,通过API获取
  118. try {
  119. const res = await api.matchmaker.getByUserId(userInfo.userId)
  120. let matchmaker = res
  121. if (res && res.data) {
  122. matchmaker = res.data
  123. }
  124. if (matchmaker && (matchmaker.matchmakerId || matchmaker.matchmaker_id)) {
  125. this.makerId = matchmaker.matchmakerId || matchmaker.matchmaker_id
  126. // 保存到userInfo中
  127. userInfo.matchmakerId = this.makerId
  128. uni.setStorageSync('userInfo', userInfo)
  129. }
  130. } catch (e) {
  131. console.error('获取红娘信息失败:', e)
  132. }
  133. }
  134. await Promise.all([
  135. this.loadBalance(),
  136. this.loadProducts(),
  137. this.loadRecommendProducts()
  138. ])
  139. },
  140. // 加载积分余额
  141. async loadBalance() {
  142. if (!this.makerId) return
  143. try {
  144. const res = await api.pointsMall.getBalance(this.makerId)
  145. this.userPoints = res.balance || 0
  146. } catch (e) {
  147. console.error('获取积分余额失败:', e)
  148. }
  149. },
  150. // 加载商品列表
  151. async loadProducts() {
  152. this.loading = true
  153. try {
  154. const res = await api.pointsMall.getProducts({ pageNum: 1, pageSize: 50 })
  155. this.allProducts = (res.list || []).map(item => ({
  156. id: item.id,
  157. name: item.name,
  158. points: item.pointsPrice,
  159. image: item.imageUrl || this.defaultImage,
  160. category: item.category,
  161. stock: item.stock,
  162. description: item.description
  163. }))
  164. } catch (e) {
  165. console.error('获取商品列表失败:', e)
  166. } finally {
  167. this.loading = false
  168. }
  169. },
  170. // 加载推荐商品
  171. async loadRecommendProducts() {
  172. try {
  173. const res = await api.pointsMall.getRecommendProducts(10)
  174. this.recommendedProducts = (res || []).map(item => ({
  175. id: item.id,
  176. name: item.name,
  177. points: item.pointsPrice,
  178. image: item.imageUrl || this.defaultImage,
  179. category: item.category,
  180. stock: item.stock
  181. }))
  182. } catch (e) {
  183. console.error('获取推荐商品失败:', e)
  184. }
  185. },
  186. // 返回上一页
  187. handleBack() {
  188. uni.navigateBack()
  189. },
  190. // 查看积分明细
  191. handlePointsDetail() {
  192. uni.navigateTo({
  193. url: '/pages/matchmaker-workbench/points-detail'
  194. })
  195. },
  196. // 赚取积分
  197. handleEarnPoints() {
  198. uni.navigateTo({
  199. url: '/pages/matchmaker-workbench/earn-points'
  200. })
  201. },
  202. // 查看更多
  203. handleViewMore() {
  204. // 切换到全部分类
  205. this.activeCategory = 0
  206. },
  207. // 商品点击 - 跳转到商品详情
  208. handleProductClick(product) {
  209. uni.navigateTo({
  210. url: `/pages/matchmaker-workbench/product-detail?id=${product.id}`
  211. })
  212. },
  213. // 图片加载失败处理
  214. handleImageError(e, index) {
  215. if (this.recommendedProducts[index]) {
  216. this.$set(this.recommendedProducts[index], 'image', this.defaultImage)
  217. }
  218. }
  219. }
  220. }
  221. </script>
  222. <style lang="scss" scoped>
  223. .points-mall {
  224. min-height: 100vh;
  225. background: #FFF9F9;
  226. display: flex;
  227. flex-direction: column;
  228. }
  229. /* 顶部导航栏 */
  230. .header {
  231. display: flex;
  232. align-items: center;
  233. justify-content: space-between;
  234. padding: 25rpx 30rpx;
  235. padding-top: calc(25rpx + env(safe-area-inset-top));
  236. background: #FFF9F9;
  237. border-bottom: 1rpx solid #F0F0F0;
  238. .back-icon {
  239. width: 44rpx;
  240. height: 44rpx;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. font-size: 32rpx;
  245. color: #333;
  246. &::before {
  247. content: '‹';
  248. }
  249. }
  250. .header-title {
  251. font-size: 38rpx;
  252. font-weight: bold;
  253. color: #333;
  254. }
  255. .header-right {
  256. display: flex;
  257. align-items: center;
  258. gap: 20rpx;
  259. .search-icon {
  260. width: 44rpx;
  261. height: 44rpx;
  262. display: flex;
  263. align-items: center;
  264. justify-content: center;
  265. font-size: 32rpx;
  266. color: #999;
  267. &::before {
  268. content: '🔍';
  269. }
  270. }
  271. .cart-icon {
  272. width: 44rpx;
  273. height: 44rpx;
  274. display: flex;
  275. align-items: center;
  276. justify-content: center;
  277. font-size: 32rpx;
  278. color: #999;
  279. position: relative;
  280. &::before {
  281. content: '🛒';
  282. }
  283. .cart-badge {
  284. position: absolute;
  285. top: -8rpx;
  286. right: -8rpx;
  287. width: 32rpx;
  288. height: 32rpx;
  289. display: flex;
  290. align-items: center;
  291. justify-content: center;
  292. background-color: #FF4444;
  293. color: #FFFFFF;
  294. font-size: 20rpx;
  295. font-weight: bold;
  296. border-radius: 50%;
  297. }
  298. }
  299. }
  300. }
  301. /* 积分显示区域 */
  302. .points-section {
  303. background: linear-gradient(135deg, #FCE4EC 0%, #F8BBD0 100%);
  304. padding: 40rpx 30rpx;
  305. border-radius: 0 0 30rpx 30rpx;
  306. .points-header {
  307. display: flex;
  308. align-items: center;
  309. justify-content: center;
  310. gap: 20rpx;
  311. margin-bottom: 30rpx;
  312. .points-label {
  313. font-size: 28rpx;
  314. color: #666666;
  315. }
  316. .points-value {
  317. font-size: 64rpx;
  318. font-weight: bold;
  319. color: #333333;
  320. }
  321. .points-icon {
  322. font-size: 48rpx;
  323. }
  324. }
  325. .points-actions {
  326. display: flex;
  327. justify-content: space-around;
  328. .action-btn {
  329. padding: 15rpx 40rpx;
  330. background-color: rgba(255, 255, 255, 0.8);
  331. color: #9C27B0;
  332. border-radius: 30rpx;
  333. font-size: 28rpx;
  334. font-weight: bold;
  335. transition: all 0.3s;
  336. &:active {
  337. background-color: rgba(255, 255, 255, 1);
  338. }
  339. }
  340. }
  341. }
  342. /* 分类标签 */
  343. .category-tabs {
  344. display: flex;
  345. gap: 20rpx;
  346. padding: 20rpx 30rpx;
  347. background: #FFFFFF;
  348. overflow-x: auto;
  349. white-space: nowrap;
  350. .category-tab {
  351. padding: 12rpx 24rpx;
  352. background: #F5F5F5;
  353. color: #666;
  354. border-radius: 20rpx;
  355. font-size: 26rpx;
  356. white-space: nowrap;
  357. transition: all 0.3s;
  358. &.active {
  359. background: #9C27B0;
  360. color: #FFFFFF;
  361. font-weight: bold;
  362. }
  363. }
  364. }
  365. /* 商品容器 */
  366. .products-container {
  367. flex: 1;
  368. background: #FFFFFF;
  369. padding: 20rpx 30rpx;
  370. }
  371. /* 分类标题 */
  372. .section-header {
  373. display: flex;
  374. align-items: center;
  375. justify-content: space-between;
  376. margin-bottom: 20rpx;
  377. .section-title {
  378. font-size: 32rpx;
  379. font-weight: bold;
  380. color: #333;
  381. }
  382. .product-count {
  383. font-size: 24rpx;
  384. color: #999;
  385. }
  386. .view-more {
  387. display: flex;
  388. align-items: center;
  389. gap: 5rpx;
  390. .view-more-text {
  391. font-size: 24rpx;
  392. color: #9C27B0;
  393. }
  394. .arrow {
  395. font-size: 28rpx;
  396. color: #9C27B0;
  397. }
  398. }
  399. }
  400. /* 空状态 */
  401. .empty-state {
  402. text-align: center;
  403. padding: 100rpx 0;
  404. color: #999;
  405. font-size: 28rpx;
  406. }
  407. /* 废弃的view-more样式 */
  408. .view-more-deprecated {
  409. display: flex;
  410. align-items: center;
  411. gap: 5rpx;
  412. .view-more-text {
  413. font-size: 24rpx;
  414. color: #9C27B0;
  415. }
  416. .arrow {
  417. font-size: 28rpx;
  418. color: #9C27B0;
  419. }
  420. }
  421. /* 商品网格 */
  422. .products-grid {
  423. display: grid;
  424. grid-template-columns: repeat(2, 1fr);
  425. gap: 20rpx;
  426. margin-bottom: 20rpx;
  427. .product-card {
  428. background: #FFFFFF;
  429. border-radius: 16rpx;
  430. overflow: hidden;
  431. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  432. transition: all 0.3s;
  433. &:active {
  434. transform: translateY(-4rpx);
  435. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.12);
  436. }
  437. .product-image {
  438. width: 100%;
  439. height: 300rpx;
  440. background: #F5F5F5;
  441. display: flex;
  442. align-items: center;
  443. justify-content: center;
  444. image {
  445. width: 100%;
  446. height: 100%;
  447. object-fit: cover;
  448. }
  449. }
  450. .product-info {
  451. padding: 15rpx;
  452. .product-name {
  453. display: block;
  454. font-size: 24rpx;
  455. color: #333;
  456. margin-bottom: 10rpx;
  457. overflow: hidden;
  458. text-overflow: ellipsis;
  459. white-space: nowrap;
  460. }
  461. .product-price {
  462. display: flex;
  463. align-items: center;
  464. gap: 5rpx;
  465. .price-value {
  466. font-size: 28rpx;
  467. font-weight: bold;
  468. color: #9C27B0;
  469. }
  470. .price-unit {
  471. font-size: 24rpx;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. /* 底部空白 */
  478. .bottom-spacer {
  479. height: 40rpx;
  480. }
  481. </style>