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