common_http.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //切换发布版本 true:正式版,连接生产环境;false:测试版,连接测试环境。
  2. var isInner = false; //发布线上后有内外网的配置
  3. var publish_version = false;
  4. // var publish_version = false;
  5. //接口服务器
  6. var server;
  7. var isLoginOut;//退出
  8. var agentName = 'wlyyAgent';
  9. if(publish_version == true) {
  10. //接口服务器
  11. server = "http://www.xmtyw.cn/wlyy/";
  12. //图片文件的服务器路径
  13. var imgUrlDomain = "http://www.xmtyw.cn/";
  14. if(isInner){
  15. server = "http://10.95.22.10:8011/wlyy/"
  16. imgUrlDomain = "http://10.95.22.10:8011/";
  17. }
  18. }else {
  19. //接口服务器
  20. server = "http://172.19.103.88:9092/wlyy/";
  21. // server = "http://192.168.131.24:8080/";//仕杰
  22. // server = "http://192.168.131.113:8080/";//俊杰
  23. // server = "http://192.168.131.102:8080/";//伟达
  24. // server = "http://192.168.131.133:8080/";//文杰
  25. // server = "http://192.168.131.131:8080/";//文彬
  26. // server = "http://192.168.131.127:8060/";//逸祥
  27. //图片文件的服务器路径
  28. var imgUrlDomain = "http://172.19.103.54/";
  29. }
  30. //弹出框通用
  31. function ask(message,ok,cancel) {
  32. if(!ok){ok = function(){return;}}
  33. art.dialog({lock: true,artIcon: 'ask',opacity: 0.4,width: 250,title: '提示',content: message,ok:ok,cancel:cancel})
  34. }
  35. function loading(message){
  36. if(!message){message="数据加载中,请稍候..."}
  37. return art.dialog({lock: true,show:true,content: '<img src="../widget/artDialog/4.1.7/images/loading.gif" class="mr10"/>'+message , tips:true})
  38. }
  39. function tip(message,icon) {
  40. if(!icon){icon='error'}
  41. art.dialog({lock: true,artIcon: icon,title: '提示',opacity: 0.4,width: 250,content: message,time:2})
  42. }
  43. //newWay 新的处理错误请求的标识
  44. function sendGet(url, params, custError, custSuccess,newWay,timeOut){
  45. sendPost(url, params, custError, custSuccess, "GET", timeOut, newWay);
  46. }
  47. //设备号
  48. var IMEI = localStorage.getItem('WLYY_IMEI') || uuid(16, 16)
  49. localStorage.setItem('WLYY_IMEI', IMEI)
  50. //请求头去除中文信息
  51. var userAgent = window.localStorage.getItem(agentName)
  52. var sendHead = null
  53. if(userAgent){
  54. var agentObj = JSON.parse(userAgent)
  55. agentObj.imei = localStorage.getItem('WLYY_IMEI')
  56. agentObj.platform = 4
  57. delete agentObj.name;
  58. delete agentObj.hospitalName;
  59. delete agentObj.currentUserRole.name;
  60. $.each(agentObj.userRole,function(i,o){
  61. delete o.name
  62. })
  63. sendHead = agentObj
  64. }
  65. /**
  66. * 统一请求ajax发送方法
  67. * url 请求地址:例如:patient/health_index/add
  68. * params 请求参数
  69. * dataType 数据类型:json等
  70. * reqType 请求方式:get 或 post
  71. * error 请求失败处理方法
  72. * success 请求成功处理方法
  73. * newWay 新的处理错误请求的标识
  74. */
  75. function sendPost(url, params, custError, custSuccess,dataType, timeOut, newWay) {
  76. if(dataType==undefined){
  77. dataType="POST"
  78. }
  79. if(isLoginOut) {
  80. return
  81. }
  82. //发送ajax请求
  83. console.log(server+url);
  84. // 增加绝对URL(含有http://及https://)的判断,以满足跨域请求(yzh)
  85. var reqUrl = server + url;
  86. if(url&&(url.indexOf("http://")>-1 || url.indexOf("https://")>-1)) {
  87. reqUrl = url;
  88. }
  89. $.ajax(reqUrl,$.extend({},{
  90. data:params || {},
  91. dataType: 'json',
  92. type: dataType,
  93. beforeSend: function(request) {
  94. var agent = sendHead || {
  95. imei: localStorage.getItem('WLYY_IMEI'),
  96. platform: 4
  97. }
  98. request.setRequestHeader("userAgent", JSON.stringify(agent));
  99. },
  100. timeout: timeOut || 60000,
  101. error: function(xht, type, throwErr) {
  102. console.log(type + " : " + throwErr);
  103. if(type=="timeout"){
  104. if(custError){
  105. custError(type);
  106. return ;
  107. }
  108. }
  109. if(custError && $.isFunction(custError)) {
  110. var flag = custError(xht, type, throwErr);
  111. if(!flag) {
  112. return ;
  113. }
  114. }
  115. if(newWay == undefined){ //旧的处理逻辑
  116. if(type == "timeout" || type == "abort") {
  117. // alert("程序开了点小差, 请稍后重试!")
  118. } else if(type == "error" || type == "parsererror" || type == "null") {
  119. // setTimeout(function() {
  120. // alert("程序开了点小差, 请稍后重试!")
  121. // },0);
  122. }
  123. }else if(newWay){
  124. alert("数据加载失败,请检查网络无误后下拉刷新。");
  125. }
  126. },
  127. success: function(res) {
  128. var tip = "";
  129. if(res.status == 999) {
  130. tip = "此账号已在别处登录,请重新登录";
  131. goToLogin(tip)
  132. return;
  133. } else if(res.status == 998) {
  134. tip = "登录超时,请重新登录";
  135. goToLogin(tip)
  136. return;
  137. } else if(res.status == 997) {
  138. tip = "此账号未登录,请先登录";
  139. goToLogin(tip)
  140. return;
  141. }else{
  142. custSuccess(res);
  143. }
  144. }
  145. }))
  146. }
  147. //跳转登录页
  148. function goToLogin(message){
  149. // window.localStorage.removeItem(agentName)
  150. top.ask(message,function(){
  151. top.location.href='/health-education/login.html'
  152. })
  153. }
  154. function uuid(len, radix) {
  155. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  156. var uuid = [],
  157. i;
  158. radix = radix || chars.length;
  159. if(len) {
  160. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  161. } else {
  162. var r;
  163. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  164. uuid[14] = '4';
  165. for(i = 0; i < 36; i++) {
  166. if(!uuid[i]) {
  167. r = 0 | Math.random() * 16;
  168. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  169. }
  170. }
  171. }
  172. return uuid.join('');
  173. }
  174. function getSysDate(plus) {
  175. var d = new Date();
  176. var year = d.getFullYear();
  177. var month = d.getMonth() + 1;
  178. var day = d.getDate();
  179. var h = d.getHours();
  180. var mins = d.getMinutes();
  181. var s = d.getSeconds();
  182. if(month < 10) month = "0" + month;
  183. if(day < 10) month = "0" + day;
  184. if(h < 10) h = "0" + h;
  185. if(mins < 10) mins = "0" + mins;
  186. if(s < 10) s = "0" + s;
  187. if(!plus) {
  188. return year + "-" + month + "-" + day + "" + h + ":" + mins + ":" + s;
  189. } else {
  190. return(year + 1) + "-" + month + "-" + day + "" + h + ":" + mins + ":" + s;
  191. }
  192. }
  193. function getSysDatePlus(plus) {
  194. var now = new Date();
  195. var d = new Date(now.getTime() + plus * 24 * 3600 * 1000);
  196. var year = d.getFullYear();
  197. var month = d.getMonth() + 1;
  198. var day = d.getDate();
  199. if(month < 10) month = "0" + month;
  200. if(day < 10) day = "0" + day;
  201. return year + "-" + month + "-" + day
  202. }
  203. //获取链接上的参数
  204. function GetRequest() {
  205. var url = location.search; //获取url中"?"符后的字串
  206. var theRequest = new Object();
  207. if (url.indexOf("?") != -1) {
  208. var str = url.substr(1);
  209. strs = str.split("&");
  210. for(var i = 0; i < strs.length; i ++) {
  211. theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
  212. }
  213. }
  214. return theRequest;
  215. }
  216. var Request = GetRequest();
  217. /*
  218. * 获取图片路径
  219. */
  220. function getImgUrl(str){
  221. if(typeof str != 'string'){
  222. return "";
  223. }
  224. if(str.length == 0){
  225. return "";
  226. }else{
  227. if(str.indexOf("../../../")>-1){
  228. //访问本地路径
  229. return str.replace('../../.','');
  230. }else if((str.indexOf("http://")>-1) || (str.indexOf("https://")>-1)){
  231. return str;
  232. }else{
  233. //服务器上的图片路径
  234. return imgUrlDomain + str
  235. }
  236. }
  237. }