From 542359a54f9abb4e4a6979088c9d9f75c84a98de Mon Sep 17 00:00:00 2001 From: 188102836 <69347367+188102836@users.noreply.github.com> Date: Sun, 13 Aug 2023 20:31:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=20(#777)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove escape character 移除此处多余的转义符 * Optimize sample code Object.assign 方法本身会改变 object,没有必要再赋值 * Optimize sample code * Update docs/misc/sign/wbi.md Co-authored-by: ud2 --------- Co-authored-by: ud2 --- docs/misc/sign/wbi.md | 51 ++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/docs/misc/sign/wbi.md b/docs/misc/sign/wbi.md index 0459048..a5d1bfb 100644 --- a/docs/misc/sign/wbi.md +++ b/docs/misc/sign/wbi.md @@ -192,7 +192,7 @@ bar=514&baz=1919810&foo=114&wts=1684746387&w_rid=d3cbd2a2316089117134038bf4caf44 ### JavaScript -需要`axios`、`md5`依赖 +需要 `axios`、`md5` 依赖 ```javascript import md5 from 'md5' @@ -218,16 +218,16 @@ function getMixinKey(orig) { function encWbi(params, img_key, sub_key) { const mixin_key = getMixinKey(img_key + sub_key), curr_time = Math.round(Date.now() / 1000), - chr_filter = /[!'\(\)*]/g + chr_filter = /[!'()*]/g let query = [] - params = Object.assign(params, {wts: curr_time}) // 添加 wts 字段 + Object.assign(params, { wts: curr_time }) // 添加 wts 字段 // 按照 key 重排参数 Object.keys(params).sort().forEach((key) => { query.push( - encodeURIComponent(key) + - '=' + - // 过滤 value 中的 "!'()*" 字符 - encodeURIComponent(('' + params[key]).replace(chr_filter, '')) + `${encodeURIComponent(key)}=${encodeURIComponent( + // 过滤 value 中的 "!'()*" 字符 + params[key].toString().replace(chr_filter, '') + )}` ) }) query = query.join('&') @@ -245,24 +245,31 @@ async function getWbiKeys() { json_content = resp.data, img_url = json_content.data.wbi_img.img_url, sub_url = json_content.data.wbi_img.sub_url + return { - img_key: img_url.substring(img_url.lastIndexOf('/') + 1, img_url.length).split('.')[0], - sub_key: sub_url.substring(sub_url.lastIndexOf('/') + 1, sub_url.length).split('.')[0] + img_key: img_url.slice( + img_url.lastIndexOf('/') + 1, + img_url.lastIndexOf('.') + ), + sub_key: sub_url.slice( + sub_url.lastIndexOf('/') + 1, + sub_url.lastIndexOf('.') + ) } } -const wbi_keys = await getWbiKeys() - -const query = encWbi( - { - foo: '114', - bar: '514', - baz: 1919810 - }, - wbi_keys.img_key, - wbi_keys.sub_key -) -console.log(query) +getWbiKeys().then((wbi_keys) => { + const query = encWbi( + { + foo: '114', + bar: '514', + baz: 1919810 + }, + wbi_keys.img_key, + wbi_keys.sub_key + ) + console.log(query) +}) ``` 输出内容为进行 Wbi 签名的后参数的 url query 形式 @@ -592,4 +599,4 @@ public class WbiTest { System.out.println(finalParam); } } -``` \ No newline at end of file +```