var serverApi = ''; var friendsLinksList = []; var themeColor = ''; var logo = ''; var lastTime = 0.00; define(['jquery', 'dialog','jsencrypt'], function ($, dialog,JSEncrypt) { // 工具类 function Tool() { this.version = "1.0.0"; this.description = "这是一个工具类"; } var ajaxJsUrl = "/api"; $.extend(Tool.prototype, { /** * ajax post * @param url (String) * @param data (Json) 需要提交的数据 * @param cb(Function) 回调函数 * @param Bearer(Boolean) 是否需要Bearer,不需要传true */ doPost: function (url, data, cb, Bearer) { var _this = this; // data.deptId = 100 ; // console.log(data) var headAttribute = ''; if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); } } else { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token')) } } $.ajax({ url: ajaxJsUrl + url + '?=' + Math.random(), type: 'POST', data: JSON.stringify(data), dataType: 'json', contentType: "application/json", beforeSend: headAttribute, success: function (data) { var code = data.code; var msg = data.msg; if (code === 401) { _this.removeAllCookie(); _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () { _this.skip('/view/login/login.html') }, '重新登录', function () { }, "取消") } else if (code === 500) { _this.initError(msg) cb(data); } else if (code != 200) { _this.initError(msg) } else { cb(data); } } }); }, doPostSign: function (url, data, cb, Bearer) { var _this = this; // data.deptId = 100 ; // console.log(data) var headAttribute = ''; if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); } } else { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token')) } } $.ajax({ url: ajaxJsUrl + url + '?=' + Math.random(), type: 'POST', data: data, dataType: 'json', contentType: false, processData: false, // jQuery不要去处理发送的数据 success: function (data) { var code = data.code; var msg = data.msg; if (code === 401) { _this.removeAllCookie(); _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () { _this.skip('/view/login/login.html') }, '重新登录', function () { }, "取消") } else if (code === 500) { _this.initError(msg) cb(data); } else if (code != 200) { _this.initError(msg) } else { cb(data); } } }); }, /** * ajax post * @param url (String) * @param data (Json) 需要提交的数据 * @param cb(Function) 回调函数 * @param Bearer(Boolean) 是否需要Bearer,不需要传true */ doPostImg: function (url, data, cb, Bearer) { var _this = this; // data.deptId = 100 ; // console.log(data) var headAttribute = ''; if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); } } else { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token')) } } $.ajax({ url: ajaxJsUrl + url + '?=' + Math.random(), type: 'POST', data: JSON.stringify(data), dataType: 'json', traditional:true, contentType: "application/json", beforeSend: headAttribute, success: function (data) { var code = data.code; var msg = data.msg; if (code === 401) { _this.removeAllCookie(); _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () { _this.skip('/view/login/login.html') }, '重新登录', function () { }, "取消") } else if (code === 500) { _this.initError(msg) cb(data); } else if (code != 200) { _this.initError(msg) } else { cb(data); } } }); }, /** * ajax put * @param url (String) * @param data (Json) 需要提交的数据 * @param cb(Function) 回调函数 * @param Bearer(Boolean) 是否需要Bearer,不需要传true */ doPut: function (url, data, cb, Bearer) { var _this = this; var headAttribute = ''; if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); } } else { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token')) } } $.ajax({ url: ajaxJsUrl + url + '?=' + Math.random(), type: 'put', data: JSON.stringify(data), dataType: 'json', contentType: "application/json", beforeSend: headAttribute, success: function (data) { var code = data.code; var msg = data.msg; if (code === 401) { _this.removeAllCookie(); _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () { _this.skip('/view/login/login.html') }, '重新登录', function () { }, "取消") } else if (code === 500) { _this.initError(msg) cb(data); } else if (code != 200) { _this.initError(msg) } else { cb(data); } } }); }, /** * ajax get * @param url(String) * @param data(Json) 需要提交的数据 * @param cb(Function) 回调函数 * @param noHead(Boolean) 是否需要Bearer,不需要传true */ doGet: function (uri, data, cb, Bearer) { var _this = this; // deptId=100& var url = ajaxJsUrl + uri + '?'; var headAttribute = '' if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); } } else { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token')) } } if (data) { url += $.map(data, function (n, i) { if (i == data.length - 1) { return i + '=' + n; } else { return i + '=' + n + '&'; } }).join(''); } $.ajax({ url: url, type: 'GET', beforeSend: headAttribute, success: function (data) { var code = data.code; var msg = data.msg; if (code === 401) { _this.removeAllCookie(); _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () { _this.skip('/view/login/login.html') }, '重新登录', function () { }, "取消") } else if (code === 500) { _this.initError(msg) } else if (code != 200) { _this.initError(msg) } else { cb(data); } }, error: function (data) { console.log(data) } }); }, /** * ajax delete * @param url(String) * @param data(Json) 需要提交的数据 * @param cb(Function) 回调函数 * @param noHead(Boolean) 是否需要Bearer,不需要传true */ doDelete: function (uri, data, cb, Bearer) { var _this = this; var url = ajaxJsUrl + uri + '?'; var headAttribute = '' if (Bearer && Bearer == true || _this.getCookie('Admin-Token') == '') { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); } } else { headAttribute = function (xhr) { xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8'); xhr.setRequestHeader('Authorization', 'Bearer ' + _this.getCookie('Admin-Token')) } } if (data) { url += $.map(data, function (n, i) { if (i == data.length - 1) { return i + '=' + n; } else { return i + '=' + n + '&'; } }).join(''); } $.ajax({ url: url, type: 'delete', beforeSend: headAttribute, success: function (data) { var code = data.code; var msg = data.msg; if (code === 401) { _this.removeAllCookie(); _this.initDialog('系统提示', '登录状态已过期,您可以继续留在该页面,或者重新登录', function () { _this.skip('/view/login/login.html') }, '重新登录', function () { }, "取消") } else if (code === 500) { _this.initError(msg) } else if (code != 200) { _this.initError(msg) } else { cb(data); } }, error: function (data) { console.log(data) } }); }, /** * 创建dialog * @param id(String) 创建的dialog的Id 用于分辨唯一的dialog * @param title (String) dialog的title * @param content(String || HTMLElement) dialog的内容 * @param ok (Function) 点击确定按钮的回调函数 * @param cancel(Function) 点击取消按钮的回调函数 * 可以通过showModal来实现modal显示 */ initDialog: function (title, content, ok, okValue, cancel, cancelValue, id) { var initDialog = dialog({ id: id ? id : '', title: title, content: content, okValue: okValue ? okValue : '确定', ok: ok, cancelValue: cancelValue ? cancelValue : '取消', cancel: cancel, padding: 40, top: '150px' }); initDialog.show(); }, /** * 创建error dialog * @param errContent */ initError: function (errContent) { var error = dialog({ content: errContent, padding: 20 }); error.show(); var time = setTimeout(function () { error.remove(); }, 1500); }, /** * 创建dialog tips * @param tips * @param align */ initTips: function (tips, align, $target, times) { var tips = dialog({ content: tips, padding: 20, skin: 'min-dialog tips', quickClose: true, align: align }); tips.show($target); if (times == undefined || times == 'undefined' || times == '' || times == 'null' || times == null) { times = 2000 } else { times = times } setTimeout(function () { tips.close().remove(); }, times); }, /** * setCookie 设置cookie * @param cname 名称 * @param cvalue 值 * @param seconds 有效期 */ setCookie: function (cname, cvalue, seconds) { var d = new Date(); d.setTime(d.getTime() + (seconds * 1000)); var expires = "expires=" + d.toGMTString(); document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/"; }, /** * getCookie 获取cookie * @param cname 名称 */ getCookie: function (cname) { var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1); if (c.indexOf(name) != -1) return c.substring(name.length, c.length); } return ""; }, /** * removeCookie 删除cookie * @param cname 名称 */ removeCookie: function (cname) { var exp = new Date(); exp.setTime(exp.getTime() - 1); document.cookie = cname + "=; expires=" + exp.toGMTString(); }, /** * getParam 获取url参数 * @param key 当前url链接 */ getParam: function (key) { var json = {}, data; $.each(location.search.substr(1).split("&"), function (i, n) { data = n.split("="); json[data[0]] = data[1]; }); return key != undefined ? json[key] : json; }, /** * getParam 判断对象是否有内容 * @param obj 传入对象 */ isEmptyObject: function (obj) { for (var key in obj) { return false; } return true; }, /** * skip 页面跳转 * @param url 跳转页面链接 */ skip: function (url) { if (url != '') { window.location = url } }, /** * getNowFormatDate 获取时间 * @param */ getNowFormatDate: function () { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var day = date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (day >= 0 && day <= 9) { day = "0" + day; } if (hours >= 0 && hours <= 9) { hours = "0" + hours; } if (minutes >= 0 && minutes <= 9) { minutes = "0" + minutes; } if (seconds >= 0 && seconds <= 9) { seconds = "0" + seconds; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds; console.log(currentdate) return currentdate; }, /** * getNowFormatDateNew 获取时间 * @param */ getNowFormatDateNew: function (data) { var date = data; var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var day = date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (day >= 0 && day <= 9) { day = "0" + day; } if (hours >= 0 && hours <= 9) { hours = "0" + hours; } if (minutes >= 0 && minutes <= 9) { minutes = "0" + minutes; } if (seconds >= 0 && seconds <= 9) { seconds = "0" + seconds; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + day + " " + hours + seperator2 + minutes + seperator2 + seconds; console.log(currentdate) return currentdate; }, /** * getWebConfig 网站配置信息 * @param */ getWebConfig: function () { this.doGet(webConfig, {}, this.webConfigInformation, true) }, /** * webConfigInformation 网站配置信息 * @param */ webConfigInformation: function (data) { if (data.code == 200) { var content = data.data; console.log(content) // content[13].configValue = 'red'; if (content[13].configValue == 'red'){ themeColor = 'red'; document.documentElement.style.setProperty('--color', '#e8041f'); document.documentElement.style.setProperty('--icon', "url('../images/icon3_red.png')"); document.documentElement.style.setProperty('--logo', "url('../images/logo_red.png')"); logo = '../images/logo_red.png'; } if (content[13].configValue == 'green'){ themeColor = 'green'; document.documentElement.style.setProperty('--color', '#007b76'); document.documentElement.style.setProperty('--icon', "url('../images/icon3.png')"); document.documentElement.style.setProperty('--logo', "url('../images/logo.png')"); logo = '../images/logo.png'; } $("#webConfigName").html(content[0].configValue); $("#webConfigRecord").html(content[1].configValue + '' + content[2].configValue); $("#webConfigAddress").html(content[0].configValue + '' + content[3].configValue); serverApi = content[11].configValue; lastTime = parseFloat(content[8].configValue); } }, /** * removeAllCookie 清除所有Cookie * @param */ removeAllCookie: function () { var keys = document.cookie.match(/[^ =;]+(?==)/g) if (keys) { for (var i = keys.length; i--;) { document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn } location.reload(); } }, /** * encrypt 加密 * @param */ encrypt: function (txt) { const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n' + 'nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==' const encryptor = new JSEncrypt() encryptor.setPublicKey(publicKey) // 设置公钥 return encryptor.encrypt(txt) // 对数据进行加密 }, /** * encrypt 解密 * @param */ decrypt: function (txt) { const privateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n' + '7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n' + 'PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n' + 'kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n' + 'cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n' + 'DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n' + 'YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n' + 'UP8iWi1Qw0Y=' const encryptor = new JSEncrypt() encryptor.setPrivateKey(privateKey) // 设置私钥 return encryptor.decrypt(txt) // 对数据进行解密 }, format: function (time, format) { var t = new Date(time); var tf = function (i) { return (i < 10 ? '0' : '') + i }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function (a) { switch (a) { case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; } }) }, /** * 计算出相差天数 * @param secondSub */ formatTotalDateSub: function (secondSub) { var days = Math.floor(secondSub / (24 * 3600)); // 计算出小时数 var leave1 = secondSub % (24*3600) ; // 计算天数后剩余的毫秒数 var hours = Math.floor(leave1 / 3600); // 计算相差分钟数 var leave2 = leave1 % (3600); // 计算小时数后剩余的毫秒数 var minutes = Math.floor(leave2 / 60); // 计算相差秒数 var leave3 = leave2 % 60; // 计算分钟数后剩余的毫秒数 var seconds = Math.round(leave3); return days + "天" + hours + "时" + minutes + "分" + seconds + '秒'; } }); return Tool; });