index.vue 12 KB

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