invite.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <template>
  2. <view class="container">
  3. <!-- 邀请统计 -->
  4. <view class="stats-card">
  5. <view class="stats-item">
  6. <text class="stats-num" v-if="summary">{{ summary.totalInvited || 0 }}</text>
  7. <text class="stats-num" v-else>0</text>
  8. <text class="stats-label">已邀请</text>
  9. </view>
  10. <view class="stats-divider"></view>
  11. <view class="stats-item">
  12. <text class="stats-num" v-if="summary">{{ summary.activeCount || 0 }}</text>
  13. <text class="stats-num" v-else>0</text>
  14. <text class="stats-label">活跃</text>
  15. </view>
  16. <view class="stats-divider"></view>
  17. <view class="stats-item">
  18. <text class="stats-num" v-if="summary">{{ formatPriceWithSymbol(summary.totalEarnings) }}</text>
  19. <text class="stats-num" v-else>{{ formatPriceWithSymbol(0) }}</text>
  20. <text class="stats-label">总收益</text>
  21. </view>
  22. </view>
  23. <!-- 邀请里程碑 -->
  24. <view class="milestone-card" v-if="milestones.length > 0">
  25. <text class="milestone-title">邀请里程碑</text>
  26. <view class="milestone-item" v-for="item in milestones" :key="item.id">
  27. <view class="milestone-header">
  28. <text class="milestone-name">{{ milestoneName(item.milestone) }}</text>
  29. <text class="milestone-reward">+{{ item.rewardPoints || 0 }}积分</text>
  30. </view>
  31. <view class="milestone-bar-wrap">
  32. <view class="milestone-bar-bg">
  33. <view class="milestone-bar-fill" :style="'width:' + milestonePercent(item) + '%'"></view>
  34. </view>
  35. <text class="milestone-count">{{ item.currentCount || 0 }}/{{ item.targetCount || 0 }}</text>
  36. </view>
  37. <view class="milestone-action" v-if="item.status === 'PENDING'">
  38. <button class="claim-btn" @click="claimReward(item)">领取</button>
  39. </view>
  40. <view class="milestone-action" v-if="item.status === 'CLAIMED'">
  41. <text class="claimed-badge">✓ 已领取</text>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- 邀请码展示 -->
  46. <view class="code-card">
  47. <text class="code-title">我的邀请码</text>
  48. <text class="code-value">{{ referralCode || '加载中...' }}</text>
  49. <view class="code-actions">
  50. <button class="action-btn" @click="copyCode">复制邀请码</button>
  51. <button class="action-btn share" @click="shareInvite">分享给好友</button>
  52. </view>
  53. </view>
  54. <!-- 绑定推荐人 -->
  55. <view class="bind-card">
  56. <text class="bind-title">我有推荐人</text>
  57. <view class="bind-row">
  58. <input v-model="bindCode" type="text" placeholder="输入推荐人的邀请码" class="bind-input" />
  59. <button class="bind-btn" @click="bindReferral">绑定</button>
  60. </view>
  61. </view>
  62. <!-- 已邀请列表 -->
  63. <view class="list-section">
  64. <text class="section-title">已邀请好友 ({{ total || 0 }})</text>
  65. <view class="empty-state" v-if="!loading && list.length === 0">
  66. <text>还没有邀请好友,快去分享吧</text>
  67. </view>
  68. <view class="invite-item" v-for="item in list" :key="item.id">
  69. <view class="invite-avatar-wrap">
  70. <text class="invite-avatar">{{ item.avatar ? '' : '👤' }}</text>
  71. </view>
  72. <view class="invite-info">
  73. <text class="invite-name">{{ item.nickname || '好友' }}</text>
  74. <text class="invite-time">{{ formatTime(item.createdAt) }}</text>
  75. </view>
  76. </view>
  77. <view class="loading-state" v-if="loading">
  78. <text>加载中...</text>
  79. </view>
  80. </view>
  81. <SharePoster :show="showPoster" :referralCode="referralCode" :qrCodeBase64="qrCodeBase64" :nickname="userNickname" :userAvatar="userAvatar" @close="showPoster = false" />
  82. </view>
  83. </template>
  84. <script>
  85. import { getReferralCode, bindReferral, getReferralList, getReferralSummary, getInviteQrCode, getInviteMilestones, claimInviteMilestone } from '../../utils/api.js'
  86. import SharePoster from '@/components/SharePoster.vue'
  87. export default {
  88. components: { SharePoster },
  89. data() {
  90. return {
  91. referralCode: '',
  92. bindCode: '',
  93. list: [],
  94. total: 0,
  95. page: 1,
  96. loading: false,
  97. hasMore: true,
  98. summary: null,
  99. showPoster: false,
  100. qrCodeBase64: '',
  101. userAvatar: '',
  102. userNickname: '',
  103. milestones: [],
  104. milestonesLoading: false
  105. }
  106. },
  107. onLoad() {
  108. this.loadCode()
  109. this.loadSummary()
  110. this.loadList()
  111. this.loadMilestones()
  112. },
  113. onReachBottom() {
  114. if (this.hasMore && !this.loading) {
  115. this.page++
  116. this.loadList()
  117. }
  118. },
  119. onShareAppMessage() {
  120. var code = this.referralCode || ''
  121. var path = '/pages/invite/join'
  122. if (code) {
  123. path = path + '?inviteCode=' + code
  124. }
  125. return {
  126. title: '我在浠艾福发现了好用的家庭成长工具,快来加入!',
  127. path: path,
  128. imageUrl: ''
  129. }
  130. },
  131. methods: {
  132. async loadCode() {
  133. try {
  134. const res = await getReferralCode()
  135. if (res.data) {
  136. this.referralCode = res.data.referralCode || res.data.code || ''
  137. }
  138. } catch (e) {
  139. console.error('获取邀请码失败', e)
  140. }
  141. },
  142. async loadSummary() {
  143. try {
  144. const res = await getReferralSummary()
  145. if (res.data) {
  146. this.summary = res.data
  147. }
  148. } catch (e) {
  149. console.error('获取邀请统计失败', e)
  150. }
  151. },
  152. async loadList() {
  153. this.loading = true
  154. try {
  155. const res = await getReferralList(this.page)
  156. if (res.data) {
  157. if (res.data.records) {
  158. this.list = this.page === 1 ? res.data.records : this.list.concat(res.data.records)
  159. }
  160. this.total = res.data.total || 0
  161. if (res.data.records) {
  162. this.hasMore = res.data.records.length >= 10
  163. } else {
  164. this.hasMore = false
  165. }
  166. }
  167. } catch (e) {
  168. console.error('获取邀请列表失败', e)
  169. }
  170. this.loading = false
  171. },
  172. copyCode() {
  173. if (!this.referralCode) return
  174. uni.setClipboardData({
  175. data: this.referralCode,
  176. success: function() {
  177. uni.showToast({ title: '已复制邀请码', icon: 'success' })
  178. }
  179. })
  180. },
  181. async shareInvite() {
  182. try {
  183. uni.showLoading({ title: '生成中...' })
  184. var res = await getInviteQrCode()
  185. if (res.data) {
  186. this.qrCodeBase64 = res.data.qrCodeBase64 || ''
  187. this.userNickname = uni.getStorageSync('nickname') || ''
  188. this.userAvatar = uni.getStorageSync('avatar') || ''
  189. this.showPoster = true
  190. }
  191. } catch (e) {
  192. uni.showToast({ title: '生成失败', icon: 'none' })
  193. } finally {
  194. uni.hideLoading()
  195. }
  196. },
  197. async bindReferral() {
  198. if (!this.bindCode) {
  199. uni.showToast({ title: '请输入邀请码', icon: 'none' })
  200. return
  201. }
  202. if (this.bindCode === this.referralCode) {
  203. uni.showToast({ title: '不能绑定自己的邀请码', icon: 'none' })
  204. return
  205. }
  206. try {
  207. await bindReferral(this.bindCode)
  208. uni.showToast({ title: '绑定成功', icon: 'success' })
  209. this.bindCode = ''
  210. } catch (e) {
  211. uni.showToast({ title: e.message || '绑定失败', icon: 'none' })
  212. }
  213. },
  214. async loadMilestones() {
  215. this.milestonesLoading = true
  216. try {
  217. var res = await getInviteMilestones()
  218. if (res.data) {
  219. this.milestones = Array.isArray(res.data) ? res.data : []
  220. }
  221. } catch (e) {
  222. console.error('获取里程碑失败', e)
  223. }
  224. this.milestonesLoading = false
  225. },
  226. async claimReward(item) {
  227. try {
  228. uni.showLoading({ title: '领取中...' })
  229. await claimInviteMilestone(item.milestone)
  230. uni.showToast({ title: '领取成功', icon: 'success' })
  231. this.loadMilestones()
  232. } catch (e) {
  233. uni.showToast({ title: e.message || '领取失败', icon: 'none' })
  234. } finally {
  235. uni.hideLoading()
  236. }
  237. },
  238. milestoneName(key) {
  239. var names = {
  240. INVITE_3: '邀请3人',
  241. INVITE_5: '邀请5人',
  242. INVITE_10: '邀请10人'
  243. }
  244. return names[key] || key
  245. },
  246. milestonePercent(item) {
  247. if (!item.targetCount) return 0
  248. var pct = Math.round((item.currentCount || 0) / item.targetCount * 100)
  249. return Math.min(pct, 100)
  250. },
  251. formatTime(time) {
  252. if (!time) return ''
  253. var t = new Date(time)
  254. return t.getFullYear() + '-' + String(t.getMonth() + 1).padStart(2, '0') + '-' + String(t.getDate()).padStart(2, '0')
  255. }
  256. }
  257. }
  258. </script>
  259. <style scoped>
  260. .container {
  261. padding: 30rpx;
  262. min-height: 100vh;
  263. background: #FFF7ED;
  264. }
  265. /* ===== 邀请统计 ===== */
  266. .stats-card {
  267. background: #fff;
  268. border-radius: 20rpx;
  269. padding: 32rpx 24rpx;
  270. display: flex;
  271. align-items: center;
  272. margin-bottom: 24rpx;
  273. box-shadow: 0 2rpx 12rpx rgba(249,115,22,0.08);
  274. }
  275. .stats-item {
  276. flex: 1;
  277. text-align: center;
  278. }
  279. .stats-num {
  280. font-size: 36rpx;
  281. font-weight: bold;
  282. color: #F97316;
  283. display: block;
  284. }
  285. .stats-label {
  286. font-size: 22rpx;
  287. color: #94A3B8;
  288. margin-top: 4rpx;
  289. display: block;
  290. }
  291. .stats-divider {
  292. width: 1rpx;
  293. height: 48rpx;
  294. background: #FED7AA;
  295. }
  296. /* ===== 邀请里程碑 ===== */
  297. .milestone-card {
  298. background: #fff;
  299. border-radius: 20rpx;
  300. padding: 30rpx;
  301. margin-bottom: 24rpx;
  302. box-shadow: 0 2rpx 12rpx rgba(249,115,22,0.08);
  303. }
  304. .milestone-title {
  305. font-size: 28rpx;
  306. font-weight: 600;
  307. color: #1E293B;
  308. display: block;
  309. margin-bottom: 24rpx;
  310. }
  311. .milestone-item {
  312. margin-bottom: 24rpx;
  313. }
  314. .milestone-item:last-child {
  315. margin-bottom: 0;
  316. }
  317. .milestone-header {
  318. display: flex;
  319. justify-content: space-between;
  320. align-items: center;
  321. margin-bottom: 12rpx;
  322. }
  323. .milestone-name {
  324. font-size: 26rpx;
  325. color: #1E293B;
  326. font-weight: 500;
  327. }
  328. .milestone-reward {
  329. font-size: 22rpx;
  330. color: #F97316;
  331. }
  332. .milestone-bar-wrap {
  333. display: flex;
  334. align-items: center;
  335. }
  336. .milestone-bar-bg {
  337. flex: 1;
  338. height: 20rpx;
  339. background: #FED7AA;
  340. border-radius: 10rpx;
  341. overflow: hidden;
  342. }
  343. .milestone-bar-fill {
  344. height: 100%;
  345. background: linear-gradient(90deg, #F97316, #EA580C);
  346. border-radius: 10rpx;
  347. }
  348. .milestone-count {
  349. font-size: 22rpx;
  350. color: #94A3B8;
  351. margin-left: 12rpx;
  352. min-width: 50rpx;
  353. text-align: right;
  354. }
  355. .milestone-action {
  356. margin-top: 12rpx;
  357. text-align: right;
  358. }
  359. .claim-btn {
  360. background: #F97316;
  361. color: #fff;
  362. font-size: 24rpx;
  363. border-radius: 30rpx;
  364. height: 52rpx;
  365. line-height: 52rpx;
  366. padding: 0 28rpx;
  367. display: inline-block;
  368. }
  369. .claim-btn::after {
  370. border: none;
  371. }
  372. .claim-btn:active {
  373. opacity: 0.9;
  374. }
  375. .claimed-badge {
  376. font-size: 24rpx;
  377. color: #10B981;
  378. }
  379. .code-card {
  380. background: linear-gradient(135deg, #F97316, #EA580C);
  381. border-radius: 20rpx;
  382. padding: 40rpx;
  383. text-align: center;
  384. color: #fff;
  385. margin-bottom: 24rpx;
  386. }
  387. .code-title {
  388. font-size: 24rpx;
  389. opacity: 0.85;
  390. }
  391. .code-value {
  392. font-size: 48rpx;
  393. font-weight: bold;
  394. letter-spacing: 8rpx;
  395. margin: 20rpx 0 30rpx;
  396. display: block;
  397. }
  398. .code-actions {
  399. display: flex;
  400. gap: 20rpx;
  401. }
  402. .action-btn {
  403. flex: 1;
  404. background: rgba(255,255,255,0.2);
  405. color: #fff;
  406. font-size: 26rpx;
  407. border-radius: 40rpx;
  408. height: 64rpx;
  409. line-height: 64rpx;
  410. border: 1rpx solid rgba(255,255,255,0.3);
  411. }
  412. .action-btn::after { border: none; }
  413. .action-btn:active { opacity: 0.8; }
  414. .bind-card {
  415. background: #fff;
  416. border-radius: 20rpx;
  417. padding: 30rpx;
  418. margin-bottom: 24rpx;
  419. box-shadow: 0 2rpx 12rpx rgba(249,115,22,0.08);
  420. }
  421. .bind-title {
  422. font-size: 28rpx;
  423. font-weight: 600;
  424. color: #1E293B;
  425. display: block;
  426. margin-bottom: 16rpx;
  427. }
  428. .bind-row {
  429. display: flex;
  430. gap: 20rpx;
  431. }
  432. .bind-input {
  433. flex: 1;
  434. border: 1rpx solid #CBD5E1;
  435. border-radius: 12rpx;
  436. padding: 20rpx;
  437. font-size: 28rpx;
  438. }
  439. .bind-btn {
  440. background: #F97316;
  441. color: #fff;
  442. font-size: 26rpx;
  443. border-radius: 12rpx;
  444. height: 64rpx;
  445. line-height: 64rpx;
  446. padding: 0 30rpx;
  447. font-weight: 500;
  448. }
  449. .bind-btn::after { border: none; }
  450. .bind-btn:active { opacity: 0.9; }
  451. .list-section {
  452. background: #fff;
  453. border-radius: 20rpx;
  454. padding: 30rpx;
  455. box-shadow: 0 2rpx 12rpx rgba(249,115,22,0.08);
  456. }
  457. .section-title {
  458. font-size: 28rpx;
  459. font-weight: 600;
  460. color: #1E293B;
  461. display: block;
  462. margin-bottom: 20rpx;
  463. }
  464. .invite-item {
  465. display: flex;
  466. align-items: center;
  467. padding: 20rpx 0;
  468. border-bottom: 1rpx solid #FED7AA;
  469. }
  470. .invite-item:last-child { border-bottom: none; }
  471. .invite-avatar-wrap {
  472. width: 60rpx;
  473. height: 60rpx;
  474. border-radius: 50%;
  475. background: #FEF3C7;
  476. display: flex;
  477. align-items: center;
  478. justify-content: center;
  479. margin-right: 20rpx;
  480. }
  481. .invite-avatar {
  482. font-size: 32rpx;
  483. }
  484. .invite-info { flex: 1; }
  485. .invite-name {
  486. font-size: 28rpx;
  487. color: #1E293B;
  488. display: block;
  489. }
  490. .invite-time {
  491. font-size: 22rpx;
  492. color: #94A3B8;
  493. margin-top: 4rpx;
  494. }
  495. .empty-state, .loading-state {
  496. text-align: center;
  497. padding: 60rpx;
  498. color: #94A3B8;
  499. font-size: 28rpx;
  500. }
  501. </style>