| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- /**
- * adb method: click — 按坐标或变量位置点击
- * 入参由 variable-parser 在调用前解析,inVars[0] 可能已是解析后的坐标值(JSON 字符串、"x,y" 或对象),也可能仍是变量名需从 variableContext 读取。
- */
- async function run(action, ctx) {
- const { device, variableContext, api, extractVarName, resolveValue } = ctx
- const inVars = action.inVars || []
- let position = null
- if (inVars.length > 0) {
- const raw = inVars[0]
- // 已是解析后的坐标值(variable-parser 已替换):对象、JSON 串或 "x,y"
- if (typeof raw === 'object' && raw !== null && (raw.x !== undefined || (Array.isArray(raw) && raw.length >= 2))) position = raw
- else if (typeof raw === 'string' && raw !== '' && (raw.trim().startsWith('{') || /^\s*-?\d+\s*,\s*-?\d+\s*$/.test(raw.trim()))) position = raw
- else position = variableContext[extractVarName(raw)]
- }
- 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 }
|