index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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>
  240. .container {
  241. min-height: 100vh;
  242. background: #f5f6fa;
  243. padding-bottom: 40rpx;
  244. }
  245. .empty-state {
  246. display: flex;
  247. flex-direction: column;
  248. align-items: center;
  249. justify-content: center;
  250. padding: 200rpx 40rpx;
  251. }
  252. .empty-icon {
  253. font-size: 100rpx;
  254. margin-bottom: 20rpx;
  255. }
  256. .empty-title {
  257. font-size: 32rpx;
  258. color: #333;
  259. font-weight: 600;
  260. margin-bottom: 12rpx;
  261. }
  262. .empty-desc {
  263. font-size: 26rpx;
  264. color: #999;
  265. margin-bottom: 40rpx;
  266. text-align: center;
  267. }
  268. .login-btn {
  269. width: 280rpx;
  270. height: 80rpx;
  271. line-height: 80rpx;
  272. background: linear-gradient(135deg, #F97316, #FB923C);
  273. color: #fff;
  274. border-radius: 40rpx;
  275. font-size: 28rpx;
  276. border: none;
  277. }
  278. .balance-card {
  279. margin: 30rpx;
  280. padding: 40rpx 30rpx;
  281. background: linear-gradient(135deg, #F97316, #FB923C);
  282. border-radius: 24rpx;
  283. color: #fff;
  284. }
  285. .balance-label {
  286. font-size: 26rpx;
  287. opacity: 0.8;
  288. margin-bottom: 10rpx;
  289. }
  290. .balance-value {
  291. font-size: 72rpx;
  292. font-weight: 700;
  293. line-height: 1;
  294. margin-bottom: 8rpx;
  295. }
  296. .balance-unit {
  297. font-size: 22rpx;
  298. opacity: 0.7;
  299. margin-bottom: 30rpx;
  300. }
  301. .balance-row {
  302. display: flex;
  303. justify-content: space-around;
  304. border-top: 1rpx solid rgba(255,255,255,0.2);
  305. padding-top: 20rpx;
  306. }
  307. .balance-item {
  308. text-align: center;
  309. }
  310. .balance-item-label {
  311. font-size: 22rpx;
  312. opacity: 0.7;
  313. display: block;
  314. margin-bottom: 6rpx;
  315. }
  316. .balance-item-value {
  317. font-size: 28rpx;
  318. font-weight: 600;
  319. }
  320. .action-row {
  321. display: flex;
  322. margin: 0 30rpx 20rpx;
  323. gap: 20rpx;
  324. }
  325. .action-btn {
  326. flex: 1;
  327. display: flex;
  328. flex-direction: column;
  329. align-items: center;
  330. padding: 24rpx;
  331. background: #fff;
  332. border-radius: 16rpx;
  333. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  334. }
  335. .action-icon {
  336. font-size: 36rpx;
  337. margin-bottom: 8rpx;
  338. }
  339. .action-text {
  340. font-size: 26rpx;
  341. color: #333;
  342. }
  343. .section {
  344. margin: 20rpx 30rpx;
  345. }
  346. .section-header {
  347. display: flex;
  348. justify-content: space-between;
  349. align-items: center;
  350. margin-bottom: 16rpx;
  351. }
  352. .section-title {
  353. font-size: 30rpx;
  354. font-weight: 600;
  355. color: #333;
  356. }
  357. .section-more {
  358. font-size: 24rpx;
  359. color: #F97316;
  360. }
  361. .log-list {
  362. background: #fff;
  363. border-radius: 16rpx;
  364. overflow: hidden;
  365. }
  366. .log-item {
  367. display: flex;
  368. justify-content: space-between;
  369. align-items: center;
  370. padding: 24rpx 30rpx;
  371. border-bottom: 1rpx solid #f0f0f0;
  372. }
  373. .log-item:last-child {
  374. border-bottom: none;
  375. }
  376. .log-left {
  377. flex: 1;
  378. margin-right: 20rpx;
  379. }
  380. .log-type {
  381. font-size: 26rpx;
  382. font-weight: 600;
  383. color: #333;
  384. display: block;
  385. margin-bottom: 4rpx;
  386. }
  387. .log-remark {
  388. font-size: 22rpx;
  389. color: #999;
  390. }
  391. .log-right {
  392. text-align: right;
  393. }
  394. .log-amount {
  395. font-size: 28rpx;
  396. font-weight: 600;
  397. display: block;
  398. }
  399. .log-plus { color: #52c41a; }
  400. .log-minus { color: #ff4d4f; }
  401. .log-time {
  402. font-size: 20rpx;
  403. color: #bbb;
  404. margin-top: 4rpx;
  405. display: block;
  406. }
  407. .empty-logs {
  408. text-align: center;
  409. padding: 60rpx;
  410. color: #999;
  411. font-size: 26rpx;
  412. background: #fff;
  413. border-radius: 16rpx;
  414. }
  415. .exchange-modal {
  416. background: #fff;
  417. border-radius: 24rpx 24rpx 0 0;
  418. max-height: 80vh;
  419. display: flex;
  420. flex-direction: column;
  421. }
  422. .modal-header {
  423. display: flex;
  424. justify-content: space-between;
  425. align-items: center;
  426. padding: 30rpx;
  427. border-bottom: 1rpx solid #f0f0f0;
  428. }
  429. .modal-title {
  430. font-size: 32rpx;
  431. font-weight: 600;
  432. color: #333;
  433. }
  434. .modal-close {
  435. font-size: 40rpx;
  436. color: #999;
  437. line-height: 1;
  438. }
  439. .modal-body {
  440. flex: 1;
  441. overflow-y: auto;
  442. padding: 20rpx 30rpx;
  443. }
  444. .coupon-list {
  445. display: flex;
  446. flex-direction: column;
  447. gap: 20rpx;
  448. }
  449. .coupon-item {
  450. display: flex;
  451. justify-content: space-between;
  452. align-items: center;
  453. padding: 24rpx;
  454. background: #f8f9ff;
  455. border-radius: 16rpx;
  456. border: 1rpx solid #e8eaff;
  457. }
  458. .coupon-info {
  459. flex: 1;
  460. margin-right: 20rpx;
  461. }
  462. .coupon-name {
  463. font-size: 28rpx;
  464. font-weight: 600;
  465. color: #333;
  466. display: block;
  467. }
  468. .coupon-desc {
  469. font-size: 22rpx;
  470. color: #999;
  471. margin-top: 4rpx;
  472. display: block;
  473. }
  474. .coupon-price {
  475. font-size: 26rpx;
  476. color: #F97316;
  477. font-weight: 600;
  478. white-space: nowrap;
  479. }
  480. .empty-coupons {
  481. text-align: center;
  482. padding: 60rpx;
  483. color: #999;
  484. font-size: 26rpx;
  485. }
  486. </style>