common_http.js 17 KB

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