index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  3. <!-- 登录浮层(全局组件,页面内弹出底部登录) -->
  4. <login-sheet ref="loginSheet" @success="onSheetLoginSuccess" />
  5. <!-- 未登录 -->
  6. <template v-if="!isLoggedIn">
  7. <view class="empty-state">
  8. <text class="empty-icon">💎</text>
  9. <text class="empty-title">登录以查看我的CF值</text>
  10. <text class="empty-desc">CF值是个人平台积分,购买商品、参与活动即可获得</text>
  11. <button class="login-btn" @click="goLogin">去登录</button>
  12. </view>
  13. </template>
  14. <!-- 已登录 -->
  15. <template v-else>
  16. <!-- 余额卡片 -->
  17. <view class="balance-card">
  18. <view class="balance-label">我的CF值余额</view>
  19. <view class="balance-value">{{ balance.available }}</view>
  20. <view class="balance-unit">CF值(1元=1积分)</view>
  21. <view class="balance-row">
  22. <view class="balance-item">
  23. <text class="balance-item-label">累计获得</text>
  24. <text class="balance-item-value">{{ balance.totalEarned }}</text>
  25. </view>
  26. <view class="balance-item">
  27. <text class="balance-item-label">已消费</text>
  28. <text class="balance-item-value">{{ balance.exchanged }}</text>
  29. </view>
  30. <view class="balance-item">
  31. <text class="balance-item-label">冻结中</text>
  32. <text class="balance-item-value">{{ balance.frozen }}</text>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 操作按钮 -->
  37. <view class="action-row">
  38. <view class="action-btn" @click="refreshData">
  39. <text class="action-icon">🔄</text>
  40. <text class="action-text">刷新</text>
  41. </view>
  42. <view class="action-btn" @click="openExchangeModal" v-if="balance.available > 0">
  43. <text class="action-icon">🎫</text>
  44. <text class="action-text">兑换优惠券</text>
  45. </view>
  46. </view>
  47. <!-- 流水列表 -->
  48. <view class="section">
  49. <view class="section-header">
  50. <text class="section-title">流水记录</text>
  51. <text class="section-more" @click="loadMoreLogs" v-if="hasMore">加载更多</text>
  52. </view>
  53. <view class="log-list" v-if="logs.length > 0">
  54. <view class="log-item" v-for="log in logs" :key="log.id">
  55. <view class="log-left">
  56. <text class="log-type">{{ logTypeLabel(log.type) }}</text>
  57. <text class="log-remark">{{ log.remark || log.refType }}</text>
  58. </view>
  59. <view class="log-right">
  60. <text class="log-amount" :class="log.amount > 0 ? 'log-plus' : 'log-minus'">
  61. {{ log.amount > 0 ? '+' : '' }}{{ log.amount }}
  62. </text>
  63. <text class="log-time">{{ formatTime(log.createdAt) }}</text>
  64. </view>
  65. </view>
  66. </view>
  67. <view class="empty-logs" v-else>
  68. <text>暂无流水记录</text>
  69. </view>
  70. </view>
  71. <!-- 家庭共享优惠券(家庭内成员共享可见) -->
  72. <view class="section">
  73. <view class="section-header">
  74. <text class="section-title">家庭共享券</text>
  75. <text class="section-more" @click="refreshCoupons" v-if="familyCoupons.length > 0">刷新</text>
  76. </view>
  77. <view class="fc-list" v-if="familyCoupons.length > 0">
  78. <view class="fc-item" v-for="coupon in familyCoupons" :key="coupon.id">
  79. <view class="fc-left">
  80. <text class="fc-name">{{ coupon.name }}</text>
  81. <text class="fc-desc">{{ couponDesc(coupon) }} · {{ couponStatusLabel(coupon.status) }}</text>
  82. </view>
  83. <view class="fc-right">
  84. <text class="fc-time">{{ formatDate(coupon.validUntil) }}</text>
  85. </view>
  86. </view>
  87. </view>
  88. <view class="empty-logs" v-else>
  89. <text>暂无家庭共享券</text>
  90. </view>
  91. </view>
  92. </template>
  93. <!-- 兑换优惠券弹窗 -->
  94. <view class="modal-mask" v-if="showExchangeModal" @click="showExchangeModal = false">
  95. <view class="exchange-modal" @click.stop>
  96. <view class="modal-header">
  97. <text class="modal-title">兑换优惠券</text>
  98. <text class="modal-close" @click="showExchangeModal = false">×</text>
  99. </view>
  100. <view class="modal-body">
  101. <view class="coupon-list" v-if="exchangeableCoupons.length > 0">
  102. <view class="coupon-item" v-for="coupon in exchangeableCoupons" :key="coupon.id" @click="doExchange(coupon)">
  103. <view class="coupon-info">
  104. <text class="coupon-name">{{ coupon.name }}</text>
  105. <text class="coupon-desc">{{ couponDesc(coupon) }}</text>
  106. </view>
  107. <view class="coupon-price">{{ coupon.pointsPrice }} CF值</view>
  108. </view>
  109. </view>
  110. <view class="empty-coupons" v-else>
  111. <text>暂无可兑换优惠券</text>
  112. </view>
  113. </view>
  114. </view>
  115. </view>
  116. </view>
  117. </template>
  118. <script>
  119. import { getCfBalance, getCfLogs, getFamilyCoupons, getExchangeableCoupons, exchangeCouponByCf } from '@/utils/api.js'
  120. import { parseDate } from '@/utils/format.js'
  121. import fontScaleMixin from '@/mixins/font-scale.js'
  122. export default {
  123. mixins: [fontScaleMixin],
  124. data() {
  125. return {
  126. isLoggedIn: false,
  127. balance: { available: 0, totalEarned: 0, exchanged: 0, frozen: 0 },
  128. logs: [],
  129. page: 1,
  130. size: 20,
  131. hasMore: false,
  132. exchangeRecords: [],
  133. showExchangeModal: false,
  134. exchangeableCoupons: [],
  135. familyCoupons: []
  136. }
  137. },
  138. onLoad() {
  139. this.checkLogin()
  140. this.loadBalance()
  141. this.loadLogs()
  142. this.loadFamilyCoupons()
  143. this._onLoadFired = true
  144. },
  145. onShow() {
  146. if (this._onLoadFired) {
  147. this._onLoadFired = false
  148. } else {
  149. if (this.isLoggedIn) this.loadBalance()
  150. }
  151. },
  152. methods: {
  153. checkLogin() {
  154. const token = uni.getStorageSync('token')
  155. this.isLoggedIn = !!token
  156. },
  157. goLogin() {
  158. this.openLoginSheet()
  159. },
  160. // 登录浮层成功回调:刷新登录态、余额与流水
  161. onLoginSuccess() {
  162. this.checkLogin()
  163. if (this.isLoggedIn) {
  164. this.loadBalance()
  165. this.loadLogs()
  166. this.loadFamilyCoupons()
  167. }
  168. },
  169. // 个人 CF 值余额快照(/api/cf/balance,个人成员账本 cf_balance)
  170. async loadBalance() {
  171. try {
  172. const res = await getCfBalance()
  173. if (res.code === 200 && res.data) {
  174. this.balance = res.data
  175. }
  176. } catch (e) {
  177. console.error('加载余额失败', e)
  178. }
  179. },
  180. // 个人 CF 值流水分页(/api/cf/logs,page/size 与 CfBalanceLogVO 分页一致)
  181. async loadLogs() {
  182. try {
  183. const res = await getCfLogs({ page: this.page, size: this.size })
  184. if (res.code === 200 && res.data) {
  185. if (this.page === 1) {
  186. this.logs = res.data.records || []
  187. } else {
  188. this.logs = this.logs.concat(res.data.records || [])
  189. }
  190. this.hasMore = this.logs.length < (res.data.total || 0)
  191. }
  192. } catch (e) {
  193. console.error('加载流水失败', e)
  194. }
  195. },
  196. // 家庭共享优惠券(/api/cf/family-coupons,家庭内成员共享可见)
  197. async loadFamilyCoupons() {
  198. try {
  199. const res = await getFamilyCoupons()
  200. if (res.code === 200) {
  201. this.familyCoupons = res.data || []
  202. }
  203. } catch (e) {
  204. console.error('加载家庭共享券失败', e)
  205. }
  206. },
  207. refreshCoupons() {
  208. this.loadFamilyCoupons()
  209. },
  210. loadMoreLogs() {
  211. if (this.hasMore) {
  212. this.page++
  213. this.loadLogs()
  214. }
  215. },
  216. refreshData() {
  217. this.page = 1
  218. this.loadBalance()
  219. this.loadLogs()
  220. this.loadFamilyCoupons()
  221. },
  222. async openExchangeModal() {
  223. this.showExchangeModal = true
  224. try {
  225. const res = await getExchangeableCoupons()
  226. if (res.code === 200) {
  227. this.exchangeableCoupons = res.data || []
  228. }
  229. } catch (e) {
  230. console.error('加载可兑换优惠券失败', e)
  231. }
  232. },
  233. async doExchange(coupon) {
  234. if (coupon.pointsPrice > this.balance.available) {
  235. uni.showToast({ title: 'CF值不足', icon: 'none' })
  236. return
  237. }
  238. uni.showLoading({ title: '兑换中...' })
  239. try {
  240. const res = await exchangeCouponByCf({ couponId: coupon.id })
  241. uni.hideLoading()
  242. if (res.code === 200) {
  243. uni.showToast({ title: '兑换成功', icon: 'success' })
  244. this.showExchangeModal = false
  245. this.loadBalance()
  246. // 该券已归家庭所有,从可兑换列表移除(下次打开弹窗时后端也会过滤掉)
  247. const idx = this.exchangeableCoupons.findIndex(function(c) { return c.id === coupon.id })
  248. if (idx >= 0) {
  249. this.exchangeableCoupons.splice(idx, 1)
  250. }
  251. } else {
  252. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  253. }
  254. } catch (e) {
  255. uni.hideLoading()
  256. uni.showToast({ title: '兑换失败', icon: 'none' })
  257. }
  258. },
  259. logTypeLabel(type) {
  260. const map = {
  261. earn: '获得', spend: '消费', freeze: '冻结',
  262. unfreeze: '解冻', withdraw: '提现', adjust: '调整', migration: '迁移'
  263. }
  264. return map[type] || type
  265. },
  266. couponDesc(coupon) {
  267. if (coupon.type === 'DISCOUNT' || coupon.type === 'PERCENT') {
  268. return (coupon.discountRate / 100).toFixed(1) + '折'
  269. }
  270. return '立减' + (coupon.value / 100).toFixed(2) + '元'
  271. },
  272. couponStatusLabel(status) {
  273. const map = {
  274. AVAILABLE: '可使用',
  275. USED: '已使用',
  276. EXPIRED: '已过期',
  277. DISABLED: '已停用'
  278. }
  279. return map[status] || status || ''
  280. },
  281. // 有效期截止日(仅日期场景,yyyy-MM-dd)
  282. formatDate(dateStr) {
  283. if (!dateStr) return ''
  284. return String(dateStr).substring(0, 10)
  285. },
  286. formatTime(dateStr) {
  287. if (!dateStr) return ''
  288. const d = parseDate(dateStr)
  289. if (!d) return ''
  290. const m = d.getMonth() + 1
  291. const day = d.getDate()
  292. const h = d.getHours()
  293. const min = String(d.getMinutes()).padStart(2, '0')
  294. return m + '/' + day + ' ' + h + ':' + min
  295. }
  296. }
  297. }
  298. </script>
  299. <style scoped lang="scss">
  300. $c-tint-indigo-border: #e8eaff; // indigo 极浅边框(单页专有)
  301. .container {
  302. min-height: 100vh;
  303. background: $app-bg-2;
  304. padding-bottom: 40rpx;
  305. }
  306. .empty-state {
  307. display: flex;
  308. flex-direction: column;
  309. align-items: center;
  310. justify-content: center;
  311. padding: 200rpx 40rpx;
  312. }
  313. .empty-icon {
  314. font-size: calc($uni-font-size-6xl * var(--font-scale, 1));
  315. margin-bottom: 20rpx;
  316. }
  317. .empty-title {
  318. font-size: calc(32rpx * var(--font-scale, 1));
  319. color: $facade-text-strong;
  320. font-weight: 600;
  321. margin-bottom: 12rpx;
  322. }
  323. .empty-desc {
  324. font-size: calc(26rpx * var(--font-scale, 1));
  325. color: $facade-text-muted;
  326. margin-bottom: 40rpx;
  327. text-align: center;
  328. }
  329. .login-btn {
  330. width: 280rpx;
  331. height: 80rpx;
  332. line-height: 80rpx;
  333. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  334. color: $app-surface;
  335. border-radius: 24rpx;
  336. font-size: calc(28rpx * var(--font-scale, 1));
  337. border: none;
  338. }
  339. .balance-card {
  340. margin: $uni-space-lg;
  341. padding: 40rpx $uni-space-page;
  342. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  343. border-radius: 24rpx;
  344. color: $app-surface;
  345. }
  346. .balance-label {
  347. font-size: calc(26rpx * var(--font-scale, 1));
  348. opacity: 0.8;
  349. margin-bottom: 10rpx;
  350. }
  351. .balance-value {
  352. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  353. font-weight: 700;
  354. line-height: 1;
  355. margin-bottom: 8rpx;
  356. }
  357. .balance-unit {
  358. font-size: calc(22rpx * var(--font-scale, 1));
  359. opacity: 0.7;
  360. margin-bottom: $uni-space-lg;
  361. }
  362. .balance-row {
  363. display: flex;
  364. justify-content: space-around;
  365. border-top: 1rpx solid rgba(255,255,255,0.2);
  366. padding-top: 20rpx;
  367. }
  368. .balance-item {
  369. text-align: center;
  370. }
  371. .balance-item-label {
  372. font-size: calc(22rpx * var(--font-scale, 1));
  373. opacity: 0.7;
  374. display: block;
  375. margin-bottom: 6rpx;
  376. }
  377. .balance-item-value {
  378. font-size: calc(28rpx * var(--font-scale, 1));
  379. font-weight: 600;
  380. }
  381. .action-row {
  382. display: flex;
  383. margin: 0 $uni-space-lg 20rpx;
  384. gap: 20rpx;
  385. }
  386. .action-btn {
  387. flex: 1;
  388. display: flex;
  389. flex-direction: column;
  390. align-items: center;
  391. padding: 24rpx;
  392. background: $app-surface;
  393. border-radius: 16rpx;
  394. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  395. }
  396. .action-icon {
  397. font-size: calc(36rpx * var(--font-scale, 1));
  398. margin-bottom: 8rpx;
  399. }
  400. .action-text {
  401. font-size: calc(26rpx * var(--font-scale, 1));
  402. color: $facade-text-strong;
  403. }
  404. .section {
  405. margin: 20rpx $uni-space-lg;
  406. }
  407. .section-header {
  408. display: flex;
  409. justify-content: space-between;
  410. align-items: center;
  411. margin-bottom: 16rpx;
  412. }
  413. .section-title {
  414. font-size: calc(30rpx * var(--font-scale, 1));
  415. font-weight: 600;
  416. color: $facade-text-strong;
  417. }
  418. .section-more {
  419. font-size: calc(24rpx * var(--font-scale, 1));
  420. color: $brand-primary;
  421. }
  422. .log-list {
  423. background: $app-surface;
  424. border-radius: 16rpx;
  425. overflow: hidden;
  426. }
  427. .log-item {
  428. display: flex;
  429. justify-content: space-between;
  430. align-items: center;
  431. padding: 24rpx $uni-space-page;
  432. border-bottom: 1rpx solid $facade-fill;
  433. }
  434. .log-item:last-child {
  435. border-bottom: none;
  436. }
  437. .log-left {
  438. flex: 1;
  439. margin-right: 20rpx;
  440. }
  441. .log-type {
  442. font-size: calc(26rpx * var(--font-scale, 1));
  443. font-weight: 600;
  444. color: $facade-text-strong;
  445. display: block;
  446. margin-bottom: 4rpx;
  447. }
  448. .log-remark {
  449. font-size: calc(22rpx * var(--font-scale, 1));
  450. color: $facade-text-muted;
  451. }
  452. .log-right {
  453. text-align: right;
  454. }
  455. .log-amount {
  456. font-size: calc(28rpx * var(--font-scale, 1));
  457. font-weight: 600;
  458. display: block;
  459. }
  460. .log-plus { color: $status-success-3; }
  461. .log-minus { color: $status-danger-2; }
  462. .log-time {
  463. font-size: calc(20rpx * var(--font-scale, 1));
  464. color: $facade-text-ghost-light;
  465. margin-top: 4rpx;
  466. display: block;
  467. }
  468. .empty-logs {
  469. text-align: center;
  470. padding: 60rpx;
  471. color: $facade-text-muted;
  472. font-size: calc(26rpx * var(--font-scale, 1));
  473. background: $app-surface;
  474. border-radius: 16rpx;
  475. }
  476. .fc-list {
  477. background: $app-surface;
  478. border-radius: 16rpx;
  479. overflow: hidden;
  480. }
  481. .fc-item {
  482. display: flex;
  483. justify-content: space-between;
  484. align-items: center;
  485. padding: 24rpx $uni-space-page;
  486. border-bottom: 1rpx solid $facade-fill;
  487. }
  488. .fc-item:last-child {
  489. border-bottom: none;
  490. }
  491. .fc-left {
  492. flex: 1;
  493. margin-right: 20rpx;
  494. }
  495. .fc-name {
  496. font-size: calc(26rpx * var(--font-scale, 1));
  497. font-weight: 600;
  498. color: $facade-text-strong;
  499. display: block;
  500. margin-bottom: 4rpx;
  501. }
  502. .fc-desc {
  503. font-size: calc(22rpx * var(--font-scale, 1));
  504. color: $facade-text-muted;
  505. display: block;
  506. }
  507. .fc-right {
  508. text-align: right;
  509. }
  510. .fc-time {
  511. font-size: calc(20rpx * var(--font-scale, 1));
  512. color: $facade-text-ghost-light;
  513. display: block;
  514. }
  515. .exchange-modal {
  516. background: $app-surface;
  517. border-radius: 24rpx 24rpx 0 0;
  518. max-height: 80vh;
  519. display: flex;
  520. flex-direction: column;
  521. }
  522. .modal-header {
  523. display: flex;
  524. justify-content: space-between;
  525. align-items: center;
  526. padding: $uni-space-page;
  527. border-bottom: 1rpx solid $facade-fill;
  528. }
  529. .modal-title {
  530. font-size: calc(32rpx * var(--font-scale, 1));
  531. font-weight: 600;
  532. color: $facade-text-strong;
  533. }
  534. .modal-close {
  535. font-size: calc(40rpx * var(--font-scale, 1));
  536. color: $facade-text-muted;
  537. line-height: 1;
  538. }
  539. .modal-body {
  540. flex: 1;
  541. overflow-y: auto;
  542. padding: 20rpx $uni-space-page;
  543. }
  544. .coupon-list {
  545. display: flex;
  546. flex-direction: column;
  547. gap: 20rpx;
  548. }
  549. .coupon-item {
  550. display: flex;
  551. justify-content: space-between;
  552. align-items: center;
  553. padding: 24rpx;
  554. background: $dim-wisdom-tint-4;
  555. border-radius: 16rpx;
  556. border: 1rpx solid $c-tint-indigo-border;
  557. }
  558. .coupon-info {
  559. flex: 1;
  560. margin-right: 20rpx;
  561. }
  562. .coupon-name {
  563. font-size: calc(28rpx * var(--font-scale, 1));
  564. font-weight: 600;
  565. color: $facade-text-strong;
  566. display: block;
  567. }
  568. .coupon-desc {
  569. font-size: calc(22rpx * var(--font-scale, 1));
  570. color: $facade-text-muted;
  571. margin-top: 4rpx;
  572. display: block;
  573. }
  574. .coupon-price {
  575. font-size: calc(26rpx * var(--font-scale, 1));
  576. color: $brand-primary;
  577. font-weight: 600;
  578. white-space: nowrap;
  579. }
  580. .empty-coupons {
  581. text-align: center;
  582. padding: 60rpx;
  583. color: $facade-text-muted;
  584. font-size: calc(26rpx * var(--font-scale, 1));
  585. }
  586. </style>