Module:Heartrate:修订间差异
来自OTTOWiki
更多操作
![]() 琪露若(留言 | 贡献) (创建页面,内容为“local p = {} function p.getHeartRate(frame) -- 获取传入参数 local args = frame.args local apiUrl = args.url -- 验证URL参数 if not apiUrl or apiUrl == "" then return "错误:缺少API URL参数" end -- 发起HTTP GET请求 local response local status, err = pcall(function() response = mw.http.get( apiUrl, { headers = { ["Accept"]…”) |
|||
第15行: | 第15行: | ||
local status, err = pcall(function() | local status, err = pcall(function() | ||
response = mw.http.get( | response = mw.http.get( | ||
apiUrl | apiUrl | ||
) | ) | ||
end) | end) |
2025年6月20日 (五) 11:05的版本
local p = {}
function p.getHeartRate(frame)
-- 获取传入参数
local args = frame.args
local apiUrl = args.url
-- 验证URL参数
if not apiUrl or apiUrl == "" then
return "错误:缺少API URL参数"
end
-- 发起HTTP GET请求
local response
local status, err = pcall(function()
response = mw.http.get(
apiUrl
)
end)
-- 检查请求是否成功
if not status or not response then
return "错误:API请求失败 - " .. (err or "未知错误")
end
if response.status ~= 200 then
return "错误:API返回HTTP状态码 " .. response.status
end
-- 解析JSON数据
local jsonData
status, jsonData = pcall(mw.text.jsonDecode, response.body)
if not status or type(jsonData) ~= "table" then
return "错误:JSON解析失败"
end
-- 验证数据结构并提取心率值
if jsonData.message == "ok"
and type(jsonData.data) == "table"
and type(jsonData.data.heart_rate) == "number" then
return jsonData.data.heart_rate
else
return "错误:无效的数据结构"
end
end
return p