index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import axios from 'axios'
  2. import Vue from 'vue'
  3. let config = {}
  4. const instance = axios.create(config)
  5. // 3. 配置信息
  6. config = {
  7. // 每次请求的协议、IP地址。 设置该配置后,每次请求路径都可以使用相对路径,例如"/admin/login"
  8. // baseURL: "http://127.0.0.1:5590",
  9. // 请求超时时间
  10. timeout: 0,
  11. // 每次请求携带cookie
  12. withCredentials: true
  13. }
  14. // 请求拦截,后期可能会用到,先注册在此
  15. instance.interceptors.request.use(
  16. function (config) {
  17. const token = localStorage.getItem('token')
  18. // const token = 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI0ODExMjEwOTM1MjUzNzcwMjQiLCJhdXRoIjoiUk9MRV9GQUNUT1JZX0RJUkNUT1IsUk9MRV9JTk5FUl9VU0VSLFJPTEVfSEFSRFdBUkUiLCJ0b2tlbklkIjoiMSIsImV4cCI6MTY5NzYxMzg1OH0.hgBlNeVkFzDnGjJFoEN91X968v4ecLUCKW_m7nnbA6TsQIISAJREv5xGGyQYSNlxbwsS35N2aa8MyrmZqJcHxQ'
  19. const site = JSON.parse(localStorage.getItem('site'))
  20. // let proxy_url = 'http://121.37.173.82:82'; //打包上线时请改用此处
  21. const proxyUrl = process.env.VUE_APP_BASE_URL // 打包上线时请改用此处
  22. // let proxy_url = '/proxy'//打包上线时此处请注释掉
  23. config.url = proxyUrl + config.url
  24. const urls = ['http://122.112.250.253:7774/jbl/api/mes/login','http://121.36.142.167:7774/jbl/api/mes/login','http://122.112.250.253:7774/jbl/api/site/all/ignore-action?_allow_anonymous=true','http://121.36.142.167:7774/jbl/api/site/all/ignore-action?_allow_anonymous=true','http://clouddevice.qingyaokeji.com/api/inout','http://122.112.250.253:7774/jbl/api/module-data/dispatch_orders/dispatch_orders/diy/update_print_status','http://122.112.250.253:7774/jbl/api/module-data/box_orders/box_orders/diy/update_print_status','http://122.112.250.253:7774/jbl/api/module-data/process_flow_card/process_flow_card/diy/update_print_status']
  25. if (urls.indexOf(config.data.url) !== -1) {
  26. config.data.header = ['Content-Type:application/json']
  27. } else {
  28. config.data.header = [`Authorization:Bearer ${token}`, 'Content-Type:application/json', `Site:${site.site}`]
  29. }
  30. // config.data = JSON.stringify(data)
  31. return config
  32. },
  33. function (error) {
  34. // 对请求错误做些什么
  35. return Promise.reject(error)
  36. }
  37. )
  38. // 2. 响应拦截
  39. instance.interceptors.response.use(
  40. (res) => {
  41. if (res.status === 200) {
  42. if (res.data.code === 200) {
  43. return res.data
  44. } else {
  45. // 若code 非 200
  46. if (res.data.code === 401) {
  47. localStorage.removeItem('mobile_token')
  48. return location.reload()
  49. }
  50. Vue.prototype.$Message.error(res.data.msg || '未知错误')
  51. return res.data || res
  52. }
  53. } else {
  54. return Vue.prototype.$Message.error('请求超时')
  55. }
  56. },
  57. // 对于错误响应的处理
  58. (err) => {
  59. Vue.prototype.$Notice.error({ title: '请求失败', desc: err })
  60. return err
  61. }
  62. )
  63. export default instance