index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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, getExchangeableCoupons, 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. async loadExchangeRecords() {
  170. try {
  171. const res = await getFamilyPlatformLogs({ page: 1, size: 50, type: 'coupon_exchange' })
  172. if (res.code === 200) {
  173. this.exchangeRecords = (res.data && res.data.records) || []
  174. }
  175. } catch (e) { console.error(e) }
  176. },
  177. refreshData() {
  178. this.page = 1
  179. this.loadBalance()
  180. this.loadLogs()
  181. },
  182. async openExchangeModal() {
  183. this.showExchangeModal = true
  184. try {
  185. const res = await getExchangeableCoupons()
  186. if (res.code === 200) {
  187. this.exchangeableCoupons = res.data || []
  188. }
  189. } catch (e) {
  190. console.error('加载可兑换优惠券失败', e)
  191. }
  192. },
  193. async doExchange(coupon) {
  194. if (coupon.pointsPrice > this.balance.available) {
  195. uni.showToast({ title: 'CF值不足', icon: 'none' })
  196. return
  197. }
  198. uni.showLoading({ title: '兑换中...' })
  199. try {
  200. const res = await exchangeCouponByCf({ couponId: coupon.id })
  201. uni.hideLoading()
  202. if (res.code === 200) {
  203. uni.showToast({ title: '兑换成功', icon: 'success' })
  204. this.showExchangeModal = false
  205. this.loadBalance()
  206. } else {
  207. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  208. }
  209. } catch (e) {
  210. uni.hideLoading()
  211. uni.showToast({ title: '兑换失败', icon: 'none' })
  212. }
  213. },
  214. logTypeLabel(type) {
  215. const map = {
  216. earn: '获得', spend: '消费', freeze: '冻结',
  217. unfreeze: '解冻', withdraw: '提现', adjust: '调整', migration: '迁移'
  218. }
  219. return map[type] || type
  220. },
  221. couponDesc(coupon) {
  222. if (coupon.type === 'DISCOUNT') {
  223. return (coupon.discountRate / 100).toFixed(1) + '折'
  224. }
  225. return '立减' + (coupon.value / 100).toFixed(2) + '元'
  226. },
  227. formatTime(dateStr) {
  228. if (!dateStr) return ''
  229. const d = new Date(dateStr)
  230. const m = d.getMonth() + 1
  231. const day = d.getDate()
  232. const h = d.getHours()
  233. const min = String(d.getMinutes()).padStart(2, '0')
  234. return m + '/' + day + ' ' + h + ':' + min
  235. }
  236. }
  237. }
  238. </script>
  239. <style scoped lang="scss">
  240. $c-tint-indigo-border: #e8eaff; // indigo 极浅边框(单页专有)
  241. .container {
  242. min-height: 100vh;
  243. background: $app-bg-2;
  244. padding-bottom: 40rpx;
  245. }
  246. .empty-state {
  247. display: flex;
  248. flex-direction: column;
  249. align-items: center;
  250. justify-content: center;
  251. padding: 200rpx 40rpx;
  252. }
  253. .empty-icon {
  254. font-size: 100rpx;
  255. margin-bottom: 20rpx;
  256. }
  257. .empty-title {
  258. font-size: 32rpx;
  259. color: $facade-text-strong;
  260. font-weight: 600;
  261. margin-bottom: 12rpx;
  262. }
  263. .empty-desc {
  264. font-size: 26rpx;
  265. color: $facade-text-muted;
  266. margin-bottom: 40rpx;
  267. text-align: center;
  268. }
  269. .login-btn {
  270. width: 280rpx;
  271. height: 80rpx;
  272. line-height: 80rpx;
  273. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  274. color: $app-surface;
  275. border-radius: 40rpx;
  276. font-size: 28rpx;
  277. border: none;
  278. }
  279. .balance-card {
  280. margin: 30rpx;
  281. padding: 40rpx 30rpx;
  282. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  283. border-radius: 24rpx;
  284. color: $app-surface;
  285. }
  286. .balance-label {
  287. font-size: 26rpx;
  288. opacity: 0.8;
  289. margin-bottom: 10rpx;
  290. }
  291. .balance-value {
  292. font-size: 72rpx;
  293. font-weight: 700;
  294. line-height: 1;
  295. margin-bottom: 8rpx;
  296. }
  297. .balance-unit {
  298. font-size: 22rpx;
  299. opacity: 0.7;
  300. margin-bottom: 30rpx;
  301. }
  302. .balance-row {
  303. display: flex;
  304. justify-content: space-around;
  305. border-top: 1rpx solid rgba(255,255,255,0.2);
  306. padding-top: 20rpx;
  307. }
  308. .balance-item {
  309. text-align: center;
  310. }
  311. .balance-item-label {
  312. font-size: 22rpx;
  313. opacity: 0.7;
  314. display: block;
  315. margin-bottom: 6rpx;
  316. }
  317. .balance-item-value {
  318. font-size: 28rpx;
  319. font-weight: 600;
  320. }
  321. .action-row {
  322. display: flex;
  323. margin: 0 30rpx 20rpx;
  324. gap: 20rpx;
  325. }
  326. .action-btn {
  327. flex: 1;
  328. display: flex;
  329. flex-direction: column;
  330. align-items: center;
  331. padding: 24rpx;
  332. background: $app-surface;
  333. border-radius: 16rpx;
  334. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  335. }
  336. .action-icon {
  337. font-size: 36rpx;
  338. margin-bottom: 8rpx;
  339. }
  340. .action-text {
  341. font-size: 26rpx;
  342. color: $facade-text-strong;
  343. }
  344. .section {
  345. margin: 20rpx 30rpx;
  346. }
  347. .section-header {
  348. display: flex;
  349. justify-content: space-between;
  350. align-items: center;
  351. margin-bottom: 16rpx;
  352. }
  353. .section-title {
  354. font-size: 30rpx;
  355. font-weight: 600;
  356. color: $facade-text-strong;
  357. }
  358. .section-more {
  359. font-size: 24rpx;
  360. color: $brand-primary;
  361. }
  362. .log-list {
  363. background: $app-surface;
  364. border-radius: 16rpx;
  365. overflow: hidden;
  366. }
  367. .log-item {
  368. display: flex;
  369. justify-content: space-between;
  370. align-items: center;
  371. padding: 24rpx 30rpx;
  372. border-bottom: 1rpx solid $facade-fill;
  373. }
  374. .log-item:last-child {
  375. border-bottom: none;
  376. }
  377. .log-left {
  378. flex: 1;
  379. margin-right: 20rpx;
  380. }
  381. .log-type {
  382. font-size: 26rpx;
  383. font-weight: 600;
  384. color: $facade-text-strong;
  385. display: block;
  386. margin-bottom: 4rpx;
  387. }
  388. .log-remark {
  389. font-size: 22rpx;
  390. color: $facade-text-muted;
  391. }
  392. .log-right {
  393. text-align: right;
  394. }
  395. .log-amount {
  396. font-size: 28rpx;
  397. font-weight: 600;
  398. display: block;
  399. }
  400. .log-plus { color: $status-success-3; }
  401. .log-minus { color: $status-danger-2; }
  402. .log-time {
  403. font-size: 20rpx;
  404. color: $facade-text-ghost-light;
  405. margin-top: 4rpx;
  406. display: block;
  407. }
  408. .empty-logs {
  409. text-align: center;
  410. padding: 60rpx;
  411. color: $facade-text-muted;
  412. font-size: 26rpx;
  413. background: $app-surface;
  414. border-radius: 16rpx;
  415. }
  416. .exchange-modal {
  417. background: $app-surface;
  418. border-radius: 24rpx 24rpx 0 0;
  419. max-height: 80vh;
  420. display: flex;
  421. flex-direction: column;
  422. }
  423. .modal-header {
  424. display: flex;
  425. justify-content: space-between;
  426. align-items: center;
  427. padding: 30rpx;
  428. border-bottom: 1rpx solid $facade-fill;
  429. }
  430. .modal-title {
  431. font-size: 32rpx;
  432. font-weight: 600;
  433. color: $facade-text-strong;
  434. }
  435. .modal-close {
  436. font-size: 40rpx;
  437. color: $facade-text-muted;
  438. line-height: 1;
  439. }
  440. .modal-body {
  441. flex: 1;
  442. overflow-y: auto;
  443. padding: 20rpx 30rpx;
  444. }
  445. .coupon-list {
  446. display: flex;
  447. flex-direction: column;
  448. gap: 20rpx;
  449. }
  450. .coupon-item {
  451. display: flex;
  452. justify-content: space-between;
  453. align-items: center;
  454. padding: 24rpx;
  455. background: $dim-wisdom-tint-4;
  456. border-radius: 16rpx;
  457. border: 1rpx solid $c-tint-indigo-border;
  458. }
  459. .coupon-info {
  460. flex: 1;
  461. margin-right: 20rpx;
  462. }
  463. .coupon-name {
  464. font-size: 28rpx;
  465. font-weight: 600;
  466. color: $facade-text-strong;
  467. display: block;
  468. }
  469. .coupon-desc {
  470. font-size: 22rpx;
  471. color: $facade-text-muted;
  472. margin-top: 4rpx;
  473. display: block;
  474. }
  475. .coupon-price {
  476. font-size: 26rpx;
  477. color: $brand-primary;
  478. font-weight: 600;
  479. white-space: nowrap;
  480. }
  481. .empty-coupons {
  482. text-align: center;
  483. padding: 60rpx;
  484. color: $facade-text-muted;
  485. font-size: 26rpx;
  486. }
  487. </style>