common_http.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. //切换发布版本 true:正式版,连接生产环境;false:测试版,连接测试环境。
  2. var publish_version = true;
  3. //var publish_version = false;
  4. //是否是在公司总部的演示环境
  5. var isdemo = false;
  6. //var isdemo = false;
  7. //接口服务器
  8. var server,
  9. appId,
  10. curr_app_version = 1, //app当前内定版本号
  11. checkReload = true,
  12. //图片识别请求路径
  13. ocrReqUrl = "",
  14. //健康档案接口服务器
  15. healthProfileServer = "",
  16. //图片文件的服务器路径
  17. imgUrlDomain = "";
  18. //设置全局变量
  19. var CITY_CODE = "450800", //"450800"
  20. CITY_NAME = "贵港市"; //贵港市
  21. if(publish_version == true) {
  22. appId = "wxad04e9c4c5255acf";
  23. //接口服务器
  24. server = server = "http://ggijk.yihu.com/wlyy/";
  25. // 图片识别请求路径
  26. ocrReqUrl = "http://ggijk.yihu.com/ocr/";
  27. // 健康档案接口服务器
  28. healthProfileServer = server;
  29. imgUrlDomain = "http://ggijk.yihu.com/";
  30. }
  31. else if(isdemo == true) {
  32. appId = "wxeb86020158cf9141";
  33. //接口服务器
  34. server = "http://ehr.yihu.com/ssgg/";
  35. // OCR测试地址
  36. ocrReqUrl = "http://www.xmtyw.cn/ocr/";
  37. // 健康档案接口服务器
  38. healthProfileServer = server;
  39. //图片文件的服务器路径
  40. imgUrlDomain = "http://www.xmtyw.cn/";
  41. }
  42. else {
  43. appId = "wxd03f859efdf0873d";
  44. // OCR测试地址
  45. ocrReqUr = "http://192.168.131.132:3000/ocr";
  46. //接口服务器
  47. // server = "http://172.19.103.88:9092/wlyy/";
  48. server = "http://192.168.131.24:8082/";
  49. // 健康档案接口服务器
  50. healthProfileServer = server;
  51. //图片文件的服务器路径
  52. imgUrlDomain = "http://172.19.103.54/";
  53. }
  54. function showErrorPage (msg) {
  55. plus.nativeUI.closeWaiting();
  56. var errorWebview = plus.webview.getWebviewById('error');
  57. if(errorWebview) {
  58. mui.fire(errorWebview, "refresh", {errorMsg: msg});
  59. } else {
  60. mui.openWindow({
  61. id: "error",
  62. url: "../../error/html/error.html",
  63. waiting:{
  64. autoShow:false
  65. },
  66. extras: {
  67. errorMsg: msg
  68. }
  69. });
  70. }
  71. }
  72. function checkNetOrbackendError() {
  73. var noNetwork = plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE;
  74. setTimeout(function() {
  75. showErrorPage(noNetwork?"网络有点问题哦,检查网络无误后,":"程序开了点小差, 请稍后重试!")
  76. },0);
  77. return noNetwork;
  78. }
  79. var isLoginOut;
  80. //newWay 新的处理错误请求的标识
  81. function sendGet(url, params, custError, custSuccess,newWay,timeOut){
  82. sendPost(url, params, custError, custSuccess, "GET", timeOut, newWay);
  83. }
  84. /**
  85. * 统一请求ajax发送方法
  86. * url 请求地址:例如:patient/health_index/add
  87. * params 请求参数
  88. * dataType 数据类型:json等
  89. * reqType 请求方式:get 或 post
  90. * error 请求失败处理方法
  91. * success 请求成功处理方法
  92. * newWay 新的处理错误请求的标识
  93. */
  94. function sendPost(url, params, custError, custSuccess,dataType, timeOut, newWay) {
  95. if(dataType==undefined){
  96. dataType="POST"
  97. }
  98. if(isLoginOut) {
  99. return
  100. }
  101. //发送ajax请求
  102. console.log(server+url);
  103. // 增加绝对URL(含有http://及https://)的判断,以满足跨域请求(yzh)
  104. var reqUrl = server + url;
  105. if(url&&(url.indexOf("http://")>-1 || url.indexOf("https://")>-1)) {
  106. reqUrl = url;
  107. }
  108. mui.ajax(reqUrl, {
  109. data: params || {},
  110. dataType: 'json',
  111. //crossDomain: true,
  112. type: dataType,
  113. timeout: timeOut || 60000,
  114. error: function(xht, type, throwErr) {
  115. if(type=="timeout"){
  116. if(custError){
  117. custError(type);
  118. return ;
  119. }
  120. }
  121. if(custError && $.isFunction(custError)) {
  122. var flag = custError(xht, type, throwErr);
  123. if(!flag) {
  124. return ;
  125. }
  126. }
  127. plus.nativeUI.closeWaiting();
  128. var curWebview = plus.webview.currentWebview();
  129. if(curWebview.id=="error") {
  130. return ;
  131. }
  132. if(newWay == undefined){ //旧的处理逻辑
  133. if(type == "timeout" || type == "abort") {
  134. checkNetOrbackendError();
  135. } else if(type == "error" || type == "parsererror" || type == "null") {
  136. setTimeout(function() {
  137. showErrorPage("程序开了点小差, 请稍后重试!")
  138. },0);
  139. }
  140. }else if(newWay){
  141. mui.toast("数据加载失败,请检查网络无误后下拉刷新。");
  142. }
  143. },
  144. success: function(res) {
  145. var random = Math.random();
  146. isLoginOut = window.localStorage.isLoginOut;
  147. if(isLoginOut) {
  148. return;
  149. }
  150. var tip = "";
  151. if(res.status == 999) {
  152. window.localStorage.isLoginOut = random;
  153. if(window.localStorage.isLoginOut != random) {
  154. return;
  155. }
  156. tip = "此账号已在别处登录,请重新登录";
  157. showConfirm(tip, 1);
  158. return;
  159. } else if(res.status == 998) {
  160. window.localStorage.isLoginOut = random;
  161. if(window.localStorage.isLoginOut != random) {
  162. return;
  163. }
  164. tip = "登录超时,请重新登录";
  165. showConfirm(tip);
  166. return;
  167. } else if(res.status == 997) {
  168. window.localStorage.isLoginOut = random;
  169. if(window.localStorage.isLoginOut != random) {
  170. return;
  171. }
  172. tip = "此账号未登录,请先登录";
  173. showConfirm(tip);
  174. return;
  175. }
  176. //添加无操作权限的返回结果
  177. else if(res.status == 403){
  178. mui.toast("观察者模式无法进行增删改操作");
  179. plus.nativeUI.closeWaiting();
  180. return;
  181. }
  182. custSuccess(res);
  183. }
  184. });
  185. }
  186. /*
  187. * 选择退出登录
  188. */
  189. function exit2Login(beOut) {
  190. //beOut 参数,记录是否是因为账号在别处登录被挤掉
  191. mui.openWindow('../../login/html/login.html', 'login', {
  192. extras: {
  193. exit: "exit",
  194. beOut: beOut
  195. },
  196. createNew: true
  197. });
  198. mui.later(function() {
  199. plus.webview.currentWebview().close("none");
  200. }, 1000)
  201. }
  202. /*
  203. * 弹出框
  204. */
  205. function showConfirm(content, beOut) {
  206. mui.confirm(content, "提示", ["确定"], function() {
  207. exit2Login(beOut);
  208. plus.storage.removeItem("isLoginOut");
  209. });
  210. }
  211. function sendPostNoAsync(url, params, error, success) {
  212. //发送ajax请求
  213. mui.ajax(server + url, {
  214. data: params || {},
  215. async: false,
  216. dataType: 'json',
  217. //crossDomain: true,
  218. type: "POST",
  219. timeout: 20000,
  220. error: error || function(xht, type, throwErr) {},
  221. success: success
  222. });
  223. }
  224. /*
  225. * 获取时间
  226. */
  227. function getCurrDate(flag) {
  228. var weekStr = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  229. var d = new Date();
  230. var year = d.getFullYear();
  231. var month = d.getMonth() + 1;
  232. var day = d.getDate();
  233. var week = d.getDay();
  234. var h = d.getHours();
  235. var mins = d.getMinutes();
  236. var s = d.getSeconds();
  237. if(month < 10) month = "0" + month;
  238. if(day < 10) day = "0" + day;
  239. if(h < 10) h = "0" + h;
  240. if(mins < 10) mins = "0" + mins;
  241. if(s < 10) s = "0" + s;
  242. if(flag == "date") {
  243. return year + "-" + month + "-" + day;
  244. } else if(flag == "time") {
  245. return h + ":" + mins + ":" + s;
  246. } else if(flag == "week") {
  247. return year + "-" + month + "-" + day + " " + weekStr[week];
  248. }
  249. return year + "-" + month + "-" + day + " " + h + ":" + mins + ":" + s;
  250. }
  251. function getSysDate(plus) {
  252. var d = new Date();
  253. var year = d.getFullYear();
  254. var month = d.getMonth() + 1;
  255. var day = d.getDate();
  256. // var week = d.getDay();
  257. var h = d.getHours();
  258. var mins = d.getMinutes();
  259. var s = d.getSeconds();
  260. if(month < 10) month = "0" + month;
  261. if(day < 10) month = "0" + day;
  262. if(h < 10) h = "0" + h;
  263. if(mins < 10) mins = "0" + mins;
  264. if(s < 10) s = "0" + s;
  265. if(!plus) {
  266. return year + "-" + month + "-" + day + "" + h + ":" + mins + ":" + s;
  267. } else {
  268. return(year + 1) + "-" + month + "-" + day + "" + h + ":" + mins + ":" + s;
  269. }
  270. }
  271. function getSysDatePlus(plus) {
  272. var now = new Date();
  273. var d = new Date(now.getTime() + plus * 24 * 3600 * 1000);
  274. var year = d.getFullYear();
  275. var month = d.getMonth() + 1;
  276. var day = d.getDate();
  277. // alert(year+" "+month+" "+day);
  278. // var h = d.getHours();
  279. // var mins = d.getMinutes();
  280. // var s = d.getSeconds();
  281. if(month < 10) month = "0" + month;
  282. if(day < 10) day = "0" + day;
  283. // if (h < 10) h = "0" + h;
  284. // if (mins < 10) mins = "0" + mins;
  285. // if (s < 10) s = "0" + s;
  286. return year + "-" + month + "-" + day
  287. }
  288. /**
  289. * 用于把用utf16编码的字符转换成实体字符,以供后台存储
  290. * @param {string} str 将要转换的字符串,其中含有utf16字符将被自动检出
  291. * @return {string} 转换后的字符串,utf16字符将被转换成&#xxxx;形式的实体字符
  292. */
  293. function utf16toEntities(str) {
  294. var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
  295. str = str.replace(patt, function(char) {
  296. var H, L, code;
  297. if(char.length === 2) {
  298. H = char.charCodeAt(0); // 取出高位
  299. L = char.charCodeAt(1); // 取出低位
  300. code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
  301. return "&#" + code + ";";
  302. } else {
  303. return char;
  304. }
  305. });
  306. return str;
  307. }
  308. // 获取异步请求处理Promise对象
  309. function getReqPromise(url, data,reqType,newWay) {
  310. return new Promise(function(resolve, reject) {
  311. sendPost(url, data,
  312. function queryFailed (xht, type, throwErr) {
  313. // dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show();
  314. // mui.toast("程序开了点小差, 请稍后重试!");
  315. if(type == "error" || type == "parsererror" || type == "null") {
  316. //mui.toast("程序开了点小差, 请稍后重试!");
  317. //throw new Error("加载失败");
  318. showErrorPage("程序开了点小差, 请稍后重试!");
  319. }
  320. return true;
  321. }
  322. , function success(res) {
  323. resolve(res);
  324. },reqType, '', (newWay===false?false:true));
  325. });
  326. }
  327. /*
  328. * reqs: 请求的参数数组,格式:[{url:'a/xxx', reqType: 'POST', data:{...}},{url:'b/xxx',data:{...}}]
  329. * */
  330. function getReqPromises(reqs, newWay) {
  331. if(!reqs || !reqs.length) {
  332. return new Promise(function(resolve, reject) {
  333. resolve([]);
  334. });
  335. }
  336. reqs = _.filter(reqs,function(req) {
  337. return req!=null
  338. });
  339. return Promise.all(_.map(reqs,function(param){
  340. return getReqPromise(param.url,param.data, param.reqType, (newWay===false?false:true));
  341. }));
  342. };
  343. //获取链接上的参数
  344. function GetRequest() {
  345. var url = location.search; //获取url中"?"符后的字串
  346. var theRequest = new Object();
  347. if (url.indexOf("?") != -1) {
  348. var str = url.substr(1);
  349. strs = str.split("&");
  350. for(var i = 0; i < strs.length; i ++) {
  351. theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
  352. }
  353. }
  354. return theRequest;
  355. }
  356. mui.plusReady(function() {
  357. setStatusbarRed();
  358. })
  359. function setStatusbarRed(){
  360. // 设置系统状态栏背景色
  361. plus.navigator.setStatusBarBackground("#17b3ec");
  362. }
  363. /*
  364. * 获取图片路径
  365. */
  366. function getImgUrl(str){
  367. if(typeof str != 'string'){
  368. return "";
  369. }
  370. if(str.length == 0){
  371. return "";
  372. }else{
  373. if(str.indexOf("../")>-1){
  374. //访问本地路径
  375. return str;
  376. }else if((str.indexOf("http://")>-1) || (str.indexOf("https://")>-1)){
  377. return str;
  378. }else{
  379. //服务器上的图片路径
  380. return imgUrlDomain + str;
  381. }
  382. }
  383. }