templates/frontend/default/api.html.twig line 1

Open in your IDE?
  1. <script>
  2. $( document ).ready(function() {
  3. {% if env == 'game' %}
  4. loginCallback();
  5. {% else %}
  6. prompt("cmhk://epg/login?return=loginCallback");
  7. {% endif %}
  8. })
  9. var missionData = [];
  10. var monthlyRemind = true;
  11. function loginCallback(res){
  12. {% if env == 'game' %}
  13. res = {
  14. "code": 200,
  15. "message":"登录成功",
  16. "info":{
  17. "userId":"xxx",
  18. "userToken":"test",
  19. "userName":"xxx",
  20. "userHeadImg":"xxx",
  21. "userTel":"66627172",
  22. "platformCode":"xxx",
  23. "mac":"xxx"
  24. }
  25. }
  26. {% endif %}
  27. console.log("login call back");
  28. console.log(res)
  29. let data;
  30. if (res && res.info)
  31. {
  32. $('#entryRequirements .userPhone span').html(res.info.userTel);
  33. $.post("{{ path('api_get_user', {'language': app.request.locale}) }}", { "data": res.info },
  34. function (data, status) {
  35. if (data.code == '200') {
  36. jwt = data.jwt;
  37. localStorage.setItem('{{ localStorage_name }}', jwt);
  38. // enable/disable entryRequirement
  39. if(data.validUser){
  40. $('.validUser').show();
  41. $('#entryRequirements .success').show();
  42. $('#entryRequirements .fail').hide();
  43. if(data.startGame){
  44. $('#entryRequirements .success').css('background-image', "url('{{ asset('images/entry_bg_success_start.png') }}')");
  45. if(!data.firstLogin){
  46. var expirationDateTop = $('#expirationDate').offset().top;
  47. var headerOffset = 0; // Adjust this value based on your header height
  48. $('html, body').animate({
  49. scrollTop: expirationDateTop - headerOffset
  50. }, 1000);
  51. }
  52. monthlyRemind = data.monthlyRemind;
  53. getCurrentMonthMission();
  54. }else{
  55. $('#entryRequirements .success').css('background-image', "url('{{ asset('images/entry_bg_success_nonstart.png') }}')");
  56. $('#mission').hide();
  57. $('#missionStatus').hide();
  58. $('.bgbox').css('height','auto');
  59. if ($(window).width() >= 700) {
  60. $('header').css('height', '195vw');
  61. } else {
  62. $('header').css('height', '260vw'); // 或根據需要設置其他值
  63. }
  64. }
  65. const currentDate = new Date();
  66. // Get the current year and month
  67. const currentYear = currentDate.getFullYear();
  68. const currentMonth = currentDate.getMonth();
  69. // Create a new date object for the first day of the next month
  70. const firstDayNextMonth = new Date(currentYear, currentMonth + 1, 1);
  71. // Subtract one day to get the last day of the current month
  72. const lastDateOfCurrentMonth = new Date(firstDayNextMonth - 1);
  73. $('div.missionDuration span').html(`${lastDateOfCurrentMonth.getMonth() + 1} 月 1 至 ${lastDateOfCurrentMonth.getDate()} 日`);
  74. $('#expirationDate p').html(`2026 年 ${data.userExpirationMonth} 月 ${data.userExpirationDate} 日`);
  75. }else{
  76. if ($(window).width() >= 700) {
  77. $('header').css('height', '195vw');
  78. } else {
  79. $('header').css('height', '260vw'); // 或根據需要設置其他值
  80. }
  81. $('.bgbox').css('height','auto');
  82. $('#entryRequirements .fail').show();
  83. $('#entryRequirements .success').hide();
  84. }
  85. }
  86. else if (data.code == '400') {
  87. $("#modal_message_success").html(data.message);
  88. $('#myModal_success').modal('show');
  89. }
  90. });
  91. }
  92. else
  93. {
  94. $("#modal_message_success").html('{{ "error.user" | trans }}');
  95. $('#myModal_success').modal('show');
  96. }
  97. }
  98. function getCurrentMonthMission(){
  99. let jwt = localStorage.getItem('{{ localStorage_name }}');
  100. $.post("{{ path('api_get_current_month_mission', {'language': app.request.locale}) }}", { "jwt": jwt },
  101. function (data, status){
  102. data.weekly_mission.forEach(function(dates) {
  103. let jsonParams = {
  104. pageIndex: 0,
  105. pageSize: 10,
  106. startTime: dates[0],
  107. endTime: dates[1],
  108. };
  109. {% if env == 'game' %}
  110. historyCallback();
  111. {% else %}
  112. prompt("cmhk://epg/history?jsonParam=" + encodeURIComponent(JSON.stringify(jsonParams)) + "&return=historyCallback");
  113. {% endif %}
  114. });
  115. setTimeout(function () {
  116. $.post("{{ path('api_get_mission_status', {'language': app.request.locale}) }}", { "data": JSON.stringify(missionData), 'jwt': jwt },
  117. function (data, status){
  118. let totalMissionCount = Object.values(data.data).length;
  119. let nonFinishMissionCount = 0;
  120. let finishMissionCount = 0;
  121. let statusData = data.data;
  122. $('#expirationDate p').html(data.exp_date);
  123. Object.keys(statusData).forEach((key,index) => {
  124. if(statusData[key] =="Done"){
  125. finishMissionCount++;
  126. }else if(statusData[key] == "fail"){
  127. nonFinishMissionCount++;
  128. }
  129. const container = document.getElementById('missionList');
  130. const containerBouns = document.getElementById('missionBonus');
  131. let missionHTML = renderMissionTemplate(key, statusData[key],index);
  132. if(key == 'bonus'){
  133. containerBouns.insertAdjacentHTML('beforeend', missionHTML);
  134. }else{
  135. container.insertAdjacentHTML('beforeend', missionHTML);
  136. }
  137. });
  138. if(finishMissionCount >= 3){
  139. if(!data.has_redeem){
  140. $('#myModal_mission_success').modal('show');
  141. }
  142. $('#missionStatus').css('background-image',"url('{{ asset('images/mission_status_done.png') }}')")
  143. }else if(totalMissionCount - nonFinishMissionCount < 3){
  144. $('#missionStatus').css('background-image',"url('{{ asset('images/mission_status_fail.png') }}')")
  145. }else{
  146. $('#missionStatus').css('background-image',"url('{{ asset('images/mission_status_wait.png') }}')")
  147. }
  148. }
  149. )
  150. }, 1000);
  151. }
  152. );
  153. }
  154. function historyCallback(res) {
  155. {% if env == 'game' %}
  156. res = {
  157. "code": 200,
  158. "message":"获取成功",
  159. "info":{
  160. "data":[
  161. {
  162. "historyId":"1",
  163. "historyName":"xxx1",
  164. "historyTime":"1767339023000",
  165. },
  166. {
  167. "historyId":"2",
  168. "historyName":"xxx2",
  169. "historyTime":"1767857423000",
  170. },
  171. {
  172. "historyId":"3",
  173. "historyName":"xxx2",
  174. "historyTime":"1768375823",
  175. }
  176. ]
  177. }
  178. }
  179. {% endif %}
  180. //console.log("history call back");
  181. //console.log(res);
  182. if (res && res.info && res.info.data){
  183. res.info.data.forEach(function(data) {
  184. missionData.push(data);
  185. })
  186. }
  187. }
  188. function renderMissionTemplate(date, status, index) {
  189. let imgSrc;
  190. switch(status){
  191. case 'fail':
  192. imgSrc = 'images/fail_btn.png';
  193. break;
  194. case 'non-start':
  195. imgSrc = 'images/non_start_btn.png';
  196. break;
  197. case 'non-finish':
  198. imgSrc = 'images/goto_btn.png';
  199. break;
  200. case 'CanSign':
  201. imgSrc = 'images/goto_btn.png';
  202. break;
  203. case 'Done':
  204. imgSrc = 'images/done_btn.png';
  205. break;
  206. case 'notQualified':
  207. imgSrc = 'images/notQualified_btn.png';
  208. break;
  209. default:
  210. imgSrc = 'images/goto_login.png';
  211. break;
  212. }
  213. return `
  214. <div class="missionItem">
  215. ${date === "bonus" ? `
  216. <p class="col-3 bonus-date" >補簽機會</p>
  217. <p class="col-5 bonus-body" >完整觀看指定影片即完成 1 次補簽</p>
  218. <div class="col-3 bonus-btn" ${status == 'Done' ? 'style="pointer-events: none;"' : "id='bonusBtn' data-toggle='modal' data-target='#myModal_video' onclick='embedVideo()'"}>
  219. <img class='missionBtn' src="{{ asset('${imgSrc}') }}">
  220. </div>
  221. ` : `
  222. <p class="col-3 mission-date">第 ${index} 周</p>
  223. <p class="col-5 mission-body">${date}</p>
  224. <button class="col-3 mission-btn" ${monthlyRemind == false ? 'onclick="monthlyReMind()"':'onclick="gotoPage()"'} ${status !== 'non-finish' ? 'style="pointer-events: none;"' : ''}>
  225. <img class='missionBtn' src="{{ asset('${imgSrc}') }}">
  226. </button>
  227. `}
  228. </div>
  229. `;
  230. }
  231. </script>