| New file |
| | |
| | | import config from '../../config/systemconfig' |
| | | |
| | | let mask; //é®ç½©å±å¯¹è±¡ |
| | | |
| | | /** |
| | | * æå¼é®ç½©å±ï¼å¾
å®ç°ï¼ |
| | | * @param {String} msg æç¤ºæ¶æ¯ |
| | | * @param {Menu} type é»è®¤0ï¼åéæï¼ï¼1ãä¸éæï¼2ãå®å
¨éæ |
| | | */ |
| | | let maskOn = function (msg, type) { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * å
³éé®ç½©å±ï¼å¾
å®ç°ï¼ |
| | | */ |
| | | let maskOff = function () { |
| | | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param {Json Object} options 请æ±åæ° |
| | | * @returns å¤çåçåæ°æè®¾ç½®äºé»è®¤å¼çåæ° |
| | | */ |
| | | let setRequestParam = function (options) { |
| | | let requestConfig = { |
| | | baseUrl: config.Host, |
| | | url: '', |
| | | data: {}, |
| | | async: config.RequestAsync, |
| | | timeout: config.RequestTimeOut, |
| | | responseType: 'json', |
| | | contentType: config.ContentType, |
| | | headers: {}, |
| | | success: function (data) { }, |
| | | error: function (xhr, response, msg) { }, |
| | | before: function () { }, |
| | | after: function () { } |
| | | }; |
| | | |
| | | //baseUrl |
| | | if (!!options.baseUrl) { |
| | | requestConfig.baseUrl = options.baseUrl; |
| | | } |
| | | //baseUrl end |
| | | |
| | | //Content-Type |
| | | if (!!options.contentType) { |
| | | requestConfig.contentType = options.contentType; |
| | | } |
| | | //Content-Type end |
| | | |
| | | //url |
| | | if (!options.url) { |
| | | console.error('urlåæ°ä¸è½ä¸ºç©º'); |
| | | } |
| | | else { |
| | | requestConfig.url = options.url; |
| | | } |
| | | //url end |
| | | |
| | | //data |
| | | if (!!options.data) { |
| | | requestConfig.data = options.data; |
| | | } |
| | | //data end |
| | | |
| | | //headers |
| | | if (!!options.headers) { |
| | | requestConfig.headers = options.headers; |
| | | } |
| | | //header end |
| | | |
| | | //responseType |
| | | if (!!options.responseType) { |
| | | requestConfig.responseType = options.response; |
| | | } |
| | | //responseType end |
| | | |
| | | //before |
| | | if (!!options.before) { |
| | | requestConfig.before = function () { |
| | | options.before(); |
| | | } |
| | | } |
| | | //before end |
| | | |
| | | //after |
| | | if (!!options.after) { |
| | | requestConfig.after = function () { |
| | | options.after(); |
| | | } |
| | | } |
| | | //after end |
| | | |
| | | //async |
| | | if (options.async != undefined && options.async != null) { |
| | | requestConfig.async = options.async; |
| | | } |
| | | //async end |
| | | |
| | | //success |
| | | if (!!options.success) { |
| | | requestConfig.success = function (data, xhr) { |
| | | options.success(data, xhr); |
| | | } |
| | | } |
| | | else { |
| | | requestConfig.success = function (data, xhr) { |
| | | console.log(xhr.responseText); |
| | | } |
| | | } |
| | | //success end |
| | | |
| | | //error |
| | | if (!!options.error) { |
| | | requestConfig.error = function (xhr, response, msg) { |
| | | options.error(xhr, response, msg); |
| | | } |
| | | } |
| | | else { |
| | | requestConfig.error = function (xhr) { |
| | | if (xhr.status % 100 > 10 && xhr.status != 415) { |
| | | //å端èªå®ä¹è¿åçé误ç |
| | | try { |
| | | let json = JSON.parse(xhr.responseText); |
| | | if (!!json.msg) { |
| | | _alert(xhr.msg, "error"); |
| | | } |
| | | } |
| | | catch (err) { |
| | | _alert(xhr.responseText, "error"); |
| | | } |
| | | } |
| | | else { |
| | | //颿ä¹å¤ç |
| | | _alert("ç½ç»è¿æ¥é误ï¼è¯·å·æ°é¡µé¢ï¼", "error"); |
| | | } |
| | | } |
| | | } |
| | | //error end |
| | | |
| | | return requestConfig; |
| | | } |
| | | |
| | | /** |
| | | * å®ä¾åä¸ä¸ªxhr对象 |
| | | * @param {String} type GET/POSTç |
| | | * @param {Json Object} requestConfig å¤çåç请æ±é
ç½® |
| | | * @returns ä¸ä¸ªå³å°å¯ä»¥åéçxhr对象 |
| | | */ |
| | | let setXhrRequest = function (type, requestConfig) { |
| | | let xhr = new XMLHttpRequest(); |
| | | |
| | | //弿¥æåå¨è¶
æ¶ï¼åæ¥æ è¶
æ¶éå¶ |
| | | if (requestConfig.async) { |
| | | xhr.timeout = requestConfig.timeout; |
| | | } else { } |
| | | //弿¥æåå¨è¶
æ¶ï¼åæ¥æ è¶
æ¶éå¶ end |
| | | |
| | | xhr.onreadystatechange = function () { //å½è¯·æ±ç¶ææ¹åæ¶è§¦åæ¤äºä»¶ |
| | | if (xhr.readyState == 4) { //请æ±ç»æå |
| | | if (xhr.status >= 200 && xhr.status < 300) { |
| | | //æåï¼ |
| | | if (requestConfig.responseType.toLowerCase() == 'json') { |
| | | let data = JSON.parse(xhr.responseText); |
| | | requestConfig.success(data, xhr); |
| | | } |
| | | else if (requestConfig.responseType.toLowerCase() == 'text' |
| | | || requestConfig.responseType.toLowerCase() == 'string') { |
| | | requestConfig.success(xhr.responseText, xhr); |
| | | } |
| | | else { |
| | | requestConfig.success(xhr.responseText, xhr); |
| | | } |
| | | |
| | | } |
| | | else if (xhr.status >= 300 && xhr.status < 400) { |
| | | //éå®åï¼å¾
éå°åå¤ç |
| | | } |
| | | else if (xhr.status >= 100 && xhr < 200) { |
| | | //è¿æ¥ä¸,å¾
éå°åå¤ç |
| | | } |
| | | else { |
| | | //å
¶ä»ï¼å
å«400ç³»åã500ç³»å |
| | | requestConfig.error(xhr, xhr.response, xhr.responseText); |
| | | } |
| | | } |
| | | } |
| | | if (type.toLowerCase() == "get") { |
| | | let url = requestConfig.baseUrl + requestConfig.url; |
| | | if(requestConfig.url.indexOf("://")){//urlå
嫿ä¸å®çåè®®æ¶ï¼åªåurlï¼ä¸åbaseUrl |
| | | url = requestConfig.url; |
| | | } |
| | | if (!!requestConfig.data && JSON.stringify(requestConfig.data) != "{}") { |
| | | if (JSON.stringify(requestConfig.data).length > 4000) { |
| | | throw "Get请æ±éå¶æå¤§ä¼ è¾4000åèï¼"; |
| | | } |
| | | url = url + "?" + convertToUrl(requestConfig.data); |
| | | } |
| | | xhr.open(type, url, requestConfig.async); |
| | | } |
| | | else { |
| | | xhr.setRequestHeader("Content-Type", requestConfig.contentType); |
| | | xhr.open(type, requestConfig.baseUrl + requestConfig.url, requestConfig.async); |
| | | } |
| | | //设置headers |
| | | if (!!requestConfig.headers) { |
| | | for (var key in requestConfig.headers) { |
| | | xhr.setRequestHeader(key, requestConfig.headers[key]); |
| | | } |
| | | } |
| | | if (!!config.RequestHeaders) { |
| | | for (var key in config.RequestHeaders) { |
| | | xhr.setRequestHeader(key, config.RequestHeaders[key]); |
| | | } |
| | | } |
| | | //设置headers end |
| | | return xhr; |
| | | } |
| | | |
| | | /** |
| | | * å°å¯¹è±¡åæ°è½¬æurlåæ° |
| | | * @param {Object} obj å¾
转æ¢ç对象 |
| | | * @returns a=1&b=2&c=%7B%22a%22:1,%22b%22:2,%22c%22:%5B%7B%22aa%22:11,%22bb%22:22%7D%5D%7D |
| | | */ |
| | | let convertToUrl = function (obj) { |
| | | if (Object.prototype.toString.call(obj) == '[object Array]') { |
| | | return "=" + encodeURIComponent(JSON.stringify(obj)); |
| | | } |
| | | else if (Object.prototype.toString.call(obj) == '[object Undefined]' |
| | | || Object.prototype.toString.call(obj) == '[object Function]') { |
| | | return "="; |
| | | } |
| | | else if (Object.prototype.toString.call(obj) == '[object Number]' |
| | | || Object.prototype.toString.call(obj) == '[object Boolean]') { |
| | | return "=" + obj; |
| | | } |
| | | else if (Object.prototype.toString.call(obj) == '[object String]' |
| | | && obj.indexOf('&') > -1) { |
| | | return encodeURI(obj); |
| | | } |
| | | else if (Object.prototype.toString.call(obj) == '[object String]' |
| | | && obj.indexOf('?') == 0) { |
| | | return encodeURI(obj) |
| | | } |
| | | else if (Object.prototype.toString.call(obj) == '[object String]' |
| | | && obj.indexOf('&') == -1) { |
| | | return "=" + encodeURI(obj); |
| | | } |
| | | var arr = []; |
| | | for (var p in obj) |
| | | if (obj.hasOwnProperty(p)) { |
| | | if (Object.prototype.toString.call(obj[p]) == '[object Array]' |
| | | || Object.prototype.toString.call(obj[p]) == '[object Object]') { |
| | | arr.push(encodeURI(p) + "=" + encodeURI(JSON.stringify(obj))); |
| | | } else { |
| | | arr.push(encodeURI(p) + "=" + encodeURI(obj[p])); |
| | | } |
| | | } |
| | | return arr.join("&"); |
| | | } |
| | | |
| | | |
| | | let out = { |
| | | setRequestParam, |
| | | setXhrRequest, |
| | | convertToUrl, |
| | | maskOn, |
| | | maskOff |
| | | } |
| | | |
| | | export default out; |