1. import Vue from 'vue'
    2. import axios from 'axios'
    3. import router from '@/router'
    4. import store from '@/store'
    5. import qs from 'qs'
    6. const defaultConfig = {
    7. needToken: true // 默认接口都需要token
    8. }
    9. const showMessage = msg => {
    10. Vue.prototype.$message({
    11. showClose: true,
    12. message: msg,
    13. type: 'error'
    14. })
    15. }
    16. const Axios = axios.create({
    17. baseURL: '',
    18. timeout: '',
    19. headers: {
    20. 'Content-Type': 'application/json',
    21. 'Cache-Control': 'no-cache'
    22. }
    23. })
    24. // request拦截器,设置全局请求
    25. Axios.interceptors.request.use(config => {
    26. config = {
    27. ...defaultConfig,
    28. ...config
    29. }
    30. if (config.needToken) {
    31. const token = store.getters.token
    32. if (token) {
    33. config.headers.Authorization = token
    34. } else {
    35. if (router.currentRoute.name !== 'Login') {
    36. router.push({
    37. name: 'Login'
    38. })
    39. return
    40. }
    41. }
    42. }
    43. // get进行序列化
    44. if (config.method === 'get') {
    45. // { a: ['b', 'c'] }
    46. // indices:a[0]=b&a[1]=c
    47. // brackets:a[0]=b&a[1]=c
    48. // repeat:a=b&a=c
    49. // comma:a=b,c
    50. config.paramsSerializer = params =>
    51. qs.stringify(params, { arrayFormat: 'repeat' })
    52. }
    53. return config
    54. })
    55. // response拦截器,做些错误处理
    56. Axios.interceptors.response.use(
    57. response => {
    58. const data = response.data
    59. console.log('response::', response)
    60. if (response.config.responseType === 'blob') {
    61. if (response.headers['content-type'].indexOf('application/json') > -1) {
    62. try {
    63. const reader = new FileReader()
    64. reader.onload = function (event) {
    65. const content = reader.result
    66. const message = JSON.parse(content) // 接口错误信息
    67. return Promise.reject(message)
    68. }
    69. reader.readAsText(data)
    70. } catch (error) {
    71. return Promise.reject(error.toStrng())
    72. }
    73. return
    74. } else {
    75. // 下载文件处理
    76. return response
    77. }
    78. }
    79. return response
    80. // return Promise.reject(data)
    81. },
    82. error => {
    83. console.log('error::', error)
    84. let text = ''
    85. if (error.response) {
    86. if (error.response.status) {
    87. const status = error.response.status
    88. switch (status) {
    89. case 404:
    90. text = '接口异常:接口不存在(404)'
    91. break
    92. case 500:
    93. text = '接口异常:服务器错误(500)'
    94. break
    95. default:
    96. text = `接口异常:状态码错误-${status}`
    97. break
    98. }
    99. } else {
    100. text = '连接服务器失败,请稍后重试'
    101. }
    102. } else if (
    103. error.code === 'ECONNABORTED' &&
    104. error.message.indexOf('timeout') > -1
    105. ) {
    106. text = '请求超时,请重试'
    107. } else if (error.message.indexOf('Invalid URL') > -1) {
    108. text = '请求地址异常'
    109. } else if (error.message === 'Network Error') {
    110. text = '网络异常,请稍后重试'
    111. } else {
    112. text = '系统异常,请重试'
    113. }
    114. if (text) {
    115. showMessage(text)
    116. }
    117. return Promise.reject(error)
    118. }
    119. )
    120. /**
    121. * POST请求
    122. *
    123. * @export
    124. * @param {*} url // 请求地址
    125. * @param {*} [params={}] // params参数
    126. * @param {*} [config={}] // config配置
    127. * @return {*}
    128. */
    129. export function POST (url, params = {}, config = {}) {
    130. return new Promise((resolve, reject) => {
    131. Axios.post(url, params, config)
    132. .then(response => {
    133. resolve(response.data)
    134. })
    135. .catch(err => {
    136. reject(err)
    137. })
    138. })
    139. }
    140. /**
    141. * PUT请求
    142. *
    143. * @export
    144. * @param {*} url // 请求地址
    145. * @param {*} [params={}] // params参数
    146. * @param {*} [config={}] // config配置
    147. * @return {*}
    148. */
    149. export function PUT (url, params = {}, config = {}) {
    150. return new Promise((resolve, reject) => {
    151. Axios.put(url, params, config)
    152. .then(response => {
    153. resolve(response.data)
    154. })
    155. .catch(err => {
    156. reject(err)
    157. })
    158. })
    159. }
    160. /**
    161. * DELETE请求
    162. *
    163. * @export
    164. * @param {*} url // 请求地址
    165. * @param {*} [params={}] // params参数
    166. * @param {*} [config={}] // config配置
    167. * @return {*}
    168. */
    169. export function DELETE (url, params = {}, config = {}) {
    170. return new Promise((resolve, reject) => {
    171. Axios.delete(url, params, config)
    172. .then(response => {
    173. resolve(response.data)
    174. })
    175. .catch(err => {
    176. reject(err)
    177. })
    178. })
    179. }
    180. /**
    181. * GET请求
    182. *
    183. * @export
    184. * @param {*} url // 请求地址
    185. * @param {*} [params={}] // params参数
    186. * @param {*} [config={}] // config配置
    187. * @return {*}
    188. */
    189. export function GET (url, params = {}, config = {}) {
    190. return new Promise((resolve, reject) => {
    191. Axios.get(url, {
    192. params,
    193. ...config
    194. })
    195. .then(response => {
    196. resolve(response.data)
    197. })
    198. .catch(error => {
    199. reject(error)
    200. })
    201. })
    202. }