common_http.js 17 KB

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