You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

278 lines
9.6 KiB

  1. /*
  2. * 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园(http://www.learun.cn)
  3. * Copyright (c) 2013-2018 北京泉江科技有限公司
  4. * 创建人:陈彬彬
  5. * 日 期:2017.03.16
  6. * 描 述:ajax操作方法
  7. */
  8. (function ($, learun) {
  9. "use strict";
  10. var httpCode = {
  11. success: 200,
  12. fail: 400,
  13. exception: 500
  14. };
  15. var exres = { code: httpCode.exception, info: '通信异常,请联系管理员!' }
  16. $.extend(learun, {
  17. // http 通信异常的时候调用此方法
  18. httpErrorLog: function (msg) {
  19. learun.log(msg);
  20. },
  21. // http请求返回数据码
  22. httpCode: httpCode,
  23. // get请求方法(异步):url地址,callback回调函数
  24. httpAsyncGet: function (url, callback) {
  25. $.ajax({
  26. url: url,
  27. type: "GET",
  28. dataType: "json",
  29. async: true,
  30. cache: false,
  31. success: function (data) {
  32. if (data.code == learun.httpCode.exception) {
  33. learun.httpErrorLog(data.info);
  34. data.info = '系统异常,请联系管理员!';
  35. }
  36. callback(data);
  37. },
  38. error: function (XMLHttpRequest, textStatus, errorThrown) {
  39. learun.httpErrorLog(textStatus);
  40. callback(exres);
  41. },
  42. beforeSend: function () {
  43. },
  44. complete: function () {
  45. }
  46. });
  47. },
  48. httpAsyncGetWithParam: function (url, param, callback) {
  49. $.ajax({
  50. url: url,
  51. type: "GET",
  52. dataType: "json",
  53. data: param,
  54. async: true,
  55. cache: false,
  56. success: function (data) {
  57. if (data.code == learun.httpCode.exception) {
  58. learun.httpErrorLog(data.info);
  59. data.info = '系统异常,请联系管理员!';
  60. }
  61. callback(data);
  62. },
  63. error: function (XMLHttpRequest, textStatus, errorThrown) {
  64. learun.httpErrorLog(textStatus);
  65. callback(exres);
  66. },
  67. beforeSend: function () {
  68. },
  69. complete: function () {
  70. }
  71. });
  72. },
  73. // get请求方法(同步):url地址,param参数
  74. httpGet: function (url, param) {
  75. var res = {};
  76. $.ajax({
  77. url: url,
  78. data: param,
  79. type: "GET",
  80. dataType: "json",
  81. async: false,
  82. cache: false,
  83. success: function (data) {
  84. if (data.code == learun.httpCode.exception) {
  85. learun.httpErrorLog(data.info);
  86. data.info = '系统异常,请联系管理员!';
  87. }
  88. res = data;
  89. },
  90. error: function (XMLHttpRequest, textStatus, errorThrown) {
  91. learun.httpErrorLog(textStatus);
  92. },
  93. beforeSend: function () {
  94. },
  95. complete: function () {
  96. }
  97. });
  98. return res;
  99. },
  100. // post请求方法(异步):url地址,param参数,callback回调函数
  101. httpAsyncPost: function (url, param, callback) {
  102. $.ajax({
  103. url: url,
  104. data: param,
  105. type: "POST",
  106. dataType: "json",
  107. async: true,
  108. cache: false,
  109. success: function (data) {
  110. if (data.code == learun.httpCode.exception) {
  111. learun.httpErrorLog(data.info);
  112. data.info = '系统异常,请联系管理员!';
  113. }
  114. callback(data);
  115. },
  116. error: function (XMLHttpRequest, textStatus, errorThrown) {
  117. learun.httpErrorLog(textStatus);
  118. callback(exres);
  119. },
  120. beforeSend: function () {
  121. },
  122. complete: function () {
  123. }
  124. });
  125. },
  126. // post请求方法(同步步):url地址,param参数,callback回调函数
  127. httpPost: function (url, param, callback) {
  128. $.ajax({
  129. url: url,
  130. data: param,
  131. type: "POST",
  132. dataType: "json",
  133. async: false,
  134. cache: false,
  135. success: function (data) {
  136. if (data.code == learun.httpCode.exception) {
  137. learun.httpErrorLog(data.info);
  138. data.info = '系统异常,请联系管理员!';
  139. }
  140. callback(data);
  141. },
  142. error: function (XMLHttpRequest, textStatus, errorThrown) {
  143. learun.httpErrorLog(textStatus);
  144. callback(exres);
  145. },
  146. beforeSend: function () {
  147. },
  148. complete: function () {
  149. }
  150. });
  151. },
  152. // ajax 异步封装
  153. httpAsync: function (type, url, param, callback) {
  154. $.ajax({
  155. url: url,
  156. data: param,
  157. type: type,
  158. dataType: "json",
  159. async: true,
  160. cache: false,
  161. success: function (res) {
  162. if (res.code == learun.httpCode.success) {
  163. callback(res.data);
  164. }
  165. else {
  166. learun.httpErrorLog(res.info);
  167. callback(null);
  168. }
  169. },
  170. error: function (XMLHttpRequest, textStatus, errorThrown) {
  171. learun.httpErrorLog(textStatus);
  172. callback(null);
  173. },
  174. beforeSend: function () {
  175. },
  176. complete: function () {
  177. }
  178. });
  179. },
  180. //同步执行
  181. httpSync: function (type, url, param, callback) {
  182. $.ajax({
  183. url: url,
  184. data: param,
  185. type: type,
  186. dataType: "json",
  187. async: false,
  188. cache: false,
  189. success: function (res) {
  190. if (res.code == learun.httpCode.success) {
  191. callback(res.data);
  192. }
  193. else {
  194. learun.httpErrorLog(res.info);
  195. callback(null);
  196. }
  197. },
  198. error: function (XMLHttpRequest, textStatus, errorThrown) {
  199. learun.httpErrorLog(textStatus);
  200. callback(null);
  201. },
  202. beforeSend: function () {
  203. },
  204. complete: function () {
  205. }
  206. });
  207. },
  208. deleteForm:function (url, param, callback) {
  209. learun.loading(true, '正在删除数据');
  210. learun.httpAsyncPost(url, param, function (res) {
  211. learun.loading(false);
  212. if (res.code == learun.httpCode.success) {
  213. if (!!callback) {
  214. callback(res);
  215. }
  216. learun.alert.success(res.info);
  217. }
  218. else {
  219. learun.alert.error(res.info);
  220. learun.httpErrorLog(res.info);
  221. }
  222. layer.close(layer.index);
  223. });
  224. },
  225. postForm:function (url, param, callback) {
  226. learun.loading(true, '正在提交数据');
  227. learun.httpAsyncPost(url, param, function (res) {
  228. learun.loading(false);
  229. if (res.code == learun.httpCode.success) {
  230. if (!!callback) {
  231. callback(res);
  232. }
  233. learun.alert.success(res.info);
  234. }
  235. else {
  236. learun.alert.error(res.info);
  237. learun.httpErrorLog(res.info);
  238. }
  239. layer.close(layer.index);
  240. });
  241. },
  242. getForm: function (url,callback) {
  243. learun.loading(true, '请稍后');
  244. learun.httpAsyncGet(url, function (res) {
  245. learun.loading(false);
  246. if (res.code == learun.httpCode.success) {
  247. if (!!callback) {
  248. callback(res);
  249. }
  250. learun.alert.success(res.info);
  251. }
  252. else {
  253. learun.alert.error(res.info);
  254. learun.httpErrorLog(res.info);
  255. }
  256. layer.close(layer.index);
  257. });
  258. },
  259. //提交数据不提示正在提交
  260. postFormSilence: function (url, param, callback) {
  261. learun.httpAsyncPost(url, param, function (res) {
  262. learun.loading(false);
  263. if (res.code == learun.httpCode.success) {
  264. if (!!callback) {
  265. callback(res);
  266. }
  267. }
  268. else {
  269. learun.alert.error(res.info);
  270. learun.httpErrorLog(res.info);
  271. }
  272. });
  273. }
  274. });
  275. })(window.jQuery, top.learun);