newDrawCarousel.html 15 KB

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