httpRequest_sub.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * 有关 访问服务器的uri
  3. */
  4. var $http = function() {
  5. // this.ip = "http://192.168.242.26:9002";
  6. this.ip = "https://b2bcnapi.sailuntire.com/api/test/b2bapi/action/api/tms";
  7. //this.ip = "http://b2bcnapi.sailuntire.com/api/web";
  8. // this.ip = "http://192.168.237.6/api/web";
  9. // 获取签收单据的主表信息
  10. this.signOrderOfHead = "/signOrderOfHead.xhtml";
  11. /****** 获取签收单据的明细信息 ******/
  12. this.signOrderOfItem = "/signOrderOfItem.xhtml";
  13. }
  14. // api的ajax
  15. $http.prototype.$post = function(url, data, callback, other) {
  16. var content = "", urlT = "";
  17. if (data.url == "json") {
  18. delete data.url;
  19. // 确认contenttype选项
  20. content = "application/json;charset=UTF-8";
  21. } else {
  22. content = "application/x-www-form-urlencoded"
  23. }
  24. var datas = this.serize(data);
  25. if ( typeof other != 'undefined' && other != "" && other != 'undefined') {
  26. urlT = this.ip + eval('this.' + url) + '/' + other;
  27. } else {
  28. urlT = this.ip + eval('this.' + url);
  29. }
  30. api.showProgress({
  31. });
  32. console.log(urlT)
  33. console.log(JSON.stringify(datas))
  34. api.ajax({
  35. url : urlT,
  36. method : 'post',
  37. data : datas,
  38. headers : {
  39. "Content-type" : content
  40. },
  41. }, function(ret, err) {
  42. api.hideProgress();
  43. api.refreshHeaderLoadDone();
  44. if (ret) {
  45. // console.log(JSON.stringify(ret));
  46. callback(ret);
  47. } else {
  48. api.toast({
  49. msg: '当前网络不稳定,请稍后再试'
  50. });
  51. }
  52. });
  53. }
  54. // get方法
  55. $http.prototype.$get = function(url, data, callback, other) {
  56. var urlT = other == 'formdata' ? (this.ip + eval('this.' + url)) : (this.ip + eval('this.' + url) + '?' + data);
  57. api.showProgress({
  58. });
  59. api.ajax({
  60. url : urlT,
  61. method : 'get',
  62. data : {},
  63. headers : {
  64. "Content-type" : "application/json;charset=UTF-8"
  65. },
  66. }, function(ret, err) {
  67. api.hideProgress();
  68. api.refreshHeaderLoadDone();
  69. if (ret) {
  70. callback(ret);
  71. } else {
  72. api.toast({
  73. msg: '当前网络不稳定,请稍后再试'
  74. });
  75. }
  76. });
  77. }
  78. // 对数据进行处理,第一个为values,第二个为file body
  79. $http.prototype.serize = function(data) {
  80. var serizeData;
  81. if ( data instanceof Array) {
  82. if (data.length > 1) {
  83. serizeData = {
  84. values : data[0]
  85. }
  86. } else {
  87. serizeData = {
  88. values : data[0],
  89. files : data[1]
  90. }
  91. }
  92. } else {
  93. if (data.type == 'body') {
  94. delete data.type;
  95. if (data.yy == 'keng') {
  96. delete data.yy
  97. serizeData = {
  98. body : data.id
  99. }
  100. } else {
  101. serizeData = {
  102. body : data
  103. }
  104. }
  105. } else {
  106. serizeData = {
  107. values : data
  108. }
  109. }
  110. }
  111. return serizeData;
  112. }
  113. function alertJson(str) {
  114. alert(JSON.stringify(str));
  115. }