utlis.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* utlis */
  2. // if (typeof console == "undefined") {
  3. // window.console = {
  4. // log: function () {}
  5. // };
  6. // }
  7. // window.console = {
  8. // log: function () {}
  9. // };
  10. isLIVE = false;
  11. switch (window.location.host.toLowerCase()) {
  12. case 'infra.cecmartech.com:8077':
  13. isLIVE = false;
  14. break;
  15. case 'line.maltssociety.com.tw':
  16. isLIVE = true;
  17. break;
  18. case 'www.mortlach.com.tw':
  19. isLIVE = true;
  20. break;
  21. }
  22. console.log('isLIVE : ' + isLIVE);
  23. function gtag_pageView(_key) {
  24. //console.log("gtag_pageView: " + _key);
  25. }
  26. function gtag_ButtonClick(_key) {
  27. console.log("gtag_ButtonClick: " + _key);
  28. window.dataLayer = window.dataLayer || [];
  29. window.dataLayer.push({
  30. 'event': 'customEvent',
  31. 'eventCategory': 'Button',
  32. 'eventAction': 'Click',
  33. 'eventLabel': _key
  34. });
  35. }
  36. function setDefault(_textbox, _value) { // depend on jQuery
  37. $(_textbox).val(_value).css({ opacity: .4 });
  38. $(_textbox).focus(
  39. function () {
  40. if ($(this).val() == _value) {
  41. $(this).val('').css({ opacity: 1 });
  42. }
  43. })
  44. .blur(function () {
  45. if ($(this).val() == '') {
  46. $(this).val(_value).css({ opacity: .4 });
  47. }
  48. });
  49. }
  50. function getParameterByName(name, url) {
  51. if (!url) url = window.location.href;
  52. name = name.replace(/[\[\]]/g, "\\$&");
  53. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  54. results = regex.exec(url);
  55. if (!results) return null;
  56. if (!results[2]) return '';
  57. return decodeURIComponent(results[2].replace(/\+/g, " "));
  58. }
  59. function setParameterByName(name, value, url) {
  60. if (!url) url = window.location.href;
  61. var re = new RegExp("([?|&])" + name + "=.*?(&|$)", "i");
  62. separator = url.indexOf('?') !== -1 ? "&" : "?";
  63. if (url.match(re)) {
  64. return url.replace(re, '$1' + name + "=" + value + '$2');
  65. }
  66. else {
  67. return url + separator + name + "=" + value;
  68. }
  69. }
  70. function loadAnimate(_namespace, $container, _callback) {
  71. var $canvas, stage, exportRoot, anim_container, dom_overlay_container, fnStartAnimation;
  72. var key = Object.keys(this[_namespace].compositions)[0];
  73. var comp = this[_namespace].getComposition(key);
  74. var lib = comp.getLibrary();
  75. $canvas = $('<canvas></canvas>').attr({
  76. width: lib.properties.width,
  77. height: lib.properties.height
  78. }).appendTo($container);
  79. var loader = new createjs.LoadQueue(false);
  80. loader.addEventListener("fileload", function (evt) { handleFileLoad(evt, comp) });
  81. loader.addEventListener("complete", function (evt) { handleComplete(evt, comp) });
  82. loader.loadManifest(lib.properties.manifest);
  83. function handleFileLoad(evt, comp) {
  84. var images = comp.getImages();
  85. if (evt && (evt.item.type == "image")) { images[evt.item.id] = evt.result; }
  86. }
  87. function handleComplete(evt, comp) {
  88. var lib = comp.getLibrary();
  89. var ss = comp.getSpriteSheet();
  90. var queue = evt.target;
  91. var ssMetadata = lib.ssMetadata;
  92. for (i = 0; i < ssMetadata.length; i++) {
  93. ss[ssMetadata[i].name] = new createjs.SpriteSheet({ "images": [queue.getResult(ssMetadata[i].name)], "frames": ssMetadata[i].frames })
  94. }
  95. exportRoot = new lib[_namespace]();
  96. stage = new lib.Stage($canvas[0]);
  97. fnStartAnimation = function () {
  98. stage.addChild(exportRoot);
  99. createjs.Ticker.setFPS(lib.properties.fps);
  100. createjs.Ticker.addEventListener("tick", stage);
  101. exportRoot.stop();
  102. if (_callback != null) {
  103. _callback(exportRoot);
  104. }
  105. }
  106. this[_namespace].compositionLoaded(lib.properties.id);
  107. fnStartAnimation();
  108. }
  109. }
  110. function setCookie(name,value,days) {
  111. var expires = "";
  112. if (days) {
  113. var date = new Date();
  114. date.setTime(date.getTime() + (days*24*60*60*1000));
  115. expires = "; expires=" + date.toUTCString();
  116. }
  117. document.cookie = name + "=" + (value || "") + expires + "; path=/";
  118. }
  119. function getCookie(name) {
  120. var nameEQ = name + "=";
  121. var ca = document.cookie.split(';');
  122. for(var i=0;i < ca.length;i++) {
  123. var c = ca[i];
  124. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  125. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  126. }
  127. return null;
  128. }
  129. function eraseCookie(name) {
  130. document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  131. }
  132. function POST(theUrl, theData) {
  133. const _url = API__DOMAIN + theUrl;
  134. // console.log(JSON.stringify(theData))
  135. return fetch(_url, {
  136. method: 'POST',
  137. headers: {
  138. 'Content-Type': 'application/json'
  139. },
  140. body: JSON.stringify(theData)
  141. })
  142. .then((response) => {
  143. return response.json();
  144. }).then((jsonData) => {
  145. // const { success, msg } = jsonData;
  146. // if (!success) {
  147. // throw new Error(msg);
  148. // }
  149. console.log(jsonData)
  150. return jsonData;
  151. })
  152. // .catch((err) => {
  153. // throw new Error(err);
  154. // });
  155. }
  156. function GET(theUrl) {
  157. const _url = API__DOMAIN + theUrl;
  158. return fetch(_url, {})
  159. .then((response) => {
  160. return response.json();
  161. }).then((jsonData) => {
  162. console.log(jsonData)
  163. return jsonData;
  164. })
  165. // .catch((err) => {
  166. // throw new Error(err);
  167. // });
  168. }
  169. const PRE__LIFFID = '2000769399-lGpVNobN'; // 測試liffId待定
  170. const PRO__LIFFID = '2000844179-J80gOzVP'; // 正式liffId待定
  171. const LIFFID = (isLIVE) ? PRO__LIFFID : PRE__LIFFID;
  172. function liffInit() {
  173. return new Promise((resolve, reject) => {
  174. liff.init({
  175. liffId: LIFFID,
  176. }).then(() => {
  177. if (!liff.isLoggedIn() && !liff.isInClient()) {
  178. liff.login();
  179. } else {
  180. liff.getProfile().then((profile) => {
  181. resolve(profile);
  182. })
  183. .catch((err)=>{
  184. console.log(err)
  185. });
  186. }
  187. }).catch((err) => {
  188. reject(new Error(err));
  189. });
  190. });
  191. }
  192. function sleep(seconds) {
  193. return new Promise((resolve, reject) => {
  194. let _t = setTimeout(() => {
  195. clearTimeout(_t);
  196. _t - null;
  197. resolve();
  198. }, seconds);
  199. });
  200. }
  201. const _PRE__HOST = 'infra.cecmartech.com:8077';
  202. const _PRO__HOST = 'line.maltssociety.com.tw';
  203. const _URL = location.hostname;
  204. const _host = (_URL.indexOf(_PRO__HOST) != -1) ? _PRO__HOST : _PRE__HOST;
  205. const API__DOMAIN = location.protocol+'//' + _host;
  206. var isIE = false;
  207. utlis = function () {
  208. //private menbers
  209. //private methods
  210. function init() {
  211. console.log('all is loaded.');
  212. }
  213. //constructor
  214. {
  215. if ($('html').is('.ie6, .ie7, .ie8')) {
  216. isIE = true;
  217. // alert('.ie6, .ie7, .ie8');
  218. }
  219. $(document).ready(function () {
  220. init();
  221. });
  222. }
  223. //public
  224. return {
  225. }
  226. }
  227. utlis = new utlis();