api-service.js 8.9 KB

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