request.js 2.6 KB

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