request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. const result = new Promise((resolve, reject) => {
  60. const handler = uni.request(Object.assign({}, opt, {
  61. success: (res) => {
  62. const data = res.data;
  63. delete pending[key]
  64. if (data.type === 'ok') {
  65. resolve(data.message)
  66. }else if (data.type === '999') {
  67. // 未登录
  68. store.commit('app/SET_TOKEN' , '');
  69. uni.navigateTo({
  70. url:'/pages/login/index'
  71. })
  72. } else {
  73. uni.showToast({
  74. icon: 'none',
  75. title: data.message || '系统错误'
  76. })
  77. reject(data)
  78. }
  79. },
  80. fail: (e) => {
  81. // console.log('fail = ', e)
  82. }
  83. }))
  84. if (pending[key]) {
  85. // 中断请求
  86. pending[key].abort()
  87. }
  88. // setTimeout 让pending[key].abort()执行完成后在存值
  89. setTimeout(() => {
  90. pending[key] = handler
  91. }, 0)
  92. })
  93. return result
  94. }
  95. export const axios = {
  96. get(url, data, options = {}) {
  97. return request({
  98. url,
  99. data,
  100. method: 'GET',
  101. ...options
  102. })
  103. },
  104. post(url, data, options = {}) {
  105. return request({
  106. url,
  107. data,
  108. method: 'POST',
  109. ...options
  110. })
  111. },
  112. getDow(url, data, options = {}) {
  113. // console.log('getDow' , options)
  114. return request({
  115. url,
  116. data,
  117. method: 'GET',
  118. ...options
  119. })
  120. },
  121. }