request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import baseUrl from '@/utils/baseurl.js'
  2. import { getToken } from '@/utils/auth';
  3. import errorCode from '@/utils/errorCode';
  4. let Fly = require("flyio/dist/npm/wx")
  5. let fly = new Fly
  6. //设置超时
  7. fly.config.timeout = 30000;
  8. //设置请求基地址
  9. fly.config.baseURL = baseUrl;
  10. fly.interceptors.request.use((config) => {
  11. // 在发送请求之前做些什么
  12. // if(isLoading){
  13. // console.log(isLoading,222)
  14. // uni.showLoading({
  15. // title: "加载中",
  16. // mask: true,
  17. // })
  18. // }
  19. const isToken = (config.headers || {}).isToken === false
  20. if (getToken() && !isToken) {
  21. config.headers['Authorization'] = `Bearer ${getToken()}`;
  22. }
  23. return config
  24. }, function(error) {
  25. // 对请求错误做些什么
  26. // if(isLoading){
  27. // uni.hideLoading();
  28. // }
  29. uni.showToast({
  30. title: error,
  31. icon:'error'
  32. });
  33. return Promise.reject(error)
  34. })
  35. // 添加响应拦截器
  36. fly.interceptors.response.use((res) => {
  37. // 对响应数据做些事
  38. const { code, msg, data } = res.data
  39. const rescode = code || 200;
  40. // 获取错误信息
  41. const resmsg = errorCode[rescode] || msg || errorCode['default']
  42. // 二进制数据则直接返回
  43. // if(isLoading){
  44. // uni.hideLoading();
  45. // }
  46. if(res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer'){
  47. return resmsg
  48. }
  49. if (rescode === 401) {
  50. //#ifndef MP-WEIXIN
  51. uni.showModal({
  52. title: '系统提示',
  53. content: '请先进行登录',
  54. mask:true,
  55. success: function (res) {
  56. if (res.confirm) {
  57. uni.reLaunch({
  58. url:"/pages/logo/index"
  59. })
  60. } else if (res.cancel) {
  61. console.log('用户点击取消');
  62. }
  63. }
  64. });
  65. //#endif
  66. //#ifdef MP-WEIXIN
  67. wx.showModal({
  68. title: '系统提示',
  69. content: '请先进行登录',
  70. mask:true,
  71. success (res) {
  72. if (res.confirm) {
  73. uni.reLaunch({
  74. url:"/pages/logo/index"
  75. })
  76. } else if (res.cancel) {
  77. console.log('用户点击取消')
  78. }
  79. }
  80. })
  81. //#endif
  82. } else if (rescode === 500) {
  83. let mess = ""
  84. if(resmsg==="用户不存在/密码错误"){
  85. mess = "工号/密码错误"
  86. }else if(resmsg==="登录用户: 不存在"){
  87. mess = "用户不存在"
  88. }else{
  89. mess = resmsg
  90. }
  91. uni.showToast({
  92. title: mess,
  93. icon:'error'
  94. });
  95. return Promise.reject(new Error(resmsg))
  96. } else if (rescode !== 200) {
  97. uni.showToast({
  98. title: resmsg,
  99. icon:'error'
  100. });
  101. return Promise.reject('error')
  102. } else {
  103. return data
  104. }
  105. }, (error) => {
  106. const { message } = error.message
  107. uni.showToast({
  108. title: message,
  109. duration: 5*1000,
  110. icon:'error'
  111. });
  112. return Promise.reject(error);
  113. })
  114. export default fly;