request.js 2.8 KB

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