index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <template>
  2. <view class="page-commission">
  3. <!-- Loading -->
  4. <view v-if="loading" class="loading-state">
  5. <text class="loading-text">加载中...</text>
  6. </view>
  7. <template v-else>
  8. <!-- Error / permission gate (US-6.5 §2-3) -->
  9. <view v-if="error" class="error-state">
  10. <text class="error-icon">🔒</text>
  11. <text class="error-text">{{ error }}</text>
  12. </view>
  13. <template v-else>
  14. <!-- ===== 我的收益 ===== -->
  15. <view class="section earnings-section">
  16. <view class="section-header">
  17. <text class="section-title">💰 我的收益</text>
  18. </view>
  19. <view class="earnings-grid">
  20. <view class="earn-item">
  21. <text class="earn-label">累计收益</text>
  22. <text class="earn-value accent">¥{{ formatMoney(stats.totalEarnings) }}</text>
  23. </view>
  24. <view class="earn-item">
  25. <text class="earn-label">可提现余额</text>
  26. <text class="earn-value">¥{{ formatMoney(stats.availableBalance) }}</text>
  27. </view>
  28. <view class="earn-item">
  29. <text class="earn-label">本月收益</text>
  30. <text class="earn-value">¥{{ formatMoney(stats.monthEarnings) }}</text>
  31. </view>
  32. <view class="earn-item">
  33. <text class="earn-label">今日预估</text>
  34. <text class="earn-value">¥{{ formatMoney(stats.todayEstimate) }}</text>
  35. </view>
  36. </view>
  37. <view class="withdraw-bar" v-if="stats.availableBalance >= 10000" @click="goWithdraw">
  38. <text class="withdraw-text">立即提现</text>
  39. <text class="withdraw-arrow">›</text>
  40. </view>
  41. </view>
  42. <!-- ===== 我的团队 ===== -->
  43. <view class="section team-section">
  44. <view class="section-header">
  45. <text class="section-title">👥 我的团队</text>
  46. <text v-if="team.length > 0" class="section-sub" @click="goTeamList">查看全部 →</text>
  47. </view>
  48. <view class="team-grid">
  49. <view class="team-stat">
  50. <text class="stat-num">{{ stats.directInvites }}</text>
  51. <text class="stat-label">直接下级</text>
  52. </view>
  53. <view class="team-stat">
  54. <text class="stat-num">{{ stats.indirectInvites }}</text>
  55. <text class="stat-label">间接下级</text>
  56. </view>
  57. <view class="team-stat">
  58. <text class="stat-num">{{ stats.convertedCount }}</text>
  59. <text class="stat-label">下级升级</text>
  60. </view>
  61. <view class="team-stat">
  62. <text class="stat-num">{{ stats.totalCommissions }}</text>
  63. <text class="stat-label">佣金笔数</text>
  64. </view>
  65. </view>
  66. <!-- Recent team members preview -->
  67. <view v-if="team.length > 0" class="team-preview">
  68. <view v-for="(m, idx) in team.slice(0, 5)" :key="idx" class="member-row">
  69. <image class="member-avatar" :src="m.avatarUrl || '/static/default-avatar.png'" mode="aspectFill" />
  70. <view class="member-info">
  71. <text class="member-name">{{ m.nickname || '微信用户' }}</text>
  72. <text class="member-date">{{ formatDate(m.createdAt) }} 加入</text>
  73. </view>
  74. <text v-if="m.isVip" class="member-vip">VIP</text>
  75. </view>
  76. </view>
  77. <view v-else class="empty-state">
  78. <text class="empty-text">暂无推广成员,快去分享推广码吧</text>
  79. </view>
  80. </view>
  81. <!-- ===== 推广工具 ===== -->
  82. <view class="section">
  83. <view class="section-header">
  84. <text class="section-title">📤 推广工具</text>
  85. </view>
  86. <view class="tool-card" @click="showQrActions">
  87. <view class="tool-left">
  88. <text class="tool-icon">▣</text>
  89. <view>
  90. <text class="tool-name">我的推广二维码</text>
  91. <text class="tool-desc">点击保存/分享</text>
  92. </view>
  93. </view>
  94. <view class="tool-right">
  95. <text class="ref-code-tag">{{ userStore.referralCode }}</text>
  96. <text class="tool-arrow">›</text>
  97. </view>
  98. </view>
  99. <view class="tool-card" @click="copyReferral">
  100. <view class="tool-left">
  101. <text class="tool-icon">📋</text>
  102. <view>
  103. <text class="tool-name">复制推广码</text>
  104. <text class="tool-desc">{{ userStore.referralCode || '加载中' }}</text>
  105. </view>
  106. </view>
  107. <text class="tool-arrow">›</text>
  108. </view>
  109. <view class="tool-card" @click="copyPromoLink">
  110. <view class="tool-left">
  111. <text class="tool-icon">🔗</text>
  112. <view>
  113. <text class="tool-name">复制推广链接</text>
  114. <text class="tool-desc">分享链接,对方点击自动绑定</text>
  115. </view>
  116. </view>
  117. <text class="tool-arrow">›</text>
  118. </view>
  119. </view>
  120. <!-- ===== 佣金明细 ===== -->
  121. <view class="section">
  122. <view class="section-header">
  123. <text class="section-title">📋 佣金明细</text>
  124. </view>
  125. <view v-if="commissions.length === 0" class="empty-state">
  126. <text class="empty-text">暂无佣金记录</text>
  127. </view>
  128. <view v-for="(c, idx) in commissions" :key="idx" class="commission-row">
  129. <view class="comm-left">
  130. <text class="comm-amount">+¥{{ (c.amount / 100).toFixed(2) }}</text>
  131. <text class="comm-level">{{ c.level === 1 ? '一级推广' : '二级推广' }}</text>
  132. <text v-if="c.remark" class="comm-remark">{{ c.remark }}</text>
  133. </view>
  134. <view class="comm-right">
  135. <text class="comm-status" :class="'status-' + c.status">
  136. {{ statusText(c.status) }}
  137. </text>
  138. <text class="comm-date">{{ formatDate(c.createdAt) }}</text>
  139. </view>
  140. </view>
  141. </view>
  142. <!-- QR code modal (hidden by default, shown via action sheet) -->
  143. </template>
  144. </template>
  145. </view>
  146. </template>
  147. <script setup>
  148. import { ref, onMounted } from 'vue'
  149. import { onPullDownRefresh } from '@dcloudio/uni-app'
  150. import { useUserStore } from '@/stores/user'
  151. import { commissionApi } from '@/utils/api'
  152. const userStore = useUserStore()
  153. const commissions = ref([])
  154. const team = ref([])
  155. const loading = ref(true)
  156. const error = ref('')
  157. const stats = ref({
  158. totalEarnings: 0,
  159. availableBalance: 0,
  160. todayEstimate: 0,
  161. monthEarnings: 0,
  162. directInvites: 0,
  163. indirectInvites: 0,
  164. convertedCount: 0,
  165. totalCommissions: 0
  166. })
  167. // US-6.5 §2-3: Only paid users can access
  168. onMounted(async () => {
  169. if (!userStore.isVipActive) {
  170. loading.value = false
  171. error.value = '开通会员后可查看推广收益'
  172. return
  173. }
  174. await fetchData()
  175. })
  176. async function fetchData() {
  177. error.value = ''
  178. loading.value = true
  179. try {
  180. const [listData, teamData, statsData] = await Promise.all([
  181. commissionApi.list(),
  182. commissionApi.team(),
  183. commissionApi.stats()
  184. ])
  185. commissions.value = listData || []
  186. team.value = teamData || []
  187. if (statsData) {
  188. stats.value = statsData
  189. }
  190. } catch (e) {
  191. error.value = '网络错误,请下拉刷新重试'
  192. } finally {
  193. loading.value = false
  194. }
  195. }
  196. // Pull-to-refresh (US-6.5 §24)
  197. onPullDownRefresh(async () => {
  198. if (!userStore.isVipActive) return
  199. await fetchData()
  200. uni.stopPullDownRefresh()
  201. })
  202. function formatMoney(cents) {
  203. if (cents == null) return '0.00'
  204. return (cents / 100).toFixed(2)
  205. }
  206. function showTeamDetail() {
  207. uni.showModal({
  208. title: '推广团队',
  209. content: `直接下级 ${stats.value.directInvites} 人\n间接下级 ${stats.value.indirectInvites} 人\n已升级为VIP ${stats.value.convertedCount} 人`,
  210. showCancel: false
  211. })
  212. }
  213. function goWithdraw() {
  214. uni.navigateTo({ url: '/pages/withdraw/index' })
  215. }
  216. function goTeamList() {
  217. uni.showActionSheet({
  218. itemList: ['查看全部成员', '收起'],
  219. success: (res) => {
  220. if (res.tapIndex === 0) {
  221. // Scroll to team section by showing a modal with full list
  222. if (team.value.length > 10) {
  223. uni.showModal({
  224. title: '推广团队',
  225. content: `直接下级 ${stats.value.directInvites} 人\n间接下级 ${stats.value.indirectInvites} 人\n已升级VIP ${stats.value.convertedCount} 人\n\n共 ${team.value.length} 名成员`,
  226. showCancel: false
  227. })
  228. }
  229. }
  230. }
  231. })
  232. }
  233. function copyPromoLink() {
  234. if (!userStore.referralCode) return
  235. const link = `pages/index/index?ref=${userStore.referralCode}`
  236. uni.setClipboardData({
  237. data: link,
  238. success: () => uni.showToast({ title: '已复制推广链接', icon: 'success' })
  239. })
  240. }
  241. function showQrActions() {
  242. if (!userStore.referralCode) return
  243. uni.showActionSheet({
  244. itemList: ['复制推广码', '保存二维码到相册'],
  245. success: (res) => {
  246. if (res.tapIndex === 0) {
  247. copyReferral()
  248. } else if (res.tapIndex === 1) {
  249. saveQrToAlbum()
  250. }
  251. }
  252. })
  253. }
  254. function copyReferral() {
  255. if (userStore.referralCode) {
  256. uni.setClipboardData({
  257. data: userStore.referralCode,
  258. success: () => uni.showToast({ title: '已复制推广码', icon: 'success' })
  259. })
  260. }
  261. }
  262. function saveQrToAlbum() {
  263. const url = `https://api.qrserver.com/v1/create-qr-code/?size=560x560&data=${userStore.referralCode}`
  264. uni.showLoading({ title: '保存中...' })
  265. uni.downloadFile({
  266. url,
  267. success: (res) => {
  268. if (res.statusCode === 200) {
  269. uni.saveImageToPhotosAlbum({
  270. filePath: res.tempFilePath,
  271. success: () => {
  272. uni.hideLoading()
  273. uni.showToast({ title: '已保存到相册', icon: 'success' })
  274. },
  275. fail: () => {
  276. uni.hideLoading()
  277. uni.showToast({ title: '保存失败,请检查相册权限', icon: 'none' })
  278. }
  279. })
  280. } else {
  281. uni.hideLoading()
  282. uni.showToast({ title: '二维码生成失败', icon: 'none' })
  283. }
  284. },
  285. fail: () => {
  286. uni.hideLoading()
  287. uni.showToast({ title: '网络错误', icon: 'none' })
  288. }
  289. })
  290. }
  291. function statusText(status) {
  292. const map = { settled: '已结算', pending: '处理中', withdrawn: '已提现', available: '可提现' }
  293. return map[status] || status
  294. }
  295. function formatDate(dateStr) {
  296. if (!dateStr) return ''
  297. const d = new Date(dateStr)
  298. return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
  299. }
  300. </script>
  301. <style scoped lang="scss">
  302. .page-commission {
  303. min-height: 100vh;
  304. background: #F8F6F1;
  305. padding: 16px;
  306. }
  307. /* Error / permission gate */
  308. .error-state {
  309. padding: 80px 20px;
  310. text-align: center;
  311. }
  312. .error-icon {
  313. font-size: 40px;
  314. display: block;
  315. margin-bottom: 12px;
  316. }
  317. .error-text {
  318. font-size: 14px;
  319. color: #A0AEC0;
  320. }
  321. /* Loading */
  322. .loading-state {
  323. padding: 60px 0;
  324. text-align: center;
  325. }
  326. .loading-text {
  327. font-size: 14px;
  328. color: #A0AEC0;
  329. }
  330. /* Section */
  331. .section {
  332. background: #fff;
  333. border-radius: 14px;
  334. padding: 16px;
  335. margin-bottom: 16px;
  336. }
  337. .section-header {
  338. display: flex;
  339. justify-content: space-between;
  340. align-items: center;
  341. margin-bottom: 14px;
  342. }
  343. .section-title {
  344. font-size: 16px;
  345. font-weight: 700;
  346. color: #1A1A2E;
  347. }
  348. .section-sub {
  349. font-size: 12px;
  350. color: #3B82F6;
  351. font-weight: 500;
  352. }
  353. /* ===== Earnings ===== */
  354. .earnings-section {
  355. background: linear-gradient(135deg, #1E3A5F, #2D4A7A);
  356. }
  357. .earnings-section .section-title {
  358. color: rgba(255,255,255,0.9);
  359. }
  360. .earnings-grid {
  361. display: grid;
  362. grid-template-columns: 1fr 1fr;
  363. gap: 12px;
  364. }
  365. .earn-item {
  366. text-align: center;
  367. padding: 8px 0;
  368. }
  369. .earn-label {
  370. font-size: 11px;
  371. color: rgba(255,255,255,0.55);
  372. display: block;
  373. margin-bottom: 4px;
  374. }
  375. .earn-value {
  376. font-size: 22px;
  377. font-weight: 700;
  378. color: #fff;
  379. display: block;
  380. }
  381. .earn-value.accent {
  382. color: #C9A84C;
  383. }
  384. .withdraw-bar {
  385. margin-top: 14px;
  386. padding: 10px 0;
  387. background: rgba(255,255,255,0.06);
  388. border: 1px solid rgba(251,191,36,0.15);
  389. border-radius: 10px;
  390. display: flex;
  391. align-items: center;
  392. justify-content: center;
  393. gap: 6px;
  394. }
  395. .withdraw-text {
  396. font-size: 14px;
  397. font-weight: 600;
  398. color: #C9A84C;
  399. }
  400. .withdraw-arrow {
  401. font-size: 16px;
  402. color: #C9A84C;
  403. }
  404. /* ===== Team ===== */
  405. .team-grid {
  406. display: grid;
  407. grid-template-columns: 1fr 1fr 1fr 1fr;
  408. gap: 8px;
  409. margin-bottom: 14px;
  410. }
  411. .team-stat {
  412. text-align: center;
  413. padding: 8px 0;
  414. }
  415. .stat-num {
  416. font-size: 24px;
  417. font-weight: 700;
  418. color: #1E3A5F;
  419. display: block;
  420. }
  421. .stat-label {
  422. font-size: 11px;
  423. color: #A0AEC0;
  424. margin-top: 2px;
  425. display: block;
  426. }
  427. .team-preview {
  428. border-top: 1px solid #F3F4F6;
  429. padding-top: 10px;
  430. }
  431. .member-row {
  432. display: flex;
  433. align-items: center;
  434. padding: 8px 0;
  435. border-bottom: 1px solid #F8F6F1;
  436. }
  437. .member-row:last-child { border-bottom: none; }
  438. .member-avatar {
  439. width: 32px;
  440. height: 32px;
  441. border-radius: 50%;
  442. margin-right: 10px;
  443. flex-shrink: 0;
  444. }
  445. .member-info { flex: 1; }
  446. .member-name {
  447. font-size: 13px;
  448. color: #1A1A2E;
  449. display: block;
  450. }
  451. .member-date {
  452. font-size: 11px;
  453. color: #A0AEC0;
  454. margin-top: 1px;
  455. display: block;
  456. }
  457. .member-vip {
  458. font-size: 10px;
  459. color: #C9A84C;
  460. background: #FEF3C7;
  461. padding: 2px 8px;
  462. border-radius: 4px;
  463. font-weight: 500;
  464. }
  465. /* ===== Tools ===== */
  466. .tool-card {
  467. display: flex;
  468. justify-content: space-between;
  469. align-items: center;
  470. padding: 14px 0;
  471. border-bottom: 1px solid #F3F4F6;
  472. }
  473. .tool-card:last-child { border-bottom: none; }
  474. .tool-left {
  475. display: flex;
  476. align-items: center;
  477. gap: 12px;
  478. flex: 1;
  479. }
  480. .tool-icon {
  481. width: 36px;
  482. height: 36px;
  483. border-radius: 8px;
  484. background: #F8F6F1;
  485. display: flex;
  486. align-items: center;
  487. justify-content: center;
  488. font-size: 16px;
  489. color: #1E3A5F;
  490. flex-shrink: 0;
  491. }
  492. .tool-name {
  493. font-size: 14px;
  494. font-weight: 600;
  495. color: #1A1A2E;
  496. display: block;
  497. }
  498. .tool-desc {
  499. font-size: 11px;
  500. color: #A0AEC0;
  501. margin-top: 2px;
  502. display: block;
  503. }
  504. .tool-right {
  505. display: flex;
  506. align-items: center;
  507. gap: 8px;
  508. }
  509. .ref-code-tag {
  510. font-size: 12px;
  511. font-weight: 600;
  512. color: #C9A84C;
  513. background: #FEF3C7;
  514. padding: 2px 10px;
  515. border-radius: 4px;
  516. letter-spacing: 1px;
  517. }
  518. .tool-arrow {
  519. font-size: 18px;
  520. color: #CBD5E0;
  521. }
  522. /* ===== Commission List ===== */
  523. .commission-row {
  524. display: flex;
  525. justify-content: space-between;
  526. align-items: center;
  527. padding: 12px 0;
  528. border-bottom: 1px solid #F3F4F6;
  529. }
  530. .commission-row:last-child { border-bottom: none; }
  531. .comm-left { flex: 1; }
  532. .comm-amount {
  533. font-size: 15px;
  534. font-weight: 600;
  535. color: #2D8B67;
  536. display: block;
  537. }
  538. .comm-level {
  539. font-size: 12px;
  540. color: #A0AEC0;
  541. margin-top: 2px;
  542. display: block;
  543. }
  544. .comm-remark {
  545. font-size: 11px;
  546. color: #8B9DAB;
  547. margin-top: 1px;
  548. display: block;
  549. }
  550. .comm-right { text-align: right; }
  551. .comm-status {
  552. font-size: 11px;
  553. padding: 2px 8px;
  554. border-radius: 4px;
  555. display: inline-block;
  556. margin-bottom: 2px;
  557. }
  558. .comm-status.status-settled { background: #ECFDF5; color: #2D8B67; }
  559. .comm-status.status-pending { background: #FEF3C7; color: #D4A017; }
  560. .comm-status.status-withdrawn { background: #F3F4F6; color: #A0AEC0; }
  561. .comm-status.status-available { background: #EFF6FF; color: #3B82F6; }
  562. .comm-date {
  563. font-size: 11px;
  564. color: #A0AEC0;
  565. display: block;
  566. }
  567. /* Empty State */
  568. .empty-state {
  569. padding: 24px 0;
  570. text-align: center;
  571. }
  572. .empty-text {
  573. font-size: 13px;
  574. color: #A0AEC0;
  575. }
  576. </style>