| 123456789101112131415161718192021222324252627282930313233343536373839 |
- /**
- * adb method: click — 按坐标或变量位置点击
- */
- async function run(action, ctx) {
- const { device, variableContext, api, extractVarName, resolveValue } = ctx
- const inVars = action.inVars || []
- let position = inVars.length > 0 ? variableContext[extractVarName(inVars[0])] : null
- if (!position && action.target) position = resolveValue(action.target, variableContext)
- if (!position) return { success: false, error: 'click 操作缺少位置参数' }
- if (typeof position === 'string') {
- if (position === '') return { success: false, error: 'click 操作缺少位置参数(位置变量为空)' }
- try {
- position = JSON.parse(position)
- } catch (e) {
- const parts = position.split(',')
- if (parts.length === 2) {
- const x = parseFloat(parts[0].trim())
- const y = parseFloat(parts[1].trim())
- if (!isNaN(x) && !isNaN(y)) position = { x: Math.round(x), y: Math.round(y) }
- else return { success: false, error: `click 操作的位置格式错误,无法解析字符串: ${position}` }
- } else return { success: false, error: `click 操作的位置格式错误,无法解析字符串: ${position}` }
- }
- }
- if (Array.isArray(position) && position.length >= 2) position = { x: position[0], y: position[1] }
- if (position?.topLeft && position?.bottomRight) {
- position = {
- x: Math.round((position.topLeft.x + position.bottomRight.x) / 2),
- y: Math.round((position.topLeft.y + position.bottomRight.y) / 2),
- }
- }
- if (!position || typeof position !== 'object' || position.x === undefined || position.y === undefined)
- return { success: false, error: 'click 操作的位置格式错误,需要 {x, y} 对象' }
- if (!api?.sendTap) return { success: false, error: '点击 API 不可用' }
- const tapResult = await api.sendTap(device, position.x, position.y)
- if (!tapResult.success) return { success: false, error: `点击失败: ${tapResult.error != null ? tapResult.error : 'unknown'}` }
- return { success: true }
- }
- module.exports = { run }
|