XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/bilibili-API-collect

优化示例代码 (#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>

542359a
188102836 <69347367+188102836@users.noreply.github.com>
提交于

代码差异

1 个文件 +29 -22
Modified docs/misc/sign/wbi.md +29 -22
@@ -192,7 +192,7 @@ bar=514&baz=1919810&foo=114&wts=1684746387&w_rid=d3cbd2a2316089117134038bf4caf44
192 192
193 193 ### JavaScript
194 194
195 需要`axios`、`md5`依赖
195 需要 `axios`、`md5` 依赖
196 196
197 197 ```javascript
198 198 import md5 from 'md5'
@@ -218,16 +218,16 @@ function getMixinKey(orig) {
218 218 function encWbi(params, img_key, sub_key) {
219 219 const mixin_key = getMixinKey(img_key + sub_key),
220 220 curr_time = Math.round(Date.now() / 1000),
221 chr_filter = /[!'\(\)*]/g
221 chr_filter = /[!'()*]/g
222 222 let query = []
223 params = Object.assign(params, {wts: curr_time}) // 添加 wts 字段
223 Object.assign(params, { wts: curr_time }) // 添加 wts 字段
224 224 // 按照 key 重排参数
225 225 Object.keys(params).sort().forEach((key) => {
226 226 query.push(
227 encodeURIComponent(key) +
228 '=' +
229 // 过滤 value 中的 "!'()*" 字符
230 encodeURIComponent(('' + params[key]).replace(chr_filter, ''))
227 `${encodeURIComponent(key)}=${encodeURIComponent(
228 // 过滤 value 中的 "!'()*" 字符
229 params[key].toString().replace(chr_filter, '')
230 )}`
231 231 )
232 232 })
233 233 query = query.join('&')
@@ -245,24 +245,31 @@ async function getWbiKeys() {
245 245 json_content = resp.data,
246 246 img_url = json_content.data.wbi_img.img_url,
247 247 sub_url = json_content.data.wbi_img.sub_url
248
248 249 return {
249 img_key: img_url.substring(img_url.lastIndexOf('/') + 1, img_url.length).split('.')[0],
250 sub_key: sub_url.substring(sub_url.lastIndexOf('/') + 1, sub_url.length).split('.')[0]
250 img_key: img_url.slice(
251 img_url.lastIndexOf('/') + 1,
252 img_url.lastIndexOf('.')
253 ),
254 sub_key: sub_url.slice(
255 sub_url.lastIndexOf('/') + 1,
256 sub_url.lastIndexOf('.')
257 )
251 258 }
252 259 }
253 260
254 const wbi_keys = await getWbiKeys()
255
256 const query = encWbi(
257 {
258 foo: '114',
259 bar: '514',
260 baz: 1919810
261 },
262 wbi_keys.img_key,
263 wbi_keys.sub_key
264 )
265 console.log(query)
261 getWbiKeys().then((wbi_keys) => {
262 const query = encWbi(
263 {
264 foo: '114',
265 bar: '514',
266 baz: 1919810
267 },
268 wbi_keys.img_key,
269 wbi_keys.sub_key
270 )
271 console.log(query)
272 })
266 273 ```
267 274
268 275 输出内容为进行 Wbi 签名的后参数的 url query 形式
@@ -592,4 +599,4 @@ public class WbiTest {
592 599 System.out.println(finalParam);
593 600 }
594 601 }
595 ```
602 ```