sso

common.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. *create by BinYiChen 2017/8/8
  3. * 公用方法
  4. */
  5. var timeout = 60000;//超时时间,默认60S
  6. /**
  7. * ajax请求封装
  8. * @param url 请求网址
  9. * @param type 请求类型 post or get
  10. * @param timeout 请求超时时间
  11. * @param async 是否异步
  12. * @param data 数据
  13. * @param dataType 数据类型
  14. * @param successfn 成功方法
  15. * @param errorfn 失败方法
  16. * @param completefn 完成方法
  17. */
  18. function request(url, type, timeout, async, data, dataType, successfn, errorfn, completefn) {
  19. async = (async == null || async == "" || typeof(async) == "undefined") ? true : async;
  20. type = (type == null || type == "" || typeof(type) == "undefined") ? "post" : type;
  21. dataType = (dataType == null || dataType == "" || typeof(dataType) == "undefined") ? "json" : dataType;
  22. data = (data == null || data == "" || typeof(data) == "undefined") ? {} : data;
  23. var index = window.parent.layer.load(1, {
  24. shade: [0.1,'#fff'] //0.1透明度的白色背景
  25. });
  26. $.ajax({
  27. url: url,
  28. type: type,
  29. timeout: timeout,
  30. async: async,
  31. headers: {
  32. "X-Xsrftoken": getXSRF()
  33. },
  34. data: data,
  35. scriptCharset: 'utf-8',
  36. dataType: dataType,
  37. success: function (res) {
  38. window.parent.layer.close(index);
  39. if (typeof successfn == "function")
  40. successfn(res);
  41. },
  42. error: function (res) {
  43. window.parent.layer.close(index);
  44. if (typeof errorfn == "function")
  45. errorfn(res);
  46. },
  47. complete: function (res) {
  48. window.parent.layer.close(index);
  49. if (typeof completefn == "function")
  50. completefn(res);
  51. }
  52. });
  53. }
  54. /**
  55. * post请求
  56. * 默认异步请求,json格式传输
  57. */
  58. function postRequest(url, data, successfn, errorfn, completefn) {
  59. request(url, 'post', timeout, 'true', data, 'json', successfn, errorfn, completefn)
  60. }
  61. /**
  62. * get请求
  63. * 默认异步请求,json格式传输
  64. */
  65. function getRequest(url, data, successfn, errorfn, completefn) {
  66. request(url, 'get', timeout, true, data, 'json', successfn, errorfn, completefn)
  67. }
  68. /**
  69. *同步请求
  70. * 默认post请求,json格式传输
  71. */
  72. function synchRequest(url, data, successfn, errorfn, completefn) {
  73. request(url, 'post', timeout, false, data, 'json', successfn, errorfn, completefn)
  74. }
  75. /*!
  76. * jQuery Cookie Plugin v1.4.1
  77. * https://github.com/carhartl/jquery-cookie
  78. *
  79. * Copyright 2013 Klaus Hartl
  80. * Released under the MIT license
  81. */
  82. (function (factory) {
  83. if (typeof define === 'function' && define.amd) {
  84. // AMD
  85. define(['jquery'], factory);
  86. } else if (typeof exports === 'object') {
  87. // CommonJS
  88. factory(require('jquery'));
  89. } else {
  90. // Browser globals
  91. factory(jQuery);
  92. }
  93. }(function ($) {
  94. var pluses = /\+/g;
  95. function encode(s) {
  96. return config.raw ? s : encodeURIComponent(s);
  97. }
  98. function decode(s) {
  99. return config.raw ? s : decodeURIComponent(s);
  100. }
  101. function stringifyCookieValue(value) {
  102. return encode(config.json ? JSON.stringify(value) : String(value));
  103. }
  104. function parseCookieValue(s) {
  105. if (s.indexOf('"') === 0) {
  106. // This is a quoted cookie as according to RFC2068, unescape...
  107. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  108. }
  109. try {
  110. // Replace server-side written pluses with spaces.
  111. // If we can't decode the cookie, ignore it, it's unusable.
  112. // If we can't parse the cookie, ignore it, it's unusable.
  113. s = decodeURIComponent(s.replace(pluses, ' '));
  114. return config.json ? JSON.parse(s) : s;
  115. } catch(e) {}
  116. }
  117. function read(s, converter) {
  118. var value = config.raw ? s : parseCookieValue(s);
  119. return $.isFunction(converter) ? converter(value) : value;
  120. }
  121. var config = $.cookie = function (key, value, options) {
  122. // Write
  123. if (value !== undefined && !$.isFunction(value)) {
  124. options = $.extend({}, config.defaults, options);
  125. if (typeof options.expires === 'number') {
  126. var days = options.expires, t = options.expires = new Date();
  127. t.setTime(+t + days * 864e+5);
  128. }
  129. return (document.cookie = [
  130. encode(key), '=', stringifyCookieValue(value),
  131. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  132. options.path ? '; path=' + options.path : '',
  133. options.domain ? '; domain=' + options.domain : '',
  134. options.secure ? '; secure' : ''
  135. ].join(''));
  136. }
  137. // Read
  138. var result = key ? undefined : {};
  139. // To prevent the for loop in the first place assign an empty array
  140. // in case there are no cookies at all. Also prevents odd result when
  141. // calling $.cookie().
  142. var cookies = document.cookie ? document.cookie.split('; ') : [];
  143. for (var i = 0, l = cookies.length; i < l; i++) {
  144. var parts = cookies[i].split('=');
  145. var name = decode(parts.shift());
  146. var cookie = parts.join('=');
  147. if (key && key === name) {
  148. // If second argument (value) is a function it's a converter...
  149. result = read(cookie, value);
  150. break;
  151. }
  152. // Prevent storing a cookie that we couldn't decode.
  153. if (!key && (cookie = read(cookie)) !== undefined) {
  154. result[name] = cookie;
  155. }
  156. }
  157. return result;
  158. };
  159. config.defaults = {};
  160. $.removeCookie = function (key, options) {
  161. if ($.cookie(key) === undefined) {
  162. return false;
  163. }
  164. // Must not alter options, thus extending a fresh object...
  165. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  166. return !$.cookie(key);
  167. };
  168. }));
  169. /**
  170. * HTTP返回状态码错误
  171. * @param res
  172. */
  173. function httpErrorMsg(res) {
  174. var showMsg = res.msg;
  175. //弹窗错误提示(暂留)
  176. window.parent.layer.msg(showMsg);
  177. }
  178. function serverErrorMsg(res) {
  179. var showMsg = res.msg;
  180. //弹窗错误提示(暂留)
  181. window.parent.layer.msg(showMsg);
  182. }
  183. // 对Date的扩展,将 Date 转化为指定格式的String
  184. // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  185. // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  186. // 例子:
  187. // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  188. // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  189. Date.prototype.Format = function (fmt) { //author: meizz
  190. var o = {
  191. "M+": this.getMonth() + 1, //月份
  192. "d+": this.getDate(), //日
  193. "h+": this.getHours(), //小时
  194. "m+": this.getMinutes(), //分
  195. "s+": this.getSeconds(), //秒
  196. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  197. "S": this.getMilliseconds() //毫秒
  198. };
  199. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  200. for (var k in o)
  201. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  202. return fmt;
  203. }
  204. /**
  205. * 时间戳转时间格式(以毫秒为单位)
  206. * @param nS
  207. * @returns {string}
  208. */
  209. function getLocalTime(nS) {
  210. return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, ' ');
  211. }
  212. /**
  213. * 获取网址参数
  214. * @param 网址
  215. * @param key名称
  216. * @returns {null}
  217. */
  218. function getQueryString(url, name) {
  219. var searchIndex = url.indexOf("?");
  220. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  221. var r = url.substr(searchIndex + 1).match(reg);
  222. if (r != null) return unescape(r[2]); return null;
  223. }
  224. function doFail(e) {
  225. serverErrorMsg(e);
  226. }
  227. function getXSRF() {
  228. var xsrf, xsrflist;
  229. xsrf = $.cookie("_xsrf");
  230. if (xsrf == null || xsrf == "" || typeof(xsrf) == "undefined") {
  231. return "";
  232. }
  233. xsrflist = xsrf.split("|");
  234. var _xsrf = atob(xsrflist[0]);
  235. return _xsrf;
  236. }