request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. if(!LoginFailure){
  68. LoginFailure = true
  69. // 未登录
  70. store.commit('app/SET_TOKEN' , '');
  71. uni.reLaunch({
  72. url:'/pages/login/index',
  73. complete:() => {
  74. LoginFailure = false
  75. }
  76. })
  77. }
  78. } else {
  79. uni.showToast({
  80. icon: 'none',
  81. title: data.message || '系统错误'
  82. })
  83. reject(data)
  84. }
  85. },
  86. fail: (e) => {
  87. // console.log('fail = ', e)
  88. reject()
  89. },
  90. complete: () => {
  91. setTimeout(() => {
  92. uni.hideLoading()
  93. },1000)
  94. }
  95. }))
  96. if (pending[key]) {
  97. // 中断请求
  98. pending[key].abort()
  99. }
  100. // setTimeout 让pending[key].abort()执行完成后在存值
  101. setTimeout(() => {
  102. pending[key] = handler
  103. }, 0)
  104. })
  105. return result
  106. }
  107. export const axios = {
  108. get(url, data, options = {}) {
  109. return request({
  110. url,
  111. data,
  112. method: 'GET',
  113. ...options
  114. })
  115. },
  116. post(url, data, options = {}) {
  117. return request({
  118. url,
  119. data,
  120. method: 'POST',
  121. ...options
  122. })
  123. },
  124. getDow(url, data, options = {}) {
  125. // console.log('getDow' , options)
  126. return request({
  127. url,
  128. data,
  129. method: 'GET',
  130. ...options
  131. })
  132. },
  133. }