question.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>无限潜能</title>
  8. <link rel="stylesheet" href="../css/question.css">
  9. <script src="../utils/size.js"></script>
  10. </head>
  11. <body>
  12. <div class="content">
  13. <div class="question-container">
  14. <img class="container-bg" src="../image/question/questionbg.jpg" alt="">
  15. <div class="title">
  16. <img src="../image/question/title.png" alt="title">
  17. </div>
  18. <div class="subject">
  19. <img class="inset" src="../image/question/inset.png" alt="inset">
  20. <img class="bottom" src="../image/question/bottom.png" alt="bottom">
  21. <div class="question-box"></div>
  22. <!--
  23. <div class="question" >
  24. <div class="audio">
  25. <audio src="http://www.ytmp3.cn/down/44537.mp3" autoplay="autoplay" preload="auto">
  26. 您的浏览器不支持 audio 标签。
  27. </audio>
  28. <img class="audiobtn" src="../image/question/audiobtn.png" alt="audiobtn">
  29. <span>甜甜圈从左数是第几个?</span>
  30. </div>
  31. <div class="question-photo"></div>
  32. <ul class="option">
  33. <li class="select">第一个</li>
  34. <li>第二个</li>
  35. <li>第三个</li>
  36. </ul>
  37. <div class="order">
  38. <span>1</span><span>/5</span>
  39. </div>
  40. </div>
  41. -->
  42. </div>
  43. </div>
  44. <div class="popup">
  45. <div class="popup-content">
  46. <canvas id="myCanvas" width="661px" height="812px">
  47. 您的浏览器不支持canvas标签。
  48. </canvas>
  49. <img class="bg" src="" alt="" crossorigin="anonymous">
  50. <div class="baocun">
  51. <p>长按图片保存并分享朋友圈</p>
  52. <p>作为领奖凭证</p>
  53. </div>
  54. <a href="./awards.html" class="lingqu"></a>
  55. <!--
  56. <div class="lingqu">
  57. <img src="../image/question/lingqu.png" alt="">
  58. </div>
  59. -->
  60. <!--
  61. <img class="head" src="../image/question/头像.jpg" alt="">
  62. -->
  63. </div>
  64. </div>
  65. </div>
  66. </body>
  67. <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  68. <script>
  69. window.onload=function(){
  70. if(localStorage.getItem('userData') == 'undefined') {
  71. alert('请重新扫码登陆')
  72. window.location.href = '../index.html'
  73. return false;
  74. }
  75. var user = JSON.parse(localStorage.getItem('userData'));
  76. var uid = user.uid;
  77. alert(uid);
  78. var avatar = user.avatar ? user.avatar : '';
  79. var name = user.wechatName ? user.wechatName : '小朋友';
  80. // var uid = 56;
  81. //获取Canvas对象(画布)
  82. var canvas = document.getElementById("myCanvas");
  83. var data = {
  84. "activityId": "2",
  85. "answer": "",
  86. "questionId": "",
  87. "requestId": ""
  88. }
  89. var ind = 0;
  90. function recursion() {
  91. ajaxData("https://activity.lingjiao.cn/activity/activity/xx", uid, data, function(result){
  92. if(result.code != 200) {
  93. alert('题目已经答完了')
  94. return false;
  95. }
  96. console.log(result)
  97. var page = result.data.currentNum;
  98. var length = result.data.questionNum;
  99. if( page >= length && result.data.isEnd === 0) {
  100. console.log('奖品', result.data.prize)
  101. localStorage.setItem('prize', result.data.prize);
  102. var prizeName = result.data.prizeName;
  103. $('.popup').show();
  104. //简单地检测当前浏览器是否支持Canvas对象,以免在一些不支持html5的浏览器中提示语法错误
  105. if(canvas.getContext){
  106. //获取对应的CanvasRenderingContext2D对象(画笔)
  107. var ctx = canvas.getContext("2d");
  108. //回调地狱,可优化,暂时这样写
  109. ctx.fillStyle = '#fff';
  110. ctx.fillRect(0,0,661,812);
  111. canvasImg(ctx, '../image/question/xx_02.png', 0, 0, '661', '812', function() {
  112. ctx.fillStyle = '#000';
  113. ctx.font = "bold 18px Arial";
  114. alert(name);
  115. alert(prizeName)
  116. ctx.fillText(name,150,206);
  117. ctx.fillText(prizeName + "等奖",200,258);
  118. })
  119. ctx.restore();
  120. }
  121. // 0.5s后生成图片
  122. setTimeout(function() {
  123. var dataURL = canvas.toDataURL("image/jpeg",1.0);
  124. $('.bg').attr('src', dataURL)
  125. }, 500)
  126. return false;
  127. }
  128. data.questionId = result.data.question.id;
  129. data.requestId = result.data.requestId;
  130. // 一开始创建div 没内容
  131. $("<div></div>", {
  132. "class": "question", // 设置样式类
  133. html: '<div class="order">' +
  134. '<span class="ind">1</span><span>/'+ length +'</span>' +
  135. '</div>', // 设置内容
  136. }).appendTo($('.question-box')); // 追加到body标签
  137. // 初次渲染第一题
  138. renderDiv(ind, result.data.question, page)
  139. $('.question').eq(ind).children('ul').find('li').on('click', function() {
  140. data.answer = $(this).data('answer');
  141. ind++;
  142. $(this).addClass('select').siblings().removeClass('select');
  143. var that = this;
  144. recursion();
  145. setTimeout(function () {
  146. $(that).parents('.question').slideUp('slow', function () {
  147. $('.question').eq(ind).find('.ind').text(page + 1);
  148. });
  149. }, 100)
  150. })
  151. })
  152. }
  153. recursion();
  154. // ajax请求
  155. function ajaxData (url, uid, data, success) {
  156. $.ajax({
  157. url: url,
  158. type: 'POST',
  159. headers: {'uid': uid},
  160. contentType:'application/json;charset=utf-8',
  161. data: JSON.stringify(data),
  162. success: success
  163. });
  164. }
  165. // 渲染元素
  166. function renderDiv (ind, datas, page) {
  167. var renders = '<div class="audio">' +
  168. '<div>'+ datas.content +'</div>' +
  169. '</div>' +
  170. '<div class="question-photo">'+
  171. '<img src="'+ datas.imgUrl +'" alt="">'+
  172. '</div>' +
  173. '<ul class="option">' +
  174. '<li data-answer="A">'+ datas.optionA +'</li>' +
  175. '<li data-answer="B">'+ datas.optionB +'</li>' +
  176. '<li data-answer="C">'+ datas.optionC +'</li>' +
  177. '</ul>'
  178. $('.question').eq(ind).prepend(renders);
  179. $('.question').eq(ind).find('.ind').text(page);
  180. }
  181. function canvasImg (ctx, url, x, y, w, h, success) {
  182. //创建新的图片对象
  183. var img = new Image();
  184. img.setAttribute('crossOrigin', 'anonymous');
  185. //指定图片的URL
  186. img.src = url;
  187. //浏览器加载图片完毕后再绘制图片
  188. img.onload = function(){
  189. //以Canvas画布上的坐标(10,10)为起始点,绘制图像
  190. ctx.drawImage(img, x, y, w, h);
  191. success();
  192. };
  193. }
  194. // 封装了一个简单截取头像的方法
  195. function circleImg(ctx, url, x, y, r) {
  196. //创建新的图片对象
  197. var img = new Image();
  198. img.setAttribute('crossOrigin', 'anonymous');
  199. //指定图片的URL
  200. img.src = url;
  201. ctx.save();
  202. var d =2 * r;
  203. var cx = x + r;
  204. var cy = y + r;
  205. img.onload = function(){
  206. ctx.arc(cx, cy, r, 0, 2 * Math.PI);
  207. ctx.clip();
  208. //以Canvas画布上的坐标(10,10)为起始点,绘制图像
  209. ctx.drawImage(img, x, y, d, d);
  210. };
  211. }
  212. }
  213. </script>
  214. </html>