Http.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class HTTP {
  2. constructor() {
  3. this.urls = [{
  4. name: '测试',
  5. url: "http://61.164.207.46:8300"
  6. }, {
  7. name: '正式',
  8. url: "https://crm.meida.com:16691"
  9. }, {
  10. name: "楚楚",
  11. url: "https://cucu.cnyunl.com:8079"
  12. }]
  13. if (process.env.NODE_ENV === 'development') {
  14. this.baseUrl = this.urls[0].url;
  15. } else {
  16. this.baseUrl = this.urls[1].url;
  17. }
  18. console.log("接口地址", this.baseUrl)
  19. }
  20. request({
  21. url,
  22. data = {},
  23. method = "POST",
  24. header = {
  25. 'content-type': 'application/json'
  26. },
  27. showLoading = ''
  28. }) {
  29. return new Promise((resolve, reject) => {
  30. this._request(url, resolve, reject, data, method, header, showLoading);
  31. })
  32. }
  33. _request(url, resolve, reject, data, method, header, showLoading) {
  34. if (showLoading) uni.showLoading({
  35. title: showLoading,
  36. mask: true
  37. })
  38. uni.request({
  39. url: this.baseUrl + '/yos/rest' + url,
  40. data: data,
  41. method: method,
  42. header: header,
  43. timeout: 60000,
  44. success: res => resolve(res.data),
  45. fial: err => reject(err),
  46. complete: (res) => {
  47. if (showLoading) uni.hideLoading()
  48. if (res.errMsg != 'request:ok') {
  49. uni.showToast({
  50. title: '网络异常,请稍后再试',
  51. icon: "none"
  52. })
  53. } else if (res.data.msg == '登陆状态已过期,请重新登陆!') {
  54. uni.redirectTo({
  55. url: '/pages/login/login',
  56. success() {
  57. uni.showToast({
  58. title: res.msg,
  59. icon: "none"
  60. })
  61. }
  62. });
  63. }
  64. }
  65. })
  66. }
  67. }
  68. export {
  69. HTTP
  70. }