api-service.js 9.0 KB

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