/* *create by BinYiChen 2017/8/8 * 公用方法 */ var timeout = 60000;//超时时间,默认60S /** * ajax请求封装 * @param url 请求网址 * @param type 请求类型 post or get * @param timeout 请求超时时间 * @param async 是否异步 * @param data 数据 * @param dataType 数据类型 * @param successfn 成功方法 * @param errorfn 失败方法 * @param completefn 完成方法 */ function request(url, type, timeout, async, data, dataType, successfn, errorfn, completefn) { async = (async == null || async == "" || typeof(async) == "undefined") ? true : async; type = (type == null || type == "" || typeof(type) == "undefined") ? "post" : type; dataType = (dataType == null || dataType == "" || typeof(dataType) == "undefined") ? "json" : dataType; data = (data == null || data == "" || typeof(data) == "undefined") ? {} : data; var index = window.parent.layer.load(1, { shade: [0.1,'#fff'] //0.1透明度的白色背景 }); $.ajax({ url: url, type: type, timeout: timeout, async: async, headers: { "X-Xsrftoken": getXSRF() }, data: data, scriptCharset: 'utf-8', dataType: dataType, success: function (res) { window.parent.layer.close(index); if (typeof successfn == "function") successfn(res); }, error: function (res) { window.parent.layer.close(index); if (typeof errorfn == "function") errorfn(res); }, complete: function (res) { window.parent.layer.close(index); if (typeof completefn == "function") completefn(res); } }); } /** * post请求 * 默认异步请求,json格式传输 */ function postRequest(url, data, successfn, errorfn, completefn) { request(url, 'post', timeout, 'true', data, 'json', successfn, errorfn, completefn) } /** * get请求 * 默认异步请求,json格式传输 */ function getRequest(url, data, successfn, errorfn, completefn) { request(url, 'get', timeout, true, data, 'json', successfn, errorfn, completefn) } /** *同步请求 * 默认post请求,json格式传输 */ function synchRequest(url, data, successfn, errorfn, completefn) { request(url, 'post', timeout, false, data, 'json', successfn, errorfn, completefn) } /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2013 Klaus Hartl * Released under the MIT license */ (function (factory) { if (typeof define === 'function' && define.amd) { // AMD define(['jquery'], factory); } else if (typeof exports === 'object') { // CommonJS factory(require('jquery')); } else { // Browser globals factory(jQuery); } }(function ($) { var pluses = /\+/g; function encode(s) { return config.raw ? s : encodeURIComponent(s); } function decode(s) { return config.raw ? s : decodeURIComponent(s); } function stringifyCookieValue(value) { return encode(config.json ? JSON.stringify(value) : String(value)); } function parseCookieValue(s) { if (s.indexOf('"') === 0) { // This is a quoted cookie as according to RFC2068, unescape... s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); } try { // Replace server-side written pluses with spaces. // If we can't decode the cookie, ignore it, it's unusable. // If we can't parse the cookie, ignore it, it's unusable. s = decodeURIComponent(s.replace(pluses, ' ')); return config.json ? JSON.parse(s) : s; } catch(e) {} } function read(s, converter) { var value = config.raw ? s : parseCookieValue(s); return $.isFunction(converter) ? converter(value) : value; } var config = $.cookie = function (key, value, options) { // Write if (value !== undefined && !$.isFunction(value)) { options = $.extend({}, config.defaults, options); if (typeof options.expires === 'number') { var days = options.expires, t = options.expires = new Date(); t.setTime(+t + days * 864e+5); } return (document.cookie = [ encode(key), '=', stringifyCookieValue(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : '' ].join('')); } // Read var result = key ? undefined : {}; // To prevent the for loop in the first place assign an empty array // in case there are no cookies at all. Also prevents odd result when // calling $.cookie(). var cookies = document.cookie ? document.cookie.split('; ') : []; for (var i = 0, l = cookies.length; i < l; i++) { var parts = cookies[i].split('='); var name = decode(parts.shift()); var cookie = parts.join('='); if (key && key === name) { // If second argument (value) is a function it's a converter... result = read(cookie, value); break; } // Prevent storing a cookie that we couldn't decode. if (!key && (cookie = read(cookie)) !== undefined) { result[name] = cookie; } } return result; }; config.defaults = {}; $.removeCookie = function (key, options) { if ($.cookie(key) === undefined) { return false; } // Must not alter options, thus extending a fresh object... $.cookie(key, '', $.extend({}, options, { expires: -1 })); return !$.cookie(key); }; })); /** * HTTP返回状态码错误 * @param res */ function httpErrorMsg(res) { var showMsg = res.msg; //弹窗错误提示(暂留) window.parent.layer.msg(showMsg); } function serverErrorMsg(res) { var showMsg = res.msg; //弹窗错误提示(暂留) window.parent.layer.msg(showMsg); } // 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } /** * 时间戳转时间格式(以毫秒为单位) * @param nS * @returns {string} */ function getLocalTime(nS) { return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, ' '); } /** * 获取网址参数 * @param 网址 * @param key名称 * @returns {null} */ function getQueryString(url, name) { var searchIndex = url.indexOf("?"); var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = url.substr(searchIndex + 1).match(reg); if (r != null) return unescape(r[2]); return null; } function doFail(e) { serverErrorMsg(e); } function getXSRF() { var xsrf, xsrflist; xsrf = $.cookie("_xsrf"); if (xsrf == null || xsrf == "" || typeof(xsrf) == "undefined") { return ""; } xsrflist = xsrf.split("|"); var _xsrf = atob(xsrflist[0]); return _xsrf; }