瑶都万能墙
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

700 lines
18 KiB

3 months ago
3 months ago
3 months ago
  1. <template>
  2. <view class="page">
  3. <navbar leftClick
  4. color="#fff"
  5. bgColor="#667eea"
  6. @leftClick="$utils.navigateBack" />
  7. <view class="turntable-container">
  8. <!-- 头部标题 -->
  9. <view class="header">
  10. <text class="title">幸运大转盘</text>
  11. <text class="subtitle">转一转好运来</text>
  12. </view>
  13. <!-- 积分余额显示 -->
  14. <!-- 注释积分显示区域 -->
  15. <!-- <view class="points-display">
  16. <view class="points-info">
  17. <text class="points-label">当前积分</text>
  18. <text class="points-value">{{ userPoints }}</text>
  19. </view>
  20. <view class="cost-info">
  21. <text class="cost-label">每次消耗</text>
  22. <text class="cost-value">{{ drawCost }}积分</text>
  23. </view>
  24. </view> -->
  25. <!-- 转盘区域 -->
  26. <view class="turntable-wrapper" v-if="prizes.length > 0">
  27. <view class="turntable" :class="{ 'spinning': isSpinning }" :style="{ transform: `rotate(${rotateAngle}deg)` }">
  28. <!-- 使用纯CSS创建转盘 -->
  29. <view class="wheel-bg">
  30. <!-- 8个扇形区域 -->
  31. <view
  32. v-for="(prize, index) in prizes"
  33. :key="index"
  34. class="wheel-sector"
  35. :style="{
  36. backgroundColor: sectorColors[index % sectorColors.length],
  37. transform: `rotate(${index * sectorAngle}deg)`
  38. }"
  39. >
  40. </view>
  41. </view>
  42. <!-- 奖品文字覆盖层 -->
  43. <view class="prizes-overlay">
  44. <view
  45. v-for="(prize, index) in prizes"
  46. :key="index"
  47. class="prize-item"
  48. :style="prizeStyles[index]"
  49. >
  50. <view class="prize-content">
  51. <text class="prize-icon">{{ getIcon(prize.type) }}</text>
  52. <text class="prize-name">{{ prize.title }}</text>
  53. <text class="prize-value" v-if="prize.price">{{ prize.price }}</text>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. <!-- 中心指针 -->
  59. <view class="pointer">
  60. <view class="pointer-triangle"></view>
  61. </view>
  62. <!-- 中心按钮 -->
  63. <view class="center-button" @click="startSpin" :class="{ disabled: isSpinning || userPoints < drawCost || isWatchingAd }">
  64. <text class="button-text">{{ getButtonText() }}</text>
  65. </view>
  66. </view>
  67. <!-- 加载中状态 -->
  68. <view v-else class="loading-wrapper">
  69. <uv-loading-icon mode="spinner" color="#fff" size="60"></uv-loading-icon>
  70. <text class="loading-text">加载中...</text>
  71. </view>
  72. <!-- 抽奖结果弹窗 -->
  73. <view class="result-modal" v-if="showResult" @click="closeResult">
  74. <view class="modal-content" @click.stop>
  75. <text class="result-title">🎉 恭喜您 🎉</text>
  76. <view class="result-prize">
  77. <!-- <text class="result-icon">{{ getIcon(currentPrize.type) }}</text> -->
  78. <image :src="currentPrize.img" mode="widthFix" style="width: 100%;"></image>
  79. <text class="result-name">{{ currentPrize.title }}</text>
  80. <text class="result-value" v-if="currentPrize.price">{{ currentPrize.price }}</text>
  81. </view>
  82. <view class="points-change">
  83. <text class="change-text">{{ getPointsChangeText() }}</text>
  84. </view>
  85. <view class="result-actions">
  86. <button class="confirm-btn" @click="closeResult">确定</button>
  87. </view>
  88. </view>
  89. </view>
  90. <!-- 积分提示 -->
  91. <!-- 注释积分提示信息 -->
  92. <!-- <view class="spin-info">
  93. <text>观看完整视频广告后消耗{{ drawCost }}积分即可抽奖快来试试手气吧</text>
  94. </view> -->
  95. <view class="spin-info">
  96. <text>观看完整视频广告即可免费抽奖快来试试手气吧</text>
  97. </view>
  98. </view>
  99. </view>
  100. </template>
  101. <script>
  102. import { mapState } from 'vuex'
  103. import rewardedVideoAdMixin from '@/mixins/rewardedVideoAd.js'
  104. export default {
  105. mixins: [rewardedVideoAdMixin],
  106. computed: {
  107. ...mapState(['userInfo']),
  108. // 获取用户当前积分
  109. userPoints() {
  110. return this.userInfo?.integerPrice || 0
  111. }
  112. },
  113. data() {
  114. return {
  115. // 奖品配置(从接口获取)
  116. prizes: [],
  117. sectorColors: [
  118. '#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4',
  119. '#FFEAA7', '#DDA0DD', '#98D8C8', '#F7DC6F'
  120. ],
  121. isSpinning: false, // 是否正在旋转
  122. rotateAngle: 0, // 旋转角度
  123. totalSpinRounds: 0, // 总旋转圈数
  124. currentAngle: 0, // 当前角度(0-360度)
  125. showResult: false, // 显示结果弹窗
  126. currentPrize: null, // 当前中奖奖品
  127. sectorAngle: 0, // 每个扇形的角度
  128. drawCost: 5, // 抽奖消耗积分
  129. pointsChange: null, // 积分变化
  130. // 预计算奖品位置样式
  131. prizeStyles: []
  132. // 视频广告相关数据已移至混入中
  133. }
  134. },
  135. onLoad() {
  136. this.getLuckDrawList()
  137. // initRewardedVideoAd() 已在混入的mounted中处理
  138. },
  139. onShow() {
  140. // 刷新用户信息以获取最新积分
  141. if (uni.getStorageSync('token')) {
  142. this.$store.commit('getUserInfo')
  143. }
  144. },
  145. methods: {
  146. // 广告观看完成回调
  147. onAdWatchComplete() {
  148. console.log('用户看完广告,开始抽奖')
  149. this.performDraw()
  150. },
  151. // 广告观看取消回调
  152. onAdWatchCancel() {
  153. uni.showToast({
  154. title: '请观看完整广告才能抽奖',
  155. icon: 'none'
  156. })
  157. },
  158. // 获取抽奖列表
  159. getLuckDrawList() {
  160. this.$api('getLuckDrawList', {}, res => {
  161. if (res.code == 200) {
  162. this.prizes = res.result || []
  163. // 计算每个扇形的角度
  164. if (this.prizes.length > 0) {
  165. this.sectorAngle = 360 / this.prizes.length
  166. this.calculatePrizeStyles()
  167. }
  168. } else {
  169. uni.showToast({
  170. title: res.message || '获取抽奖信息失败',
  171. icon: 'none'
  172. })
  173. }
  174. })
  175. },
  176. // 获取按钮文字
  177. getButtonText() {
  178. if (this.isWatchingAd) {
  179. return '观看广告中...'
  180. }
  181. if (this.isSpinning) {
  182. return '抽奖中...'
  183. }
  184. // 注释积分不足检查
  185. // if (this.userPoints < this.drawCost) {
  186. // return '积分不足'
  187. // }
  188. return '观看广告抽奖'
  189. },
  190. // 根据奖品类型获取图标
  191. getIcon(type) {
  192. const iconMap = {
  193. '0': '💰',
  194. 'points': '⭐',
  195. 'gift': '🎁',
  196. 'coupon': '🎫',
  197. 'thanks': '🤝'
  198. }
  199. return iconMap[type] || '🎁'
  200. },
  201. // 计算奖品位置样式
  202. calculatePrizeStyles() {
  203. this.prizeStyles = this.prizes.map((prize, index) => {
  204. // 计算奖品在圆形中的位置
  205. const angle = (index * this.sectorAngle + this.sectorAngle / 2) * Math.PI / 180; // 转换为弧度
  206. const radius = 150; // 奖品距离中心的距离
  207. const x = Math.cos(angle - Math.PI/2) * radius; // 减去90度,因为我们希望0度在顶部
  208. const y = Math.sin(angle - Math.PI/2) * radius;
  209. return `left: calc(50% + ${x}rpx); top: calc(50% + ${y}rpx); transform: translate(-50%, -50%);`
  210. })
  211. },
  212. // 开始抽奖
  213. startSpin() {
  214. // 检查是否可以抽奖
  215. if (this.isSpinning || this.isWatchingAd) {
  216. return
  217. }
  218. // 注释积分检查逻辑
  219. // // 检查积分是否足够
  220. // if (this.userPoints < this.drawCost) {
  221. // uni.showModal({
  222. // title: '积分不足',
  223. // content: `抽奖需要消耗${this.drawCost}积分,您当前积分为${this.userPoints}`,
  224. // showCancel: true,
  225. // cancelText: '取消',
  226. // confirmText: '去赚积分',
  227. // success: (res) => {
  228. // if (res.confirm) {
  229. // // 跳转到积分获取页面或任务页面
  230. // uni.navigateBack()
  231. // }
  232. // }
  233. // })
  234. // return
  235. // }
  236. // 先播放广告,再进行抽奖
  237. this.showRewardedVideoAd({
  238. onSuccess: this.onAdWatchComplete,
  239. onCancel: this.onAdWatchCancel,
  240. fallbackTitle: '广告加载失败',
  241. fallbackContent: '无法加载广告,是否直接抽奖?'
  242. })
  243. },
  244. // 执行抽奖(观看广告后调用)
  245. performDraw() {
  246. this.isSpinning = true
  247. // 调用抽奖接口
  248. this.$api('luckDraw', {}, res => {
  249. if (res.code == 200) {
  250. const result = res.result
  251. this.currentPrize = result.gift
  252. this.pointsChange = result.remainingIntegral || null
  253. // 更新用户信息(积分变化)
  254. this.$store.commit('getUserInfo')
  255. // 找到中奖奖品的索引
  256. const prizeIndex = this.prizes.findIndex(prize => prize.id == result.gift.id)
  257. if (prizeIndex !== -1) {
  258. // 计算目标角度
  259. // 奖品在扇形中心的角度
  260. const prizeAngle = prizeIndex * this.sectorAngle + this.sectorAngle / 2
  261. // 要让奖品转到指针位置(0度),目标角度就是负的奖品角度
  262. let targetAngle = -prizeAngle
  263. // 确保目标角度为正值(0-360度范围内)
  264. if (targetAngle < 0) {
  265. targetAngle += 360
  266. }
  267. // 增加旋转圈数
  268. const spinRounds = 5 // 转5圈
  269. this.totalSpinRounds += spinRounds
  270. this.currentAngle = targetAngle
  271. // 计算最终的旋转角度 = 总圈数 * 360 + 当前角度
  272. this.rotateAngle = this.totalSpinRounds * 360 + this.currentAngle
  273. // 动画结束后显示结果
  274. setTimeout(() => {
  275. this.isSpinning = false
  276. this.showResult = true
  277. this.handlePrizeResult()
  278. }, 3000)
  279. } else {
  280. this.isSpinning = false
  281. uni.showToast({
  282. title: '抽奖异常,请重试',
  283. icon: 'none'
  284. })
  285. }
  286. } else {
  287. this.isSpinning = false
  288. uni.showToast({
  289. title: res.message || '抽奖失败,请重试',
  290. icon: 'none'
  291. })
  292. }
  293. })
  294. },
  295. // 处理中奖结果
  296. handlePrizeResult() {
  297. if (this.currentPrize) {
  298. console.log(`中奖信息:`, this.currentPrize)
  299. // 根据奖品类型显示不同提示
  300. if (this.currentPrize.type === 'money') {
  301. console.log(`获得现金奖励: ${this.currentPrize.prizeValue}`)
  302. } else if (this.currentPrize.type === 'points') {
  303. console.log(`获得积分奖励: ${this.currentPrize.prizeValue}`)
  304. } else if (this.currentPrize.type === 'gift') {
  305. console.log(`获得礼品: ${this.currentPrize.prizeName}`)
  306. }
  307. }
  308. },
  309. // 关闭结果弹窗
  310. closeResult() {
  311. this.showResult = false
  312. this.pointsChange = null
  313. // 刷新抽奖信息
  314. this.getLuckDrawList()
  315. },
  316. // 积分变化提示文字
  317. getPointsChangeText() {
  318. // 注释积分消耗提示
  319. // let text = `消耗 ${this.drawCost} 积分`
  320. let text = `恭喜您获得奖品!`
  321. if (this.currentPrize && this.currentPrize.type === 'points') {
  322. const gained = parseInt(this.currentPrize.prizeValue)
  323. // 注释积分计算逻辑
  324. // const net = gained - this.drawCost
  325. // if (net > 0) {
  326. // text += `,获得 ${gained} 积分,净收益 +${net} 积分`
  327. // } else if (net < 0) {
  328. // text += `,获得 ${gained} 积分,净损失 ${Math.abs(net)} 积分`
  329. // } else {
  330. // text += `,获得 ${gained} 积分,收支平衡`
  331. // }
  332. text = `获得 ${gained} 积分奖励!`
  333. }
  334. return text
  335. }
  336. }
  337. }
  338. </script>
  339. <style scoped lang="scss">
  340. .turntable-container {
  341. min-height: 100vh;
  342. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  343. padding: 40rpx;
  344. display: flex;
  345. flex-direction: column;
  346. align-items: center;
  347. }
  348. .header {
  349. text-align: center;
  350. margin-bottom: 40rpx;
  351. color: white;
  352. .title {
  353. font-size: 48rpx;
  354. font-weight: bold;
  355. display: block;
  356. margin-bottom: 20rpx;
  357. }
  358. .subtitle {
  359. font-size: 28rpx;
  360. opacity: 0.9;
  361. }
  362. }
  363. .points-display {
  364. background: rgba(255, 255, 255, 0.2);
  365. border-radius: 20rpx;
  366. padding: 30rpx;
  367. margin-bottom: 40rpx;
  368. backdrop-filter: blur(10rpx);
  369. display: flex;
  370. justify-content: space-between;
  371. align-items: center;
  372. width: 100%;
  373. max-width: 600rpx;
  374. .points-info, .cost-info {
  375. display: flex;
  376. align-items: center;
  377. color: white;
  378. .points-label, .cost-label {
  379. font-size: 28rpx;
  380. margin-right: 10rpx;
  381. }
  382. .points-value {
  383. font-size: 32rpx;
  384. font-weight: bold;
  385. color: #FFD700;
  386. }
  387. .cost-value {
  388. font-size: 28rpx;
  389. font-weight: bold;
  390. color: #FF6B6B;
  391. }
  392. }
  393. }
  394. .turntable-wrapper {
  395. position: relative;
  396. width: 600rpx;
  397. height: 600rpx;
  398. margin-bottom: 60rpx;
  399. }
  400. .loading-wrapper {
  401. display: flex;
  402. flex-direction: column;
  403. align-items: center;
  404. justify-content: center;
  405. height: 600rpx;
  406. .loading-text {
  407. color: white;
  408. font-size: 28rpx;
  409. margin-top: 20rpx;
  410. }
  411. }
  412. .turntable {
  413. width: 100%;
  414. height: 100%;
  415. border-radius: 50%;
  416. position: relative;
  417. transition: transform 3s cubic-bezier(0.23, 1, 0.32, 1);
  418. box-shadow: 0 0 40rpx rgba(0, 0, 0, 0.3);
  419. overflow: hidden;
  420. &.spinning {
  421. transition-duration: 3s;
  422. }
  423. }
  424. .wheel-bg {
  425. position: absolute;
  426. width: 100%;
  427. height: 100%;
  428. border-radius: 50%;
  429. overflow: hidden;
  430. }
  431. .wheel-sector {
  432. position: absolute;
  433. width: 50%;
  434. height: 50%;
  435. top: 0%;
  436. left: 50%;
  437. transform-origin: 0% 100%;
  438. &::before {
  439. content: '';
  440. position: absolute;
  441. top: 0;
  442. left: 0;
  443. width: 100%;
  444. height: 100%;
  445. background: inherit;
  446. clip-path: polygon(0% 100%, 50% 0%, 100% 100%);
  447. }
  448. // 添加边框线
  449. &::after {
  450. content: '';
  451. position: absolute;
  452. top: 0;
  453. left: 0;
  454. width: 100%;
  455. height: 100%;
  456. border-right: 2rpx solid rgba(255,255,255,0.3);
  457. transform-origin: 0% 100%;
  458. }
  459. }
  460. .prizes-overlay {
  461. position: absolute;
  462. width: 100%;
  463. height: 100%;
  464. top: 0;
  465. left: 0;
  466. z-index: 990;
  467. }
  468. .prize-item {
  469. position: absolute;
  470. width: 120rpx;
  471. height: 80rpx;
  472. z-index: 10;
  473. }
  474. .prize-content {
  475. width: 100%;
  476. height: 100%;
  477. display: flex;
  478. flex-direction: column;
  479. align-items: center;
  480. justify-content: center;
  481. text-align: center;
  482. color: white;
  483. text-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.8);
  484. .prize-icon {
  485. font-size: 32rpx;
  486. display: block;
  487. margin-bottom: 4rpx;
  488. }
  489. .prize-name {
  490. font-size: 18rpx;
  491. display: block;
  492. font-weight: bold;
  493. margin-bottom: 2rpx;
  494. line-height: 1.2;
  495. }
  496. .prize-value {
  497. font-size: 20rpx;
  498. display: block;
  499. font-weight: bold;
  500. line-height: 1.2;
  501. }
  502. }
  503. .pointer {
  504. position: absolute;
  505. top: -20rpx;
  506. left: 50%;
  507. transform: translateX(-50%);
  508. z-index: 100;
  509. .pointer-triangle {
  510. width: 0;
  511. height: 0;
  512. border-left: 20rpx solid transparent;
  513. border-right: 20rpx solid transparent;
  514. border-top: 60rpx solid #FF4757;
  515. filter: drop-shadow(0 4rpx 8rpx rgba(0, 0, 0, 0.3));
  516. }
  517. }
  518. .center-button {
  519. position: absolute;
  520. top: 50%;
  521. left: 50%;
  522. transform: translate(-50%, -50%);
  523. width: 160rpx;
  524. height: 160rpx;
  525. border-radius: 50%;
  526. background: linear-gradient(145deg, #FF6B6B, #FF4757);
  527. display: flex;
  528. align-items: center;
  529. justify-content: center;
  530. box-shadow: 0 8rpx 20rpx rgba(255, 71, 87, 0.4);
  531. z-index: 50;
  532. transition: transform 0.2s;
  533. &:active:not(.disabled) {
  534. transform: translate(-50%, -50%) scale(0.95);
  535. }
  536. &.disabled {
  537. opacity: 0.6;
  538. cursor: not-allowed;
  539. background: linear-gradient(145deg, #999, #777);
  540. }
  541. .button-text {
  542. color: white;
  543. font-size: 24rpx;
  544. font-weight: bold;
  545. text-align: center;
  546. }
  547. }
  548. .result-modal {
  549. position: fixed;
  550. top: 0;
  551. left: 0;
  552. width: 100vw;
  553. height: 100vh;
  554. background: rgba(0, 0, 0, 0.7);
  555. display: flex;
  556. align-items: center;
  557. justify-content: center;
  558. z-index: 1000;
  559. .modal-content {
  560. background: white;
  561. border-radius: 20rpx;
  562. padding: 60rpx 40rpx;
  563. text-align: center;
  564. box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.3);
  565. min-width: 500rpx;
  566. .result-title {
  567. font-size: 36rpx;
  568. font-weight: bold;
  569. color: #FF6B6B;
  570. margin-bottom: 40rpx;
  571. }
  572. .result-prize {
  573. margin-bottom: 30rpx;
  574. .result-icon {
  575. font-size: 60rpx;
  576. display: block;
  577. margin-bottom: 20rpx;
  578. }
  579. .result-name {
  580. font-size: 32rpx;
  581. color: #333;
  582. display: block;
  583. margin-bottom: 10rpx;
  584. }
  585. .result-value {
  586. font-size: 36rpx;
  587. color: #FF6B6B;
  588. font-weight: bold;
  589. }
  590. }
  591. .points-change {
  592. margin-bottom: 40rpx;
  593. padding: 20rpx;
  594. background: #f5f5f5;
  595. border-radius: 10rpx;
  596. .change-text {
  597. font-size: 24rpx;
  598. color: #666;
  599. line-height: 1.4;
  600. }
  601. }
  602. .confirm-btn {
  603. background: linear-gradient(45deg, #FF6B6B, #FF4757);
  604. color: white;
  605. border: none;
  606. border-radius: 40rpx;
  607. padding: 20rpx 60rpx;
  608. font-size: 28rpx;
  609. font-weight: bold;
  610. }
  611. }
  612. }
  613. .spin-info {
  614. text-align: center;
  615. color: white;
  616. font-size: 28rpx;
  617. background: rgba(255, 255, 255, 0.2);
  618. padding: 20rpx 40rpx;
  619. border-radius: 40rpx;
  620. backdrop-filter: blur(10rpx);
  621. }
  622. </style>