优化wbi.md里的Swift代码 (#1208)

修复Swift代码中时间戳类型转化后多出.0的问题
This commit is contained in:
Seamain 2025-04-13 11:22:43 +08:00 committed by GitHub
parent aecbbdbd90
commit 4abf38f2aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1108,9 +1108,9 @@ mod tests {
需要 [Alamofire](https://github.com/Alamofire/Alamofire) 和 [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) 库 需要 [Alamofire](https://github.com/Alamofire/Alamofire) 和 [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) 库
```swift ```swift
import Foundation
import CommonCrypto
import Alamofire import Alamofire
import CommonCrypto
import Foundation
import SwiftyJSON import SwiftyJSON
func biliWbiSign(param: String, completion: @escaping (String?) -> Void) { func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
@ -1124,35 +1124,39 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
let currTime = round(Date().timeIntervalSince1970) let currTime = round(Date().timeIntervalSince1970)
params["wts"] = currTime params["wts"] = currTime
params = params.sorted { $0.key < $1.key }.reduce(into: [:]) { $0[$1.key] = $1.value } params = params.sorted { $0.key < $1.key }.reduce(into: [:]) { $0[$1.key] = $1.value }
params = params.mapValues { String(describing: $0).filter { !"!'()*".contains($0) } } params = params.mapValues { value in
if let doubleValue = value as? Double, doubleValue.truncatingRemainder(dividingBy: 1) == 0 {
return String(Int(doubleValue)).filter { !"!'()*".contains($0) }
}
return String(describing: value).filter { !"!'()*".contains($0) }
}
let query = params.map { "\($0.key)=\($0.value)" }.joined(separator: "&") let query = params.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
let wbiSign = calculateMD5(string: query + mixinKey) let wbiSign = calculateMD5(string: query + mixinKey)
params["w_rid"] = wbiSign params["w_rid"] = wbiSign
return params return params
} }
func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) { func getWbiKeys(completion: @escaping (Result<(imgKey: String, subKey: String), Error>) -> Void) {
let headers: HTTPHeaders = [ let headers: HTTPHeaders = [
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"Referer": "https://www.bilibili.com/" "Referer": "https://www.bilibili.com/"
] ]
AF.request("https://api.bilibili.com/x/web-interface/nav", headers: headers).responseJSON { response in AF.request("https://api.bilibili.com/x/web-interface/nav", headers: headers).responseJSON { response in
switch response.result { switch response.result {
case .success(let value): case .success(let value):
let json = JSON(value) let json = JSON(value)
let imgURL = json["data"]["wbi_img"]["img_url"].string ?? "" let imgURL = json["data"]["wbi_img"]["img_url"].string ?? ""
let subURL = json["data"]["wbi_img"]["sub_url"].string ?? "" let subURL = json["data"]["wbi_img"]["sub_url"].string ?? ""
let imgKey = imgURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? "" let imgKey = imgURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
let subKey = subURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? "" let subKey = subURL.components(separatedBy: "/").last?.components(separatedBy: ".").first ?? ""
completion(.success((imgKey, subKey))) completion(.success((imgKey, subKey)))
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func calculateMD5(string: String) -> String { func calculateMD5(string: String) -> String {
let data = Data(string.utf8) let data = Data(string.utf8)
var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
@ -1174,7 +1178,7 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
case .success(let keys): case .success(let keys):
let spdParam = param.components(separatedBy: "&") let spdParam = param.components(separatedBy: "&")
var spdDicParam = [String: String]() var spdDicParam = [String: String]()
spdParam.forEach { pair in for pair in spdParam {
let components = pair.components(separatedBy: "=") let components = pair.components(separatedBy: "=")
if components.count == 2 { if components.count == 2 {
spdDicParam[components[0]] = components[1] spdDicParam[components[0]] = components[1]
@ -1191,6 +1195,22 @@ func biliWbiSign(param: String, completion: @escaping (String?) -> Void) {
} }
} }
// 使用示例
biliWbiSign(param: "bar=514&foo=114&zab=1919810") {
signedQuery in
if let signedQuery = signedQuery {
print("签名后的参数: \(signedQuery)")
} else {
print("签名失败")
}
}
RunLoop.main.run()//程序类型为命令行程序时需要添加这行代码
```
```text
签名后的参数: bar=514&wts=1741082093&foo=114&zab=1919810&w_rid=04775bb3debbb45bab86a93a1c08d12a
``` ```