rotarytable.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view class="almost-lottery">
  3. <view class="almost-lottery__wheel">
  4. <almost-lottery :lottery-size="lotteryConfig.lotterySize" :action-size="lotteryConfig.actionSize"
  5. :ring-count="10" :duration="3" :img-circled="true" :canvasCached="true" :prize-list="prizeList"
  6. :prize-index="prizeIndex" @reset-index="prizeIndex = -1" @draw-start="handleDrawStart"
  7. @draw-end="handleDrawEnd" @finish="handleDrawFinish" v-if="prizeList.length" />
  8. <view class="almost-lottery__count">
  9. <!--<text class="text">剩余免费抽奖 3次</text>-->
  10. </view>
  11. </view>
  12. <!-- rule -->
  13. <view class="almost-lottery__rule">
  14. <view class="rule-head">
  15. <view class="line"></view>
  16. <text class="title">活动规则</text>
  17. <view class="line"></view>
  18. </view>
  19. <view class="rule-body">
  20. <view class="item">
  21. <view class="text">
  22. <text>{{config.content}}</text>
  23. </view>
  24. </view>
  25. <template v-if="isApple">
  26. <view class="item">
  27. <view class="number">1</view>
  28. <view class="text">本次活动由发起,与Apple.Inc无关。</view>
  29. </view>
  30. <view class="item">
  31. <view class="number">2</view>
  32. <view class="text">苹果公司不是本活动的赞助商且没有以任何形式参与活动。</view>
  33. </view>
  34. <view class="item">
  35. <view class="number">3</view>
  36. <view class="text">本活动仅限17岁以上用户参加。</view>
  37. </view>
  38. </template>
  39. <template v-else>
  40. <view class="item">
  41. <view class="text">本活动仅限17岁以上用户参加。</view>
  42. </view>
  43. </template>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import AlmostLottery from '@/uni_modules/almost-lottery/components/almost-lottery/almost-lottery.vue'
  50. import {
  51. clearCacheFile,
  52. clearStore
  53. } from '@/uni_modules/almost-lottery/utils/almost-utils.js'
  54. export default {
  55. name: 'Home',
  56. components: {
  57. AlmostLottery
  58. },
  59. data() {
  60. return {
  61. // 开启调试模式
  62. isDev: true,
  63. // 以下是转盘配置相关数据
  64. lotteryConfig: {
  65. // 抽奖转盘的整体尺寸,单位rpx
  66. lotterySize: 600,
  67. // 抽奖按钮的尺寸,单位rpx
  68. actionSize: 200
  69. },
  70. config: [],
  71. // 以下是奖品配置数据
  72. // 奖品数据
  73. prizeList: [],
  74. // 中奖下标
  75. prizeIndex: -1,
  76. // 是否正在抽奖中,避免重复触发
  77. prizeing: false,
  78. }
  79. },
  80. computed: {
  81. isApple() {
  82. return uni.getSystemInfoSync().platform === 'ios'
  83. }
  84. },
  85. methods: {
  86. // 获取奖品列表
  87. async getPrizeList() {
  88. uni.showLoading({
  89. title: '奖品准备中...'
  90. })
  91. // 等待接口返回的数据进一步处理
  92. let res = await this.requestApiGetPrizeList()
  93. console.log('获取奖品列表', res)
  94. if (res.ok) {
  95. let data = res.data
  96. if (data.length) {
  97. this.config = res.config;
  98. this.prizeList = data;
  99. console.log('已获取到奖品列表数据,开始绘制抽奖转盘')
  100. // 计算开始绘制的时间
  101. if (console.time) {
  102. console.time('绘制转盘用时')
  103. }
  104. }
  105. } else {
  106. uni.hideLoading()
  107. uni.showToast({
  108. title: '获取奖品失败',
  109. mask: true,
  110. icon: 'none'
  111. })
  112. }
  113. },
  114. // 模拟请求 获取奖品列表 接口,
  115. // 注意这里返回的是一个 Promise
  116. requestApiGetPrizeList() {
  117. return new Promise((resolve, reject) => {
  118. let requestTimer = setTimeout(() => {
  119. clearTimeout(requestTimer)
  120. requestTimer = null
  121. let _this = this;
  122. _this.$request.get('Rotarytableprize.index', {
  123. samkey: (new Date()).valueOf()
  124. }).then(res => {
  125. if (res.errno == 0) {
  126. // prizeWeight 中奖概率,数值越大中奖概率越高,权重一样时随机中奖
  127. resolve({
  128. ok: true,
  129. config: res.data.config,
  130. data: res.data.prizelist
  131. })
  132. }
  133. });
  134. }, 200)
  135. })
  136. },
  137. // 本次抽奖开始
  138. handleDrawStart() {
  139. console.log('触发抽奖按钮')
  140. if (this.prizeing) return
  141. this.prizeing = true
  142. let _this = this;
  143. _this.$request.get('Rotarytableprize.drawstart', {
  144. samkey: (new Date()).valueOf()
  145. }).then(res => {
  146. if (res.errno == 0) {
  147. this.tryLotteryDraw();
  148. }else{
  149. this.prizeing = false;
  150. }
  151. });
  152. },
  153. // 尝试发起抽奖
  154. tryLotteryDraw() {
  155. console.log('旋转开始,获取中奖下标......')
  156. this.remoteGetPrizeIndex()
  157. },
  158. // 远程请求接口获取中奖下标
  159. // 大哥,这里只是模拟,别告诉我你不会对接自己的接口
  160. remoteGetPrizeIndex() {
  161. console.warn('###当前处于模拟的请求接口,并返回了中奖信息###')
  162. // 模拟请求接口获取中奖信息
  163. let stoTimer = setTimeout(() => {
  164. stoTimer = null
  165. let list = [...this.prizeList]
  166. let _this = this;
  167. _this.$request.get('Rotarytableprize.getprizeid', {
  168. samkey: (new Date()).valueOf()
  169. }).then(res => {
  170. if (res.errno == 0) {
  171. // 这里随机产生的 prizeId 是模拟后端返回的 prizeId
  172. let prizeId = res.data.prizeId;
  173. // 拿到后端返回的 prizeId 后,开始循环比对得出那个中奖的数据
  174. for (let i = 0; i < list.length; i++) {
  175. let item = list[i]
  176. if (item.prizeId === prizeId) {
  177. // 中奖下标
  178. this.prizeIndex = i
  179. break
  180. }
  181. }
  182. //console.log(this.prizeIndex)
  183. //console.log(this.prizeList)
  184. console.log('本次抽中奖品 =>', this.prizeList[this.prizeIndex].prizeName)
  185. }
  186. });
  187. }, 200)
  188. },
  189. // 本次抽奖结束
  190. handleDrawEnd() {
  191. console.log('旋转结束,执行拿到结果后到逻辑')
  192. // 旋转结束后,开始处理拿到结果后的逻辑
  193. let prizeName = this.prizeList[this.prizeIndex].prizeName
  194. let tipContent = ''
  195. if (prizeName === '谢谢参与') {
  196. tipContent = '很遗憾,没有中奖,请再接再厉!'
  197. } else {
  198. tipContent = `恭喜您,获得 ${prizeName} !`
  199. }
  200. uni.showModal({
  201. content: tipContent,
  202. showCancel: false,
  203. complete: () => {
  204. this.prizeing = false
  205. }
  206. })
  207. },
  208. // 抽奖转盘绘制完成
  209. handleDrawFinish(res) {
  210. console.log('抽奖转盘绘制完成', res)
  211. if (res.ok) {
  212. // 计算结束绘制的时间
  213. if (console.timeEnd) {
  214. console.timeEnd('绘制转盘用时')
  215. }
  216. }
  217. let stoTimer = setTimeout(() => {
  218. stoTimer = null
  219. uni.hideLoading()
  220. uni.showToast({
  221. title: res.msg,
  222. mask: true,
  223. icon: 'none'
  224. })
  225. }, 50)
  226. }
  227. },
  228. onLoad() {
  229. this.prizeList = []
  230. this.getPrizeList()
  231. },
  232. /**
  233. * 页面相关事件处理函数--监听用户下拉动作
  234. */
  235. onPullDownRefresh: function() {
  236. setTimeout(() => {
  237. uni.stopPullDownRefresh()
  238. }, 200);
  239. },
  240. onUnload() {
  241. uni.hideLoading()
  242. }
  243. }
  244. </script>
  245. <style lang="scss" scoped>
  246. .almost-lottery {
  247. flex: 1;
  248. background-color: #FF893F;
  249. padding-top: 100rpx;
  250. padding-bottom: 380rpx;
  251. }
  252. .almost-lottery__wheel {
  253. text-align: center;
  254. .almost-lottery__count {
  255. display: flex;
  256. flex-direction: column;
  257. justify-content: center;
  258. align-items: center;
  259. text-align: center;
  260. padding: 40rpx 0;
  261. }
  262. .text {
  263. color: #FFFFFF;
  264. font-size: 24rpx;
  265. }
  266. }
  267. .almost-lottery__rule {
  268. padding: 0 28rpx;
  269. color: #FFF8CB;
  270. .rule-head {
  271. display: flex;
  272. justify-content: space-around;
  273. align-items: center;
  274. margin: 40rpx 0;
  275. .line {
  276. flex: 1;
  277. height: 1px;
  278. background-color: #FFF3A5;
  279. }
  280. .title {
  281. width: 280rpx;
  282. color: #F63857;
  283. line-height: 70rpx;
  284. text-align: center;
  285. margin: 0 20rpx;
  286. border-radius: 8rpx;
  287. background-image: linear-gradient(0deg, rgba(255, 242, 158, 1), rgba(255, 244, 168, 1));
  288. }
  289. }
  290. .rule-body {
  291. color: #FFF8CB;
  292. font-size: 24rpx;
  293. padding: 10rpx 0 40rpx;
  294. .item {
  295. display: flex;
  296. margin-bottom: 10rpx;
  297. }
  298. .number {
  299. position: relative;
  300. top: 4rpx;
  301. width: 28rpx;
  302. height: 28rpx;
  303. line-height: 28rpx;
  304. text-align: center;
  305. color: #F63857;
  306. background: #FFF8CB;
  307. border-radius: 50%;
  308. margin-right: 10rpx;
  309. }
  310. .text {
  311. flex: 1;
  312. }
  313. .item-rule .text {
  314. display: flex;
  315. flex-direction: column;
  316. }
  317. }
  318. }
  319. .almost-lottery__action-dev {
  320. display: flex;
  321. flex-direction: row;
  322. justify-content: center;
  323. align-items: center;
  324. width: 400rpx;
  325. height: 80rpx;
  326. border-radius: 10rpx;
  327. text-align: center;
  328. background-color: red;
  329. margin: 0 auto 40rpx;
  330. .text {
  331. color: #FFFFFF;
  332. font-size: 28rpx;
  333. }
  334. }
  335. </style>