index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. },
  112. onShow() {
  113. if (this.isLoggedIn) this.loadBalance()
  114. },
  115. methods: {
  116. checkLogin() {
  117. const token = uni.getStorageSync('token')
  118. this.isLoggedIn = !!token
  119. },
  120. goLogin() {
  121. uni.navigateTo({ url: '/pages/login/login' })
  122. },
  123. async loadBalance() {
  124. try {
  125. const res = await getFamilyPlatformBalance()
  126. if (res.code === 200 && res.data) {
  127. this.balance = res.data
  128. }
  129. } catch (e) {
  130. console.error('加载余额失败', e)
  131. }
  132. },
  133. async loadLogs() {
  134. try {
  135. const res = await getFamilyPlatformLogs({ page: this.page, size: this.size })
  136. if (res.code === 200 && res.data) {
  137. if (this.page === 1) {
  138. this.logs = res.data.records || []
  139. } else {
  140. this.logs = this.logs.concat(res.data.records || [])
  141. }
  142. this.hasMore = this.logs.length < (res.data.total || 0)
  143. }
  144. } catch (e) {
  145. console.error('加载流水失败', e)
  146. }
  147. },
  148. loadMoreLogs() {
  149. if (this.hasMore) {
  150. this.page++
  151. this.loadLogs()
  152. }
  153. },
  154. async loadExchangeRecords() {
  155. try {
  156. const res = await getFamilyPlatformLogs({ page: 1, size: 50, type: 'coupon_exchange' })
  157. if (res.code === 200) {
  158. this.exchangeRecords = res.data?.records || []
  159. }
  160. } catch (e) { console.error(e) }
  161. },
  162. refreshData() {
  163. this.page = 1
  164. this.loadBalance()
  165. this.loadLogs()
  166. },
  167. async openExchangeModal() {
  168. this.showExchangeModal = true
  169. try {
  170. const res = await getExchangeableCoupons()
  171. if (res.code === 200) {
  172. this.exchangeableCoupons = res.data || []
  173. }
  174. } catch (e) {
  175. console.error('加载可兑换优惠券失败', e)
  176. }
  177. },
  178. async doExchange(coupon) {
  179. if (coupon.pointsPrice > this.balance.available) {
  180. uni.showToast({ title: 'CF值不足', icon: 'none' })
  181. return
  182. }
  183. uni.showLoading({ title: '兑换中...' })
  184. try {
  185. const res = await exchangeCouponByCf({ couponId: coupon.id })
  186. uni.hideLoading()
  187. if (res.code === 200) {
  188. uni.showToast({ title: '兑换成功', icon: 'success' })
  189. this.showExchangeModal = false
  190. this.loadBalance()
  191. } else {
  192. uni.showToast({ title: res.message || '兑换失败', icon: 'none' })
  193. }
  194. } catch (e) {
  195. uni.hideLoading()
  196. uni.showToast({ title: '兑换失败', icon: 'none' })
  197. }
  198. },
  199. logTypeLabel(type) {
  200. const map = {
  201. earn: '获得', spend: '消费', freeze: '冻结',
  202. unfreeze: '解冻', withdraw: '提现', adjust: '调整', migration: '迁移'
  203. }
  204. return map[type] || type
  205. },
  206. couponDesc(coupon) {
  207. if (coupon.type === 'DISCOUNT') {
  208. return (coupon.discountRate / 100).toFixed(1) + '折'
  209. }
  210. return '立减' + (coupon.value / 100).toFixed(2) + '元'
  211. },
  212. formatTime(dateStr) {
  213. if (!dateStr) return ''
  214. const d = new Date(dateStr)
  215. const m = d.getMonth() + 1
  216. const day = d.getDate()
  217. const h = d.getHours()
  218. const min = String(d.getMinutes()).padStart(2, '0')
  219. return m + '/' + day + ' ' + h + ':' + min
  220. }
  221. }
  222. }
  223. </script>
  224. <style scoped>
  225. .container {
  226. min-height: 100vh;
  227. background: #f5f6fa;
  228. padding-bottom: 40rpx;
  229. }
  230. .empty-state {
  231. display: flex;
  232. flex-direction: column;
  233. align-items: center;
  234. justify-content: center;
  235. padding: 200rpx 40rpx;
  236. }
  237. .empty-icon {
  238. font-size: 100rpx;
  239. margin-bottom: 20rpx;
  240. }
  241. .empty-title {
  242. font-size: 32rpx;
  243. color: #333;
  244. font-weight: 600;
  245. margin-bottom: 12rpx;
  246. }
  247. .empty-desc {
  248. font-size: 26rpx;
  249. color: #999;
  250. margin-bottom: 40rpx;
  251. text-align: center;
  252. }
  253. .login-btn {
  254. width: 280rpx;
  255. height: 80rpx;
  256. line-height: 80rpx;
  257. background: linear-gradient(135deg, #667eea, #764ba2);
  258. color: #fff;
  259. border-radius: 40rpx;
  260. font-size: 28rpx;
  261. border: none;
  262. }
  263. .balance-card {
  264. margin: 30rpx;
  265. padding: 40rpx 30rpx;
  266. background: linear-gradient(135deg, #667eea, #764ba2);
  267. border-radius: 24rpx;
  268. color: #fff;
  269. }
  270. .balance-label {
  271. font-size: 26rpx;
  272. opacity: 0.8;
  273. margin-bottom: 10rpx;
  274. }
  275. .balance-value {
  276. font-size: 72rpx;
  277. font-weight: 700;
  278. line-height: 1;
  279. margin-bottom: 8rpx;
  280. }
  281. .balance-unit {
  282. font-size: 22rpx;
  283. opacity: 0.7;
  284. margin-bottom: 30rpx;
  285. }
  286. .balance-row {
  287. display: flex;
  288. justify-content: space-around;
  289. border-top: 1rpx solid rgba(255,255,255,0.2);
  290. padding-top: 20rpx;
  291. }
  292. .balance-item {
  293. text-align: center;
  294. }
  295. .balance-item-label {
  296. font-size: 22rpx;
  297. opacity: 0.7;
  298. display: block;
  299. margin-bottom: 6rpx;
  300. }
  301. .balance-item-value {
  302. font-size: 28rpx;
  303. font-weight: 600;
  304. }
  305. .action-row {
  306. display: flex;
  307. margin: 0 30rpx 20rpx;
  308. gap: 20rpx;
  309. }
  310. .action-btn {
  311. flex: 1;
  312. display: flex;
  313. flex-direction: column;
  314. align-items: center;
  315. padding: 24rpx;
  316. background: #fff;
  317. border-radius: 16rpx;
  318. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  319. }
  320. .action-icon {
  321. font-size: 36rpx;
  322. margin-bottom: 8rpx;
  323. }
  324. .action-text {
  325. font-size: 26rpx;
  326. color: #333;
  327. }
  328. .section {
  329. margin: 20rpx 30rpx;
  330. }
  331. .section-header {
  332. display: flex;
  333. justify-content: space-between;
  334. align-items: center;
  335. margin-bottom: 16rpx;
  336. }
  337. .section-title {
  338. font-size: 30rpx;
  339. font-weight: 600;
  340. color: #333;
  341. }
  342. .section-more {
  343. font-size: 24rpx;
  344. color: #667eea;
  345. }
  346. .log-list {
  347. background: #fff;
  348. border-radius: 16rpx;
  349. overflow: hidden;
  350. }
  351. .log-item {
  352. display: flex;
  353. justify-content: space-between;
  354. align-items: center;
  355. padding: 24rpx 30rpx;
  356. border-bottom: 1rpx solid #f0f0f0;
  357. }
  358. .log-item:last-child {
  359. border-bottom: none;
  360. }
  361. .log-left {
  362. flex: 1;
  363. margin-right: 20rpx;
  364. }
  365. .log-type {
  366. font-size: 26rpx;
  367. font-weight: 600;
  368. color: #333;
  369. display: block;
  370. margin-bottom: 4rpx;
  371. }
  372. .log-remark {
  373. font-size: 22rpx;
  374. color: #999;
  375. }
  376. .log-right {
  377. text-align: right;
  378. }
  379. .log-amount {
  380. font-size: 28rpx;
  381. font-weight: 600;
  382. display: block;
  383. }
  384. .log-plus { color: #52c41a; }
  385. .log-minus { color: #ff4d4f; }
  386. .log-time {
  387. font-size: 20rpx;
  388. color: #bbb;
  389. margin-top: 4rpx;
  390. display: block;
  391. }
  392. .empty-logs {
  393. text-align: center;
  394. padding: 60rpx;
  395. color: #999;
  396. font-size: 26rpx;
  397. background: #fff;
  398. border-radius: 16rpx;
  399. }
  400. .exchange-modal {
  401. background: #fff;
  402. border-radius: 24rpx 24rpx 0 0;
  403. max-height: 80vh;
  404. display: flex;
  405. flex-direction: column;
  406. }
  407. .modal-header {
  408. display: flex;
  409. justify-content: space-between;
  410. align-items: center;
  411. padding: 30rpx;
  412. border-bottom: 1rpx solid #f0f0f0;
  413. }
  414. .modal-title {
  415. font-size: 32rpx;
  416. font-weight: 600;
  417. color: #333;
  418. }
  419. .modal-close {
  420. font-size: 40rpx;
  421. color: #999;
  422. line-height: 1;
  423. }
  424. .modal-body {
  425. flex: 1;
  426. overflow-y: auto;
  427. padding: 20rpx 30rpx;
  428. }
  429. .coupon-list {
  430. display: flex;
  431. flex-direction: column;
  432. gap: 20rpx;
  433. }
  434. .coupon-item {
  435. display: flex;
  436. justify-content: space-between;
  437. align-items: center;
  438. padding: 24rpx;
  439. background: #f8f9ff;
  440. border-radius: 16rpx;
  441. border: 1rpx solid #e8eaff;
  442. }
  443. .coupon-info {
  444. flex: 1;
  445. margin-right: 20rpx;
  446. }
  447. .coupon-name {
  448. font-size: 28rpx;
  449. font-weight: 600;
  450. color: #333;
  451. display: block;
  452. }
  453. .coupon-desc {
  454. font-size: 22rpx;
  455. color: #999;
  456. margin-top: 4rpx;
  457. display: block;
  458. }
  459. .coupon-price {
  460. font-size: 26rpx;
  461. color: #667eea;
  462. font-weight: 600;
  463. white-space: nowrap;
  464. }
  465. .empty-coupons {
  466. text-align: center;
  467. padding: 60rpx;
  468. color: #999;
  469. font-size: 26rpx;
  470. }
  471. </style>