upload.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import ENV_CONFIG from '@/.env.js'
  2. // api前缀
  3. export const HTTP_URL_DEV_PREFIX = '/api'
  4. export const HTTP_URL_PROD_PREFIX = '/api'
  5. const env = ENV_CONFIG[process.env.ENV_TYPE];
  6. // 基于uview-ui的http配置
  7. export const UVIEWUI_HTTP_CONFIG = {
  8. // 地址
  9. baseUrl: env.apiUrl+env.apiUrlPrefix,
  10. // 请求方式
  11. method: 'post',
  12. // 参数类型
  13. dataType: 'json',
  14. // 是否显示请求中的loading
  15. showLoading: true,
  16. // 请求loading中的文字提示
  17. loadingText: '请求中...',
  18. // 在此时间内,请求还没回来的话,就显示加载中动画,单位ms
  19. loadingTime: 800,
  20. // 是否在拦截器中返回服务端的原始数据
  21. originalData: true,
  22. // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透
  23. loadingMask: true,
  24. // header:{
  25. // token:uni.getStorageSync('token')
  26. // },
  27. }
  28. // 此处配置请求拦截器
  29. export const httpRequest = (config) => {
  30. const token = uni.getStorageSync('token');
  31. config.header = {
  32. token: token
  33. }
  34. return config;
  35. }
  36. // 此处配置响应拦截器
  37. export const httpResponse = (res) => {
  38. return new Promise(function(resolve, reject) {
  39. if (res.statusCode == 200) {
  40. console.log('成功>>>>');
  41. if(res.data.code==0){
  42. uni.showToast({
  43. icon:'none',
  44. title:res.data.msg
  45. })
  46. return reject(res.data);
  47. }
  48. if(res.data.code==401){
  49. console.log('接口未登录>>>>401');
  50. // 未登录
  51. uni.removeStorageSync('token');
  52. uni.switchTab({
  53. url: '/pages/home/index'
  54. })
  55. return reject(res.data);
  56. }
  57. return resolve(res.data);
  58. }
  59. if (res.statusCode == 401) {
  60. console.log('未登录>>>>');
  61. // 未登录
  62. uni.switchTab({
  63. url: '/pages/home/index'
  64. })
  65. uni.removeStorageSync('token');
  66. return reject(res.data);
  67. }
  68. if (res.statusCode == 500) {
  69. console.log('服务器异常>>>>500');
  70. uni.showToast({
  71. title: '服务器异常,请联系客服!',
  72. icon: 'none',
  73. duration: 3000
  74. })
  75. return reject(res.data);
  76. }
  77. return resolve(res.data);
  78. })
  79. }
  80. // http安装方法
  81. export const installHttpConfig = (Vue, vm) => {
  82. Vue.prototype.$u.http.setConfig(UVIEWUI_HTTP_CONFIG);
  83. Vue.prototype.$u.http.interceptor.request = httpRequest
  84. Vue.prototype.$u.http.interceptor.response = httpResponse
  85. }