goto.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // $('.sec05-moblie-container').slick({
  2. // arrows: false,
  3. // slidesToShow: 1,
  4. // slidesToScroll: 1,
  5. // infinite: false,
  6. // centerMode: true,
  7. // });
  8. $(".sendbox").hide();
  9. $(document).on("click", ".collectcontent", function (event) {
  10. $(".sendbox").show(500);
  11. $(this).addClass("select");
  12. $(".select").siblings().removeClass('select');
  13. });
  14. $(document).on("click", ".close", function (event) {
  15. // $(this).siblings('.collectcard').removeClass('select');
  16. $(".sendbox").hide(500);
  17. $(".collectcontent").removeClass("select");
  18. });
  19. let userId;
  20. // 收藏頁面
  21. function collection(collectuserid='') {
  22. console.log('Call collection(' + collectuserid + ')'); // test
  23. userId = collectuserid;
  24. var collectcontent = '';
  25. $.ajax({
  26. method: "GET",
  27. url: "https://api.ptt.cx:8750/api/v1/line/collection/" + collectuserid + "",
  28. dataType: "json",
  29. })
  30. .done(function (msg) {
  31. if (msg == "error: user don't have any nft") { // 沒NFT
  32. collectcontent += '<p>您可以到我們的「NFT商店」頁面,去購買您喜愛的NFT收藏品哦!</p>';
  33. } else { // 有NFT
  34. console.log(msg)
  35. let len = Object.keys(msg).length;
  36. let arr = []
  37. for(let i = 0; i < len; i++) {
  38. arr.push(msg[i]);
  39. }
  40. arr = arr.filter((item, i) => {
  41. return item !== undefined;
  42. })
  43. console.log(arr);
  44. for(let i = 0; i < arr.length; i++) {
  45. collectcontent += '\
  46. <div class="collectcontent col">\
  47. <input style="display: none;" type="radio" data-amount="'+ arr[i].amount +'" value="'+ arr[i].uid + '" name="nftid" id="' + arr[i].uid + '">\
  48. <label for="'+ arr[i].uid + '">\
  49. <div class="collectcard card h-100">\
  50. <a target="_blank">\
  51. <img src="'+ arr[i].imgurl + '"class="card-img-top" alt="...">\
  52. </a>\
  53. <div class="card-body p-2">\
  54. <a target="_blank">\
  55. <h5 class="card-title pt-3">'+ arr[i].title + '</h5>\
  56. <p class="card-text text-mute">'+ arr[i].context + '</p>\
  57. <p class="card-text">數量:'+ arr[i].amount + '</p>\
  58. <p class="card-text card-price text-end">$'+ arr[i].price + '</p>\
  59. </a>\
  60. </div>\
  61. </div>\
  62. </label>\
  63. </div>';
  64. }
  65. }
  66. $('.sec05-moblie-container').html(collectcontent);
  67. });
  68. }
  69. // 購買頁面
  70. function shop(collectuserid='') {
  71. console.log('Call shop(' + collectuserid + ')'); // test
  72. userId = collectuserid;
  73. $.ajax({
  74. method: "GET",
  75. url: "https://api.ptt.cx:8750/api/v1/line/shop/" + collectuserid + "",
  76. dataType: "json",
  77. })
  78. .done(function (msg) {
  79. console.log(msg);
  80. // console.log(Object.keys(msg).length);
  81. let msgLen = Object.keys(msg).length;
  82. console.log(msgLen);
  83. var nftmall = '';
  84. var buybox = "";
  85. for (var i = 0; i < msgLen; i++) {
  86. nftmall += '\
  87. <div class="nftmall col">\
  88. <input style="display:none" type="radio" data-amount="'+msg[i].amount+'" value="'+ msg[i].id + '" name="nftid" class="nftname" id="' + msg[i].id + '" >\
  89. <label for="'+ msg[i].id + '" data-amount="'+msg[i].amount+'">\
  90. <div class="collectcard card h-100">\
  91. <a target="_blank">\
  92. <img src="'+ msg[i].imgurl + '"class="card-img-top" alt="...">\
  93. </a>\
  94. <div class="card-body p-2">\
  95. <a target="_blank">\
  96. <h5 class="card-title pt-3">'+ msg[i].title + '</h5>\
  97. <p class="card-text text-mute">'+ msg[i].context + '</p>\
  98. <p class="card-text">數量:'+ msg[i].amount + '</p>\
  99. <p class="card-text card-price text-end">$'+ msg[i].price + '</p>\
  100. </a>\
  101. </div>\
  102. </div>\
  103. </label>\
  104. </div>';
  105. }
  106. buybox += '\
  107. <div class="p-2">\
  108. <input id="userid" style="border-radius: 10px; border:none; display: none;" class="p-2 w-100 mb-3" type="text" name="userid" value="' + collectuserid + '" placeholder="請輸入您的userid" required>\
  109. <input type="number" name="amount" id="number" placeholder="請輸入數量" class="p-2 w-100 mb-3" style="border-radius: 10px; border:none;" required>\
  110. <p class="text-error text-white mb-2"></p>\
  111. <div class="text-end px-2">\
  112. <p class="text-start text-white mb-2">※一次只能購買一則NFT收藏品</p>\
  113. <button type="button" style="border:1px solid #fff; border-radius: 30px;" class="close btn text-white px-3">取消</button>\
  114. <span style="border-radius: 30px; background:#fff; border: none;" class="send-btn px-3 ms-1 py-2 btn span">購買</span>\
  115. </div>\
  116. </div>\
  117. </div>';
  118. $('.sec06-nft-mall').html(nftmall);
  119. $('.buybox').html(buybox);
  120. $('.span').click(function(){
  121. var nftid = $("input[name='nftid']:checked").val();
  122. var amount = $("input[name='amount']").val();
  123. if(amount > $("input[name='nftid']:checked").data('amount') || !amount) {
  124. console.log('over');
  125. $('.text-error').text('請輸入合適購買數量');
  126. return
  127. } else {
  128. $('.text-error').text('');
  129. }
  130. // var data = `{"nftid":"` + nftid +`","userid":"` + userid +`"}`;
  131. console.log(amount); // test
  132. var url = `https://api.ptt.cx:8750/api/v1/linepay/request?line_id=${userId}&nft_id=${nftid}&amount=${amount}`;
  133. console.log(url);
  134. var xhr = new XMLHttpRequest();
  135. xhr.open("POST", url, false);
  136. xhr.setRequestHeader("accept", "application/json");
  137. xhr.setRequestHeader("Content-Type", "application/json");
  138. xhr.onreadystatechange = function () {
  139. if (xhr.readyState === 4) {
  140. console.log(xhr.status);
  141. const res = JSON.parse(xhr.responseText);
  142. if(device == 'desktop'){
  143. location.replace(res.web);
  144. } else {
  145. location.replace(res.app);
  146. }
  147. // alert("購買成功");
  148. }
  149. };
  150. xhr.send();
  151. })
  152. });
  153. }
  154. // Qrcode頁面
  155. function get_data(collectuserid='') {
  156. console.log('Call get_data(' + collectuserid + ')'); // test
  157. $.ajax({
  158. method: "GET",
  159. url: "https://api.ptt.cx:8750/api/v1/line/receive/" + collectuserid + "",
  160. dataType: "json",
  161. })
  162. .done(function (msg) {
  163. console.log(msg);
  164. var Qrcodeimg = '';
  165. var useraddress = '';
  166. Qrcodeimg += '\
  167. <img class="img-fluid" src="./qrcode/'+ collectuserid + '.png" alt="">\
  168. ';
  169. useraddress += '\
  170. '+ msg.useraddress + '\
  171. ';
  172. $('#Qrcode').html(Qrcodeimg);
  173. $('#copy').html(useraddress);
  174. });
  175. }
  176. $(".buybox").hide();
  177. $(document).on("click", ".nftmall", function (event) {
  178. $(".buybox").show(500);
  179. $(this).addClass("select");
  180. $(".select").siblings().removeClass('select');
  181. });
  182. $(document).on("click", ".close", function (event) {
  183. // $(this).siblings('.collectcard').removeClass('select');
  184. $(".buybox").hide(500);
  185. $(".nftmall").removeClass("select");
  186. $(".nftname").prop("checked", false);
  187. });
  188. $.fn.serializeObject = function () {
  189. var o = {};
  190. var a = this.serializeArray();
  191. $.each(a, function () {
  192. if (o[this.name]) {
  193. console.log(o[this.name], o[this.value]); // test
  194. if (!o[this.name].push) {
  195. o[this.name] = [o[this.name]];
  196. }
  197. o[this.name].push(this.value || '');
  198. } else {
  199. o[this.name] = this.value || '';
  200. }
  201. });
  202. return o;
  203. };
  204. // 確定發送按鈕處理
  205. $(".collect-send").submit(function (e) {
  206. var nftuid = $("input[name='nftid']:checked").val();
  207. var amount = $("input[name='amount']").val();
  208. var address = $('#address').val();
  209. console.log(amount);
  210. var data = `{"nftuid": ${nftuid}, "to": "${address}", "userid": "${userId}", "amount": ${amount}}`;
  211. var headers = {'accept': 'application/json'}
  212. console.log(data); // test
  213. var url = `https://api.ptt.cx:8750/api/v1/line/send/${userId}?to=${address}&nftuid=${nftuid}&amount=${amount}`;
  214. axios({
  215. method: 'get',
  216. url: url,
  217. headers,
  218. data: ''
  219. }).then(res => {
  220. console.log(res);
  221. window.location.href = './collect.html';
  222. }).catch(err => {
  223. console.log(err);
  224. })
  225. });
  226. const getDeviceType = () => {
  227. const ua = navigator.userAgent;
  228. if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
  229. return "tablet";
  230. }
  231. if (
  232. /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(
  233. ua
  234. )
  235. ) {
  236. return "mobile";
  237. }
  238. return "desktop";
  239. };
  240. const device = getDeviceType();
  241. function copyEvent(id) {
  242. var str = document.getElementById(id);
  243. window.getSelection().selectAllChildren(str);
  244. document.execCommand("Copy")
  245. toastr.options = {
  246. // 參數設定[註1]
  247. "closeButton": false, // 顯示關閉按鈕
  248. "debug": false, // 除錯
  249. "newestOnTop": false, // 最新一筆顯示在最上面
  250. "progressBar": false, // 顯示隱藏時間進度條
  251. "positionClass": "toast-top-center", // 位置的類別
  252. "preventDuplicates": false, // 隱藏重覆訊息
  253. "onclick": null, // 當點選提示訊息時,則執行此函式
  254. "showDuration": "300", // 顯示時間(單位: 毫秒)
  255. "hideDuration": "1000", // 隱藏時間(單位: 毫秒)
  256. "timeOut": "1000", // 當超過此設定時間時,則隱藏提示訊息(單位: 毫秒)
  257. "extendedTimeOut": "1000", // 當使用者觸碰到提示訊息時,離開後超過此設定時間則隱藏提示訊息(單位: 毫秒)
  258. "showEasing": "swing", // 顯示動畫時間曲線
  259. "hideEasing": "linear", // 隱藏動畫時間曲線
  260. "showMethod": "fadeIn", // 顯示動畫效果
  261. "hideMethod": "fadeOut" // 隱藏動畫效果
  262. }
  263. toastr.success("複製成功");
  264. }