request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // uni.showToast({
  88. // icon: 'none',
  89. // title: '系统错误'
  90. // })
  91. reject()
  92. },
  93. complete: () => {
  94. setTimeout(() => {
  95. uni.hideLoading()
  96. },1000)
  97. }
  98. }))
  99. if (pending[key]) {
  100. // 中断请求
  101. pending[key].abort()
  102. }
  103. // setTimeout 让pending[key].abort()执行完成后在存值
  104. setTimeout(() => {
  105. pending[key] = handler
  106. }, 0)
  107. })
  108. return result
  109. }
  110. export const axios = {
  111. get(url, data, options = {}) {
  112. return request({
  113. url,
  114. data,
  115. method: 'GET',
  116. ...options
  117. })
  118. },
  119. post(url, data, options = {}) {
  120. return request({
  121. url,
  122. data,
  123. method: 'POST',
  124. ...options
  125. })
  126. },
  127. getDow(url, data, options = {}) {
  128. // console.log('getDow' , options)
  129. return request({
  130. url,
  131. data,
  132. method: 'GET',
  133. ...options
  134. })
  135. },
  136. }