script_profiles.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. const btnLoginPage = document.querySelector('.btn-login');
  2. const btnUserProfile = document.querySelector('.btn-userProfile');
  3. const btnLogout = document.querySelector('.btn-logout');
  4. function getCookie(name) {
  5. const value = `; ${document.cookie}`;
  6. const parts = value.split(`; ${name}=`);
  7. if (parts.length === 2) return parts.pop().split(';').shift();
  8. }
  9. let userBasics = JSON.parse(localStorage.getItem('user_profile')) || [];
  10. function checkLocal() {
  11. if(userBasics.length == 0){ return };
  12. $('.userName').html(`<h2 class="user-name text-white mt-4 fw-bold">Hello,${userBasics.user_info.userName}</h2>`);
  13. }
  14. checkLocal()
  15. function renderView() {
  16. let token = getCookie('jwt_token');
  17. if(!token) {
  18. return;
  19. }
  20. // axios.defaults.withCredentials = false;
  21. axios({
  22. method: 'post',
  23. url: 'https://www.choozmo.com:8887/user_profile',
  24. headers: {
  25. 'accept': 'text/html',
  26. 'Authorization': `Bearer ${token}`
  27. }
  28. }).then(res => {
  29. console.log(res.data);
  30. const userInfo = res.data;
  31. localStorage.setItem('user_profile', JSON.stringify(res.data));
  32. if(userInfo.user_info.left_sec < 20){
  33. Swal.fire({
  34. title: "剩餘秒數不足",
  35. text: '您的影片額度即將不足,加值以持續使用',
  36. icon: 'warning',
  37. showCancelButton: true,
  38. cancelButtonText: 'Cancel',
  39. confirmButtonColor: '#3085d6',
  40. confirmButtonText: '加值'
  41. }).then(result => {
  42. if (result.isConfirmed) {
  43. location.href = "pricing.html";
  44. }
  45. });
  46. }
  47. // const str = `<img src="static/img/undraw_male_avatar_323b.svg" alt="">
  48. // <p class="card-profile-txt">User Profile</p>
  49. // <p class="card-profile-cnt">${userInfo.user_info.userName}</p>
  50. // <p class="card-profile-cnt">${userInfo.user_info.email}</p>
  51. // <div class="d-flex justify-content-around">
  52. // <div>
  53. // <p set-lan="html:used">已使用</p>
  54. // <p><strong>${userInfo.user_info.total_sec}</strong><span set-lan="html:sec">秒</span></p>
  55. // </div>
  56. // <div>
  57. // <p set-lan="html:left">未使用</p>
  58. // <p><strong>${userInfo.user_info.left_sec}</strong><span set-lan="html:sec">秒</span></p>
  59. // </div>
  60. // </div>`;
  61. const str = `<img src="static/img/undraw_male_avatar_323b.svg" alt="">
  62. <p class="card-profile-txt">User Profile</p>
  63. <p class="card-profile-cnt">${userInfo.user_info.userName}</p>
  64. <p class="card-profile-cnt">${userInfo.user_info.email}</p>
  65. <div class="d-flex justify-content-around">
  66. <div>
  67. <p set-lan="html:used">已使用</p>
  68. <p><strong>${userInfo.user_info.total_sec}</strong><span set-lan="html:sec">秒</span></p>
  69. </div>
  70. <div>
  71. <p set-lan="html:left">未使用</p>
  72. <p><strong>${userInfo.user_info.left_sec}</strong><span set-lan="html:sec">秒</span></p>
  73. </div>
  74. </div>`;
  75. // 使用者名稱
  76. const userName = `<h2 class="user-name text-white mt-4 fw-bold">Hello,${userInfo.user_info.userName}</h2>`;
  77. // 已使用
  78. const usedtime=`<h1 class="text-center">${userInfo.user_info.total_sec}&ensp;<span style="font-size:15px;" set-lan="html:sec">秒</span></h1>`;
  79. // 未使用
  80. const NotUsedTime=`<h1 class="text-center">${userInfo.user_info.left_sec}&ensp;<span style="font-size:15px;" set-lan="html:sec">秒</span></h1>`;
  81. // user資訊
  82. const infContent=`
  83. <div class="inf-content">
  84. <p>${userInfo.user_info.userName}</p>
  85. <p>***************&nbsp;<a href="./reset_pwd_email.html" set-lan="html:resetPsd">更改密碼</a></p>
  86. <p>${userInfo.user_info.email}</p>
  87. </div>`;
  88. // 歷史紀錄
  89. // const historicalrecord
  90. var historicalrecord=''
  91. for (var i = 0; i < userInfo.video_info.length; i++) {
  92. historicalrecord+='\
  93. <tr>\
  94. <td class="px-0">'+userInfo.video_info[i].time_stamp+'</td>\
  95. <td class="px-0">'+userInfo.video_info[i].title+'</td>\
  96. <td class="px-0 w-25">'+userInfo.video_info[i].duration+'</td>\
  97. </tr>`\
  98. ';
  99. }
  100. $('.historical-content').html(historicalrecord);
  101. $('.userinf').html(infContent);
  102. $('.userName').html(userName);
  103. $('.used-time').html(usedtime);
  104. $('.not-used-time').html(NotUsedTime);
  105. $('.card-profile').html(str);
  106. $('.card-profile').html(str);
  107. }).catch(err => {
  108. console.log(err);
  109. })
  110. }
  111. renderView();
  112. function getDraft() {
  113. let token = getCookie('jwt_token');
  114. axios({
  115. method: 'post',
  116. url: 'https://www.choozmo.com:8887/draft_list',
  117. headers: {
  118. 'accept': 'application/json',
  119. 'Authorization': `Bearer ${token}`
  120. }
  121. }).then(res => {
  122. console.log(res.data);
  123. let result = [...res.data];
  124. let str = '';
  125. if(result.length > 0){
  126. for(let i = 0;i < result.length; i++) {
  127. str += `<tr>
  128. <td>${result[i].title}</td>
  129. <td>
  130. <span class="me-3 draft-content-icon" id="${result[i].id}" onclick="gotoDraft(${result[i].id})">
  131. <i class="fas fa-edit"></i>
  132. </span>
  133. <span class="ms-3 draft-content-icon" id="${result[i].id}" onclick="deleteDraft(${result[i].id})">
  134. <i class="fas fa-trash-alt"></i>
  135. </span>
  136. </td>
  137. </tr>`
  138. }
  139. $('.draft-content').html(str);
  140. } else {
  141. str = '<div><h3>目前沒有草稿喔</h3></div>';
  142. $('.draft-content').html(str);
  143. }
  144. }).catch(err => {
  145. console.log(err);
  146. });
  147. }
  148. getDraft();
  149. function checkLogin() {
  150. let token = getCookie('jwt_token');
  151. if(token) {
  152. btnLoginPage.style.display = 'none';
  153. btnLogout.style.display = 'block';
  154. btnUserProfile.style.display = 'block';
  155. } else {
  156. window.location.href = 'login.html';
  157. btnLoginPage.style.display = 'block';
  158. btnLogout.style.display = 'none';
  159. btnUserProfile.style.display = 'none';
  160. }
  161. }
  162. checkLogin();
  163. $(".historical-record").hide();
  164. $( ".check-history" ).click(function() {
  165. $(".historical-record").toggle(150);
  166. $(".arrowdown").toggleClass("arrowdoup");
  167. });
  168. $('.draft-table').hide();
  169. $('.draft .arrowdown').click(function() {
  170. $(".draft-table").toggle(150);
  171. $(".draft .arrowdown").toggleClass("arrowdoup");
  172. });
  173. var loaded_data = ''
  174. function openNav() {
  175. document.getElementById("mySidenav").style.width = "250px";
  176. document.querySelector('.loader').style.display = "block";
  177. let token = getCookie('jwt_token');
  178. axios({
  179. method: 'post',
  180. url: 'https://www.choozmo.com:8887/history_input',
  181. headers: {
  182. 'accept': 'application/json',
  183. 'Authorization': `Bearer ${token}`
  184. }
  185. }).then(res => {
  186. console.log(res.data);
  187. loaded_data = res.data;
  188. for (var obj of loaded_data) {
  189. var historyList = document.querySelector('.historyList');
  190. var list = document.createElement('li');
  191. list.id = obj.id;
  192. // div-imgfr
  193. var divImgfr = document.createElement('div');
  194. divImgfr.classList.add('item_imgfr');
  195. var img = document.createElement('img');
  196. img.setAttribute('src', obj['image_urls'][0]);
  197. divImgfr.appendChild(img);
  198. // div-content
  199. var contentBox = document.createElement('div');
  200. contentBox.classList.add('content-box');
  201. var boxTitle = document.createElement('p');
  202. boxTitle.classList.add('box-title');
  203. boxTitle.textContent = obj.name;
  204. boxTitle.id = obj.id;
  205. boxTitle.setAttribute('onclick', `direct(${obj.id})`);
  206. var boxLink = document.createElement('span');
  207. boxLink.classList.add('box-link');
  208. boxLink.setAttribute("data-url", obj.link);
  209. boxLink.setAttribute('onclick', 'view()');
  210. boxLink.innerHTML = '<i class="fas fa-play-circle me-1"></i>觀看影片';
  211. contentBox.appendChild(boxTitle);
  212. contentBox.appendChild(boxLink);
  213. list.classList.add("historyList-item");
  214. list.setAttribute('onclick', `direct(${obj.id})`);
  215. list.appendChild(divImgfr);
  216. list.appendChild(contentBox);
  217. historyList.appendChild(list);
  218. }
  219. document.querySelector('.loader').style.display = "none";
  220. }).catch(err => {
  221. console.log(err);
  222. });
  223. }
  224. function direct(id) {
  225. location.href = `make_video.html?id=${id}`;
  226. }
  227. function view() {
  228. event.stopPropagation();
  229. console.log(event.target);
  230. if (event.target.nodeName === 'I') {
  231. return;
  232. } else {
  233. window.open(`http://${event.target.dataset.url}`, '_blank');
  234. }
  235. }
  236. function gotoDraft (id) {
  237. location.href = `make_video.html?draftid=${id}`;
  238. }
  239. function deleteDraft(id) {
  240. let token = getCookie('jwt_token');
  241. axios({
  242. method: 'post',
  243. url: 'https://www.choozmo.com:8887/del_draft',
  244. headers: {
  245. 'accept': 'application/json',
  246. 'Authorization': `Bearer ${token}`,
  247. 'Content-Type': 'application/json'
  248. },
  249. data: { "id": id }
  250. }).then(res => {
  251. console.log(res.data);
  252. getDraft();
  253. }).catch(err => {
  254. console.log(err);
  255. });
  256. }