Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qs 处理参数的时的一个坑 #206

Open
yaogengzhu opened this issue Apr 10, 2024 · 0 comments
Open

qs 处理参数的时的一个坑 #206

yaogengzhu opened this issue Apr 10, 2024 · 0 comments

Comments

@yaogengzhu
Copy link
Owner

在处理 '2024-04-01 19:00:00' 遇到空格时,会格式化处理,因此优化方案如下

const formatParams = (data = {}, method = 'get') => {
  const newParams = Object.assign(data);
  const keys = Object.keys(newParams).sort((a, b) => a.localeCompare(b));
  const newParams1 = {};
  keys.forEach((key) => {
    newParams1[key] = newParams[key];
  });
  const stirngA = qs.stringify(newParams1, {
    encoder: (str) => {
      if (typeof str !== 'string') return str;
      return str.replace(/ /g, ' ');
    },
  });
  const stringSignTemp = `${stirngA}&key=${config.hyKey}`;
  const sign = md5.md5Hash(stringSignTemp).toString().toUpperCase();
  if (method === 'get') {
    const otherParams = Object.assign(newParams1, {
      sign,
    });
    return qs.stringify(otherParams, {
      encoder: (str) => {
        if (typeof str !== 'string') return str;
        return str.replace(/ /g, ' ');
      },
    });
  }
  const otherParams = Object.assign(newParams1, {
    sign,
  });
  return otherParams;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant