common_http.js 17 KB

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