demo.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div class="luckDrawBox">
  3. <h4>基本用法</h4>
  4. <div>
  5. <nut-cell>
  6. <span slot="title">剩余抽奖次数:{{ num }}</span>
  7. </nut-cell>
  8. </div>
  9. <nut-luckdraw
  10. class="drawTable"
  11. ref="luckDrawPrize"
  12. :luck-width="luckWidth"
  13. :luck-height="luckheight"
  14. :prize-list="prizeList"
  15. :turns-number="turnsNumber"
  16. :turns-time="turnsTime"
  17. :prize-index="prizeIndex"
  18. :style-opt="styleOpt"
  19. @end-turns="endTurns"
  20. >
  21. <template slot="item" slot-scope="scope">
  22. <div class="drawTable-name">{{ scope.item.prizeName }}</div>
  23. <div class="drawTable-img">
  24. <img :src="scope.item.prizeImg">
  25. </div>
  26. </template>
  27. </nut-luckdraw>
  28. <div @click="startTurns" class="pointer" :style="pointerStyle"></div>
  29. </div>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. // 转盘大小
  36. luckWidth: '300px',
  37. luckheight: '300px',
  38. // 转盘指针图片样式
  39. pointerStyle: {
  40. width: '80px',
  41. height: '80px',
  42. backgroundImage: 'url("https://img11.360buyimg.com/imagetools/jfs/t1/89512/11/15244/137408/5e6f15edEf57fa3ff/cb57747119b3bf89.png")',
  43. backgroundSize: 'contain',
  44. backgroundRepeat: 'no-repeat',
  45. },
  46. // 转盘上要展示的奖品数据
  47. prizeList: [
  48. {
  49. id: 'xiaomi',
  50. prizeName: '小米手机',
  51. prizeImg: 'https://img14.360buyimg.com/imagetools/jfs/t1/104165/34/15186/96522/5e6f1435E46bc0cb0/d4e878a15bfd9362.png',
  52. },
  53. {
  54. id: 'blue',
  55. prizeName: '蓝牙耳机',
  56. prizeImg: 'https://img13.360buyimg.com/imagetools/jfs/t1/91864/11/15108/139003/5e6f146dE1c7b511d/1ddc5aa6e502060a.jpg',
  57. },
  58. {
  59. id: 'apple',
  60. prizeName: 'apple watch',
  61. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/105385/19/15140/111093/5e6f1506E48bd0dfb/829a98a8cdb4c27f.png',
  62. },
  63. {
  64. id: 'fruit',
  65. prizeName: '迪士尼苹果',
  66. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/108308/11/8890/237603/5e6f157eE489cccf1/26e0437cfd93b9c8.png',
  67. },
  68. {
  69. id: 'fish',
  70. prizeName: '海鲜套餐',
  71. prizeImg: 'https://img14.360buyimg.com/imagetools/jfs/t1/90507/38/15165/448364/5e6f15b4E5df0c718/4bd4c3d375eec312.png',
  72. },
  73. {
  74. id: 'thanks',
  75. prizeName: '谢谢参与',
  76. prizeImg: 'https://img11.360buyimg.com/imagetools/jfs/t1/96116/38/15085/5181/5e6f15d1E48e31d30/71353b61dff705d4.png',
  77. }
  78. ],
  79. // 转动圈数
  80. turnsNumber: 5,
  81. // 转动需要持续的时间(秒)
  82. turnsTime: 5,
  83. // 转盘样式的选项
  84. styleOpt: {
  85. // 转盘中每一块扇形的背景色,根据奖品的index来取每一块的对应颜色
  86. prizeBgColors: ['rgb(255, 231, 149)','rgb(255, 247, 223)','rgb(255, 231, 149)','rgb(255, 247, 223)','rgb(255, 231, 149)','rgb(255, 247, 223)'],
  87. // 每一个扇形的外边框颜色
  88. borderColor: '#ff9800',
  89. },
  90. // 中奖的奖品的index(此数据可根据后台返回的值重新赋值)
  91. prizeIndex: -1,
  92. // 用来锁定转盘,避免同时多次点击转动
  93. lock: false,
  94. // 剩余抽奖次数
  95. num: 5,
  96. }
  97. },
  98. methods: {
  99. startTurns() {
  100. // 如果还不可以转动
  101. if (!this.canBeRotated()) {
  102. return false;
  103. }
  104. // 开始转动
  105. // 先上锁
  106. this.lock = true;
  107. // 设置在哪里停下,应该与后台交互,这里随机抽取0~5 ,这里应该是后台返回的中奖信息,现在是测试
  108. const index = Math.floor(Math.random() * this.prizeList.length);
  109. // 成功后次数减少一次
  110. this.num--;
  111. this.prizeIndex = index;
  112. // 告诉子组件,开始转动了
  113. this.$refs.luckDrawPrize.rotate(index);
  114. },
  115. // 已经转动完转盘触发的函数
  116. endTurns() {
  117. console.log(123)
  118. // 提示中奖
  119. this.$dialog({
  120. content: `恭喜中奖!!!${this.prizeList[this.prizeIndex].prizeName}`,
  121. noCloseBtn: false,
  122. noOkBtn: true,
  123. cancelBtnTxt: "我知道了"
  124. });
  125. // 解锁
  126. this.lock = false;
  127. },
  128. // 判断是否可以转动
  129. canBeRotated() {
  130. if (this.num <= 0) {
  131. alert('已经没有次数了,继续加油赚积分吧!');
  132. this.$dialog({
  133. content: `已经没有次数了,继续加油赚积分吧!`,
  134. noCloseBtn: false,
  135. noOkBtn: true,
  136. cancelBtnTxt: "我知道了"
  137. });
  138. return false;
  139. }
  140. if (this.lock) {
  141. return false;
  142. }
  143. return true;
  144. },
  145. }
  146. }
  147. </script>
  148. <style lang="scss">
  149. .luckDrawBox {
  150. h3 {
  151. width: 100%;
  152. text-align: center;
  153. font-size: 24px;
  154. }
  155. // .drawTable {
  156. // position: absolute;
  157. // left: 50%;
  158. // top: 50%;
  159. // transform: translate(-50%, -50%);
  160. // }
  161. // .drawTable-name {
  162. // position: absolute;
  163. // left: 10px;
  164. // top: 20px;
  165. // width: calc(100% - 20px);
  166. // font-size: 12px;
  167. // text-align: center;
  168. // color: #ff5722;
  169. // }
  170. // .drawTable-img {
  171. // position: absolute;
  172. // /*要居中就要50% - 宽度 / 2*/
  173. // left: calc(50% - 30px / 2);
  174. // top: 60px;
  175. // width: 30px;
  176. // height: 30px;
  177. // img {
  178. // display: inline-block;
  179. // width: 100%;
  180. // height: 100%;
  181. // }
  182. // }
  183. // .pointer {
  184. // position: absolute;
  185. // // left: calc(50% - 35px);
  186. // // top: calc(50% - 40px);
  187. // left: 50%;
  188. // top: 50%;
  189. // transform: translate(-43.75%, -50%);
  190. // // background-image: url("https://img11.360buyimg.com/imagetools/jfs/t1/106989/15/11126/137350/5e265414E8ee514bc/3456bd0d3a0454da.png");
  191. // // background-size: contain;
  192. // // background-repeat: no-repeat;
  193. // }
  194. }
  195. </style>