request.js 1.4 KB

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