httpRequest.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import router from '@/router'
  4. import qs from 'qs'
  5. import merge from 'lodash/merge'
  6. import { clearLoginInfo } from '@/utils'
  7. export const entryName = '亿职赞管理系统';
  8. const http = axios.create({
  9. timeout: 1000 * 30,
  10. withCredentials: true,
  11. headers: {
  12. 'Content-Type': 'application/json; charset=utf-8'
  13. }
  14. })
  15. /**
  16. * 请求拦截
  17. */
  18. http.interceptors.request.use(config => {
  19. config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
  20. return config
  21. }, error => {
  22. return Promise.reject(error)
  23. })
  24. /**
  25. * 响应拦截
  26. */
  27. http.interceptors.response.use(response => {
  28. if (response.data && response.data.code === 401) { // 401, token失效
  29. clearLoginInfo()
  30. router.push({ name: 'login' })
  31. }
  32. return response
  33. }, error => {
  34. return Promise.reject(error)
  35. })
  36. /**
  37. * 请求地址处理
  38. * @param {*} actionName action方法名称
  39. */
  40. http.adornUrl = (actionName) => {
  41. // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
  42. // return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
  43. // return 'http://yizhizan.edccc.cn/sqx_fast/' + actionName
  44. return 'https://manger.bosszan.com/sqx_fast/' + actionName
  45. // return 'https://zpadmin.xianmaxiong.com/sqx_fast/' + actionName
  46. // return '/sqx_fast/' + actionName
  47. // return 'http://localhost:7155/sqx_fast/' + actionName
  48. }
  49. http.adornWss = (actionName) => {
  50. // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
  51. // return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi/' : window.SITE_CONFIG.baseUrl) + actionName
  52. // return 'wss://yizhizan.edccc.cn/wss/' + actionName
  53. return 'wss://manger.bosszan.com/wss/' + actionName
  54. // return 'https://zpadmin.xianmaxiong.com.com/sqx_fast/' + actionName
  55. }
  56. /**
  57. * get请求参数处理
  58. * @param {*} params 参数对象
  59. * @param {*} openDefultParams 是否开启默认参数?
  60. */
  61. http.adornParams = (params = {}, openDefultParams = false) => {
  62. var defaults = {
  63. 't': new Date().getTime()
  64. }
  65. return openDefultParams ? merge(defaults, params) : params
  66. }
  67. /**
  68. * post请求数据处理
  69. * @param {*} data 数据对象
  70. * @param {*} openDefultdata 是否开启默认数据?
  71. * @param {*} contentType 数据格式
  72. * json: 'application/json; charset=utf-8'
  73. * form: 'application/x-www-form-urlencoded; charset=utf-8'
  74. */
  75. http.adornData = (data = {}, openDefultdata = false, contentType = 'json') => {
  76. var defaults = {
  77. 't': new Date().getTime()
  78. }
  79. data = openDefultdata ? merge(defaults, data) : data
  80. return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
  81. }
  82. export default http