common_http.js 15 KB

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