drawCarousel.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>转盘抽奖</title>
  7. <link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css" />
  8. <style>
  9. .el-overlay {
  10. background-color:rgba(0, 0, 0, 0.8);
  11. }
  12. ::-webkit-scrollbar {
  13. width: 6px;
  14. height: 6px;
  15. }
  16. ::-webkit-scrollbar-track {
  17. width: 3px;
  18. background: rgba(#101F1C, 0.1);
  19. -webkit-border-radius: 2em;
  20. -moz-border-radius: 2em;
  21. border-radius: 2em;
  22. }
  23. ::-webkit-scrollbar-thumb {
  24. background-color: rgba(144,147,153,.5);
  25. background-clip: padding-box;
  26. min-height: 28px;
  27. -webkit-border-radius: 2em;
  28. -moz-border-radius: 2em;
  29. border-radius: 2em;
  30. transition: background-color .3s;
  31. cursor: pointer;
  32. }
  33. ::-webkit-scrollbar-thumb:hover {
  34. background-color: rgba(144,147,153,.3);
  35. }
  36. * {
  37. margin: 0;
  38. padding: 0;
  39. box-sizing: border-box;
  40. }
  41. #app {
  42. background: url('https://xiaoyou.dgtis.com/images/image/2023/09/12/5opx47olb1unf4po3vxt.png') no-repeat;
  43. /* background: url('https://xiaoyou.dgtis.com/images/image/2023/09/19/y5epqefrpodtfzyw0qd9.png') no-repeat; */
  44. background-size: 100% 100%;
  45. width: 640px;
  46. height: 680px;
  47. margin: 0 auto;
  48. margin-top: 20px;
  49. }
  50. .el-dialog__body{
  51. padding-top: 0px !important;
  52. }
  53. [v-cloak] {
  54. display: none;
  55. }
  56. .rules{
  57. cursor: pointer;
  58. position: absolute;
  59. width: 50px;
  60. height: 50px;
  61. background: #fff;
  62. border-radius: 50%;
  63. display: flex;
  64. justify-content: center;
  65. align-items: center;
  66. font-size: 14px;
  67. font-weight: 600;
  68. left: 8%;
  69. top: 22%;
  70. }
  71. .recordBtn {
  72. cursor: pointer;
  73. position: absolute;
  74. width: 50px;
  75. height: 50px;
  76. background: #fff;
  77. border-radius: 50%;
  78. display: flex;
  79. justify-content: center;
  80. align-items: center;
  81. font-size: 14px;
  82. font-weight: 600;
  83. right: 8%;
  84. top: 22%;
  85. }
  86. .recordBtn>img {
  87. width: 80px;
  88. height: auto;
  89. }
  90. .container {
  91. overflow: hidden;
  92. width: 500px;
  93. height: 500px;
  94. margin: 0 auto;
  95. position: relative;
  96. top: 22%;
  97. }
  98. .prize-list {
  99. width: 100%;
  100. height: 100%;
  101. border-radius: 50%;
  102. border: 10px solid #98d3fc;
  103. overflow: hidden;
  104. }
  105. .prize-item {
  106. /*border: 2px solid red;*/
  107. position: absolute;
  108. left: 0;
  109. right: 0;
  110. top: -10px;
  111. margin: auto;
  112. }
  113. .prize-item img {
  114. width: 35%;
  115. /* height: 25%; */
  116. margin: 35px auto 10px;
  117. display: block;
  118. }
  119. .prize-item p {
  120. color: #fff;
  121. font-size: 12px;
  122. text-align: center;
  123. line-height: 20px;
  124. }
  125. .btn {
  126. width: 80px;
  127. height: 110px;
  128. background: url('https://xiaoyou.dgtis.com/images/image/2023/09/12/xvtwunkntyc65fngc9pb.png') no-repeat center / 100% 100%;
  129. position: absolute;
  130. left: 0;
  131. right: 0;
  132. top: -5%;
  133. bottom: 0;
  134. margin: auto;
  135. cursor: pointer;
  136. }
  137. /* .btn::before {
  138. content: "";
  139. width: 41px;
  140. height: 39px;
  141. background: url('https://xiaoyou.dgtis.com/images/image/2023/08/15/2vac6uuqd3gpbl35l8e1.png') no-repeat center / 100% 100%;
  142. position: absolute;
  143. left: 0;
  144. right: 0;
  145. top: -30px;
  146. margin: auto;
  147. -webkit-transform: rotate(-5deg);
  148. transform: rotate(-5deg);
  149. } */
  150. .recordListBox{
  151. max-height: 200px;
  152. overflow: auto;
  153. }
  154. .title{
  155. font-size: 15px;
  156. font-weight: 600;
  157. }
  158. .recordItem{
  159. display: flex;
  160. justify-content: space-around;
  161. align-items: center;
  162. margin: 10px 0;
  163. }
  164. </style>
  165. </head>
  166. <body>
  167. <div id="app" v-cloak class="app">
  168. <div class="rules" @click="showRules">
  169. 抽奖<br>规则
  170. </div>
  171. <!-- <div class="recordBtn" @click="showRecordList">
  172. 获奖<br>名单
  173. </div> -->
  174. <!-- <div class="recordBtn" @click="showRecordList">
  175. <img src="https://xiaoyou.dgtis.com/images/image/2023/09/06/lt1vbg426idwq725ysdm.png" alt="抽奖记录">
  176. </div> -->
  177. <div class="container">
  178. <div class="prize-list" ref="prizeWrap" :style="bgColor">
  179. <div class="prize-item" v-for="(item, index) in prizeList" :style="prizeStyle(index)">
  180. <img :src="item.imgUrl" alt="">
  181. <p>{{ item.prizeName }}</p>
  182. </div>
  183. </div>
  184. <div class="btn" @click="start"></div>
  185. </div>
  186. <el-dialog
  187. v-model="dialogVisible"
  188. title="抽奖规则"
  189. width="80%"
  190. center
  191. >
  192. <div style="max-height: 200px;overflow: auto;" v-html="rule"></div>
  193. </el-dialog>
  194. <el-dialog
  195. v-model="dialogRecordList"
  196. title="获奖名单"
  197. width="80%"
  198. center
  199. >
  200. <div class="recordListBox">
  201. <div class="recordItem title">
  202. <div style="width: 30%;text-align: center;">姓名</div>
  203. <div style="width: 30%;text-align: center;">奖品</div>
  204. <div style="width: 40%;text-align: center;">获奖时间</div>
  205. </div>
  206. <el-empty description="暂无数据" v-if="recordList.length==0" :image-size="20"></el-empty>
  207. <div class="recordItem" v-for="(item, index) in recordList">
  208. <div style="width: 30%;text-align: center;">{{item.userName}}</div>
  209. <div style="width: 30%;text-align: center;">{{item.prizeName}}</div>
  210. <div style="width: 40%;text-align: center;">{{item.createTime}}</div>
  211. </div>
  212. </div>
  213. </el-dialog>
  214. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  215. <script src="https://unpkg.com/element-plus"></script>
  216. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/2.1.0/jquery.js"></script>
  217. <script>
  218. const {
  219. createApp,
  220. onBeforeMount,
  221. onMounted,
  222. onUnmounted,
  223. ref,
  224. reactive,
  225. toRefs,
  226. computed,
  227. nextTick
  228. } = Vue
  229. createApp({
  230. setup() {
  231. const state = reactive({
  232. recordList:[],
  233. dialogRecordList:false,
  234. dialogVisible:false,
  235. rule:'',
  236. token: '',
  237. actId: '',
  238. prizeList: [
  239. // {
  240. // prizeName: '手机',
  241. // imgUrl: 'https://xiaoyou.dgtis.com/images/image/2023/08/15/6b8jfssjhybfj572ybs3.jpg'
  242. // },
  243. // {
  244. // prizeName: '手表',
  245. // imgUrl: 'https://img1.baidu.com/it/u=2631716577,1296460670&fm=253&fmt=auto&app=120&f=JPEG'
  246. // },
  247. // {
  248. // prizeName: '苹果',
  249. // imgUrl: 'https://img2.baidu.com/it/u=2611478896,137965957&fm=253&fmt=auto&app=138&f=JPEG'
  250. // },
  251. // {
  252. // prizeName: '棒棒糖',
  253. // imgUrl: 'https://img2.baidu.com/it/u=576980037,1655121105&fm=253&fmt=auto&app=138&f=PNG'
  254. // },
  255. // {
  256. // prizeName: '娃娃',
  257. // imgUrl: 'https://img2.baidu.com/it/u=4075390137,3967712457&fm=253&fmt=auto&app=138&f=PNG'
  258. // },
  259. // {
  260. // prizeName: '木马',
  261. // imgUrl: 'https://img1.baidu.com/it/u=2434318933,2727681086&fm=253&fmt=auto&app=120&f=JPEG'
  262. // },
  263. // {
  264. // prizeName: '德芙',
  265. // imgUrl: 'https://img0.baidu.com/it/u=1378564582,2397555841&fm=253&fmt=auto&app=120&f=JPEG'
  266. // },
  267. // {
  268. // prizeName: '玫瑰',
  269. // imgUrl: 'https://img1.baidu.com/it/u=1125656938,422247900&fm=253&fmt=auto&app=120&f=JPEG'
  270. // },
  271. ], // 后台配置的奖品数据
  272. isRunning: false, // 是否正在抽奖
  273. baseRunAngle: 360 * 5, // 总共转动角度 至少5圈
  274. prizeId: 0, // 中奖id
  275. })
  276. const prizeWrap = ref(null)
  277. // 平均每个奖品角度
  278. const rotateAngle = computed(() => {
  279. const _degree = 360 / state.prizeList.length
  280. return _degree
  281. })
  282. // 要执行总角度数
  283. const totalRunAngle = computed(() => {
  284. return state.baseRunAngle + 360 - state.prizeId * rotateAngle.value - rotateAngle.value / 2
  285. })
  286. // 计算绘制转盘背景
  287. const bgColor = computed(() => {
  288. const _len = state.prizeList.length
  289. const colorList = ['#5352b3', '#363589']
  290. let colorVal = ''
  291. for (let i = 0; i < _len; i++) {
  292. colorVal += `${colorList[i % 2]} ${rotateAngle.value * i}deg ${rotateAngle.value * (i + 1)}deg,`
  293. }
  294. return `
  295. background: conic-gradient(${colorVal.slice(0, -1)});
  296. `
  297. })
  298. // 每个奖品布局
  299. const prizeStyle = computed(() => {
  300. const _degree = rotateAngle.value
  301. return (i) => {
  302. return `
  303. width: ${2 * 250 * Math.sin(_degree / 2 * Math.PI / 180)}px;
  304. height: 250px;
  305. transform: rotate(${_degree * i + _degree / 2}deg);
  306. transform-origin: 50% 100%;
  307. `
  308. }
  309. })
  310. onBeforeMount(()=>{
  311. debugger
  312. window.addEventListener('message', function (e) {
  313. console.log("接收值:", e.data)
  314. state.token = e.data.token;
  315. state.actId = e.data.actId;
  316. ElementPlus.ElLoading.service();
  317. getTurntableInfo();
  318. ElementPlus.ElLoading.service().close();
  319. })
  320. })
  321. onMounted(() => {
  322. prizeWrap.value.style = `${bgColor.value} transform: rotate(-${rotateAngle.value / 2}deg)`;
  323. })
  324. onUnmounted(() => {
  325. prizeWrap.value.removeEventListener('transitionend', stopRun)
  326. })
  327. // 获取大转盘信息
  328. async function getTurntableInfo() {
  329. debugger
  330. const postData = {
  331. actId: state.actId,
  332. };
  333. let res = await httpAjax('get', '/mall-prize/prize/info', postData)
  334. if (res.errno === 0) {
  335. console.log('获取大转盘信息',res.data);
  336. state.prizeList = res.data.pool;
  337. state.rule = res.data.content;
  338. } else {
  339. ElementPlus.ElMessage({
  340. showClose: true,
  341. message: res.errmsg,
  342. type: 'error',
  343. });
  344. }
  345. };
  346. // 大转盘抽奖按钮
  347. async function starDraw() {
  348. const postData = {
  349. actId: state.actId,
  350. };
  351. let res = await httpAjax('post', '/mall-prize/prize', getFormData(postData));
  352. if (res.errno === 0) {
  353. console.log('抽奖',res.data);
  354. state.prizeId = res.data.index;
  355. startRun();
  356. } else {
  357. state.isRunning = false;
  358. ElementPlus.ElMessage({
  359. showClose: true,
  360. message: res.errmsg,
  361. type: 'error',
  362. });
  363. }
  364. };
  365. //抽奖记录
  366. // async function getRecordList() {
  367. // const postData = {
  368. // actId: state.actId,
  369. // };
  370. // let res = await httpAjax('get', '/mall-prize/prize/user/list', postData);
  371. // if (res.errno === 0) {
  372. // console.log('抽奖记录',res.data);
  373. // state.recordList = res.data;
  374. // } else {
  375. // state.isRunning = false;
  376. // ElementPlus.ElMessage({
  377. // showClose: true,
  378. // message: res.errmsg,
  379. // type: 'error',
  380. // });
  381. // }
  382. // };
  383. //获奖名单
  384. async function getRecordList() {
  385. const postData = {
  386. actId: state.actId,
  387. };
  388. let res = await httpAjax('get', '/mall-prize/prize/winners', postData);
  389. if (res.errno === 0) {
  390. state.recordList = res.data;
  391. } else {
  392. state.isRunning = false;
  393. ElementPlus.ElMessage({
  394. showClose: true,
  395. message: res.errmsg,
  396. type: 'error',
  397. });
  398. }
  399. };
  400. // 简单封装ajax
  401. const httpAjax = (type, url, data) => {
  402. ajaxHeaders = {
  403. "Authorization": state.token
  404. }
  405. return new Promise((resolve, reject) => {
  406. $.ajax({
  407. type: type,
  408. // url: 'http://192.168.100.208:9083/admin' + url, //本地
  409. // url: "http://47.103.79.143:9085/admin" + url,//测试环境
  410. url:"https://xiaoyou.dgtis.com/admin"+ url,//正式环境
  411. data: data,
  412. contentType: type === 'post' ? false : true,
  413. processData: type === 'post' ? false : true,
  414. headers: ajaxHeaders,
  415. success: function (res) {
  416. if (res.errno == 503) {
  417. window.location.href = 'https://dgt.dgtis.com/oneportal/login'
  418. } else {
  419. resolve(res);
  420. }
  421. },
  422. error: function (error) {
  423. reject(error);
  424. ElementPlus.ElMessage({
  425. showClose: true,
  426. message: '网络错误,请稍后再试!',
  427. type: 'error',
  428. });
  429. }
  430. })
  431. })
  432. }
  433. // 处理formData数据格式
  434. const getFormData = (datas) => {
  435. let formData = new FormData();
  436. for (let key in datas) {
  437. formData.append(key, datas[key]);
  438. }
  439. return formData;
  440. }
  441. // 获取随机数
  442. const getRandomNum = () => {
  443. const num = Math.floor(Math.random() * state.prizeList.length)
  444. return num
  445. }
  446. const showRules = () =>{
  447. state.dialogVisible = true;
  448. }
  449. const showRecordList = () =>{
  450. getRecordList();
  451. state.dialogRecordList = true;
  452. }
  453. const start = () => {
  454. debugger
  455. // ElementPlus.ElMessage({
  456. // showClose: true,
  457. // message: '已无抽奖次数!',
  458. // type: 'warning',
  459. // })
  460. // return
  461. if (!state.isRunning) {
  462. state.isRunning = true;
  463. starDraw();
  464. // console.log('开始抽奖,后台请求中奖奖品')
  465. // // 请求返回的奖品编号 这里使用随机数
  466. // const prizeId = getRandomNum()
  467. // console.log('中奖ID>>>', prizeId, state.prizeList[prizeId])
  468. // state.prizeId = prizeId
  469. }
  470. }
  471. const startRun = () => {
  472. console.log(state.isRunning, totalRunAngle.value)
  473. // 设置动效
  474. prizeWrap.value.style = `
  475. ${bgColor.value}
  476. transform: rotate(${totalRunAngle.value}deg);
  477. transition: all 4s ease;
  478. `
  479. // 监听transition动效停止事件
  480. prizeWrap.value.addEventListener('transitionend', stopRun)
  481. }
  482. const stopRun = (e) => {
  483. console.log(e)
  484. state.isRunning = false
  485. prizeWrap.value.style = `
  486. ${bgColor.value}
  487. transform: rotate(${totalRunAngle.value - state.baseRunAngle}deg);
  488. `
  489. if(state.prizeList[state.prizeId].prizeType!='40'){
  490. ElementPlus.ElMessageBox.alert("恭喜,您中了" + state.prizeList[state.prizeId].prizeName + "," + "可在“个人中心”我的礼品券中查看,在礼品兑换中兑换相应物品。", {
  491. showClose:false,
  492. center:true,
  493. confirmButtonText: '确定',
  494. })
  495. // ElementPlus.ElMessage({
  496. // offset:window.screen.height / 2,
  497. // duration:3000,
  498. // showClose: true,
  499. // message: "恭喜,您中了" + state.prizeList[state.prizeId].prizeName + "," + "可在“个人中心”我的礼品券中查看,在礼品兑换中兑换响应物品。",
  500. // type: 'success',
  501. // })
  502. }else{
  503. ElementPlus.ElMessageBox.alert("很遗憾,您未中奖。", {
  504. showClose:false,
  505. center:true,
  506. confirmButtonText: '确认',
  507. })
  508. // ElementPlus.ElMessage({
  509. // offset:window.screen.height / 4,
  510. // duration:10000,
  511. // showClose: true,
  512. // message: "很遗憾,您未中奖。",
  513. // type: 'error',
  514. // })
  515. }
  516. }
  517. return {
  518. ...toRefs(state),
  519. bgColor,
  520. prizeStyle,
  521. prizeWrap,
  522. start,
  523. showRules,
  524. showRecordList
  525. }
  526. }
  527. }).use(ElementPlus).mount('#app')
  528. </script>
  529. </body>
  530. </html>