logic-signup.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. var state = {
  2. token: "",
  3. ecId: 0,
  4. from: "",
  5. mcId: 0,
  6. mcName: "",
  7. mcType: 0,
  8. beginSecond: null,
  9. endSecond: null,
  10. coiId: 0,
  11. coiName: "",
  12. teamNum: 0,
  13. nickName: "",
  14. coiRs: [],
  15. configParam: {
  16. labelName: "昵称",
  17. labelOrg: "组织",
  18. subTitle: ""
  19. },
  20. introduce: { title: "", content: "" },
  21. activityRules: { title: "", content: "" },
  22. mcState: 0
  23. };
  24. function initSignupPage() {
  25. const params = new URLSearchParams(window.location.search);
  26. state.token = params.get('token') || '';
  27. state.ecId = params.get('id') || 0;
  28. state.from = params.get('from') || '';
  29. window.cardfunc.init(state.token, state.ecId);
  30. window.cardfunc.getCardConfig(onConfigLoaded);
  31. // Bind events
  32. // Dropdown logic is already in HTML script, but we can enhance or replace it.
  33. // For now, I'll assume the HTML script functions are available or I should override them
  34. // if I want to connect them to state.
  35. // The HTML script defines `selectOption` globally. I should probably piggyback on that or replace it.
  36. // Let's expose necessary functions to window so HTML can call them
  37. window.selectOption = selectOption;
  38. window.openConfirm = openConfirm;
  39. }
  40. function onConfigLoaded(config) {
  41. // Load logic from signup.vue loadConfig
  42. const pageConfig = window.cardfunc.parseCardConfig(config['signup']);
  43. if(pageConfig) {
  44. // Load params
  45. const param = pageConfig.param;
  46. if (param) {
  47. if (param.labelName) state.configParam.labelName = param.labelName;
  48. if (param.labelOrg) state.configParam.labelOrg = param.labelOrg;
  49. if (param.subTitle) state.configParam.subTitle = param.subTitle;
  50. }
  51. // Load introduce/rules if needed to display (HTML structure is static, but content could be dynamic)
  52. // The new UI seems to have static "Match Intro".
  53. // If we want to use dynamic intro, we need to inject it.
  54. // For now, let's stick to the static UI as per "migration to new UI" request,
  55. // unless the new UI requires dynamic text. The HTML provided has hardcoded intro.
  56. // I will leave it static for now to preserve the new design.
  57. }
  58. // Fetch Data
  59. getCardDetailQuery();
  60. }
  61. function getCardDetailQuery() {
  62. uni.request({
  63. url: window.apiCardDetailQuery,
  64. header: {
  65. "Content-Type": "application/x-www-form-urlencoded",
  66. "token": state.token
  67. },
  68. method: "POST",
  69. data: { ecId: state.ecId },
  70. success: (res) => {
  71. const data = res.data.data;
  72. if(data) {
  73. state.mcType = data.mcType;
  74. state.mcId = data.mcId;
  75. state.mcName = data.mcName;
  76. state.beginSecond = data.beginSecond;
  77. state.endSecond = data.endSecond;
  78. state.coiId = data.coiId;
  79. state.coiName = data.coiName;
  80. state.teamNum = data.teamNum;
  81. state.nickName = data.nickName;
  82. state.mcState = window.tools.checkMcState(state.beginSecond, state.endSecond);
  83. // Update Time UI
  84. updateTimeUI();
  85. // Fetch Dropdown Options
  86. getOnlineMcSignUpDetail();
  87. // Pre-fill form if data exists
  88. if(state.nickName) {
  89. document.getElementById('userName').value = state.nickName;
  90. }
  91. if(state.coiId > 0 && state.coiName) {
  92. // We will update dropdown after fetching details
  93. }
  94. }
  95. }
  96. });
  97. }
  98. function getOnlineMcSignUpDetail() {
  99. uni.request({
  100. url: window.apiOnlineMcSignUpDetail,
  101. header: {
  102. "Content-Type": "application/x-www-form-urlencoded",
  103. "token": state.token,
  104. },
  105. method: "POST",
  106. data: { mcId: state.mcId },
  107. success: (res) => {
  108. const data = res.data.data;
  109. if(data) {
  110. state.coiRs = data.coiRs;
  111. if (!state.nickName && data.name) {
  112. state.nickName = data.name;
  113. document.getElementById('userName').value = state.nickName;
  114. }
  115. renderDropdown();
  116. // If user already selected a team (re-entering page)
  117. if(state.coiId > 0) {
  118. // Find team name if not set
  119. const team = state.coiRs.find(t => t.coiId == state.coiId);
  120. if(team) selectOption(team.coiId, team.coiName, true); // true = skip UI toggle
  121. }
  122. }
  123. }
  124. });
  125. }
  126. function updateTimeUI() {
  127. // Format: 11.28 08:00 - 11.30 18:00
  128. const timeStr = window.tools.fmtMcTime2(state.beginSecond, state.endSecond);
  129. // Need to find the element. The HTML has "11.28 08:00 - 11.30 18:00" hardcoded.
  130. // I'll search for the element containing the time.
  131. // The HTML structure: <span class="font-bold text-gray-800 font-mono truncate">...</span>
  132. // I should give it an ID in the HTML update step, but for logic file I'll assume I can access it.
  133. // I'll use document.querySelector to find it relative to "比赛时间" text or add an ID in Step 10.
  134. // Let's assume I will add ID `matchTimeDisplay` to the span.
  135. const el = document.getElementById('matchTimeDisplay');
  136. if(el) el.innerText = timeStr;
  137. }
  138. function renderDropdown() {
  139. const ul = document.querySelector('#dropdownMenu ul');
  140. if(!ul) return;
  141. let html = '';
  142. state.coiRs.forEach(team => {
  143. html += `
  144. <li onclick="selectOption('${team.coiId}', '${team.coiName}')" class="px-4 py-3 hover:bg-blue-50 cursor-pointer flex items-center justify-between border-b border-gray-50 last:border-0 transition-colors group/item">
  145. <div class="flex items-center">
  146. <i class="fas fa-user-friends text-blue-200 mr-2 group-hover/item:text-primary transition-colors"></i>
  147. <span class="font-bold">${team.coiName}</span>
  148. </div>
  149. <i class="fas fa-check text-primary opacity-0 check-icon" data-val="${team.coiId}"></i>
  150. </li>
  151. `;
  152. });
  153. ul.innerHTML = html;
  154. }
  155. function selectOption(value, text, skipToggle = false) {
  156. state.coiId = value;
  157. state.coiName = text;
  158. document.getElementById('teamSelect').value = value;
  159. const selectedText = document.getElementById('selectedText');
  160. selectedText.innerHTML = '<i class="fas fa-user-friends text-primary mr-2"></i>' + text;
  161. selectedText.classList.remove('text-gray-400', 'font-normal');
  162. selectedText.classList.add('text-gray-800', 'font-bold');
  163. // Update check icons
  164. const icons = document.querySelectorAll('.check-icon');
  165. icons.forEach(icon => {
  166. if(icon.getAttribute('data-val') == value) {
  167. icon.classList.remove('opacity-0');
  168. } else {
  169. icon.classList.add('opacity-0');
  170. }
  171. });
  172. if(!skipToggle) {
  173. // closeDropdown is defined in HTML script.
  174. // Ideally I should move that logic here or call it.
  175. // Since I'm injecting logic.js at the bottom, I can call global functions from HTML.
  176. if(window.closeDropdown) window.closeDropdown();
  177. }
  178. }
  179. function openConfirm() {
  180. if(!window.checkToken(state.token)) return;
  181. const nameInput = document.getElementById('userName');
  182. state.nickName = nameInput.value.trim();
  183. if (!state.nickName) {
  184. uni.showToast({ title: `请填写${state.configParam.labelName}`, icon: 'none' });
  185. return;
  186. }
  187. if (!state.coiId) {
  188. uni.showToast({ title: `请选择${state.configParam.labelOrg}`, icon: 'none' });
  189. return;
  190. }
  191. document.getElementById('confirmName').innerText = state.nickName;
  192. document.getElementById('confirmTeamText').innerText = state.coiName;
  193. document.getElementById('confirmModal').classList.remove('hidden');
  194. }
  195. // Overwrite the HTML's onclick="location.href='#'" for confirm button
  196. // I'll bind the event dynamically in init or replace the HTML in Step 10.
  197. // Better to bind dynamically.
  198. function confirmSignup() {
  199. uni.request({
  200. url: window.apiOnlineMcSignUp,
  201. header: {
  202. "Content-Type": "application/x-www-form-urlencoded",
  203. "token": state.token,
  204. },
  205. method: "POST",
  206. data: {
  207. mcId: state.mcId,
  208. coiId: state.coiId,
  209. selectTeam: 0, // Default
  210. nickName: state.nickName
  211. },
  212. success: (res) => {
  213. if (window.checkResCode(res)) {
  214. uni.showToast({ title: '比赛报名成功!', icon: 'none' });
  215. setTimeout(() => {
  216. const queryString = `token=${state.token}&id=${state.ecId}`;
  217. window.location.href = `ranklist.html?${queryString}`;
  218. }, 1500);
  219. }
  220. },
  221. fail: () => {
  222. uni.showToast({ title: '出错了,报名失败', icon: 'none' });
  223. }
  224. });
  225. }
  226. // Hook up confirm button
  227. document.addEventListener('DOMContentLoaded', () => {
  228. // Wait for init
  229. const confirmBtn = document.querySelector('#confirmModal button:last-child');
  230. if(confirmBtn) {
  231. confirmBtn.removeAttribute('onclick'); // Remove inline handler
  232. confirmBtn.addEventListener('click', confirmSignup);
  233. }
  234. });