index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <template>
  2. <view class="container">
  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>
  31. </view>
  32. <!-- 操作按钮 -->
  33. <view class="action-row">
  34. <view class="action-btn" @click="refreshData">
  35. <text class="action-icon">🔄</text>
  36. <text class="action-text">刷新</text>
  37. </view>
  38. <view class="action-btn" @click="openExchangeModal" v-if="balance.available > 0">
  39. <text class="action-icon">🎫</text>
  40. <text class="action-text">兑换优惠券</text>
  41. </view>
  42. </view>
  43. <!-- 流水列表 -->
  44. <view class="section">
  45. <view class="section-header">
  46. <text class="section-title">流水记录</text>
  47. <text class="section-more" @click="loadMoreLogs" v-if="hasMore">加载更多</text>
  48. </view>
  49. <view class="log-list" v-if="logs.length > 0">
  50. <view class="log-item" v-for="log in logs" :key="log.id">
  51. <view class="log-left">
  52. <text class="log-type">{{ logTypeLabel(log.type) }}</text>
  53. <text class="log-remark">{{ log.remark || log.refType }}</text>
  54. </view>
  55. <view class="log-right">
  56. <text class="log-amount" :class="log.amount > 0 ? 'log-plus' : 'log-minus'">
  57. {{ log.amount > 0 ? '+' : '' }}{{ log.amount }}
  58. </text>
  59. <text class="log-time">{{ formatTime(log.createdAt) }}</text>
  60. </view>
  61. </view>
  62. </view>
  63. <view class="empty-logs" v-else>
  64. <text>暂无流水记录</text>
  65. </view>
  66. </view>
  67. </template>
  68. <!-- 兑换优惠券弹窗 -->
  69. <view class="modal-mask" v-if="showExchangeModal" @click="showExchangeModal = false">
  70. <view class="exchange-modal" @click.stop>
  71. <view class="modal-header">
  72. <text class="modal-title">兑换优惠券</text>
  73. <text class="modal-close" @click="showExchangeModal = false">×</text>
  74. </view>
  75. <view class="modal-body">
  76. <view class="coupon-list" v-if="exchangeableCoupons.length > 0">
  77. <view class="coupon-item" v-for="coupon in exchangeableCoupons" :key="coupon.id" @click="doExchange(coupon)">
  78. <view class="coupon-info">
  79. <text class="coupon-name">{{ coupon.name }}</text>
  80. <text class="coupon-desc">{{ couponDesc(coupon) }}</text>
  81. </view>
  82. <view class="coupon-price">{{ coupon.pointsPrice }} CF值</view>
  83. </view>
  84. </view>
  85. <view class="empty-coupons" v-else>
  86. <text>暂无可兑换优惠券</text>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. </view>
  92. </template>
  93. <script>
  94. import { getFamilyPlatformBalance, getFamilyPlatformLogs, exchangeCouponByCf } from '@/utils/api.js'
  95. export default {
  96. data() {
  97. return {
  98. isLoggedIn: false,
  99. balance: { available: 0, totalEarned: 0, exchanged: 0, frozen: 0 },
  100. logs: [],
  101. page: 1,
  102. size: 20,
  103. hasMore: false,
  104. exchangeRecords: [],
  105. showExchangeModal: false,
  106. exchangeableCoupons: []
  107. }
  108. },
  109. onLoad() {
  110. this.checkLogin()
  111. this.loadBalance()
  112. this.loadLogs()
  113. this._onLoadFired = true
  114. },
  115. onShow() {
  116. if (this._onLoadFired) {
  117. this._onLoadFired = false
  118. } else {
  119. if (this.isLoggedIn) this.loadBalance()
  120. }
  121. },
  122. methods: {
  123. checkLogin() {
  124. const token = uni.getStorageSync('token')
  125. this.isLoggedIn = !!token
  126. },
  127. goLogin() {
  128. this.openLoginSheet()
  129. },
  130. // 登录浮层成功回调:刷新登录态、余额与流水
  131. onLoginSuccess() {
  132. this.checkLogin()
  133. if (this.isLoggedIn) {
  134. this.loadBalance()
  135. this.loadLogs()
  136. }
  137. },
  138. async loadBalance() {
  139. try {
  140. const res = await getFamilyPlatformBalance()
  141. if (res.code === 200 && res.data) {
  142. this.balance = res.data
  143. }
  144. } catch (e) {
  145. console.error('加载余额失败', e)
  146. }
  147. },
  148. async loadLogs() {
  149. try {
  150. const res = await getFamilyPlatformLogs({ page: this.page, size: this.size })
  151. if (res.code === 200 && res.data) {
  152. if (this.page === 1) {
  153. this.logs = res.data.records || []
  154. } else {
  155. this.logs = this.logs.concat(res.data.records || [])
  156. }
  157. this.hasMore = this.logs.length < (res.data.total || 0)
  158. }
  159. } catch (e) {
  160. console.error('加载流水失败', e)
  161. }
  162. },
  163. loadMoreLogs() {
  164. if (this.hasMore) {
  165. this.page++
  166. this.loadLogs()
  167. }
  168. },
  169. refreshData() {
  170. this.page = 1
  171. this.loadBalance()
  172. this.loadLogs()
  173. },
  174. async openExchangeModal() {
  175. this.showExchangeModal = true
  176. try {
  177. const res = await getExchangeableCoupons()
  178. if (res.code === 200) {
  179. this.exchangeableCoupons = res.data || []
  180. }
  181. } catch (e) {
  182. console.error('加载可兑换优惠券失败', e)
  183. }
  184. },
  185. async doExchange(coupon) {
  186. if (coupon.pointsPrice > this.balance.available) {
  187. uni.showToast({ title: 'CF值不足', icon: 'none' })
  188. return
  189. }
  190. uni.showLoading({ title: '兑换中...' })
  191. try {
  192. const res = await exchangeCouponByCf({ couponId: coupon.id })
  193. uni.hideLoading()
  194. if (res.code === 200) {
  195. uni.showToast({ title: '兑换成功', icon: 'success' })
  196. this.showExchangeModal = false
  197. this.loadBalance()
  198. // 该券已归家庭所有,从可兑换列表移除(下次打开弹窗时后端也会过滤掉)
  199. const idx = this.exchangeableCoupons.findIndex(function(c) { return c.id === coupon.id })
  200. if (idx >= 0) {
  201. this.exchangeableCoupons.splice(idx, 1)
  202. }
  203. } else {
  204. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  205. }
  206. } catch (e) {
  207. uni.hideLoading()
  208. uni.showToast({ title: '兑换失败', icon: 'none' })
  209. }
  210. },
  211. logTypeLabel(type) {
  212. const map = {
  213. earn: '获得', spend: '消费', freeze: '冻结',
  214. unfreeze: '解冻', withdraw: '提现', adjust: '调整', migration: '迁移'
  215. }
  216. return map[type] || type
  217. },
  218. couponDesc(coupon) {
  219. if (coupon.type === 'DISCOUNT') {
  220. return (coupon.discountRate / 100).toFixed(1) + '折'
  221. }
  222. return '立减' + (coupon.value / 100).toFixed(2) + '元'
  223. },
  224. formatTime(dateStr) {
  225. if (!dateStr) return ''
  226. const d = new Date(dateStr)
  227. const m = d.getMonth() + 1
  228. const day = d.getDate()
  229. const h = d.getHours()
  230. const min = String(d.getMinutes()).padStart(2, '0')
  231. return m + '/' + day + ' ' + h + ':' + min
  232. }
  233. }
  234. }
  235. </script>
  236. <style scoped lang="scss">
  237. $c-tint-indigo-border: #e8eaff; // indigo 极浅边框(单页专有)
  238. .container {
  239. min-height: 100vh;
  240. background: $app-bg-2;
  241. padding-bottom: 40rpx;
  242. }
  243. .empty-state {
  244. display: flex;
  245. flex-direction: column;
  246. align-items: center;
  247. justify-content: center;
  248. padding: 200rpx 40rpx;
  249. }
  250. .empty-icon {
  251. font-size: 100rpx;
  252. margin-bottom: 20rpx;
  253. }
  254. .empty-title {
  255. font-size: 32rpx;
  256. color: $facade-text-strong;
  257. font-weight: 600;
  258. margin-bottom: 12rpx;
  259. }
  260. .empty-desc {
  261. font-size: 26rpx;
  262. color: $facade-text-muted;
  263. margin-bottom: 40rpx;
  264. text-align: center;
  265. }
  266. .login-btn {
  267. width: 280rpx;
  268. height: 80rpx;
  269. line-height: 80rpx;
  270. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  271. color: $app-surface;
  272. border-radius: 40rpx;
  273. font-size: 28rpx;
  274. border: none;
  275. }
  276. .balance-card {
  277. margin: 30rpx;
  278. padding: 40rpx 30rpx;
  279. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  280. border-radius: 24rpx;
  281. color: $app-surface;
  282. }
  283. .balance-label {
  284. font-size: 26rpx;
  285. opacity: 0.8;
  286. margin-bottom: 10rpx;
  287. }
  288. .balance-value {
  289. font-size: 72rpx;
  290. font-weight: 700;
  291. line-height: 1;
  292. margin-bottom: 8rpx;
  293. }
  294. .balance-unit {
  295. font-size: 22rpx;
  296. opacity: 0.7;
  297. margin-bottom: 30rpx;
  298. }
  299. .balance-row {
  300. display: flex;
  301. justify-content: space-around;
  302. border-top: 1rpx solid rgba(255,255,255,0.2);
  303. padding-top: 20rpx;
  304. }
  305. .balance-item {
  306. text-align: center;
  307. }
  308. .balance-item-label {
  309. font-size: 22rpx;
  310. opacity: 0.7;
  311. display: block;
  312. margin-bottom: 6rpx;
  313. }
  314. .balance-item-value {
  315. font-size: 28rpx;
  316. font-weight: 600;
  317. }
  318. .action-row {
  319. display: flex;
  320. margin: 0 30rpx 20rpx;
  321. gap: 20rpx;
  322. }
  323. .action-btn {
  324. flex: 1;
  325. display: flex;
  326. flex-direction: column;
  327. align-items: center;
  328. padding: 24rpx;
  329. background: $app-surface;
  330. border-radius: 16rpx;
  331. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  332. }
  333. .action-icon {
  334. font-size: 36rpx;
  335. margin-bottom: 8rpx;
  336. }
  337. .action-text {
  338. font-size: 26rpx;
  339. color: $facade-text-strong;
  340. }
  341. .section {
  342. margin: 20rpx 30rpx;
  343. }
  344. .section-header {
  345. display: flex;
  346. justify-content: space-between;
  347. align-items: center;
  348. margin-bottom: 16rpx;
  349. }
  350. .section-title {
  351. font-size: 30rpx;
  352. font-weight: 600;
  353. color: $facade-text-strong;
  354. }
  355. .section-more {
  356. font-size: 24rpx;
  357. color: $brand-primary;
  358. }
  359. .log-list {
  360. background: $app-surface;
  361. border-radius: 16rpx;
  362. overflow: hidden;
  363. }
  364. .log-item {
  365. display: flex;
  366. justify-content: space-between;
  367. align-items: center;
  368. padding: 24rpx 30rpx;
  369. border-bottom: 1rpx solid $facade-fill;
  370. }
  371. .log-item:last-child {
  372. border-bottom: none;
  373. }
  374. .log-left {
  375. flex: 1;
  376. margin-right: 20rpx;
  377. }
  378. .log-type {
  379. font-size: 26rpx;
  380. font-weight: 600;
  381. color: $facade-text-strong;
  382. display: block;
  383. margin-bottom: 4rpx;
  384. }
  385. .log-remark {
  386. font-size: 22rpx;
  387. color: $facade-text-muted;
  388. }
  389. .log-right {
  390. text-align: right;
  391. }
  392. .log-amount {
  393. font-size: 28rpx;
  394. font-weight: 600;
  395. display: block;
  396. }
  397. .log-plus { color: $status-success-3; }
  398. .log-minus { color: $status-danger-2; }
  399. .log-time {
  400. font-size: 20rpx;
  401. color: $facade-text-ghost-light;
  402. margin-top: 4rpx;
  403. display: block;
  404. }
  405. .empty-logs {
  406. text-align: center;
  407. padding: 60rpx;
  408. color: $facade-text-muted;
  409. font-size: 26rpx;
  410. background: $app-surface;
  411. border-radius: 16rpx;
  412. }
  413. .exchange-modal {
  414. background: $app-surface;
  415. border-radius: 24rpx 24rpx 0 0;
  416. max-height: 80vh;
  417. display: flex;
  418. flex-direction: column;
  419. }
  420. .modal-header {
  421. display: flex;
  422. justify-content: space-between;
  423. align-items: center;
  424. padding: 30rpx;
  425. border-bottom: 1rpx solid $facade-fill;
  426. }
  427. .modal-title {
  428. font-size: 32rpx;
  429. font-weight: 600;
  430. color: $facade-text-strong;
  431. }
  432. .modal-close {
  433. font-size: 40rpx;
  434. color: $facade-text-muted;
  435. line-height: 1;
  436. }
  437. .modal-body {
  438. flex: 1;
  439. overflow-y: auto;
  440. padding: 20rpx 30rpx;
  441. }
  442. .coupon-list {
  443. display: flex;
  444. flex-direction: column;
  445. gap: 20rpx;
  446. }
  447. .coupon-item {
  448. display: flex;
  449. justify-content: space-between;
  450. align-items: center;
  451. padding: 24rpx;
  452. background: $dim-wisdom-tint-4;
  453. border-radius: 16rpx;
  454. border: 1rpx solid $c-tint-indigo-border;
  455. }
  456. .coupon-info {
  457. flex: 1;
  458. margin-right: 20rpx;
  459. }
  460. .coupon-name {
  461. font-size: 28rpx;
  462. font-weight: 600;
  463. color: $facade-text-strong;
  464. display: block;
  465. }
  466. .coupon-desc {
  467. font-size: 22rpx;
  468. color: $facade-text-muted;
  469. margin-top: 4rpx;
  470. display: block;
  471. }
  472. .coupon-price {
  473. font-size: 26rpx;
  474. color: $brand-primary;
  475. font-weight: 600;
  476. white-space: nowrap;
  477. }
  478. .empty-coupons {
  479. text-align: center;
  480. padding: 60rpx;
  481. color: $facade-text-muted;
  482. font-size: 26rpx;
  483. }
  484. </style>