request.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const axios = require('axios')
  2. /* axios.defaults.withCredentials = true*/
  3. // create an axios instance
  4. const service = axios.create()
  5. const baseUrl = 'http://127.0.0.1:7074/'
  6. const basePrefixUrl = 'api'
  7. const timeout = 600000
  8. const uploadTimeout = 600000
  9. function request(config) {
  10. return service(config)
  11. }
  12. /* get 默认取数据 */
  13. function get(config) {
  14. return _get({ ...config, baseUrl })
  15. }
  16. function getPay(config) {
  17. return _get({ ...config, basePayUrl })
  18. }
  19. function _get(config = {
  20. baseUrl,
  21. url: '',
  22. data: {}
  23. }) {
  24. const thisBasePrefixUrl = config.basePrefixUrl !== undefined ? config.basePrefixUrl : basePrefixUrl
  25. return service.get(config.baseUrl + thisBasePrefixUrl + config.url, {
  26. params: {
  27. ...config.data
  28. },
  29. headers: config.headers || {},
  30. timeout: config.timeout || timeout
  31. })
  32. }
  33. function post(config) {
  34. return _post({ ...config, baseUrl })
  35. }
  36. function postPay(config) {
  37. return _post({ ...config, baseUrl: basePayUrl })
  38. }
  39. /* post 默认提交数据 */
  40. function _post(config = {
  41. baseUrl,
  42. url: '',
  43. data: {}
  44. }) {
  45. const thisBasePrefixUrl = config.basePrefixUrl !== undefined ? config.basePrefixUrl : basePrefixUrl
  46. return service.post(config.baseUrl + thisBasePrefixUrl + config.url, {
  47. ...config.data
  48. }, {
  49. headers: config.headers || {},
  50. timeout: config.timeout || timeout,
  51. roles:config.roles,
  52. })
  53. }
  54. module.exports = {
  55. get,
  56. post
  57. }