api-service.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. (function($) {
  2. var publish_version = false;
  3. var isInner = false; //发布线上后有内外网的配置
  4. var agentName = "wlyyAgentForDoc";
  5. var server, userAgent, loginUrl, imserver, socketUrl, imgServer;
  6. var IMEI = localStorage.getItem('WLYY_IMEI') || uuid(16, 16)
  7. localStorage.setItem('WLYY_IMEI', IMEI)
  8. $.support.cors = true;
  9. if(publish_version) { // 生产环境配置
  10. server = "http://www.xmtyw.cn/wlyy/"
  11. imserver = "http://27.155.101.77:3000/api/v2";
  12. socketUrl = "http://27.155.101.77:3000";
  13. loginUrl = "/PC-prescription/login.html";
  14. imgServer = "http://www.xmtyw.cn/";
  15. if(isInner){
  16. server = "http://10.95.22.10:8011/wlyy/"
  17. imserver = "http://10.95.22.138:3000/api/v2";
  18. socketUrl = "http://10.95.22.138:3000";
  19. loginUrl = "/PC-prescription/login.html";
  20. imgServer = "http://10.95.22.140:23001/";
  21. }
  22. } else { // 测试环境配置
  23. server = "http://172.19.103.88:9092/wlyy/"
  24. imserver = "http://172.19.103.88:3000/api/v2";
  25. socketUrl = "http://172.19.103.88:3000";
  26. // server = "http://192.168.131.133:8080/";
  27. // server = "http://192.168.131.127:8060/"; //逸祥
  28. // server = "http://192.168.131.24:8080/"; //仕杰
  29. // imserver = "http://192.168.131.24:3000/api/v2"
  30. // socketUrl = "http://192.168.131.24:3000";
  31. loginUrl = "/PC-prescription/login.html";
  32. imgServer = "http://172.19.103.54/";
  33. }
  34. userAgent = localStorage.getItem(agentName)
  35. //hard code userAgent for test start
  36. //全科
  37. // userAgent = {"id":4800,"uid":"zbqD201703150222","imei":"864394010176834","token":"9b23e4ec0bcd3b9fa72a52f91544a4e9","platform":4,"hospital":"3502050100"}
  38. //健管
  39. // userAgent = {"id":4821,"uid":"zbqD201703150228","imei":"864394010176834","token":"73f5ace5c7d4cdb811f743902fd705d9","platform":4,"hospital":"3502050100"};
  40. // userAgent = JSON.stringify(userAgent);
  41. //hard code userAgent for test end
  42. if(userAgent) {
  43. try {
  44. userAgent = JSON.parse(userAgent)
  45. } catch(e) {
  46. toastr && toastr.warning("登录失效,请重新登录")
  47. toLoginPage()
  48. }
  49. } else {
  50. toLoginPage()
  51. }
  52. var isRelogined = sessionStorage.getItem('wlyy_relogin')
  53. // if(isRelogined && top !== window && location.href.indexOf("login.html") < 0) {
  54. if(isRelogined && location.href.indexOf("login.html") < 0){
  55. sessionStorage.removeItem('wlyy_relogin')
  56. //top.location.reload(true)
  57. }
  58. function httpGet(url, options) {
  59. //发送ajax请求
  60. return new Promise(function(resolve, reject) {
  61. $.ajax(server + url,
  62. $.extend({}, {
  63. type: 'GET',
  64. dataType: 'JSON',
  65. beforeSend: function(request) {
  66. var agent = userAgent || {
  67. imei: localStorage.getItem('WLYY_IMEI'),
  68. platform: 4
  69. }
  70. request.setRequestHeader("userAgent", JSON.stringify(agent));
  71. },
  72. error: function(res) {
  73. reject(res)
  74. },
  75. success: function(res) {
  76. failCodeHandle(res)
  77. resolve(res)
  78. }
  79. }, options));
  80. })
  81. }
  82. function httpPost(url, options) {
  83. //发送ajax请求
  84. return new Promise(function(resolve, reject) {
  85. $.ajax(server + url,
  86. $.extend({}, {
  87. type: 'POST',
  88. dataType: 'JSON',
  89. beforeSend: function(request) {
  90. var agent = userAgent || {
  91. imei: localStorage.getItem('WLYY_IMEI')
  92. }
  93. request.setRequestHeader("userAgent", JSON.stringify(agent));
  94. },
  95. error: function(res) {
  96. reject(res)
  97. },
  98. success: function(res) {
  99. failCodeHandle(res)
  100. resolve(res)
  101. }
  102. }, options));
  103. })
  104. }
  105. function imHttpGet(url, options) {
  106. //发送ajax请求
  107. return new Promise(function(resolve, reject) {
  108. $.ajax(imserver + url,
  109. $.extend({}, {
  110. type: 'GET',
  111. dataType: 'JSON',
  112. beforeSend: function(request) {
  113. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  114. },
  115. error: function(res) {
  116. reject(res)
  117. },
  118. success: function(res) {
  119. failCodeHandle(res)
  120. resolve(res)
  121. }
  122. }, options));
  123. })
  124. }
  125. function imHttpPost(url, options) {
  126. //发送ajax请求
  127. return new Promise(function(resolve, reject) {
  128. $.ajax(imserver + url,
  129. $.extend({}, {
  130. type: 'POST',
  131. dataType: 'JSON',
  132. beforeSend: function(request) {
  133. request.setRequestHeader("userAgent", JSON.stringify(userAgent));
  134. },
  135. error: function(res) {
  136. reject(res)
  137. },
  138. success: function(res) {
  139. failCodeHandle(res)
  140. resolve(res)
  141. }
  142. }, options));
  143. })
  144. }
  145. function toLoginPage() {
  146. if(location.href.indexOf('login.html') > -1) {
  147. return;
  148. }
  149. setTimeout(function() {
  150. sessionStorage.setItem("wlyy_relogin", 1)
  151. top.location.replace(loginUrl + '?redirect_url=' + encodeURIComponent(top.location.href))
  152. }, 2000)
  153. }
  154. var count = 0;
  155. function failCodeHandle(res) {
  156. var tip = "";
  157. if(res.status == 999) {
  158. tip = "此账号已在别处登录,请重新登录";
  159. } else if(res.status == 998) {
  160. tip = "登录超时,请重新登录";
  161. } else if(res.status == 997) {
  162. tip = "此账号未登录,请先登录"
  163. }
  164. if(tip) {
  165. count ++;
  166. if(count == 1){
  167. toastr && toastr.warning(tip)
  168. toLoginPage()
  169. }
  170. }
  171. }
  172. function uuid(len, radix) {
  173. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  174. var uuid = [],
  175. i;
  176. radix = radix || chars.length;
  177. if(len) {
  178. // Compact form
  179. for(i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  180. } else {
  181. // rfc4122, version 4 form
  182. var r;
  183. // rfc4122 requires these characters
  184. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  185. uuid[14] = '4';
  186. // Fill in random data. At i==19 set the high bits of clock sequence as
  187. // per rfc4122, sec. 4.1.5
  188. for(i = 0; i < 36; i++) {
  189. if(!uuid[i]) {
  190. r = 0 | Math.random() * 16;
  191. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  192. }
  193. }
  194. }
  195. return uuid.join('');
  196. }
  197. /*
  198. * 获取图片路径方法修改
  199. */
  200. function getImgUrl(str) {
  201. if(typeof str != 'string') {
  202. return "";
  203. }
  204. if(str.length == 0) {
  205. return "";
  206. } else {
  207. if(str.indexOf("../") > -1) {
  208. //访问本地路径
  209. return str;
  210. } else if((str.indexOf("http://") > -1) || (str.indexOf("https://") > -1)) {
  211. return str;
  212. } else {
  213. //服务器上的图片路径
  214. return imgServer + str;
  215. }
  216. }
  217. }
  218. APIService = {
  219. agentName: agentName,
  220. server: server,
  221. imserver: imserver,
  222. socketUrl: socketUrl,
  223. userAgent: userAgent,
  224. httpGet: httpGet,
  225. httpPost: httpPost,
  226. imHttpGet: imHttpGet,
  227. imHttpPost: imHttpPost,
  228. getImgUrl: getImgUrl,
  229. failCodeHandle: failCodeHandle,
  230. loginIm: function(data){
  231. return imHttpPost('/users/login', data);
  232. },
  233. getDoctorInfo: function() {
  234. return httpGet('doctor/baseinfo');
  235. },
  236. getTeamInfo: function(doctor) {
  237. return httpGet('/doctor/admin-teams/team/' + doctor + '/teams');
  238. }
  239. }
  240. window.APIService = APIService;
  241. })(jQuery)