logic-index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var state = {
  2. token: "",
  3. ecId: 0,
  4. pageName: "index",
  5. ecName: '',
  6. ecDesc: '',
  7. beginSecond: null,
  8. endSecond: null,
  9. secondCardName: '',
  10. isJoin: null,
  11. isFinished: false,
  12. countdown: "",
  13. interval: null,
  14. mcState: 0, // 0: Not started, 1: In progress, 2: Finished
  15. // Query params
  16. type: "锦标赛",
  17. btnText: "开始比赛"
  18. };
  19. function initIndexPage() {
  20. const params = new URLSearchParams(window.location.search);
  21. state.token = params.get('token') || '';
  22. state.ecId = params.get('id') || 0;
  23. state.type = params.get('type') || "锦标赛";
  24. state.btnText = params.get('btnText') || "开始比赛";
  25. if(window.cardfunc) {
  26. window.cardfunc.init(state.token, state.ecId);
  27. window.cardfunc.getCardConfig(onConfigLoaded);
  28. }
  29. // Bind events
  30. const btn = document.getElementById('action-btn');
  31. if(btn) {
  32. btn.addEventListener('click', btnClick);
  33. }
  34. }
  35. function onConfigLoaded(config) {
  36. // Load CSS if any (cardfunc handles common config, we handle page specific if needed)
  37. // For pure HTML with Tailwind, maybe we don't need dynamic CSS injection as much,
  38. // or we rely on cardfunc.loadCardCommonConfig which is already called.
  39. // Now fetch data
  40. getCardBaseQuery();
  41. getUserJoinCardQuery();
  42. // matchRsDetailQuery(); // Optional: for red dot
  43. }
  44. function getCardBaseQuery() {
  45. uni.request({
  46. url: window.apiCardBaseQuery,
  47. header: {
  48. "Content-Type": "application/x-www-form-urlencoded",
  49. "token": state.token,
  50. },
  51. method: "POST",
  52. data: {
  53. ecId: state.ecId,
  54. pageName: state.pageName
  55. },
  56. success: (res) => {
  57. const data = res.data.data;
  58. if(data) {
  59. state.ecName = data.ecName;
  60. state.ecDesc = data.ecDesc;
  61. state.beginSecond = data.beginSecond;
  62. state.endSecond = data.endSecond;
  63. state.secondCardName = data.secondCardName;
  64. updateCountdown();
  65. if(state.interval) clearInterval(state.interval);
  66. state.interval = setInterval(updateCountdown, 60000);
  67. }
  68. }
  69. });
  70. }
  71. function getUserJoinCardQuery() {
  72. uni.request({
  73. url: window.apiUserJoinCardQuery,
  74. header: {
  75. "Content-Type": "application/x-www-form-urlencoded",
  76. "token": state.token
  77. },
  78. method: "POST",
  79. data: {
  80. ecId: state.ecId
  81. },
  82. success: (res) => {
  83. const code = res.data.code;
  84. const data = res.data.data;
  85. if (code == 0) {
  86. state.isJoin = data.isJoin;
  87. updateUI();
  88. }
  89. }
  90. });
  91. }
  92. function updateCountdown() {
  93. if (state.endSecond > 0) {
  94. const now = Date.now() / 1000;
  95. // Calculate state based on time
  96. if(state.beginSecond > now) {
  97. state.mcState = 0; // Not started
  98. } else if (state.endSecond > now) {
  99. state.mcState = 1; // In progress
  100. } else {
  101. state.mcState = 2; // Finished
  102. state.isFinished = true;
  103. }
  104. let targetTime = state.mcState === 0 ? state.beginSecond : state.endSecond;
  105. let dif = targetTime - now;
  106. if(state.mcState === 2) {
  107. dif = 0;
  108. }
  109. // Update DOM
  110. const timerContainer = document.getElementById('timer-container');
  111. if(timerContainer) {
  112. let label = state.mcState === 0 ? "距开始" : (state.mcState === 1 ? "距结束" : "已结束");
  113. if(state.mcState === 2) {
  114. timerContainer.parentElement.className = "absolute top-[5%] right-[5%] bg-gray-800/80 backdrop-blur-md rounded-full";
  115. timerContainer.parentElement.style.padding = "1.5vw 3vw";
  116. timerContainer.innerHTML = '<span class="text-gray-200 font-bold" style="font-size: 4vw;">已结束</span>';
  117. } else {
  118. let days = Math.floor(dif / (3600 * 24));
  119. let hours = Math.floor((dif % (3600 * 24)) / 3600);
  120. // let minutes = Math.floor((dif % 3600) / 60);
  121. timerContainer.innerHTML = `
  122. <span class="opacity-90" style="font-size: 3.5vw;">${label}</span>
  123. <span class="font-bold text-yellow-300 tabular-nums" style="font-size: 4.5vw;">
  124. <span id="days">${days.toString().padStart(2,'0')}</span>天<span id="hours">${hours.toString().padStart(2,'0')}</span>时
  125. </span>
  126. `;
  127. }
  128. }
  129. updateUI();
  130. }
  131. }
  132. function updateUI() {
  133. const btn = document.getElementById('action-btn');
  134. if(!btn) return;
  135. if(state.isJoin) {
  136. // Already joined, show "Enter Game" or "View Rank"
  137. if (state.mcState === 0) {
  138. // Not started but joined? Maybe "Wait for start" or just "View Rank"
  139. btn.innerHTML = '查看排名 <i class="fas fa-list-ol opacity-80" style="font-size: 4vw;"></i>';
  140. btn.className = "w-full bg-gradient-to-r from-blue-500 to-indigo-600 text-white font-bold shadow-xl border border-white/20 active:scale-95 transition-transform tracking-wide flex items-center justify-center gap-[2vw]";
  141. } else if (state.mcState === 1) {
  142. btn.innerHTML = '进入比赛 <i class="fas fa-flag-checkered opacity-80" style="font-size: 4vw;"></i>';
  143. btn.className = "w-full bg-gradient-to-r from-green-400 to-emerald-600 text-white font-bold shadow-xl border border-white/20 active:scale-95 transition-transform tracking-wide flex items-center justify-center gap-[2vw]";
  144. } else {
  145. btn.innerHTML = '查看数据';
  146. btn.className = "w-full bg-slate-600 text-white font-bold shadow-xl active:scale-95 transition-transform tracking-wide flex items-center justify-center gap-[2vw]";
  147. }
  148. } else {
  149. // Not joined
  150. if (state.mcState === 0 || state.mcState === 1) {
  151. btn.innerHTML = '开始报名 <i class="fas fa-chevron-right opacity-80" style="font-size: 4vw;"></i>';
  152. btn.className = "w-full bg-gradient-to-r from-orange-500 to-red-600 text-white font-bold shadow-xl border border-white/20 active:scale-95 transition-transform tracking-wide flex items-center justify-center gap-[2vw]";
  153. } else {
  154. btn.innerHTML = '查看排名'; // Event finished
  155. btn.className = "w-full bg-slate-600 text-white font-bold shadow-xl active:scale-95 transition-transform tracking-wide flex items-center justify-center gap-[2vw]";
  156. }
  157. }
  158. // Adjust styles based on vw (done in HTML mostly, but classes updated above)
  159. btn.style.fontSize = "5vw";
  160. btn.style.padding = "3.5vw 0";
  161. btn.style.borderRadius = "9999px";
  162. }
  163. function btnClick() {
  164. const queryString = `token=${state.token}&id=${state.ecId}&type=${state.type}&btnText=${state.btnText}`;
  165. if (state.isJoin) {
  166. const url = `ranklist.html?${queryString}&full=true`;
  167. window.uni.navigateTo({ url: url });
  168. } else {
  169. if (!state.isFinished) {
  170. if (state.secondCardName == 'rankList') {
  171. const url = `ranklist.html?${queryString}&full=true`;
  172. window.uni.navigateTo({ url: url });
  173. } else {
  174. const url = `signup.html?${queryString}&full=true`;
  175. window.uni.navigateTo({ url: url });
  176. }
  177. } else {
  178. const url = `ranklist.html?${queryString}&full=true`;
  179. window.uni.navigateTo({ url: url });
  180. }
  181. }
  182. }