index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <template>
  2. <scroll-view scroll-y class="vip-page">
  3. <!-- 顶部背景 -->
  4. <view class="header-bg">
  5. <!-- 返回按钮 -->
  6. <view class="back-btn" @click="goBack">
  7. <text class="back-icon">←</text>
  8. </view>
  9. <text class="crown-icon">👑</text>
  10. <text class="title">VIP会员特权</text>
  11. <text class="subtitle">开通会员,享受专属服务</text>
  12. </view>
  13. <!-- 特权列表 -->
  14. <view class="privileges-section">
  15. <view class="privilege-item" v-for="(item, index) in privileges" :key="index">
  16. <view class="privilege-icon">{{ item.icon }}</view>
  17. <view class="privilege-content">
  18. <text class="privilege-title">{{ item.title }}</text>
  19. <text class="privilege-desc">{{ item.desc }}</text>
  20. </view>
  21. </view>
  22. </view>
  23. <!-- VIP套餐 -->
  24. <view class="packages-section">
  25. <view class="section-title">选择套餐</view>
  26. <view class="packages-list">
  27. <view
  28. class="package-card"
  29. v-for="(pkg, index) in packages"
  30. :key="index"
  31. :class="{ 'active': selectedPackage === index }"
  32. @click="selectPackage(index)"
  33. >
  34. <text class="package-name">{{ pkg.name }}</text>
  35. <view class="package-price">
  36. <text class="price-symbol">¥</text>
  37. <text class="price-value">{{ pkg.price }}</text>
  38. <text class="price-unit">/{{ pkg.duration }}</text>
  39. </view>
  40. <text class="package-original" v-if="pkg.originalPrice">原价 ¥{{ pkg.originalPrice }}</text>
  41. <view class="package-benefits">
  42. <text class="benefit-item" v-for="(benefit, idx) in pkg.benefits" :key="idx">
  43. {{ benefit }}
  44. </text>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. <!-- 底部占位 -->
  50. <view class="bottom-placeholder"></view>
  51. <!-- 底部购买按钮 -->
  52. <view class="bottom-bar">
  53. <view class="total-info">
  54. <text class="total-label">合计:</text>
  55. <text class="total-price" v-if="selectedPackagePrice > 0">¥{{ selectedPackagePrice }}</text>
  56. <text class="total-price-hint" v-else>请选择套餐</text>
  57. </view>
  58. <button class="buy-btn" @click="handlePurchase">立即开通</button>
  59. </view>
  60. </scroll-view>
  61. </template>
  62. <script>
  63. import userAuth from '@/utils/userAuth.js'
  64. export default {
  65. data() {
  66. return {
  67. gatewayURL: 'https://api.zhongruanke.cn',
  68. currentUserId: null,
  69. selectedPackage: null, // 默认不选中任何套餐
  70. isVip: false,
  71. vipExpireTime: null,
  72. remainingDays: 0,
  73. privileges: [
  74. {
  75. icon: '👑',
  76. title: '专属身份标识',
  77. desc: '尊贵VIP标识,提升个人魅力'
  78. },
  79. {
  80. icon: '💎',
  81. title: '优先推荐',
  82. desc: '优先展示给优质用户,提高匹配率'
  83. },
  84. {
  85. icon: '💌',
  86. title: '无限查看联系方式',
  87. desc: '查看心仪对象的联系方式,无限制'
  88. },
  89. {
  90. icon: '👀',
  91. title: '查看访客记录',
  92. desc: '查看谁看过你,主动出击'
  93. },
  94. {
  95. icon: '📞',
  96. title: '专属红娘服务',
  97. desc: '一对一专属红娘,贴心服务'
  98. },
  99. {
  100. icon: '🎉',
  101. title: '线下活动优先',
  102. desc: '优先参加平台举办的线下活动'
  103. }
  104. ],
  105. packages: []
  106. }
  107. },
  108. onLoad() {
  109. // 获取登录用户ID - 使用修复后的工具类
  110. this.currentUserId = userAuth.getUserId()
  111. // 加载VIP信息
  112. this.loadVipInfo()
  113. },
  114. computed: {
  115. selectedPackagePrice() {
  116. if (this.selectedPackage === null || this.selectedPackage === undefined) {
  117. return 0
  118. }
  119. return this.packages[this.selectedPackage]?.price || 0
  120. }
  121. },
  122. methods: {
  123. // 加载VIP信息
  124. loadVipInfo() {
  125. const params = {}
  126. if (this.currentUserId) {
  127. params.userId = this.currentUserId
  128. }
  129. uni.request({
  130. url: this.gatewayURL + '/api/vip/inf',
  131. method: 'GET',
  132. data: params,
  133. success: (res) => {
  134. if (res.data && res.data.code === 200) {
  135. const data = res.data.data
  136. // 更新VIP状态
  137. this.isVip = data.isVip
  138. this.vipExpireTime = data.vipExpireTime
  139. this.remainingDays = data.remainingDays
  140. // 更新套餐列表
  141. this.packages = data.packages.map(pkg => ({
  142. packageId: pkg.packageId,
  143. name: pkg.name,
  144. price: pkg.price,
  145. originalPrice: pkg.originalPrice,
  146. duration: pkg.duration,
  147. benefits: pkg.benefits,
  148. recommend: pkg.recommend
  149. }))
  150. }
  151. },
  152. fail: (err) => {
  153. console.error('获取VIP信息失败:', err)
  154. uni.showToast({
  155. title: '加载VIP信息失败',
  156. icon: 'none'
  157. })
  158. }
  159. })
  160. },
  161. goBack() {
  162. uni.navigateBack({
  163. delta: 1
  164. })
  165. },
  166. selectPackage(index) {
  167. this.selectedPackage = index
  168. },
  169. handlePurchase() {
  170. // 检查是否选择了套餐
  171. if (this.selectedPackage === null || this.selectedPackage === undefined) {
  172. uni.showToast({
  173. title: '请先选择一个套餐',
  174. icon: 'none'
  175. })
  176. return
  177. }
  178. const pkg = this.packages[this.selectedPackage]
  179. if (!pkg) {
  180. uni.showToast({
  181. title: '请选择套餐',
  182. icon: 'none'
  183. })
  184. return
  185. }
  186. // 获取用户ID
  187. const userId = this.currentUserId || 1
  188. uni.showModal({
  189. title: '确认开通',
  190. content: `确认开通${pkg.name}(¥${pkg.price})?`,
  191. success: (res) => {
  192. if (res.confirm) {
  193. this.requestPay(userId, pkg)
  194. }
  195. }
  196. })
  197. },
  198. /**
  199. * 请求支付参数并调起微信支付
  200. */
  201. requestPay(userId, pkg) {
  202. uni.showLoading({
  203. title: '处理中...'
  204. })
  205. uni.request({
  206. url: this.gatewayURL + '/api/vip/purchase',
  207. method: 'POST',
  208. data: {
  209. userId: userId,
  210. packageId: pkg.packageId
  211. },
  212. header: {
  213. 'content-type': 'application/json',
  214. 'token': uni.getStorageSync('token') // 携带登录态
  215. },
  216. success: (res) => {
  217. uni.hideLoading()
  218. if (res.data && res.data.code === 200) {
  219. const payParams = res.data.ext // 后端返回的支付参数
  220. this.callWxPay(payParams, pkg) // 调起微信支付
  221. } else {
  222. uni.showToast({
  223. title: res.data?.message || '获取支付参数失败',
  224. icon: 'none'
  225. })
  226. }
  227. },
  228. fail: (err) => {
  229. uni.hideLoading()
  230. console.error('请求支付参数失败:', err)
  231. uni.showToast({
  232. title: '网络请求失败',
  233. icon: 'none'
  234. })
  235. }
  236. })
  237. },
  238. /**
  239. * 调起微信支付组件
  240. */
  241. callWxPay(payParams, pkg) {
  242. uni.requestPayment({
  243. provider: 'wxpay',
  244. timeStamp: payParams.timeStamp,
  245. nonceStr: payParams.nonceStr,
  246. package: payParams.package,
  247. signType: payParams.signType,
  248. paySign: payParams.paySign,
  249. total_fee: payParams.totalFee,
  250. success: (payResult) => {
  251. if (payResult.errMsg === 'requestPayment:ok') {
  252. uni.showToast({
  253. title: '支付成功!VIP已开通',
  254. icon: 'success',
  255. duration: 2000
  256. })
  257. // 延迟返回上一页,并触发页面刷新
  258. setTimeout(() => {
  259. // 通过事件通知个人页面刷新数据
  260. uni.$emit('vipPurchased')
  261. // 返回上一页
  262. uni.navigateBack({
  263. success: () => {
  264. }
  265. })
  266. }, 2000)
  267. }
  268. },
  269. fail: (payError) => {
  270. console.error('支付失败:', payError)
  271. if (payError.errMsg.includes('cancel')) {
  272. uni.showToast({
  273. title: '已取消支付',
  274. icon: 'none'
  275. })
  276. } else {
  277. uni.showToast({
  278. title: `支付失败:${payError.errMsg}`,
  279. icon: 'none'
  280. })
  281. }
  282. }
  283. })
  284. }
  285. }
  286. }
  287. </script>
  288. <style lang="scss" scoped>
  289. /* 原有样式保持不变 */
  290. .vip-page {
  291. height: 100vh;
  292. background: #F5F5F5;
  293. }
  294. .bottom-placeholder {
  295. height: 180rpx;
  296. }
  297. .header-bg {
  298. background: linear-gradient(135deg, #FFB300 0%, #FFA000 100%);
  299. padding: 60rpx 60rpx 120rpx;
  300. padding-top: calc(60rpx + env(safe-area-inset-top));
  301. text-align: center;
  302. position: relative;
  303. border-bottom: 4rpx solid #FF8F00;
  304. .back-btn {
  305. position: absolute;
  306. left: 30rpx;
  307. top: calc(60rpx + env(safe-area-inset-top));
  308. width: 70rpx;
  309. height: 70rpx;
  310. display: flex;
  311. align-items: center;
  312. justify-content: center;
  313. z-index: 10;
  314. background: rgba(255, 255, 255, 0.2);
  315. border-radius: 8rpx;
  316. transition: all 0.2s ease;
  317. &:active {
  318. opacity: 0.8;
  319. }
  320. .back-icon {
  321. font-size: 48rpx;
  322. color: #FFFFFF;
  323. font-weight: bold;
  324. }
  325. }
  326. .crown-icon {
  327. font-size: 120rpx;
  328. margin-bottom: 20rpx;
  329. display: block;
  330. filter: drop-shadow(0 4rpx 8rpx rgba(0, 0, 0, 0.2));
  331. }
  332. .title {
  333. display: block;
  334. font-size: 44rpx;
  335. font-weight: bold;
  336. color: #FFFFFF;
  337. margin-bottom: 15rpx;
  338. letter-spacing: 2rpx;
  339. }
  340. .subtitle {
  341. display: block;
  342. font-size: 26rpx;
  343. color: #FFFFFF;
  344. opacity: 0.95;
  345. }
  346. }
  347. .privileges-section {
  348. margin: -20rpx 30rpx 30rpx;
  349. background-color: #FFFFFF;
  350. border-radius: 16rpx;
  351. padding: 35rpx 30rpx;
  352. border: 2rpx solid #E0E0E0;
  353. .section-title {
  354. font-size: 30rpx;
  355. font-weight: bold;
  356. color: #333333;
  357. margin-bottom: 25rpx;
  358. text-align: center;
  359. }
  360. .privilege-item {
  361. display: flex;
  362. align-items: flex-start;
  363. margin-bottom: 25rpx;
  364. padding: 20rpx;
  365. background: #FFFBF0;
  366. border-radius: 12rpx;
  367. border: 2rpx solid #FFE082;
  368. transition: all 0.2s ease;
  369. &:active {
  370. background: #FFF8E1;
  371. }
  372. &:last-child {
  373. margin-bottom: 0;
  374. }
  375. .privilege-icon {
  376. width: 70rpx;
  377. height: 70rpx;
  378. background: #FFF3E0;
  379. border-radius: 12rpx;
  380. display: flex;
  381. align-items: center;
  382. justify-content: center;
  383. font-size: 36rpx;
  384. margin-right: 20rpx;
  385. flex-shrink: 0;
  386. border: 2rpx solid #FFD54F;
  387. }
  388. .privilege-content {
  389. flex: 1;
  390. min-width: 0;
  391. overflow: hidden;
  392. .privilege-title {
  393. display: block;
  394. font-size: 28rpx;
  395. font-weight: bold;
  396. color: #333333;
  397. margin-bottom: 8rpx;
  398. word-wrap: break-word;
  399. word-break: break-all;
  400. white-space: normal;
  401. }
  402. .privilege-desc {
  403. display: block;
  404. font-size: 22rpx;
  405. color: #999999;
  406. line-height: 1.4;
  407. word-wrap: break-word;
  408. white-space: normal;
  409. }
  410. }
  411. }
  412. }
  413. .packages-section {
  414. margin: 30rpx;
  415. .section-title {
  416. font-size: 32rpx;
  417. font-weight: bold;
  418. color: #333333;
  419. margin-bottom: 25rpx;
  420. text-align: center;
  421. padding-bottom: 15rpx;
  422. border-bottom: 3rpx solid #FFB300;
  423. }
  424. .packages-list {
  425. display: flex;
  426. flex-direction: column;
  427. gap: 20rpx;
  428. .package-card {
  429. background-color: #FFFFFF;
  430. border-radius: 16rpx;
  431. padding: 35rpx 30rpx;
  432. position: relative;
  433. border: 3rpx solid #E0E0E0;
  434. transition: all 0.2s ease;
  435. &:active {
  436. transform: translateY(-2rpx);
  437. }
  438. .package-name {
  439. font-size: 34rpx;
  440. font-weight: bold;
  441. color: #333333;
  442. margin-bottom: 15rpx;
  443. display: block;
  444. }
  445. .package-price {
  446. display: flex;
  447. align-items: baseline;
  448. margin-bottom: 8rpx;
  449. .price-symbol {
  450. font-size: 30rpx;
  451. color: #FFB300;
  452. font-weight: bold;
  453. }
  454. .price-value {
  455. font-size: 52rpx;
  456. font-weight: bold;
  457. color: #FFB300;
  458. margin: 0 5rpx;
  459. }
  460. .price-unit {
  461. font-size: 26rpx;
  462. color: #999999;
  463. }
  464. }
  465. .package-original {
  466. font-size: 24rpx;
  467. color: #CCCCCC;
  468. text-decoration: line-through;
  469. margin-bottom: 18rpx;
  470. display: block;
  471. }
  472. .package-benefits {
  473. display: flex;
  474. flex-wrap: wrap;
  475. gap: 12rpx;
  476. .benefit-item {
  477. background: #FFF3E0;
  478. padding: 8rpx 20rpx;
  479. border-radius: 8rpx;
  480. font-size: 22rpx;
  481. color: #F57C00;
  482. border: 1rpx solid #FFD54F;
  483. font-weight: 500;
  484. }
  485. }
  486. &.active {
  487. background: linear-gradient(135deg, #FFFBF0 0%, #FFF8E1 100%);
  488. border-color: #FFB300;
  489. border-width: 4rpx;
  490. &::before {
  491. content: '✓ 已选择';
  492. position: absolute;
  493. top: 20rpx;
  494. right: 20rpx;
  495. background: #FFB300;
  496. color: #FFFFFF;
  497. padding: 6rpx 16rpx;
  498. border-radius: 6rpx;
  499. font-size: 22rpx;
  500. font-weight: 600;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. .bottom-bar {
  507. position: fixed;
  508. bottom: 0;
  509. left: 0;
  510. right: 0;
  511. background-color: #FFFFFF;
  512. padding: 20rpx 30rpx;
  513. padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
  514. border-top: 2rpx solid #E0E0E0;
  515. display: flex;
  516. align-items: center;
  517. justify-content: space-between;
  518. z-index: 999;
  519. .total-info {
  520. display: flex;
  521. align-items: baseline;
  522. .total-label {
  523. font-size: 28rpx;
  524. color: #666666;
  525. font-weight: 500;
  526. }
  527. .total-price {
  528. font-size: 48rpx;
  529. font-weight: bold;
  530. color: #FFB300;
  531. margin-left: 10rpx;
  532. }
  533. .total-price-hint {
  534. font-size: 28rpx;
  535. color: #999999;
  536. margin-left: 10rpx;
  537. }
  538. }
  539. .buy-btn {
  540. background: linear-gradient(135deg, #FFB300 0%, #FFA000 100%);
  541. color: #FFFFFF;
  542. padding: 20rpx 60rpx;
  543. border-radius: 50rpx;
  544. font-size: 30rpx;
  545. font-weight: bold;
  546. border: none;
  547. box-shadow: 0 6rpx 16rpx rgba(255, 179, 0, 0.4);
  548. transition: all 0.2s ease;
  549. &:active {
  550. transform: translateY(2rpx);
  551. box-shadow: 0 4rpx 12rpx rgba(255, 179, 0, 0.3);
  552. }
  553. &::after {
  554. border: none;
  555. }
  556. }
  557. }
  558. </style>