PageRenderTime 25ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tpl/static/luckyFruit/wap/tigerslot.js

https://gitlab.com/colin.luo/shbs
JavaScript | 458 lines | 381 code | 53 blank | 24 comment | 57 complexity | 17e6d9e67d9277bd1e23c324992b1391 MD5 | raw file
  1. (function () {
  2. var lastTime = 0;
  3. var vendors = ['ms', 'moz', 'webkit', 'o'];
  4. for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  5. window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  6. window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
  7. || window[vendors[x] + 'CancelRequestAnimationFrame'];
  8. }
  9. if (!window.requestAnimationFrame)
  10. window.requestAnimationFrame = function (callback, element) {
  11. var currTime = new Date().getTime();
  12. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  13. var id = window.setTimeout(function () { callback(currTime + timeToCall); },
  14. timeToCall);
  15. lastTime = currTime + timeToCall;
  16. return id;
  17. };
  18. if (!window.cancelAnimationFrame)
  19. window.cancelAnimationFrame = function (id) {
  20. clearTimeout(id);
  21. };
  22. }());
  23. (function () {
  24. window.GameTimer = function (fn, timeout) {
  25. this.__fn = fn;
  26. this.__timeout = timeout;
  27. this.__running = false;
  28. this.__lastTime = Date.now();
  29. this.__stopcallback = null;
  30. };
  31. window.GameTimer.prototype.__runer = function () {
  32. if (Date.now() - this.__lastTime >= this.__timeout) {
  33. this.__lastTime = Date.now();
  34. this.__fn.call(this);
  35. }
  36. if (this.__running) {
  37. window.requestAnimationFrame(this.__runer.bind(this));
  38. }
  39. else {
  40. if (typeof this.__stopcallback === 'function') {
  41. window.setTimeout(this.__stopcallback,100);
  42. }
  43. }
  44. };
  45. window.GameTimer.prototype.start = function () {
  46. this.__running = true;
  47. this.__runer();
  48. };
  49. window.GameTimer.prototype.stop = function (callback) {
  50. this.__running = false;
  51. this.__stopcallback = callback;
  52. };
  53. })();
  54. $(function () {
  55. // var url_rndprize = '/index.php?g=Wap&m=LuckyFruit&a=getajax';
  56. var url_getprize = '兑奖地址';
  57. var itemPositions = [
  58. 0, //苹果
  59. 100,//芒果
  60. 200,//布林
  61. 300,//香蕉
  62. 400,//草莓
  63. 500,//梨
  64. 600,//桔子
  65. 700,//青苹果
  66. 800//樱桃
  67. ];
  68. //游戏开始
  69. var gameStart = function () {
  70. lightFlicker.stop();
  71. lightRandom.stop();
  72. lightCycle.start();
  73. //
  74. var marketing_id = $('.tigerslot').attr('activity_id');
  75. var token = $('.tigerslot').attr('data-token');
  76. var wechat_id = $('.tigerslot').attr('wechat_id');
  77. var rid = $('.tigerslot').attr('rid');
  78. $.post(url_rndprize, {
  79. id: marketing_id,
  80. token: token,
  81. wechat_id:wechat_id,
  82. rid:rid
  83. }, function (result) {
  84. if(result.error){
  85. if(result.msg=='您已经中过了'){
  86. $("#result").slideToggle(500);
  87. }else{
  88. alert(result.msg);
  89. }
  90. //$("#result").slideDown(500);
  91. return;
  92. }
  93. if(result.success){
  94. boxCycle.start(result.data);
  95. }
  96. },'json');
  97. };
  98. //游戏结束
  99. var gameOver = function (resultData) {
  100. lightFlicker.start();
  101. lightRandom.stop();
  102. lightCycle.stop();
  103. //
  104. if(resultData.type == 0){
  105. alert(resultData.prize_type);
  106. $('.machine .gamebutton').removeClass('disabled');
  107. }else{
  108. $('.machine .gamebutton').addClass('disabled');
  109. $("#sncode").text(resultData.sn);
  110. $("#prize").text(resultData.prize);
  111. var msg ="<p>恭喜!尊敬的<font color=red>"+resultData.wechaName+"</font>请您保持手机通畅!<br/>你的领奖序号:<font color=red>"+resultData.sn+"</font></p>";
  112. alert(msg);
  113. // $("#result").slideDown(500);
  114. }
  115. var rest_chance = parseInt($('#rest_chance').text()) - 1;
  116. rest_chance = rest_chance<0 ? 0 : rest_chance;
  117. $('#rest_chance').text(rest_chance);
  118. };
  119. //准备兑奖
  120. var getprize = function (listid, prizeid, code) {
  121. var tel=prompt('获奖纪录id:' + listid + ' ,奖品ID:' + prizeid + ' ,兑奖编码:' + code +'\n请输入手机号码兑奖:');
  122. if ($.trim(tel)) {
  123. /*
  124. $.post(url_getprize, {
  125. listid: listid, prizeid: prizeid, code: code
  126. }, function (result) {
  127. //操作成功,
  128. //setPrizeList(listid);
  129. });
  130. */
  131. }
  132. else {
  133. return false;
  134. }
  135. };
  136. //
  137. var setPrizeList = function (listid) {
  138. console.log($prizelist);
  139. var p = $prizelist.find('li[prizelist_id="' + listid + '"]');
  140. p.addClass('hasGetPrize');
  141. };
  142. var $machine = $('.machine');
  143. var $slotBox = $('.tigerslot .box');
  144. var light_html = '';
  145. for (var i = 0; i < 21; i++) {
  146. light_html += '<div class="light l'+ i +'"></div>';
  147. }
  148. var $lights = $(light_html).appendTo($machine);
  149. var $result = $('#result').on('click', '.close-btn', function(){
  150. $result.slideUp();
  151. var submitData = {
  152. marketing_id: $('.tigerslot').attr('activity_id'),
  153. sn: $.trim($("#sncode").text()),
  154. wxid: $('.tigerslot').attr('data-token')
  155. };
  156. $.post('###',
  157. submitData,
  158. function(data) {
  159. if (data.error == 1) {
  160. alert(data.msg);
  161. return;
  162. }
  163. if (data.success == 1) {
  164. //window.location.reload();
  165. $('#result #prize').empty();
  166. $('#result #sncode').empty();
  167. $('.machine .gamebutton').removeClass('disabled');
  168. return;
  169. } else {
  170. }
  171. });
  172. });
  173. var $request_reward = $('#request-reward').on('click', '.close-btn', function(){
  174. $request_reward.slideUp();
  175. })
  176. var $gameButton = $('.machine .gamebutton').tap(function () {
  177. var $this = $(this);
  178. if (!$this.hasClass('disabled')) {
  179. $this.addClass('disabled');
  180. $this.toggleClass(function (index, classname) {
  181. if (classname.indexOf('stop') > -1) {
  182. boxCycle.stop(function (resultData) {
  183. gameOver(resultData);
  184. //$this.removeClass('disabled');
  185. });
  186. } else {
  187. gameStart();
  188. window.setTimeout(function () {
  189. $this.removeClass('disabled');
  190. },1500);
  191. }
  192. return 'stop';
  193. });
  194. }
  195. });
  196. var $prizelist = $('.part.prizelist').on('tap', '.getprize', function () {
  197. var $this = $(this), $parent = $this.parent();
  198. var code = $parent.find('.code').html();
  199. $('#sn').val(code);
  200. $("#request-reward").slideToggle(500);
  201. return false;
  202. });
  203. //提交手机号码
  204. $('.part').on('tap', '#submit-btn', function () {
  205. var tel = $("#tel").val();
  206. //var telreg = '/^1[3|4|5|8][0-9]\d{4,8}$/';
  207. if (tel == '') {
  208. alert("请认真输入有效资料");
  209. return
  210. }
  211. var submitData = {
  212. lid: $('.tigerslot').attr('activity_id'),
  213. sncode: $("#sncode").text(),
  214. tel: tel,
  215. wxname: '',
  216. wechaid:$('.tigerslot').attr('wechat_id'),
  217. rid:0,
  218. action: "add"
  219. };
  220. $.post('/index.php?g=Wap&m=Lottery&a=add', submitData,
  221. function(data) {
  222. if (data.error == 1) {
  223. alert(data.msg);
  224. return;
  225. }
  226. if (data.success == true) {
  227. alert('恭喜您,提交成功!');
  228. setTimeout(function(){
  229. window.location.reload();
  230. },2000);
  231. return;
  232. }
  233. },'json')
  234. return false;
  235. });
  236. //提交验证码
  237. $('.part').on('tap', '#ver-btn', function () {
  238. var ver_code = $("#password").val();
  239. var sn = $('#sn').val();
  240. if (ver_code == '') {
  241. alert("请输入密码");
  242. return;
  243. }
  244. var submitData = {
  245. id: $('.tigerslot').attr('activity_id'),
  246. parssword: ver_code,
  247. rid: $('.tigerslot').attr('rid')
  248. };
  249. $.post('/index.php?g=Wap&m=Lottery&a=exchange', submitData,
  250. function(data) {
  251. if (data.success == true) {
  252. alert('恭喜,验证成功!');
  253. setTimeout(function(){
  254. window.location.reload();
  255. },2000);
  256. } else {
  257. alert(data.msg);
  258. }
  259. }
  260. ,'json')
  261. });
  262. var lightCycle = new function () {
  263. var currIndex = 0, maxIndex = $lights.length - 1;
  264. $('.l0').addClass('on');
  265. var tmr = new GameTimer(function () {
  266. $lights.each(function(){
  267. var $this = $(this);
  268. if($this.hasClass('on')){
  269. currIndex++;
  270. if (currIndex > maxIndex) {
  271. currIndex = 0;
  272. }
  273. $this.removeClass('on');
  274. $('.l' + currIndex).addClass('on');
  275. return false;
  276. }
  277. });
  278. }, 100);
  279. this.start = function () {
  280. tmr.start();
  281. };
  282. this.stop = function () {
  283. tmr.stop();
  284. };
  285. };
  286. var lightRandom = new function () {
  287. var tmr = new GameTimer(function () {
  288. $lights.each(function () {
  289. var r = Math.random() * 1000;
  290. if (r < 400) {
  291. $(this).addClass('on');
  292. } else {
  293. $(this).removeClass('on');
  294. }
  295. });
  296. }, 100);
  297. this.start = function () {
  298. tmr.start();
  299. };
  300. this.stop = function () {
  301. tmr.stop();
  302. };
  303. };
  304. var lightFlicker = new function () {
  305. $lights.each(function (index) {
  306. if ((index >> 1) == index / 2) {
  307. $(this).addClass('on');
  308. } else {
  309. $(this).removeClass('on');
  310. }
  311. });
  312. var tmr = new GameTimer(function () {
  313. $lights.toggleClass('on');
  314. }, 100);
  315. this.start = function () {
  316. tmr.start();
  317. };
  318. this.stop = function () {
  319. tmr.stop();
  320. };
  321. };
  322. var boxCycle = new function () {
  323. var speed_left = 0, speed_middle = 0, speed_right = 0, maxSpeed = 25;
  324. var running = false, toStop = false, toStopCount = 0;
  325. var boxPos_left = 0, boxPos_middle = 0, boxPos_right = 0;
  326. var toLeftIndex = 0, toMiddleIndex = 0, toRightIndex = 0;
  327. var resultData;
  328. var $box = $('.tigerslot .box'), $box_left = $('.tigerslot .strip.left .box'), $box_middle = $('.tigerslot .strip.middle .box'), $box_right = $('.tigerslot .strip.right .box');
  329. var fn_stop_callback = null;
  330. var tmr = new GameTimer(function () {
  331. if (toStop) {
  332. toStopCount--;
  333. speed_left = 0;
  334. boxPos_left = -itemPositions[toLeftIndex];
  335. if (toStopCount < 25) {
  336. speed_middle = 0;
  337. boxPos_middle = -itemPositions[toMiddleIndex];
  338. }
  339. if (toStopCount < 0) {
  340. speed_right = 0;
  341. boxPos_right = -itemPositions[toRightIndex];
  342. }
  343. } else {
  344. speed_left += 1;
  345. speed_middle += 1;
  346. speed_right += 1;
  347. if (speed_left > maxSpeed) {
  348. speed_left = maxSpeed;
  349. }
  350. if (speed_middle > maxSpeed) {
  351. speed_middle = maxSpeed;
  352. }
  353. if (speed_right > maxSpeed) {
  354. speed_right = maxSpeed;
  355. }
  356. }
  357. boxPos_left += speed_left;
  358. boxPos_middle += speed_middle;
  359. boxPos_right += speed_right;
  360. $box_left.css('background-position', '0 ' + boxPos_left + 'px')
  361. $box_middle.css('background-position', '0 ' + boxPos_middle + 'px')
  362. $box_right.css('background-position', '0 ' + boxPos_right + 'px')
  363. if (speed_left == 0 && speed_middle == 0 && speed_right == 0) {
  364. tmr.stop(fn_stop_callback.bind(this, resultData));
  365. }
  366. }, 33);
  367. this.start = function (data) {
  368. toLeftIndex = data.left; toMiddleIndex = data.middle; toRightIndex = data.right;
  369. running = true; toStop = false;
  370. resultData = data;
  371. tmr.start();
  372. };
  373. this.stop = function (fn) {
  374. fn_stop_callback = fn;
  375. toStop = true;
  376. toStopCount = 50;
  377. };
  378. this.reset = function () {
  379. $box_left.css('background-position', '0 ' + itemPositions[0] + 'px');
  380. $box_middle.css('background-position', '0 ' + itemPositions[0] + 'px');
  381. $box_right.css('background-position', '0 ' + itemPositions[0] + 'px');
  382. };
  383. this.reset();
  384. };
  385. //顶部滚动中奖信息
  386. AutoScrollHeader = (function(obj){
  387. $(obj).find("ul:first").animate({
  388. marginTop:"-15px"
  389. },500,function(){
  390. $(this).css({marginTop:"0px"}).find("li:first").appendTo(this);
  391. });
  392. });
  393. if($('.scroll-reward-info li').length >1){
  394. setInterval('AutoScroll(".scroll-reward-info")',4000);
  395. }
  396. //手机号码格式判断
  397. function istel(value) {
  398. var regxEny = /^[0-9]*$/;
  399. return regxEny.test(value);
  400. }
  401. lightFlicker.start();
  402. window.setTimeout(function () {
  403. lightFlicker.stop();
  404. }, 2000)
  405. });