request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // 封装请求
  2. import config from './config.js'
  3. import {
  4. getToken
  5. } from "./common.js"
  6. // 请求域名
  7. // const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
  8. const baseUrl = config.baseUrl
  9. let LoginFailure = false
  10. // 存储请求信息,当接口正在请求中,不会进行下一次接口请求
  11. const pending = {}
  12. // 处理请求成功
  13. const isHttpSuccess = (stauts) => {
  14. return stauts >= 200 && stauts < 300
  15. }
  16. // 处理请求错误
  17. const errorHandle = (res) => {
  18. // 日志处理 全局错误提示
  19. if (res && res.statusCode !== 200) {
  20. // 浏览器错误
  21. uni.showToast({
  22. title: '系统错误,请联系管理员',
  23. icon: 'none',
  24. duration: 2000
  25. })
  26. } else {
  27. // console.log('errorHandle1', res)
  28. // 服务器错误
  29. uni.showToast({
  30. title: res.data.msg || '系统错误,请联系管理员',
  31. icon: 'none',
  32. duration: 2000
  33. })
  34. }
  35. }
  36. export const request = async (opt = {}) => {
  37. // 考虑与uniapp的接口进行兼容
  38. let headers = {
  39. "Content-Type": "application/x-www-form-urlencoded"
  40. }
  41. const token = getToken();
  42. if (token) {
  43. headers['authorization'] = token;
  44. }
  45. console.log('headers = ', headers)
  46. const {
  47. data,
  48. success,
  49. fail
  50. } = opt
  51. // pending 中的键名
  52. const key = opt.url + '&' + (opt.method || 'GET')
  53. // 处理请求地址
  54. opt.url = baseUrl + opt.url
  55. // 处理公共参数
  56. opt.data = data
  57. opt.timeout = 5000 // 请求超时时间
  58. opt.header = headers
  59. const result = new Promise((resolve, reject) => {
  60. const handler = uni.request(Object.assign({}, opt, {
  61. success: (res) => {
  62. const data = res.data;
  63. console.log('data = ', res)
  64. delete pending[key]
  65. if (data.type === 'ok') {
  66. resolve(data.message)
  67. } else {
  68. uni.showToast({
  69. icon: 'none',
  70. title: data.message || '系统错误'
  71. })
  72. reject(data)
  73. }
  74. },
  75. fail: (e) => {
  76. console.log('fail = ', e)
  77. }
  78. }))
  79. if (pending[key]) {
  80. // 中断请求
  81. pending[key].abort()
  82. }
  83. // setTimeout 让pending[key].abort()执行完成后在存值
  84. setTimeout(() => {
  85. pending[key] = handler
  86. }, 0)
  87. })
  88. return result
  89. }
  90. export const axios = {
  91. get(url, data, options = {}) {
  92. return request({
  93. url,
  94. data,
  95. method: 'GET',
  96. ...options
  97. })
  98. },
  99. post(url, data, options = {}) {
  100. return request({
  101. url,
  102. data,
  103. method: 'POST',
  104. ...options
  105. })
  106. },
  107. getDow(url, data, options = {}) {
  108. // console.log('getDow' , options)
  109. return request({
  110. url,
  111. data,
  112. method: 'GET',
  113. ...options
  114. })
  115. },
  116. }