var YourTeam = YourTeam || {}; /** * This is a description of the Ajax constructor function. * @class Ajax * @classdesc This is a description of the Ajax class. * @author Darren Chen @2021/02/06 * @copyright Darren Chen @Your Team Co., Ltd. 2021 */ class Ajax { /** * constructor * @constructor * @param {String} apiUrl API URL. def="". * @param {String} sendData the data object which you will be send. def= null. * @param {Fcnction} successFunction successFunction. def=null.(use def successFunction) * @param {Fcnction} errorFunction errorFunction. def=null.(use def successFunction) * @param {String} type HTTP METHOD. def="GET". * @param {Object} conType content type * @param {String} dataType data format. def="JSON". * @param {String} isAsync the api synchronization. def=false. * @return {Object} this */ constructor(apiUrl = "", sendData = null, successFunction = null, errorFunction = null, type = "GET", isAsync = false, conType = this.contentType.Json, dataType = "JSON") { this.conType = conType; this.apiUrl = apiUrl; this.isAsync = isAsync; this.type = type; this.dataType = dataType; this.sendData = sendData; this.extSuccessFunction = successFunction; /*if (successFunction) this.successFunction = successFunction;*/ if (errorFunction) this.errorFunction = errorFunction; return this; } /** * dataToString * @description convert the data object to string * @param {Object} data API URL. def="". * @return {String} strJsonData */ dataToString = function (data) { let strJsonData = data ? (data.Data ? JSON.stringify(data.Data) : null) : null; return strJsonData; } /** * beforeSendFunction * @description beforeSendFunction */ beforeSendFunction = function (xhr) { let cookies = new YourTeam.Utility.Cookie(); let token = cookies.get("JWT-Authorization"); xhr.setRequestHeader('Authorization', "Bearer " + token); } /** * successFunction * @description successFunction * @param {Object} data the data object from the api return * @return {Object} data */ successFunction = function (data, callback) { if (data && data.unauthorized == 401) { let cookies = new YourTeam.Utility.Cookie(); let href = location.href; cookies.remove("JWT-Authorization"); if (href.indexOf("localhost:5966") == -1) { location.href = "/login"; } else { location.href = "login.html"; } } if (callback) { callback(data); } return data; } /** * errorFunction * @description errorFunction * @param {Object} xhr HTTP STATUS * @param {Object} ajaxOptions the ajax option message * @param {String} thrownError the error message */ errorFunction = function (xhr, ajaxOptions, thrownError) { if (xhr.status != "201" && xhr.status != "204") { console.log(xhr.status); console.log(thrownError); window.alert("執行失敗,請聯絡客服人員。"); } else if (xhr.status == "401") { window.alert("登入時間超時,請重新登入!"); cookies.remove("JWT-Authorization"); location.href = "~/Login/Login"; } } /** * completeFunction * @description completeFunction */ completeFunction = function () { } /** * content type * @description content type */ contentType = { Json: 1, FormData : 2 } /** * enum * @description enum * @param {Object} eo object * @return {Object} enum */ enums(eo) { var enums = function () { }; for (var key in eo) { var enumObject = new enums(); enums[key] = eo[key]; } return enums; } /** * send * @description send * @param {Object} conType content type * @param {Object} sendData send data * @param {Function} successFunction the ajax success function * @param {Function} errorFunction the ajax error function * @return {Object} this */ send = function (conType = this.contentType.Json, sendData = null, successFunction = null, errorFunction = null) { //if (!CheckMultiSend(this.apiUrl, this.type, this.dataToString(this.sendData))) { //連點防呆 // return false; //} if (sendData) { this.sendData = sendData; } if (successFunction) { this.successFunction = successFunction; } if (errorFunction) { this.errorFunction = errorFunction; } if (conType === this.contentType.Json) { $.ajax({ url: this.apiUrl, async: this.isAsync, type: this.type, dataType: this.dataType, traditional: true, contentType: "application/json; charset=utf-8", data: this.dataToString(this.sendData), //cache: false, //processData: false, beforeSend: this.beforeSendFunction, success: (data) => this.successFunction(data, this.extSuccessFunction), error: this.errorFunction, complete: this.completeFunction, statusCode: { 201: (data) => this.successFunction(data, this.extSuccessFunction), } }); } else if (conType === this.contentType.FormData) { $.ajax({ url: this.apiUrl, async: this.isAsync, type: this.type, dataType: this.dataType, traditional: true, contentType: false, enctype: 'multipart/form-data', data: this.sendData, cache: false, processData: false, beforeSend: this.beforeSendFunction, success: this.successFunction, error: this.errorFunction, complete: this.completeFunction }); alert(conType); } else { console.error("the content type is null."); } return this; } } YourTeam.Ajax = YourTeam.Ajax || Ajax; var ckeckMultiSendArr = []; function CheckMultiSend(url, type, data) { //根據 url type data let text = String(url) + String(type); if (type != "GET") { if (ckeckMultiSendArr.indexOf(text) == -1) { ckeckMultiSendArr.unshift(text); let timer = setInterval(function () { ckeckMultiSendArr.splice(0); clearInterval(timer); }, 500) } else { return false } } return true; } //var YourTeamAjax = { // apiUrl: "", // isAsync: false, // type: "GET", // dataType: "JSON", // sendData: null, // dataToString: function (data) { // let strJsonData = data ? (data.Data ? JSON.stringify(data.Data) : null) : null; // return strJsonData; // }, // beforeSendFunction: function () { // $.blockUI({ message: '