request.js 1.4 KB

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