/*global $, alert, g_cus, g_de, g_api, g_db, g_ul, btoa, console, i18n */ var g_db = { /** * Check the capability * @private * @method SupportLocalStorage * @return {Object} description */ SupportLocalStorage: function () { 'use strict'; return typeof (localStorage) !== "undefined"; }, /** * Insert data * @private * @method SetItem * @param {Object} sKey * @param {Object} sValue * @return {Object} description */ SetItem: function (sKey, sValue) { 'use strict'; var bRes = false; if (this.SupportLocalStorage()) { localStorage.setItem(sKey, sValue); bRes = true; } return bRes; }, /** * Fetch data * @private * @method GetItem * @param {Object} sKey * @return {Object} description */ GetItem: function (sKey) { 'use strict'; var sRes = null; if (this.SupportLocalStorage()) { sRes = localStorage.getItem(sKey); } return sRes; }, /** * Remove data * @private * @method RemoveItem * @param {Object} sKey * @return {Object} description */ RemoveItem: function (sKey) { 'use strict'; var bRes = false; if (this.SupportLocalStorage()) { localStorage.removeItem(sKey); bRes = true; } return bRes; }, /** * Description for GetDic * @private * @method GetDic * @return {Object} description */ GetDic: function (sKey) { 'use strict'; var dicRes = null, vTemp; if (this.SupportLocalStorage()) { vTemp = localStorage.getItem(sKey); if (null !== vTemp) { dicRes = JSON.parse(vTemp); } } return dicRes; }, /** * Description for SetDic * @private * @method SetDic * @return {Object} description */ SetDic: function (sKey, dicValue) { 'use strict'; var bRes = false; if (this.SupportLocalStorage()) { localStorage.setItem(sKey, JSON.stringify(dicValue)); bRes = true; } return bRes; } }; var g_gd = { webapilonginurl: "/api/Service/GetLogin", webapiurl: "/api/Cmd/GetData", projectname: "Eurotran", projectver: "Origtek", orgid: "TE", userid: "EUROTRAN", relpath: "", debugmode: window.location.host === '192.168.1.105', debugcolor: "#732C6B", IsEDU: g_db.GetItem("isedu") === "true" }; var g_ul = { /** * Get token from db * @returns {String} Token in localStorage */ GetToken: function () { 'use strict'; return g_db.GetItem("token"); }, /** * Set token to db * @param {String} sTokenValue api token */ SetToken: function (sTokenValue) { 'use strict'; g_db.SetItem("token", sTokenValue); }, /** * Set signature to db * @param {String} sSignatureValue api signature */ GetSignature: function () { 'use strict'; return g_db.GetItem("signature"); }, /** * Set signature to db * @param {String} sSignatureValue api signature */ SetSignature: function (sSignatureValue) { 'use strict'; g_db.SetItem("signature", sSignatureValue); }, /** * Set language * @param {String} language method */ SetLang: function (sLang) { 'use strict'; g_db.SetItem("lang", sLang); }, /** * Get language * @returns {String} language in localStorage */ GetLang: function () { 'use strict'; return g_db.GetItem("lang"); }, /** * Check is edu environment * @returns {String} login method in localStorage */ IsEDU: function () { 'use strict'; return g_db.GetItem("isedu"); } }; var g_api = { ConnectLite: function (i_sModuleName, i_sFuncName, i_dicData, i_sSuccessFunc, i_FailFunc, i_bAsyn, i_sShwd) { window.IsWaiting = i_sShwd; return this.ConnectLiteWithoutToken(i_sModuleName, i_sFuncName, i_dicData, i_sSuccessFunc, i_FailFunc, i_bAsyn); }, ConnectService: function (i_sModuleName, i_sFuncName, i_dicData, i_sSuccessFunc, i_FailFunc, i_bAsyn, i_sShwd) { window.IsWaiting = i_sShwd; return this.ConnectWebLiteWithoutToken(i_sModuleName, i_sFuncName, i_dicData, i_sSuccessFunc, i_FailFunc, i_bAsyn); }, ConnectLiteWithoutToken: function (i_sModuleName, i_sFuncName, i_dicData, i_sSuccessFunc, i_FailFunc, i_bAsyn) { var dicData = {}, dicParameters = {}, token = g_ul.GetToken(), lang = g_ul.GetLang(), signature = g_ul.GetSignature(); dicParameters.ORIGID = g_gd.orgid; dicParameters.USERID = g_gd.userid; dicParameters.MODULE = i_sModuleName; dicParameters.TYPE = i_sFuncName; dicParameters.PROJECT = g_gd.projectname; dicParameters.PROJECTVER = g_gd.projectver; dicParameters.TRACEDUMP = null; i_dicData = i_dicData || {}; if (g_db.GetItem('dblockDict') !== null) { i_dicData.dblockDict = g_db.GetItem('dblockDict'); } dicParameters.DATA = i_dicData; if (lang !== null) { dicParameters.LANG = lang; } if (token !== null) { dicParameters.TOKEN = token; } if (signature !== null) { dicParameters.SIGNATURE = signature; } dicParameters.CUSTOMDATA = {}; if (window.sProgramId) { dicParameters.CUSTOMDATA.program_id = sProgramId; } dicParameters.CUSTOMDATA.module_id = "WebSite"; dicData.url = i_dicData.hasOwnProperty("url") ? i_dicData.url : g_gd.webapiurl; dicData.successfunc = i_sSuccessFunc; dicData.dicparameters = dicParameters; dicData.failfunc = ("function" === typeof (i_FailFunc)) ? i_FailFunc : function (jqXHR, textStatus, errorThrown) { alert("ConnectLite Fail jqXHR:" + jqXHR + " textStatus:" + textStatus + " errorThrown:" + errorThrown); }; dicData.useasync = ("boolean" === typeof (i_bAsyn)) ? i_bAsyn : true; return this.AjaxPost(dicData); }, //w.CallAjax = function (url, fnname, data, sucfn, failfn, wait, async, alwaysfn) { ConnectWebLiteWithoutToken: function (i_sUrl, i_sFuncName, i_dicData, i_sSuccessFunc, i_FailFunc, i_bAsyn) { var dicData = {}, dicParameters = {}, token = g_ul.GetToken(), lang = g_ul.GetLang(), signature = g_ul.GetSignature(); dicParameters.ORIGID = g_gd.orgid; dicParameters.USERID = g_gd.userid; dicParameters.MODULE = ''; dicParameters.TYPE = i_sFuncName; dicParameters.PROJECT = g_gd.projectname; dicParameters.PROJECTVER = g_gd.projectver; dicParameters.TRACEDUMP = null; if (g_db.GetItem('dblockDict') !== null) { i_dicData.dblockDict = g_db.GetItem('dblockDict'); } dicParameters.DATA = i_dicData; if (lang !== null) { dicParameters.LANG = lang; } if (token !== null) { dicParameters.TOKEN = token; } if (signature !== null) { dicParameters.SIGNATURE = signature; } dicParameters.CUSTOMDATA = {}; if (window.sProgramId) { dicParameters.CUSTOMDATA.program_id = sProgramId; } dicParameters.CUSTOMDATA.module_id = "WebSite"; dicData.url = getWebServiceUrl(i_sUrl, i_sFuncName); dicData.successfunc = i_sSuccessFunc; dicData.dicparameters = dicParameters; dicData.failfunc = ("function" === typeof (i_FailFunc)) ? i_FailFunc : function (jqXHR, textStatus, errorThrown) { alert("ConnectLite Fail jqXHR:" + jqXHR + " textStatus:" + textStatus + " errorThrown:" + errorThrown); }; dicData.useasync = ("boolean" === typeof (i_bAsyn)) ? i_bAsyn : true; return this.AjaxPost(dicData); }, AjaxPost: function (i_dicData) { 'use strict'; var defaultOption = { useasync: true, successfunc: null, failfunc: null, alwaysfunc: null, url: null, dicparameters: null }, runOption = $.extend(defaultOption, i_dicData), runSuccess = function (res) { if (res.RESULT === -1) { //layer.alert(i18next.t("message.TokenVerifyFailed"), { icon: 0, title: i18next.t("common.Tips") }, function (index) { // window.top.location.href = '/Page/login.html'; //}); } else { if (runOption.successfunc) { runOption.successfunc(res); } } }; return $.ajax({ type: 'POST', url: runOption.url, data: "=" + btoa2(encodeURIComponent(JSON.stringify(runOption.dicparameters))), success: runSuccess, error: runOption.failfunc, beforeSend: function (xhr) { var token = g_ul.GetToken(), timestamp = $.now(), nonce = rndnum(); xhr.setRequestHeader("orgid", runOption.dicparameters.ORIGID); xhr.setRequestHeader("userid", runOption.dicparameters.USERID); xhr.setRequestHeader("token", token); xhr.setRequestHeader("timestamp", timestamp); xhr.setRequestHeader("nonce", nonce); }, async: true !== runOption.useasync ? false : true }).always(runOption.alwaysfunc); } }; /** * 如果頁面js需要動態加載請在配置在這裡 * * * * * window.UEDITOR_HOME_URL = "/xxxx/xxxx/"; */ (function ($, w, d) { /******************取得host***************************/ w.gWebUrl = window.location.origin || gethost(); //w.gServerUrl = 'https://www.eurotran.com:9001'; //w.gServerUrl = 'https://www.origtek.com:8106'; w.gServerUrl = 'http://localhost:3466'; if ('www.eurotran.com.tw|www.g-yi.com.cn'.indexOf(window.location.hostname) > -1) { w.gServerUrl = 'https://www.eurotran.com:9001'; } else if ('www.origtek.com'.indexOf(window.location.hostname) > -1) { w.gServerUrl = 'https://www.origtek.com:8106'; } else if ('192.168.1.105'.indexOf(window.location.hostname) > -1) { w.gServerUrl = 'https://192.168.1.105:9001'; } if (window.location.pathname.indexOf('/TG/') > -1) { g_gd.orgid = 'TG'; } else if (window.location.pathname.indexOf('/SG/') > -1) { g_gd.orgid = 'SG'; } /** * 定義系統所有公用 Service.fnction */ w.ComFn = { GetOrgInfo: 'GetOrgInfo', GetSysSet: 'GetSysSet', GetArguments: 'GetArguments', GetNewsCount: 'GetNewsCount', GetNewsPage: 'GetNewsPage', GetExhibitionPage: 'GetExhibitionPage', GetNewsInfo: 'GetNewsInfo', GetFileList: 'GetUploadFiles' }; /** * 定義系統所有公用 Service */ w.Service = { apiappcom: 'Common', apite: 'TEAPI', apitg: 'TGAPI', apiwebcom: 'Com' }; /** * For display javascript exception on UI */ w.onerror = function (message, source, lineno, colno, error) { console.log(source + " line:" + lineno + " colno:" + colno + " " + message); if (parent.SysSet && parent.SysSet.IsOpenMail === 'Y') { g_api.ConnectLite('Log', 'ErrorMessage', { ErrorSource: source, Errorlineno: lineno, Errorcolno: colno, ErrorMessage: message }, function (res) { if (res.RESULT) { } }); } }; /** * 依據組織信息設定website * @param {Function} callback 回調函數 * @param {Boolean} isreget 是否查詢DB */ w.runByOrgInfo = function (callback, isreget) { isreget = isreget || false; var org = g_db.GetDic('OrgInfo'); if (!org || isreget) { g_api.ConnectLite(Service.apiwebcom, ComFn.GetOrgInfo, {}, function (res) { if (res.RESULT) { org = res.DATA.rel; g_db.SetDic('OrgInfo', org); if (typeof callback === 'function') { callback(org); } } }); } else { if (typeof callback === 'function') { callback(org); } } }; /** * Ajax是否等待 */ w.IsWaiting = null; /** * 翻譯語系 * @param {HTMLElement} dom 翻譯回調函數 */ w.transLang = function (dom) { i18next = ("undefined" == typeof i18next) ? parent.i18next : i18next; var oHandleData = dom === undefined ? $('[data-i18n]') : dom.find('[data-i18n]'), oHandlePlaceholder = dom === undefined ? $('[placeholderid]') : dom.find('[placeholderid]'); oHandleData.each(function (idx, el) { var i18key = $(el).attr('data-i18n'); if (i18key) { var sLan = i18next.t(i18key); if (el.nodeName == 'INPUT' && el.type == 'button') { $(el).val(sLan); } else { $(el).html(sLan); } } }); oHandlePlaceholder.each(function (idx, el) { var i18key = $(el).attr("placeholderid"); if (i18key) { var sLan = i18next.t(i18key); if (sLan !== i18key) { $(el).attr("placeholder", sLan); } } }); }; /** * 設定多於系 * @param {String} lng 語種 * @param {String} dom 要翻譯的html標籤 * @param {Function} callback 回調函數 */ w.setLang = function (lng, dom, callback) { if (!lng) return; g_ul.SetLang(lng); i18next = ("undefined" == typeof i18next) ? parent.i18next : i18next; $.getJSON(gServerUrl + "/Scripts/lang/" + (g_gd.orgid || 'TE') + "/" + lng + ".json?v=" + new Date().getTime().toString(), function (json) { var oResources = {}; oResources[lng] = { translation: json }; i18next.init({ lng: lng, resources: oResources, useLocalStorage: false, //是否将语言包存储在localstorage //ns: { namespaces: ['trans'], defaultNs: 'trans' }, //加载的语言包 localStorageExpirationTime: 86400000 // 有效周期,单位ms。默认1 }, function (err, t) { transLang(dom); if (typeof callback === 'function') { callback(t); } }); }).error(function () { g_api.ConnectLite('Language', 'CreateLangJson', {}, function (res) { if (res.RESULT) { setLang(lng, dom, callback); } }); }); }; /** * 開啟Waiting視窗 * @param {String} msg 提示文字 */ w.showWaiting = function (msg) { $.blockUI({ message: $('
' + (msg || 'Waiting...') + '
'), css: { 'font-size': (navigator.userAgent.match(/mobile/i) ? '20px' : '36px'), border: '0px', 'border-radius': '10px', 'background-color': '#FFF', padding: '15px 15px', opacity: .5, color: 'orange', cursor: 'wait', 'z-index': 1000000001 }, baseZ: 1000000000 }); w.setTimeout($.unblockUI, 60000);//預設開啟60秒後關閉 }; /** * 關閉Waiting視窗 * @param {Number} iSleep 延遲時間,單位為毫秒 */ w.closeWaiting = function (iSleep) { $(function () { if (iSleep == undefined) { iSleep = 100; } setTimeout($.unblockUI, iSleep); }); }; /** * 從物件陣列中移除屬性為objPropery,值為objValue元素的物件 * @param {Array} arrPerson 陣列物件 * @param {String} objPropery 物件的屬性 * @param {String} objPropery 對象的值 * @return {Array} 過濾後陣列 */ w.Jsonremove = function (arrPerson, objPropery, objValue) { return $.grep(arrPerson, function (cur, i) { return cur[objPropery] != objValue; }); }; /** * 從物件陣列中獲取屬性為objPropery,值為objValue元素的物件 * @param {Array} arrPerson 陣列物件 * @param {String} objPropery 物件的屬性 * @param {String} objPropery 對象的值 * @return {Array} 過濾後陣列 */ w.Jsonget = function (arrPerson, objPropery, objValue) { return $.grep(arrPerson, function (cur, i) { return cur[objPropery] == objValue; }); }; /** * 將json轉換字串 * @param {Object} json json物件 * @return {String} json字串 */ w.Tostr = function (json) { return JSON.stringify(json); }; /** * 下載文件 * @param {String} path 文件路徑(相對路勁) */ w.DownLoadFile = function (path, filename) { var sUrl = gServerUrl + "/Controller.ashx"; sUrl += '?action=downfile&path=' + path; if (filename) { sUrl += '&filename=' + filename; } window.location.href = sUrl; closeWaiting(); }; /** * 用於表單序列化去除禁用欄位 * @param {HTMLElement} dom 父層物件 */ w.reFreshInput = function (dom) { dom.find('[disabled]').each(function () { $(this).attr('hasdisable', 1).removeAttr('disabled'); }); }; /** * 用於表單序列化恢復禁用欄位 * @param {HTMLElement} dom 父層物件 */ w.reSetInput = function (dom) { dom.find('[hasdisable]').each(function () { $(this).removeAttr('hasdisable').prop('disabled', true); }); }; /** * 序列化form表單 * @param {HTMLElement} form 表單form物件 * @param {String} type 傳回類型 * @return {Object Or String} 表單資料 */ w.getFormSerialize = function (form, type) { var formdata = {}; reFreshInput(form); if (type) { formdata = form.serializeJSON(); } else { formdata = form.serializeObject(); } reSetInput(form); return formdata; }; /** * 取得Url參數 * @param {String} name 取得部分的名稱 例如輸入"Action",就能取到"Add"之類參數 * @return {String}參數值 */ w.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構造一個含有目標參數的正則表達式對象 var r = window.location.search.substr(1).match(reg); //匹配目標參數 if (r != null) return unescape(r[2]); return null; //返回參數值 }; /** * 對網址、路徑進行編碼 * @param {String} url 要編碼的url或路徑 * @return {String} 編碼後的url或路徑 */ w.encodeURL = function (url) { return encodeURIComponent(url).replace(/\'/g, "%27").replace(/\!/g, "%21").replace(/\(/g, "%28").replace(/\)/g, "%29"); }; /** * 對網址、路徑進行解碼 * @param {String} url 要解碼的url或路徑 * @return {String} 解碼後的url或路徑 */ w.decodeURL = function (url) { return decodeURIComponent(url); }; /** * 時間格式化處理 * @param {Date} date 日期時間 * @param {Boolean} type 格式(日期 || 日期+時間) * @return {Boolean} 是否可傳回空 */ w.newDate = function (date, type, empty) { var r = ''; if (date) { if (typeof date == 'string') { r = date.replace('T', ' ').replaceAll('-', '/'); if (r.indexOf(".") > -1) { r = r.slice(0, r.indexOf(".")); } } else { r = new Date(date); } r = new Date(r); } else { if (!empty) { r = new Date(); } } return r === '' ? '' : !type ? r.formate("yyyy/MM/dd HH:mm") : r.formate("yyyy/MM/dd"); }; /** * 產生guid * @param {Number} len 指定长度,比如guid(8, 16) // "098F4D35" * @param {Number} radix 基数 * @return {String} guid */ w.guid = function (len, radix) { var buf = new Uint16Array(8), cryptObj = window.crypto || window.msCrypto, // For IE11 s4 = function (num) { var ret = num.toString(16); while (ret.length < 4) { ret = '0' + ret; } return ret; }; cryptObj.getRandomValues(buf); return s4(buf[0]) + s4(buf[1]) + '-' + s4(buf[2]) + '-' + s4(buf[3]) + '-' + s4(buf[4]) + '-' + s4(buf[5]) + s4(buf[6]) + s4(buf[7]); }; /** * 產生隨機數 * @param {Number} len 指定长度,比如random(8) * @return {String} rnd 亂數碼 */ rndnum = function (len) { var rnd = ""; len = len || 10; for (var i = 0; i < len; i++) rnd += Math.floor(Math.random() * 10); return rnd; }; /** * 停止冒泡行为时 * @param {HTMLElement} e 事件对象 */ w.stopBubble = function (e) { //如果提供了事件对象,则这是一个非IE浏览器 if (e && e.stopPropagation) //因此它支持W3C的stopPropagation()方法 e.stopPropagation(); else //否则,我们需要使用IE的方式来取消事件冒泡 window.event.cancelBubble = true; }; /** * 阻止默认行为时 * @param {HTMLElement} e 事件对象 */ w.stopDefault = function (e) { //阻止默认浏览器动作(W3C) if (e && e.preventDefault) e.preventDefault(); //IE中阻止函数器默认动作的方式 else window.event.returnValue = false; return false; }; /** * 產生下拉選單(公用) * @param {Object} list datalist * @param {String} id 顯示的id名稱 * @param {String} name 顯示的name名稱 * @param {Boolean} showid 是否顯示id * @param {String} cusattr 客制化添加屬性 * @param {Boolean} isreapet 是否允許重複 * @return {String} option html */ w.createOptions = function (list, id, name, showid, cusattr, isreapet) { isreapet == isreapet || true; list = list || []; var Options = [], originAry = []; if (typeof list === 'number') { var intNum = list; while (list > 0) { var svalue = intNum - list + 1; svalue = $.trim(svalue); Options.push($('