drawCarousel.html 14 KB

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