core.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @author Scott Chen
  3. * @version 1.0
  4. * 2017-09-23 08:55
  5. */
  6. var svr_url = 'http://127.0.0.1:8090/ccnet';
  7. (function (window, $, undefined) {
  8. //--------------------------------- ajax 定义开始 ---------------------------------//
  9. /**
  10. * ajax
  11. * @constructor
  12. */
  13. var Ajax = function (options) {
  14. return new Ajax.fn.init(options);
  15. };
  16. Ajax.fn = Ajax.prototype = {
  17. defaults: {
  18. //是否返回全部消息(ajax不处理code,msg)
  19. returnAll: true,
  20. //处理服务端返回的data数据集合名称
  21. dataParamName: 'list',
  22. async: true,
  23. type: 'post',
  24. //contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  25. contentType: 'application/json; charset=utf-8',
  26. data: null,
  27. url: '',
  28. dataType: 'json'
  29. },
  30. constructor: Ajax,
  31. init: function(options) {
  32. options = options || {};
  33. this.options = $.extend({}, this.defaults, options);
  34. return this;
  35. },
  36. _ajax:function (param, callback, target, err_callback) {
  37. this.init(param);
  38. var options = this.options;
  39. var obj = {};
  40. obj.async = options.async;
  41. obj.type = options.type;
  42. obj.contentType = options.contentType;
  43. obj.data = options.data;
  44. obj.url = options.url;
  45. obj.dataType = options.dataType;
  46. if (options.jsonp && options.jsonp != "") {
  47. obj.jsonp = options.jsonp;
  48. }
  49. if (options.jsonpCallback && options.jsonpCallback != "") {
  50. obj.jsonpCallback = options.jsonpCallback;
  51. }
  52. obj.success = function (data, textStatus, jqXHR) {
  53. if (!data) {
  54. alert("服务端没有请求消息返回");
  55. return false;
  56. }
  57. if (!data.code) {
  58. alert("服务端返回result.code为空");
  59. return false;
  60. }
  61. //不作code过滤处理
  62. if(options.returnAll) {
  63. if (callback && typeof callback === 'function') {
  64. callback(data);
  65. return;
  66. }else{
  67. return data;
  68. }
  69. }
  70. //过虑处理code
  71. if (data.code == '0') {
  72. if(!data.data || (data.data && $.isEmptyObject(data.data))) {
  73. if (callback && typeof callback === 'function') {
  74. callback();
  75. }else{
  76. alert('【' + data.code + "】" + data.msg);
  77. }
  78. return;
  79. }
  80. if (data.data && typeof data.data === 'object') {
  81. var json = data.data;
  82. //请求返回
  83. if (callback && typeof callback === 'function') {
  84. if (target && typeof target === 'object') {
  85. callback(json, target);
  86. } else {
  87. callback(json);
  88. }
  89. return;
  90. } else {
  91. return json;
  92. }
  93. } else {
  94. alert("服务端返数据格式不能解析");
  95. return false;
  96. }
  97. } else {
  98. //code != '0'
  99. if(!data.data || (data.data && $.isEmptyObject(data.data))) {
  100. if (err_callback && typeof err_callback === 'function') {
  101. err_callback();
  102. }else{
  103. alert('【' + data.code + "】" + data.msg);
  104. }
  105. return;
  106. }
  107. if (data.data && typeof data.data === 'object') {
  108. var json = data.data;
  109. //请求返回
  110. if (err_callback && typeof err_callback === 'function') {
  111. err_callback(json);
  112. } else {
  113. alert('【' + data.code + "】" + data.msg);
  114. }
  115. return;
  116. } else {
  117. if (err_callback && typeof err_callback === 'function') {
  118. err_callback();
  119. } else {
  120. alert('【' + data.code + "】" + data.msg);
  121. }
  122. return;
  123. }
  124. }
  125. };
  126. obj.error = function (XMLHttpRequest, textStatus, errorThrown) {
  127. if (err_callback && typeof err_callback === 'function') {
  128. err_callback();
  129. return;
  130. } else {
  131. // 通常 textStatus 和 errorThrown 之中
  132. // 只有一个会包含信息
  133. // 调用本次AJAX请求时传递的options参数
  134. this;
  135. if(textStatus && textStatus.error) {
  136. alert(textStatus.error);
  137. return;
  138. }
  139. }
  140. };
  141. $.ajax(obj);
  142. }
  143. };
  144. Ajax.fn.init.prototype = Ajax.fn;
  145. var expose = function() {
  146. return {
  147. iAjax: function (param, callback, target, err_callback) {
  148. return Ajax.fn._ajax(param, callback, target, err_callback);
  149. }
  150. }
  151. }();
  152. window.top.Req = window.Req = expose;
  153. //--------------------------------- ajax 定义结束 ---------------------------------//
  154. })(window, jQuery);