common.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.uid },
  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(responsedata){
  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(responsedata.info.user){
  67. $("#py1_avatar").html('<img src="' + data.info.user.avatar+ '" class="layui-circle">');
  68. $("#py1_username").html(data.info.user.name + '(我)');
  69. }else if(responsedata.info.players){
  70. $("#py2_avatar").html('<img src="' + data.info.players.avatar + '" class="layui-circle">');
  71. $("#py2_username").html(data.info.players.name);
  72. }
  73. },onSocket:function(e){
  74. var data=JSON.stringify(e.data);
  75. var _this=this;
  76. var type=data.type;
  77. switch(type){
  78. case 'init':
  79. _this.bdSocket(function(res){
  80. console.log(res.msg);
  81. });
  82. break;
  83. case 'player_join':
  84. _this.player_join(data);
  85. break;
  86. case 'question':
  87. _this.loadQuestion(data);
  88. break;
  89. case 'round_end':
  90. break;
  91. case 'game_end':
  92. $(".g-doc .g-inner").addClass("end");
  93. $("#Jvs").hide();
  94. break;
  95. }
  96. //_this.data.question_id
  97. },startGame:function(){
  98. if(this.userInfo.user_id)
  99. this.Join();
  100. else this.tips("登录失败");
  101. },initSocket:function(){
  102. var _this=this;
  103. var ws = new WebSocket("ws://183.234.61.252:8282");
  104. ws.onmessage=function(e){
  105. _this.onSocket(e);
  106. }
  107. },loadQuestion(responsedata) {
  108. console.log(responsedata);
  109. _this.data.question_id= responsedata.info.question.question_id;
  110. $("#Jqtitle").html(responsedata.info.question.title );
  111. var question="";
  112. var answers = responsedata.info.options
  113. for(var i in answers){
  114. 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>';
  115. }
  116. $("#question").html(question);
  117. $("#Jpage").html(responsedata.info.questions_count+"/5")
  118. // is_end = responsedata.info.question.is_end;
  119. daoshu();
  120. },listen:function(){
  121. var _this=this;
  122. $(".rulebtn").on("touchend",function(){
  123. $(".popwind,.popbg").fadeIn(200);
  124. })
  125. $(".close").on("touchend",function(){
  126. $(".popwind,.popbg").fadeOut(200);
  127. })
  128. $("#start_game").on("touchend",function(){
  129. _this.startGame();
  130. })
  131. $("#question .item").on("touchend",function(){
  132. var l = $("#question .item.cur_answer").length;
  133. if(l>0)return;
  134. _this.data.option_id=this.attr("data-option_id");
  135. this.addClass("cur_answer");
  136. _this.Answer();
  137. })
  138. },daoshu:function(argument) {
  139. clearInterval(ts);
  140. sum = 9;
  141. angle = 0;
  142. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  143. rightContent.setAttribute('style', 'transform: rotate(0deg)');
  144. ts = setInterval(function() {
  145. if (sum >= 0) textCircle.innerHTML = sum;
  146. sum = sum - 1;
  147. $("#Jvs .con").show();
  148. $("#Jvs .tit").html("抢答中");
  149. angle += 36;
  150. if (angle <= 360) {
  151. if (angle > 180) {
  152. rightContent.setAttribute('style', 'transform: rotate(' + (angle - 180) + 'deg)')
  153. } else {
  154. leftContent.setAttribute('style', 'transform: rotate(' + angle + 'deg)')
  155. }
  156. }
  157. if (sum < -1) {
  158. answer();
  159. sum = 9;
  160. angle = 0;
  161. leftContent.setAttribute('style', 'transform: rotate(0deg)');
  162. rightContent.setAttribute('style', 'transform: rotate(0deg)')
  163. }
  164. }, 1000)
  165. }, tips: function (text, time) {
  166. //弹窗工具
  167. time = time ? time : 1500;
  168. var para = document.createElement("p"); //创建新的<p> 元素
  169. para.innerHTML = text;
  170. para.setAttribute("class", "poptis");
  171. document.body.appendChild(para);
  172. para.style.marginLeft = -para.offsetWidth / 2+"px";
  173. setTimeout(function () {
  174. document.body.removeChild(para);
  175. }, time);
  176. },init:function(){
  177. var _this=this;
  178. this.listen();
  179. var ycy =this.GET("ycy");
  180. if(ycy){
  181. var b64 = new Base64();
  182. ycy=b64.decode(ycy);
  183. ycy=decodeURIComponent(ycy);
  184. ycy= JSON.parse(ycy);
  185. this.data.uid=ycy.uid;
  186. this.data.mt=ycy.mt;
  187. }
  188. if( this.GET("user_id")){
  189. this.data.uid=this.GET("user_id");
  190. this.data.mt=this.GET("mt");
  191. }
  192. this.getUserInfo(function(res){
  193. _this.userInfo=res.info;
  194. _this.initSocket();
  195. })
  196. }
  197. }
  198. window.common=common;