http.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Config from "@/plugins/config.js"
  2. import axios from "axios"
  3. import {
  4. getToken
  5. } from "./auth"
  6. axios.defaults.baseURL = Config.baseUrl
  7. axios.defaults.headers = {
  8. "X-Requested-With": "XMLHttpRequest",
  9. "content-type": "application/json"
  10. }
  11. axios.defaults.responseType = "json"
  12. axios.defaults.timeout = 60 * 1000
  13. /**
  14. * http单个请求
  15. * @param {object} params 请求参数
  16. * @param {integer} successCode 接口正常返回结果标识
  17. *
  18. * @returns Promise
  19. */
  20. export default function request(params, successCode = 0, method = "POST") {
  21. var url = params.url // 请求路径
  22. var data = {
  23. app_type: "pc",
  24. app_type_name: "PC"
  25. }
  26. var token = getToken()
  27. if (token) data.token = token
  28. // 参数
  29. if (params.data != undefined) Object.assign(data, params.data)
  30. //异步
  31. return axios({
  32. url: url,
  33. method: method,
  34. data
  35. })
  36. .then(res => {
  37. const {
  38. code
  39. } = res.data || {}
  40. if (process.client && code == -3 && window.$nuxt.$route.name != 'close') {
  41. window.$nuxt.$router.push('/close');
  42. return;
  43. }
  44. if (code == successCode) return res.data
  45. else return Promise.reject(res.data)
  46. })
  47. .catch(error => {
  48. const {
  49. error_code
  50. } = error
  51. if (error_code === "TOKEN_ERROR") {
  52. error.message = '登录错误'
  53. vue.$store.dispatch("member/remove_token")
  54. if (params.forceLogin) {
  55. vue.$router.push(`/auth/login?redirect=${encodeURIComponent(vue.$router.history.current.fullPath)}`)
  56. }
  57. }
  58. return Promise.reject(error)
  59. })
  60. }
  61. /**
  62. * 并发请求
  63. * @param {array} params 并发请求参数数组,传入数组中对象的顺序要匹配 data 中 url
  64. * @var 该方法为并发请求,数据会在全部请求完之后才返回
  65. * 该方法不建议使用。
  66. */
  67. export function conRequest(params) {
  68. if (Object.prototype.toString.call(params) != "[object Array]") {
  69. return Promise.reject({
  70. code: -1,
  71. msg: "参数必须为数组"
  72. })
  73. }
  74. //同步并发
  75. var quest = []
  76. for (var i = 0; i < url.length; i++) {
  77. quest.push(
  78. axios({
  79. url: params[i].url,
  80. method: method,
  81. params: params[i].data
  82. })
  83. )
  84. }
  85. axios
  86. .all(quest)
  87. .then(
  88. axios.spread(() => {
  89. // 请求全部完成后执行
  90. var res = []
  91. for (let i = 0; i < arguments.length; i++) {
  92. res.push(arguments[i].data)
  93. }
  94. return res
  95. })
  96. )
  97. .catch(error => {
  98. const {
  99. error_code
  100. } = error
  101. if (error_code === "TOKEN_ERROR") {
  102. error.message = '登录错误'
  103. vue.$store.dispatch("member/remove_token")
  104. if (params.forceLogin) {
  105. vue.$router.push(`/auth/login?redirect=${encodeURIComponent(vue.$router.history.current.fullPath)}`)
  106. }
  107. }
  108. return Promise.reject(error)
  109. })
  110. }