common_http.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * 统一请求ajax发送方法
  3. * url 请求地址:例如:patient/health_index/add
  4. * params 请求参数
  5. * dataType 数据类型:json等
  6. * reqType 请求方式:get 或 post
  7. * error 请求失败处理方法
  8. * success 请求成功处理方法
  9. */
  10. function sendPost(url, params, dataType, reqType, error, success,timeOut) {
  11. var userAgent = window.localStorage.getItem(agentName);
  12. var serverUrl = "";
  13. if(url.indexOf("http://")>-1 || url.indexOf("https://")>-1 ) {
  14. serverUrl = url;
  15. } else {
  16. serverUrl = server + url;
  17. }
  18. //发送ajax请求
  19. $.ajax(serverUrl, {
  20. data: params,
  21. dataType: dataType,
  22. beforeSend: function(request) {
  23. request.setRequestHeader("userAgent", userAgent);
  24. },
  25. timeout: timeOut || 60000,
  26. type: reqType,
  27. error: function(res) {
  28. if(res.status == 999 || res.status == 998 || res.status == 997){
  29. loginUrl(res.status);
  30. return;
  31. }
  32. error(res);
  33. },
  34. success: function(res) {
  35. if(res.status == 999 || res.status == 998 || res.status == 997){
  36. loginUrl(res.status);
  37. return;
  38. }
  39. success(res);
  40. }
  41. });
  42. }
  43. function sendPostNoCache(url, params, dataType, reqType, error, success,timeOut) {
  44. var userAgent = window.localStorage.getItem(agentName);
  45. //发送ajax请求
  46. $.ajax(server + url, {
  47. data: params,
  48. dataType: dataType,
  49. cache: false,
  50. beforeSend: function(request) {
  51. request.setRequestHeader("userAgent", userAgent);
  52. },
  53. timeout: timeOut || 60000,
  54. type: reqType,
  55. error: function(res) {
  56. if(res.status == 999 || res.status == 998 || res.status == 997){
  57. loginUrl(res.status);
  58. return;
  59. }
  60. error(res);
  61. },
  62. success: function(res) {
  63. if(res.status == 999 || res.status == 998 || res.status == 997){
  64. loginUrl(res.status);
  65. return;
  66. }
  67. success(res);
  68. }
  69. });
  70. }
  71. /**
  72. * 统一请求ajax发送方法
  73. * url 请求地址:例如:patient/health_index/add
  74. * params 请求参数
  75. * dataType 数据类型:json等
  76. * reqType 请求方式:get 或 post
  77. * error 请求失败处理方法
  78. * success 请求成功处理方法
  79. */
  80. function sendPostAsync(url, params, dataType, reqType, error, success) {
  81. var userAgent = window.localStorage.getItem(agentName);
  82. //发送ajax请求
  83. $.ajax(server + url, {
  84. data: params,
  85. dataType: dataType,
  86. async:false,
  87. beforeSend: function(request) {
  88. request.setRequestHeader("userAgent", userAgent);
  89. },
  90. type: reqType,
  91. error: function(res) {
  92. if(res.status == 999 || res.status == 998 || res.status == 997){
  93. loginUrl(res.status);
  94. return;
  95. }
  96. error(res);
  97. },
  98. success: function(res) {
  99. if(res.status == 999 || res.status == 998 || res.status == 997){
  100. loginUrl(res.status);
  101. return;
  102. }
  103. success(res);
  104. }
  105. });
  106. }
  107. //重新登陆
  108. var count = 0;
  109. function loginUrl(status){
  110. var pageurl = "";
  111. // var hreftemp = server + "wx/html/home/html/zhmm-login.html?type=0&openid=";
  112. var hreftemp = "../../home/html/login.html?type=0&openid=";
  113. var content = "";
  114. count ++;
  115. try{
  116. if(d){
  117. d.close();
  118. }
  119. }catch(e){
  120. //TODO handle the exception
  121. }
  122. //如果有两个以上的请求会获得999等错误码的时候,只处理第一个
  123. if(count >= 2){
  124. return false;
  125. }
  126. if(status == 999){
  127. content = "此账号已在别处登录,需要请重新登录";
  128. }else if(status == 998){
  129. content = "登录超时,需要请重新登录";
  130. }else if(status == 997){
  131. content = "此账号未登录,需要请重新登录";
  132. // hreftemp=server + "wx/html/home/html/zhmm-login.html?type=0&openid="
  133. hreftemp = "../../home/html/login.html?type=0&openid=";
  134. }
  135. var openid = "";
  136. var userAgent = window.localStorage.getItem(agentName);
  137. if(userAgent){
  138. var jsonstr = $.parseJSON(userAgent);
  139. openid = jsonstr.openid;
  140. }
  141. if(openid==null||openid==""){
  142. Request = GetRequest();
  143. openid = Request["openid"];
  144. }
  145. clearAgent();
  146. //将保存访问路径的逻辑放到clearAgent后面
  147. pageurl = window.parent.location.href;
  148. saveAgentPage(pageurl);
  149. dialog({
  150. skin:"ui-dialog ax-popup pror",
  151. content: content,
  152. ok: function (){
  153. count = 0;
  154. window.parent.location.href = hreftemp+openid;
  155. }
  156. }).showModal();
  157. }
  158. // 获取异步请求处理Promise对象
  159. function getReqPromise(url, params, dataType, reqType) {
  160. return new Promise(function(resolve, reject) {
  161. sendPost(url, params,dataType, reqType,
  162. function queryFailed (xht, type, throwErr) {
  163. reject({
  164. xht:xht, type:type, throwErr:throwErr
  165. })
  166. }
  167. , function success(res) {
  168. resolve(res);
  169. });
  170. });
  171. }
  172. /*
  173. * reqs: 请求的参数数组,格式:[{url:'a/xxx', reqType: 'POST', data:{...}},{url:'b/xxx',data:{...}}]
  174. * */
  175. function getReqPromises(reqs) {
  176. if(!reqs || !reqs.length) {
  177. return new Promise(function(resolve, reject) {
  178. resolve([]);
  179. });
  180. }
  181. return Promise.all(_.map(reqs,function(param){
  182. return getReqPromise(param.url,param.data, 'json', param.reqType);
  183. }));
  184. };
  185. /*
  186. * 获取图片路径方法修改
  187. */
  188. function getImgUrl(str){
  189. if(typeof str != 'string'){
  190. return "";
  191. }
  192. if(str.length == 0){
  193. return "";
  194. }else{
  195. if(str.indexOf("../")>-1){
  196. //访问本地路径
  197. return str;
  198. }else if((str.indexOf("http://")>-1) || (str.indexOf("https://")>-1)){
  199. return str;
  200. }else{
  201. //服务器上的图片路径
  202. return imgUrlDomain + str;
  203. }
  204. }
  205. }