drawCarousel.html 15 KB

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