request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const platform = uni.getSystemInfoSync().platform
  2. var base_url = 'https://hire.hdlkeji.com'
  3. export default {
  4. config: {
  5. baseUrl: base_url,
  6. headers: {
  7. "content-type": "application/json",
  8. "Token": uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
  9. //"content-type": "multipart/form-data",
  10. },
  11. dataType: "json",
  12. responseType: "text"
  13. },
  14. interceptor: {
  15. request: function(_config) {},
  16. response: null
  17. },
  18. request(options) {
  19. // uni.showLoading({
  20. // title: "加载中",
  21. // mask: true,
  22. // });
  23. return new Promise((resolve, reject) => {
  24. let _config = null
  25. options.url = this.config.baseUrl + options.url
  26. // if (uni.getStorageSync('token')) {
  27. // // options.data["Authorization"] = uni.getStorageSync('token')
  28. // options.header["token"] = uni.getStorageSync('token') || ''
  29. // } else {
  30. // console.log('token为空')
  31. // // if (!getCurrentPages().route == "pages/doctor/index") {
  32. // // uni.reLaunch({
  33. // // url: '/pages/doctor/index'
  34. // // })
  35. // // }
  36. // }
  37. //console.log(options.header)
  38. options.header = Object.assign(this.config.headers, options.header)
  39. options.fail = (response) => {
  40. uni.showToast({
  41. icon: "none",
  42. title: '网络连接失败,请检查网络'
  43. })
  44. reject(response)
  45. }
  46. options.complete = (response) => {
  47. response.config = _config
  48. if (response.statusCode === 200 || response.statusCode === 201) { //成功
  49. resolve(response.data);
  50. } else {
  51. if (response.statusCode === 404) {
  52. uni.showToast({
  53. icon: "none",
  54. title: '接口不存在'
  55. })
  56. }
  57. if (response.statusCode === 500) {
  58. uni.showToast({
  59. icon: "none",
  60. title: '服务器错误'
  61. })
  62. }
  63. reject(response)
  64. }
  65. }
  66. _config = Object.assign({}, this.config, options)
  67. _config.requestId = new Date().getTime()
  68. if (this.interceptor.request) {
  69. this.interceptor.request(_config)
  70. }
  71. uni.request(_config);
  72. });
  73. },
  74. get(url, data, showtips, options) {
  75. data = data ? data : {};
  76. if (!options) {
  77. options = {}
  78. }
  79. options.url = url
  80. options.data = data
  81. options.header = {}
  82. options.method = 'GET'
  83. options.showtips = showtips
  84. return this.request(options)
  85. },
  86. post(url, data, showtips, options) {
  87. data = data ? data : {};
  88. if (!options) {
  89. options = {}
  90. }
  91. options.url = url
  92. options.data = data
  93. options.header = {}
  94. options.method = 'POST'
  95. options.showtips = showtips
  96. return this.request(options)
  97. },
  98. put(url, data, showtips, options) {
  99. if (!options) {
  100. options = {}
  101. }
  102. options.url = url
  103. options.data = data
  104. options.method = 'PUT'
  105. options.showtips = showtips
  106. return this.request(options)
  107. },
  108. del(url, data, showtips, options) {
  109. if (!options) {
  110. options = {}
  111. }
  112. options.url = url
  113. options.data = data
  114. options.showtips = showtips
  115. options.method = 'DELETE'
  116. return this.request(options)
  117. }
  118. }