common.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. common={
  2. data:{
  3. baseurl:"http://183.234.61.252:8090/",
  4. getUserInfo:"Home/User/Info",
  5. register:"",
  6. WebSocket:"",
  7. bdSocket:"Home/User/Bind",
  8. Join:"Home/User/Join",
  9. Answer:"Home/User/Answer",
  10. Join:"Home/Game/Join",
  11. uid:"",
  12. },userInfo:{
  13. },GET: function (name) {
  14. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  15. var r = window.location.search.substr(1).match(reg);
  16. if (r != null) return unescape(r[2]);
  17. return null;
  18. },getUserInfo:function(fn){
  19. var _this=this;
  20. $.ajax({
  21. type: "GET",
  22. url: this.data.baseurl+this.data.getUserInfo,
  23. data: {user_id:this.data.uid , mt:this.data.mt },
  24. dataType: "json",
  25. success: function(data){
  26. fn&&fn(data);
  27. }
  28. });
  29. },bdSocket:function(fn){
  30. var _this=this;
  31. $.ajax({
  32. type: "POST",
  33. url: this.data.baseurl+this.data.bdSocket,
  34. data: {client_id:this.data.client_id },
  35. dataType: "json",
  36. success: function(data){
  37. fn&&fn(data);
  38. }
  39. });
  40. },Join:function(fn){
  41. var _this=this;
  42. $.ajax({
  43. type: "GET",
  44. url: this.data.baseurl+this.data.Join,
  45. dataType: "json",
  46. success: function(data){
  47. fn&&fn(data);
  48. }
  49. });
  50. },Answer:function(fn){
  51. var _this=this;
  52. $.ajax({
  53. type: "GET",
  54. url: this.data.baseurl+this.data.Answer,
  55. data:{question_id:_this.data.question_id,option_id:_this.data.option_id},
  56. dataType: "json",
  57. success: function(data){
  58. fn&&fn(data);
  59. }
  60. });
  61. },player_join:function(data){
  62. // if(responsedata.info.user){
  63. // $("#username").html(responsedata.info.user.name);
  64. // $("#avatar").html('<img src="' + responsedata.info.user.avatar + '" class="layui-circle">');
  65. // }
  66. // if(data.info.user.user_id){
  67. // $("#py1_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
  68. // $("#py1_username").html(data.info.user.name + '(我)');
  69. // }
  70. if(data.info.user.user_id){
  71. $("#py1_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
  72. $("#py1_username").html(data.info.user.name + '(我)');
  73. }
  74. },onSocket:function(e){
  75. var data=JSON.parse(e.data);
  76. var _this=this;
  77. var type=data.type;
  78. switch(type){
  79. case 'init':
  80. _this.data.client_id=data.client_id;
  81. _this.bdSocket(function(res){
  82. _this.data.isReady=1;
  83. console.log(res.msg);
  84. });
  85. break;
  86. case 'player_join':
  87. _this.player_join(data);
  88. break;
  89. case 'question':
  90. _this.loadQuestion(data);
  91. break;
  92. case 'round_end':
  93. break;
  94. case 'game_end':
  95. $(".g-doc .g-inner").addClass("end");
  96. $("#Jvs").hide();
  97. break;
  98. }
  99. //_this.data.question_id
  100. },startGame:function(){
  101. if(this.data.isReady=="1")
  102. this.Join(function(res){
  103. common.tips(res.msg);
  104. });
  105. else this.tips("登录失败");
  106. },initSocket:function(){
  107. var _this=this;
  108. var ws = new WebSocket("ws://183.234.61.252:8282");
  109. ws.onmessage=function(e){
  110. _this.onSocket(e);
  111. }
  112. },loadQuestion(responsedata) {
  113. console.log(responsedata);
  114. _this.data.question_id= responsedata.info.question.question_id;
  115. $("#Jqtitle").html(responsedata.info.question.title );
  116. var question="";
  117. var answers = responsedata.info.options
  118. for(var i in answers){
  119. question += '<li class="btn-2 item item'+i+' answer layui-btn layui-btn-primary" data-option_id="'+answers[i].option_id+'">'+answers[i].title+'</li>';
  120. }
  121. $("#question").html(question);
  122. $("#Jpage").html(responsedata.info.questions_count+"/5");
  123. $(".welcome").hide();
  124. $(".gstart").show();
  125. $(".g-doc .g-inner").addClass("doing");
  126. // is_end = responsedata.info.question.is_end;
  127. this.daoshu();
  128. },listen:function(){
  129. var _this=this;
  130. $(".rulebtn").on("touchend",function(){
  131. $(".popwind,.popbg").fadeIn(200);
  132. })
  133. $(".close").on("touchend",function(){
  134. $(".popwind,.popbg").fadeOut(200);
  135. })
  136. $("#start_game").on("touchend",function(){
  137. _this.startGame();
  138. })
  139. $("#question .item").on("touchend",function(){
  140. var l = $("#question .item.cur_answer").length;
  141. if(l>0)return;
  142. _this.data.option_id=this.attr("data-option_id");
  143. this.addClass("cur_answer");
  144. _this.Answer();
  145. })
  146. },daoshu:function(argument) {
  147. clearInterval(ts);
  148. this.data.sum = 9;
  149. this.data.angle = 0;
  150. var leftContent = document.querySelector(".left-content");
  151. var rightContent = document.querySelector(".right-content");
  152. var textCircle = document.querySelector(".text-circle");
  153. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  154. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  155. this.data.ts = setInterval(function() {
  156. if (sum >= 0) textCircle.innerHTML = sum;
  157. sum = sum - 1;
  158. $("#Jvs .con").show();
  159. $("#Jvs .tit").html("抢答中");
  160. angle += 36;
  161. if (angle <= 360) {
  162. if (angle > 180) {
  163. rightContent.setAttribute('style', 'transform: rotate(' + (angle - 180) + 'deg)')
  164. } else {
  165. leftContent.setAttribute('style', 'transform: rotate(' + angle + 'deg)')
  166. }
  167. }
  168. if (sum < -1) {
  169. answer();
  170. sum = 9;
  171. angle = 0;
  172. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  173. rightContent.setAttribute('style', 'transform: rotate(0deg)')
  174. }
  175. }, 1000)
  176. }, tips: function (text, time) {
  177. //弹窗工具
  178. time = time ? time : 1500;
  179. var para = document.createElement("p"); //创建新的<p> 元素
  180. para.innerHTML = text;
  181. para.setAttribute("class", "poptis");
  182. document.body.appendChild(para);
  183. para.style.marginLeft = -para.offsetWidth / 2+"px";
  184. setTimeout(function () {
  185. document.body.removeChild(para);
  186. }, time);
  187. },init:function(){
  188. var _this=this;
  189. this.listen();
  190. var ycy =this.GET("ycy");
  191. if(ycy){
  192. var b64 = new Base64();
  193. ycy=b64.decode(ycy);
  194. ycy=decodeURIComponent(ycy);
  195. ycy= JSON.parse(ycy);
  196. this.data.uid=ycy.uid;
  197. this.data.mt=ycy.mt;
  198. }
  199. if( this.GET("user_id")){
  200. this.data.uid=this.GET("user_id");
  201. this.data.mt=this.GET("mt");
  202. }
  203. this.getUserInfo(function(res){
  204. _this.userInfo=res.info;
  205. _this.initSocket();
  206. })
  207. }
  208. }
  209. window.common=common;