http.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import store from './store/store'
  2. import { getHashQuery } from './utils'
  3. axios.defaults.timeout = config.timeout // 响应时间
  4. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
  5. axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? '/api' : config.baseURL // 配置接口地址
  6. // axios.defaults.baseURL = config.baseURL // 配置接口地址
  7. //
  8. axios.defaults.transformRequest = [function (data) {
  9. return qs.stringify(data)
  10. }]
  11. // POST传参序列化(添加请求拦截器)
  12. axios.interceptors.request.use((conf) => {
  13. if (location.href.indexOf('shareID') > -1) {
  14. if (conf.method === 'post') {
  15. // let data = qs.parse(conf.data)
  16. conf.data = {
  17. ...conf.data,
  18. shareID: getHashQuery('shareID')
  19. }
  20. } else if (conf.method === 'get') {
  21. conf.params = {
  22. ...conf.params,
  23. shareID: getHashQuery('shareID')
  24. }
  25. }
  26. }
  27. conf.params = {
  28. ...conf.params,
  29. _t: new Date().getTime()
  30. }
  31. if (store.state.user && store.state.user.code) {
  32. conf.headers.code = store.state.user.code
  33. if (conf.headers.noAuth) {
  34. delete conf.headers.noAuth
  35. }
  36. } else {
  37. if (process.env.NODE_ENV === 'development') {
  38. conf.headers.code = '4E400B1990904F46B649D5F0C00A3F85'
  39. } else if (conf.headers.noAuth) {
  40. delete conf.headers.noAuth
  41. } else {
  42. location.href = config.authorizaURL + '?path=' + encodeURIComponent(location.hash.slice(2, location.hash.length)) // 跳授权页
  43. return Promise.reject('no-code')
  44. }
  45. }
  46. // if (['get', 'delete', 'GET', 'DELETE'].indexOf(conf.method) > -1) {
  47. // let ret={}
  48. // for (let it in conf.params) {
  49. // ret[it]= conf.params[it].replace(/\[/g,'%5B').replace(/\]/g,'%5D')
  50. // }
  51. // conf.params=ret
  52. // }
  53. return conf
  54. }, (error) => {
  55. Vue.$vux.toast.show({
  56. text: '参数校验失败',
  57. type: 'warn',
  58. position: 'middle'
  59. })
  60. return Promise.reject(error)
  61. })
  62. // 返回状态判断(添加响应拦截器)
  63. axios.interceptors.response.use((res) => {
  64. // console.log(res.config.url.indexOf('/pay/getGroupItemSheet')=== -1)
  65. // 对响应数据做些事
  66. if (res.data.code === 0) {
  67. return Promise.resolve(res.data)
  68. } else {
  69. // console.log(res)
  70. let times = 10
  71. if (res.config.url.indexOf('/pay/getGroupItemSheet') === -1 && times > 0) {
  72. Vue.$vux.toast.show({
  73. text: res.data.msg || '服务器错误',
  74. type: 'warn',
  75. position: 'middle'
  76. })
  77. return Promise.reject(res)
  78. times--
  79. }
  80. }
  81. }, (error) => {
  82. console.log(error)
  83. if (error !== 'no-code') {
  84. Vue.$vux.toast.show({
  85. text: '服务器错误',
  86. type: 'warn',
  87. position: 'middle'
  88. })
  89. }
  90. return Promise.reject(error)
  91. })
  92. export function fetchPost (url, params, options = {}) {
  93. return new Promise((resolve, reject) => {
  94. axios.post(url, params, options)
  95. .then(response => {
  96. resolve(response)
  97. }, err => {
  98. reject(err)
  99. })
  100. .catch((error) => {
  101. reject(error)
  102. })
  103. })
  104. }
  105. export function upLoadPost (url, params) {
  106. return new Promise((resolve, reject) => {
  107. axios.post(url, params, {
  108. headers: {
  109. 'Content-Type': 'multipart/form-data'
  110. }
  111. })
  112. .then(response => {
  113. resolve(response)
  114. }, err => {
  115. reject(err)
  116. })
  117. .catch((error) => {
  118. reject(error)
  119. })
  120. })
  121. }
  122. export function fetchGet (url, param, options) {
  123. return new Promise((resolve, reject) => {
  124. axios.get(url, { params: param, ...options })
  125. .then(response => {
  126. resolve(response)
  127. }, err => {
  128. reject(err)
  129. })
  130. .catch((error) => {
  131. reject(error)
  132. })
  133. })
  134. }
  135. export default {
  136. fetchPost,
  137. fetchGet
  138. }