press.js 1.2 KB

1234567891011121314151617181920
  1. /**
  2. * adb method: press — 按图点击(匹配图片位置后点击)
  3. */
  4. async function run(action, ctx) {
  5. const { device, folderPath, variableContext, api, extractVarName } = ctx
  6. const inVars = action.inVars || []
  7. const imagePath = inVars.length > 0 ? (variableContext[extractVarName(inVars[0])] || inVars[0]) : action.value
  8. if (!imagePath) return { success: false, error: 'press 操作缺少图片路径' }
  9. const fullPath = imagePath.startsWith('/') || imagePath.includes(':') ? imagePath : `${folderPath}/${imagePath}`
  10. if (!api?.matchImageAndGetCoordinate) return { success: false, error: '图像匹配 API 不可用' }
  11. const matchResult = await api.matchImageAndGetCoordinate(device, fullPath, folderPath)
  12. if (!matchResult.success) return { success: false, error: `图像匹配失败: ${matchResult.error != null ? matchResult.error : 'unknown'}` }
  13. const { x, y } = matchResult.clickPosition
  14. if (!api?.sendTap) return { success: false, error: '点击 API 不可用' }
  15. const tapResult = await api.sendTap(device, x, y)
  16. if (!tapResult.success) return { success: false, error: `点击失败: ${tapResult.error != null ? tapResult.error : 'unknown'}` }
  17. return { success: true }
  18. }
  19. module.exports = { run }