common_http.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //切换发布版本 true:正式版,连接生产环境;false:测试版,连接测试环境。
  2. var publish_version = true;
  3. //var publish_version = false;
  4. var curr_app_version = 5; //app当前内定版本号
  5. //接口服务器
  6. var server;
  7. //档案信息服务器
  8. var templateServer;
  9. var appId;
  10. var checkReload = true;
  11. // 图片识别请求路径
  12. var ocrReqUrl = "";
  13. // 健康档案接口服务器
  14. var healthProfileServer = "";
  15. //上饶县人民医院机构代码
  16. var srOrgCode = "49229004X";
  17. if(publish_version == true) {
  18. appId = "wxad04e9c4c5255acf";
  19. //接口服务器
  20. server = "http://srijk.yihu.com/wlyy/";
  21. // 图片识别请求路径
  22. ocrReqUrl = "http://www.xmtyw.cn/ocr/";
  23. // 健康档案接口服务器
  24. healthProfileServer = server;
  25. //图片文件的服务器路径
  26. var imgUrlDomain = "http://srijk.yihu.com/";
  27. templateServer = "";
  28. // 健康档案(上饶)服务地址
  29. profileServer = "http://srijk.yihu.com/wlyy"
  30. } else {
  31. appId = "wxd03f859efdf0873d";
  32. // OCR小温测试地址
  33. ocrReqUrl = "http://192.168.131.132:3000/ocr";
  34. templateServer = "http://172.19.103.73:8080/app-ehr/html/mytemplate/html/visit-record-list.html";
  35. //接口服务器
  36. server = "http://172.19.103.72:9096/csijk/";
  37. //server = "http://192.168.131.115:8081/";//张丹
  38. // server = "http://192.168.131.24:8082/";//仕杰
  39. // server = "http://192.168.131.250:8081/";//伟达
  40. // server = "http://192.168.131.133:8080/";//文杰
  41. server = "http://192.168.131.24:8082/";//文彬
  42. // server = "http://192.168.131.127:8060/";//逸祥
  43. // 健康档案接口服务器
  44. healthProfileServer = server;
  45. //图片文件的服务器路径
  46. var imgUrlDomain = "http://172.19.103.54/";
  47. // 健康档案(上饶)服务地址
  48. profileServer = "http://ehr.yihu.com/csijk"
  49. }
  50. function showErrorPage (msg) {
  51. plus.nativeUI.closeWaiting();
  52. var errorWebview = plus.webview.getWebviewById('error');
  53. if(errorWebview) {
  54. mui.fire(errorWebview, "refresh", {errorMsg: msg});
  55. } else {
  56. mui.openWindow({
  57. id: "error",
  58. url: "../../error/html/error.html",
  59. waiting:{
  60. autoShow:false
  61. },
  62. extras: {
  63. errorMsg: msg
  64. }
  65. });
  66. }
  67. }
  68. function checkNetOrbackendError() {
  69. var noNetwork = plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE;
  70. setTimeout(function() {
  71. showErrorPage(noNetwork?"网络有点问题哦,检查网络无误后,":"程序开了点小差, 请稍后重试!")
  72. },0);
  73. return noNetwork;
  74. }
  75. var isLoginOut;
  76. //newWay 新的处理错误请求的标识
  77. function sendGet(url, params, custError, custSuccess,newWay,timeOut){
  78. sendPost(url, params, custError, custSuccess, "GET", timeOut, newWay);
  79. }
  80. /**
  81. * 统一请求ajax发送方法
  82. * url 请求地址:例如:patient/health_index/add
  83. * params 请求参数
  84. * dataType 数据类型:json等
  85. * reqType 请求方式:get 或 post
  86. * error 请求失败处理方法
  87. * success 请求成功处理方法
  88. * newWay 新的处理错误请求的标识
  89. */
  90. function sendPost(url, params, custError, custSuccess,dataType, timeOut, newWay) {
  91. // 预先检查是否有网络
  92. // if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
  93. // if(newWay == undefined){
  94. // showErrorPage("网络有点问题哦,检查网络无误后,");
  95. // return ;
  96. // }else if(newWay){
  97. // mui.toast("数据加载失败,请检查网络无误后下拉刷新。");
  98. // }
  99. // }
  100. if(dataType==undefined){
  101. dataType="POST"
  102. }
  103. if(isLoginOut) {
  104. return
  105. }
  106. //发送ajax请求
  107. console.log(server+url);
  108. // 增加绝对URL(含有http://及https://)的判断,以满足跨域请求(yzh)
  109. var reqUrl = server + url;
  110. if(url&&(url.indexOf("http://")>-1 || url.indexOf("https://")>-1)) {
  111. reqUrl = url;
  112. }
  113. mui.ajax(reqUrl, {
  114. // mui.ajax( url, {
  115. data: params || {},
  116. dataType: 'json',
  117. //crossDomain: true,
  118. type: dataType,
  119. timeout: timeOut || 60000,
  120. error: function(xht, type, throwErr) {
  121. console.log(reqUrl+" "+type + " : " + throwErr);
  122. // var random = Math.random();
  123. // isLoginOut = window.localStorage.isLoginOut;
  124. // if(isLoginOut) {
  125. // return;
  126. // }
  127. //
  128. // var tip = "";
  129. if(type=="timeout"){
  130. if(custError){
  131. custError(type);
  132. return ;
  133. }
  134. }
  135. if(custError && $.isFunction(custError)) {
  136. var flag = custError(xht, type, throwErr);
  137. if(!flag) {
  138. return ;
  139. }
  140. }
  141. plus.nativeUI.closeWaiting();
  142. var curWebview = plus.webview.currentWebview();
  143. if(curWebview.id=="error") {
  144. return ;
  145. }
  146. if(newWay == undefined){ //旧的处理逻辑
  147. if(type == "timeout" || type == "abort") {
  148. //window.localStorage.isLoginOut = random;
  149. //if(window.localStorage.isLoginOut != random) {
  150. //return;
  151. //}
  152. // tip = "您的网络有点问题哦,请检查网络无误后重试~";
  153. //
  154. // if(checkReload){
  155. // mui.confirm(tip, "提示", ["重试", "重新登录"], function(e) {
  156. // if(e.index == 0){
  157. // plus.webview.currentWebview().reload();
  158. // } else {
  159. // exit2Login();
  160. // }
  161. // });
  162. // return;
  163. // }
  164. //
  165. // showConfirm(tip);
  166. checkNetOrbackendError();
  167. } else if(type == "error" || type == "parsererror" || type == "null") {
  168. //window.localStorage.isLoginOut = random;
  169. //if(window.localStorage.isLoginOut != random) {
  170. // return;
  171. //}
  172. setTimeout(function() {
  173. showErrorPage("程序开了点小差, 请稍后重试!")
  174. },0);
  175. }
  176. }else if(newWay){
  177. mui.toast("数据加载失败,请检查网络无误后下拉刷新。");
  178. }
  179. /*
  180. if(checkReload){
  181. window.localStorage.isLoginOut = random;
  182. if(window.localStorage.isLoginOut != random) {
  183. return;
  184. }
  185. mui.confirm("您的网络有点问题哦,请检查网络后重试~", "提示", ["重试", "重新登录"], function(e) {
  186. if(e.index == 0){
  187. plus.webview.currentWebview().reload();
  188. } else {
  189. exit2Login();
  190. }
  191. });
  192. return;
  193. }
  194. if(custError){
  195. custError(xht, type, throwErr);
  196. return;
  197. }
  198. var tip = ""
  199. if(type == "timeout") {
  200. window.localStorage.isLoginOut = random;
  201. if(window.localStorage.isLoginOut != random) {
  202. return;
  203. }
  204. tip = "您的网络有点问题哦,请检查网络后重试~";
  205. }
  206. if(type == "abort") {
  207. window.localStorage.isLoginOut = random;
  208. if(window.localStorage.isLoginOut != random) {
  209. return;
  210. }
  211. tip = "您的网络有点问题哦,请检查网络后重试~";
  212. }
  213. if(type == "error" || type == "parsererror" || type == "null") {
  214. window.localStorage.isLoginOut = random;
  215. if(window.localStorage.isLoginOut != random) {
  216. return;
  217. }
  218. tip = "您的网络有点问题哦,请检查网络后重试~";
  219. }
  220. showConfirm(tip);*/
  221. },
  222. success: function(res) {
  223. var random = Math.random();
  224. isLoginOut = window.localStorage.isLoginOut;
  225. // console.error(isLoginOut);
  226. if(isLoginOut) {
  227. return;
  228. }
  229. var tip = "";
  230. if(res.status == 999) {
  231. window.localStorage.isLoginOut = random;
  232. if(window.localStorage.isLoginOut != random) {
  233. return;
  234. }
  235. tip = "此账号已在别处登录,请重新登录";
  236. showConfirm(tip, 1);
  237. return;
  238. } else if(res.status == 998) {
  239. window.localStorage.isLoginOut = random;
  240. if(window.localStorage.isLoginOut != random) {
  241. return;
  242. }
  243. tip = "登录超时,请重新登录";
  244. showConfirm(tip);
  245. return;
  246. } else if(res.status == 997) {
  247. window.localStorage.isLoginOut = random;
  248. if(window.localStorage.isLoginOut != random) {
  249. return;
  250. }
  251. tip = "此账号未登录,请先登录";
  252. showConfirm(tip);
  253. return;
  254. }
  255. //添加无操作权限的返回结果
  256. else if(res.status == 403){
  257. mui.toast("观察者模式无法进行增删改操作");
  258. plus.nativeUI.closeWaiting();
  259. return;
  260. }
  261. custSuccess(res);
  262. }
  263. });
  264. }
  265. /*
  266. * 选择退出登录
  267. */
  268. function exit2Login(beOut) {
  269. //beOut 参数,记录是否是因为账号在别处登录被挤掉
  270. // var loginWv = plus.webview.getWebviewById("login");
  271. // console.error(loginWv);
  272. // if(loginWv) {
  273. // loginWv.show("auto");
  274. // } else {
  275. mui.openWindow('../../login/html/login.html', 'login', {
  276. extras: {
  277. exit: "exit",
  278. beOut: beOut
  279. },
  280. createNew: true
  281. });
  282. // }
  283. mui.later(function() {
  284. plus.webview.currentWebview().close("none");
  285. }, 1000)
  286. }
  287. /*
  288. * 弹出框
  289. */
  290. function showConfirm(content, beOut) {
  291. mui.confirm(content, "提示", ["确定"], function() {
  292. exit2Login(beOut);
  293. plus.storage.removeItem("isLoginOut");
  294. });
  295. }
  296. function sendPostNoAsync(url, params, error, success) {
  297. //发送ajax请求
  298. mui.ajax(server + url, {
  299. // mui.ajax( url, {
  300. data: params || {},
  301. async: false,
  302. dataType: 'json',
  303. //crossDomain: true,
  304. type: "POST",
  305. timeout: 20000,
  306. error: error || function(xht, type, throwErr) {},
  307. success: success
  308. });
  309. }
  310. /*
  311. * 获取时间
  312. */
  313. function getCurrDate(flag) {
  314. var weekStr = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  315. var d = new Date();
  316. var year = d.getFullYear();
  317. var month = d.getMonth() + 1;
  318. var day = d.getDate();
  319. var week = d.getDay();
  320. var h = d.getHours();
  321. var mins = d.getMinutes();
  322. var s = d.getSeconds();
  323. if(month < 10) month = "0" + month;
  324. if(day < 10) day = "0" + day;
  325. if(h < 10) h = "0" + h;
  326. if(mins < 10) mins = "0" + mins;
  327. if(s < 10) s = "0" + s;
  328. if(flag == "date") {
  329. return year + "-" + month + "-" + day;
  330. } else if(flag == "time") {
  331. return h + ":" + mins + ":" + s;
  332. } else if(flag == "week") {
  333. return year + "-" + month + "-" + day + " " + weekStr[week];
  334. }
  335. return year + "-" + month + "-" + day + " " + h + ":" + mins + ":" + s;
  336. }
  337. function getSysDate(plus) {
  338. var d = new Date();
  339. var year = d.getFullYear();
  340. var month = d.getMonth() + 1;
  341. var day = d.getDate();
  342. // var week = d.getDay();
  343. var h = d.getHours();
  344. var mins = d.getMinutes();
  345. var s = d.getSeconds();
  346. if(month < 10) month = "0" + month;
  347. if(day < 10) month = "0" + day;
  348. if(h < 10) h = "0" + h;
  349. if(mins < 10) mins = "0" + mins;
  350. if(s < 10) s = "0" + s;
  351. if(!plus) {
  352. return year + "-" + month + "-" + day + "" + h + ":" + mins + ":" + s;
  353. } else {
  354. return(year + 1) + "-" + month + "-" + day + "" + h + ":" + mins + ":" + s;
  355. }
  356. }
  357. function getSysDatePlus(plus) {
  358. var now = new Date();
  359. var d = new Date(now.getTime() + plus * 24 * 3600 * 1000);
  360. var year = d.getFullYear();
  361. var month = d.getMonth() + 1;
  362. var day = d.getDate();
  363. // alert(year+" "+month+" "+day);
  364. // var h = d.getHours();
  365. // var mins = d.getMinutes();
  366. // var s = d.getSeconds();
  367. if(month < 10) month = "0" + month;
  368. if(day < 10) day = "0" + day;
  369. // if (h < 10) h = "0" + h;
  370. // if (mins < 10) mins = "0" + mins;
  371. // if (s < 10) s = "0" + s;
  372. return year + "-" + month + "-" + day
  373. }
  374. /**
  375. * 图片批量上传
  376. * @param {Object} images 图片数组
  377. * @param {Function} callBackHandler 上传结果处理方法
  378. */
  379. //function uploadImages(images, callBackHandler) {
  380. // var task = plus.uploader.createUpload(server + '/upload/image', {
  381. // method: "POST"
  382. // }, callBackHandler);
  383. // // task.addData("client", "HelloH5+");
  384. // // task.addData("uid", getUid());
  385. // for (var i = 0; i < images.length; i++) {
  386. // var image = images[i];
  387. // task.addFile(image, {});
  388. // }
  389. // task.start();
  390. //}
  391. /**
  392. * 群组消息推送
  393. * @param {Object} group_id 群组ID
  394. * @param {Object} from_id 发送者ID
  395. * @param {Object} content 消息内容
  396. */
  397. //function group_sendMsg(group_id,from_id,content)
  398. //{
  399. // mui.ajax(group_sendmsg, {
  400. // data: {group_id:group_uid,content:content,from_id:from_id},
  401. // dataType: 'json',
  402. // type: "POST",
  403. // timeout: 10000,
  404. // error: function(xht, type, throwErr) {
  405. //
  406. // },
  407. // success: function(result){
  408. //
  409. // }
  410. // });
  411. //}
  412. function getsuifangUrl() {
  413. var userAgent = plus.storage.getItem("userAgent");
  414. userAgentJson = JSON.parse(userAgent);
  415. var id = userAgentJson.id;
  416. var uid = userAgentJson.uid;
  417. var imei = userAgentJson.imei;
  418. var token = userAgentJson.token;
  419. var platform = 2;
  420. var openid = "123456";
  421. var sf_url = sf_serverip + "index.php?m=FvDoctor&c=index" + "&id=" + id + "&uid=" + uid + "&imei=" + imei + "&token=" + token + "&platform=" + platform + "&openid=" + openid;
  422. // console.error(sf_url);
  423. return sf_url;
  424. }
  425. /**
  426. * 用于把用utf16编码的字符转换成实体字符,以供后台存储
  427. * @param {string} str 将要转换的字符串,其中含有utf16字符将被自动检出
  428. * @return {string} 转换后的字符串,utf16字符将被转换成&#xxxx;形式的实体字符
  429. */
  430. function utf16toEntities(str) {
  431. var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
  432. str = str.replace(patt, function(char) {
  433. var H, L, code;
  434. if(char.length === 2) {
  435. H = char.charCodeAt(0); // 取出高位
  436. L = char.charCodeAt(1); // 取出低位
  437. code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
  438. return "&#" + code + ";";
  439. } else {
  440. return char;
  441. }
  442. });
  443. return str;
  444. }
  445. // 获取异步请求处理Promise对象
  446. function getReqPromise(url, data,reqType,newWay) {
  447. return new Promise(function(resolve, reject) {
  448. sendPost(url, data,
  449. function queryFailed (xht, type, throwErr) {
  450. // dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show();
  451. // mui.toast("程序开了点小差, 请稍后重试!");
  452. if(type == "error" || type == "parsererror" || type == "null") {
  453. //mui.toast("程序开了点小差, 请稍后重试!");
  454. //throw new Error("加载失败");
  455. showErrorPage("程序开了点小差, 请稍后重试!");
  456. }
  457. return true;
  458. }
  459. , function success(res) {
  460. resolve(res);
  461. },reqType, '', (newWay===false?false:true));
  462. });
  463. }
  464. /*
  465. * reqs: 请求的参数数组,格式:[{url:'a/xxx', reqType: 'POST', data:{...}},{url:'b/xxx',data:{...}}]
  466. * */
  467. function getReqPromises(reqs, newWay) {
  468. if(!reqs || !reqs.length) {
  469. return new Promise(function(resolve, reject) {
  470. resolve([]);
  471. });
  472. }
  473. reqs = _.filter(reqs,function(req) {
  474. return req!=null
  475. });
  476. return Promise.all(_.map(reqs,function(param){
  477. return getReqPromise(param.url,param.data, param.reqType, (newWay===false?false:true));
  478. }));
  479. };
  480. //获取链接上的参数
  481. function GetRequest() {
  482. var url = location.search; //获取url中"?"符后的字串
  483. var theRequest = new Object();
  484. if (url.indexOf("?") != -1) {
  485. var str = url.substr(1);
  486. strs = str.split("&");
  487. for(var i = 0; i < strs.length; i ++) {
  488. theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
  489. }
  490. }
  491. return theRequest;
  492. }
  493. mui.plusReady(function() {
  494. setStatusbarRed();
  495. })
  496. function setStatusbarRed(){
  497. // 设置系统状态栏背景色
  498. plus.navigator.setStatusBarBackground("#17b3ec");
  499. }
  500. /*
  501. * 获取图片路径
  502. */
  503. function getImgUrl(str){
  504. if(typeof str != 'string'){
  505. return "";
  506. }
  507. if(str.length == 0){
  508. return "";
  509. }else{
  510. if(str.indexOf("../")>-1){
  511. //访问本地路径
  512. return str;
  513. }else if((str.indexOf("http://")>-1) || (str.indexOf("https://")>-1)){
  514. return str;
  515. }else{
  516. //服务器上的图片路径
  517. return imgUrlDomain + str;
  518. }
  519. }
  520. }