智慧水务管理系统 - 精河县供水工程综合管理平台

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. import utils from '../utils.js';
  3. import AxiosURLSearchParams from './AxiosURLSearchParams.js';
  4. /**
  5. * It replaces URL-encoded forms of `:`, `$`, `,`, and spaces with
  6. * their plain counterparts (`:`, `$`, `,`, `+`).
  7. *
  8. * @param {string} val The value to be encoded.
  9. *
  10. * @returns {string} The encoded value.
  11. */
  12. export function encode(val) {
  13. return encodeURIComponent(val)
  14. .replace(/%3A/gi, ':')
  15. .replace(/%24/g, '$')
  16. .replace(/%2C/gi, ',')
  17. .replace(/%20/g, '+');
  18. }
  19. /**
  20. * Build a URL by appending params to the end
  21. *
  22. * @param {string} url The base of the url (e.g., http://www.google.com)
  23. * @param {object} [params] The params to be appended
  24. * @param {?(object|Function)} options
  25. *
  26. * @returns {string} The formatted url
  27. */
  28. export default function buildURL(url, params, options) {
  29. if (!params) {
  30. return url;
  31. }
  32. const _options = utils.isFunction(options)
  33. ? {
  34. serialize: options,
  35. }
  36. : options;
  37. // Read serializer options pollution-safely: own properties and methods on a
  38. // class/template prototype are honored, but values injected onto a polluted
  39. // Object.prototype are ignored.
  40. const _encode = utils.getSafeProp(_options, 'encode') || encode;
  41. const serializeFn = utils.getSafeProp(_options, 'serialize');
  42. let serializedParams;
  43. if (serializeFn) {
  44. serializedParams = serializeFn(params, _options);
  45. } else {
  46. serializedParams = utils.isURLSearchParams(params)
  47. ? params.toString()
  48. : new AxiosURLSearchParams(params, _options).toString(_encode);
  49. }
  50. if (serializedParams) {
  51. const hashmarkIndex = url.indexOf('#');
  52. if (hashmarkIndex !== -1) {
  53. url = url.slice(0, hashmarkIndex);
  54. }
  55. url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
  56. }
  57. return url;
  58. }