优化示例代码 (#777)
* Remove escape character 移除此处多余的转义符 * Optimize sample code Object.assign 方法本身会改变 object,没有必要再赋值 * Optimize sample code * Update docs/misc/sign/wbi.md Co-authored-by: ud2 <sjx233@qq.com> --------- Co-authored-by: ud2 <sjx233@qq.com>
This commit is contained in:
@@ -218,16 +218,16 @@ function getMixinKey(orig) {
|
|||||||
function encWbi(params, img_key, sub_key) {
|
function encWbi(params, img_key, sub_key) {
|
||||||
const mixin_key = getMixinKey(img_key + sub_key),
|
const mixin_key = getMixinKey(img_key + sub_key),
|
||||||
curr_time = Math.round(Date.now() / 1000),
|
curr_time = Math.round(Date.now() / 1000),
|
||||||
chr_filter = /[!'\(\)*]/g
|
chr_filter = /[!'()*]/g
|
||||||
let query = []
|
let query = []
|
||||||
params = Object.assign(params, {wts: curr_time}) // 添加 wts 字段
|
Object.assign(params, { wts: curr_time }) // 添加 wts 字段
|
||||||
// 按照 key 重排参数
|
// 按照 key 重排参数
|
||||||
Object.keys(params).sort().forEach((key) => {
|
Object.keys(params).sort().forEach((key) => {
|
||||||
query.push(
|
query.push(
|
||||||
encodeURIComponent(key) +
|
`${encodeURIComponent(key)}=${encodeURIComponent(
|
||||||
'=' +
|
|
||||||
// 过滤 value 中的 "!'()*" 字符
|
// 过滤 value 中的 "!'()*" 字符
|
||||||
encodeURIComponent(('' + params[key]).replace(chr_filter, ''))
|
params[key].toString().replace(chr_filter, '')
|
||||||
|
)}`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
query = query.join('&')
|
query = query.join('&')
|
||||||
@@ -245,14 +245,20 @@ async function getWbiKeys() {
|
|||||||
json_content = resp.data,
|
json_content = resp.data,
|
||||||
img_url = json_content.data.wbi_img.img_url,
|
img_url = json_content.data.wbi_img.img_url,
|
||||||
sub_url = json_content.data.wbi_img.sub_url
|
sub_url = json_content.data.wbi_img.sub_url
|
||||||
|
|
||||||
return {
|
return {
|
||||||
img_key: img_url.substring(img_url.lastIndexOf('/') + 1, img_url.length).split('.')[0],
|
img_key: img_url.slice(
|
||||||
sub_key: sub_url.substring(sub_url.lastIndexOf('/') + 1, sub_url.length).split('.')[0]
|
img_url.lastIndexOf('/') + 1,
|
||||||
|
img_url.lastIndexOf('.')
|
||||||
|
),
|
||||||
|
sub_key: sub_url.slice(
|
||||||
|
sub_url.lastIndexOf('/') + 1,
|
||||||
|
sub_url.lastIndexOf('.')
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const wbi_keys = await getWbiKeys()
|
getWbiKeys().then((wbi_keys) => {
|
||||||
|
|
||||||
const query = encWbi(
|
const query = encWbi(
|
||||||
{
|
{
|
||||||
foo: '114',
|
foo: '114',
|
||||||
@@ -263,6 +269,7 @@ const query = encWbi(
|
|||||||
wbi_keys.sub_key
|
wbi_keys.sub_key
|
||||||
)
|
)
|
||||||
console.log(query)
|
console.log(query)
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
输出内容为进行 Wbi 签名的后参数的 url query 形式
|
输出内容为进行 Wbi 签名的后参数的 url query 形式
|
||||||
|
|||||||
Reference in New Issue
Block a user