http.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import config from '../config.js'
  2. class Http {
  3. constructor(base_url = '') {
  4. this._baseUrl = base_url;
  5. this._requestCount = 0;
  6. this._showWait = true;
  7. this._showError = true;
  8. }
  9. setWait(value) {
  10. this._showWait = value;
  11. return this;
  12. }
  13. autoError(value) {
  14. this._showError = value;
  15. return this;
  16. }
  17. request(url, option = {}) {
  18. let that = this;
  19. return new Promise((resolve, reject) => {
  20. option.url = that._baseUrl + url;
  21. option.method = option.method || "POST";
  22. option.header = {
  23. 'Content-Type': 'application/x-www-form-urlencoded',
  24. };
  25. // option.header.token = uni.getStorageSync('access_token') || "";
  26. option.fail = function(res) {
  27. setTimeout(function(){
  28. var nowtime=(new Date().getTime())/1000;
  29. var errortime=uni.getStorageSync('errortime');
  30. if(!errortime) {
  31. errortime=nowtime;
  32. uni.setStorageSync('errortime',nowtime);
  33. }
  34. if(nowtime-errortime>3600*12){
  35. uni.setStorageSync('errortime',nowtime);
  36. uni.showToast({
  37. title: '网络连接不可用,请稍后重试',
  38. icon:'none',
  39. duration: 1000,
  40. position: "bottom"
  41. });
  42. }
  43. },100)
  44. };
  45. option.success = function(res) {
  46. if (res.statusCode == 200) {
  47. if (res.data && res.data.code == 200) {
  48. resolve(res.data, res);
  49. } else {
  50. if (res.data && res.data.message && that._showError) {
  51. setTimeout(() => {
  52. uni.showToast({
  53. icon: 'none',
  54. title: res.data.message
  55. });
  56. }, 20);
  57. }
  58. reject(res.data, res);
  59. }
  60. } else {
  61. if (res.data && res.data.message && that._showError) {
  62. setTimeout(() => {
  63. uni.showToast({
  64. icon: 'none',
  65. title: res.data.message
  66. });
  67. }, 20);
  68. }
  69. reject(res.data, res);
  70. }
  71. };
  72. let _complete = option.complete;
  73. option.complete = function(res) {
  74. that._showWait = true;
  75. that._showError = true;
  76. if (!(--that._requestCount)) uni.hideLoading();
  77. _complete && _complete(res);
  78. };
  79. if (that._showWait && !that._requestCount) {
  80. uni.showLoading({
  81. title: '加载中',
  82. mask: true
  83. });
  84. }
  85. that._requestCount++;
  86. uni.request(option);
  87. });
  88. }
  89. get(url, data = {}) {
  90. if (data._showWait === false) this.setWait(false);
  91. data.agentid=this.agentid();
  92. return this.request(url, {
  93. method: "GET",
  94. data
  95. });
  96. }
  97. post(url, data = {}) {
  98. if (data._showWait === false) this.setWait(false);
  99. data.agentid=this.agentid();
  100. return this.request(url, {
  101. method: "POST",
  102. data
  103. });
  104. }
  105. agentid(){
  106. if(uni.getStorageSync('agentid')) return uni.getStorageSync('agentid');
  107. else{
  108. var agentid=this.randomstr(18);
  109. uni.setStorageSync('agentid',agentid);
  110. return agentid;
  111. }
  112. }
  113. randomstr(len){
  114. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  115. var maxPos = chars.length;
  116.    var pwd='';
  117. for (var i = 0; i < len; i++) {
  118.     pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  119.   }
  120.   return pwd;
  121. }
  122. }
  123. export default new Http(config.apiUri);