common_http.js 15 KB

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