http.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const baseUrl = process.env.VUE_APP_BASE_URL;
  2. function parseUrl(url) {
  3. let _baseUrl = baseUrl;
  4. if (url.startsWith('http')) {
  5. return url;
  6. }
  7. if (!_baseUrl.endsWith('/')) {
  8. _baseUrl += '/';
  9. }
  10. if (url.startsWith('/')) {
  11. url = url.slice(1);
  12. }
  13. return _baseUrl + url;
  14. }
  15. const http = {
  16. parseUrl: parseUrl,
  17. setToken(token) {
  18. wx.setStorageSync('token', token);
  19. this.token = token;
  20. },
  21. clearToken() {
  22. this.token = '';
  23. wx.removeStorageSync('token');
  24. console.log('clear token');
  25. },
  26. getToken() {
  27. if (!this.token) {
  28. try {
  29. this.token = wx.getStorageSync('token');
  30. } catch (e) {}
  31. }
  32. return this.token;
  33. },
  34. get(url, params, options) {
  35. options = options || {};
  36. return new Promise((resolve, reject) => {
  37. wx.request({
  38. method: 'GET',
  39. url: parseUrl(url),
  40. data: params,
  41. dataType: 'json',
  42. header: {
  43. Accept: 'application/json',
  44. Authorization: this.getToken() ? 'Bearer ' + this.getToken() : 'Bearer ',
  45. ...(options.header || {})
  46. },
  47. success(res) {
  48. if (res && res.statusCode === 200) {
  49. resolve(res.data);
  50. } else {
  51. reject(res.data || res);
  52. }
  53. },
  54. fail(err) {
  55. reject(err.data || err);
  56. }
  57. });
  58. });
  59. },
  60. post(url, data, options) {
  61. console.log('post');
  62. options = options || {};
  63. return new Promise((resolve, reject) => {
  64. wx.request({
  65. method: 'post',
  66. url: parseUrl(url),
  67. data: data,
  68. dataType: 'json',
  69. header: {
  70. Accept: 'application/json',
  71. 'content-type': 'application/x-www-form-urlencoded',
  72. Authorization: this.getToken() ? 'Bearer ' + this.getToken() : '',
  73. ...(options.header || {})
  74. },
  75. success(res) {
  76. if (res && res.statusCode === 200) {
  77. resolve(res.data);
  78. } else {
  79. reject(res.data || res);
  80. }
  81. },
  82. fail(err) {
  83. reject(err.data || err);
  84. }
  85. });
  86. });
  87. },
  88. uploadFile(filePath, options) {
  89. options = options || {};
  90. return new Promise((resolve, reject) => {
  91. wx.uploadFile({
  92. url: baseUrl + '/upload/file',
  93. filePath: filePath,
  94. name: 'file',
  95. header: {
  96. Accept: 'application/json',
  97. 'content-type': 'application/x-www-form-urlencoded',
  98. Authorization: this.getToken() ? 'Bearer ' + this.getToken() : '',
  99. ...(options.header || {})
  100. },
  101. formData: options.formData || {},
  102. success(res) {
  103. if (res && res.statusCode === 200) {
  104. try {
  105. resolve(res.data);
  106. } catch (e) {
  107. reject(e);
  108. }
  109. } else {
  110. reject(res);
  111. }
  112. },
  113. fail(err) {
  114. reject(err);
  115. }
  116. });
  117. });
  118. }
  119. };
  120. export default {
  121. http: http,
  122. install(_Vue) {
  123. _Vue.prototype.$baseUrl = baseUrl;
  124. _Vue.prototype.$http = http;
  125. _Vue.prototype.$request = options => {
  126. options = options || {};
  127. options.url = parseUrl(options.url);
  128. return new Promise((resolve, reject) => {
  129. options.success = res => {
  130. resolve(res);
  131. };
  132. options.success = err => {
  133. reject(err);
  134. };
  135. });
  136. };
  137. }
  138. };