autojs实现云端脚本(七)

在autojs实现云端脚本(六)中, 我们实现了下载运行指定脚本,
今天,我们把代码整理一下, 完完整整的.
autojs交流群: 284029554
点击注册: leanCloud

以下代码在前六节课中都可以找到, 这里只是整理到一块了, 经过测试可正常运行.

云脚本功能完成

'ui';
ui.layout(
  
    
      
        
          
            
              
            
          
        
      
    
  
)
threads.start(
  function () {
    var config = {
      appId: '填写你自己的',
      appKey: '填写你自己的',
    }
    var scriptList = downLoadScriptList(config)
    console.log('scriptList=')
    console.log(scriptList)
    // 间隔行变色
    for (var i = 0; i < scriptList.length; i++) {
      if (i % 2 == 0) {
        scriptList[i].bg = '#87CEEB'
      } else {
        scriptList[i].bg = '#C0FF3E'
      }
    }
    log('ui.scripts.setDataSource(scriptList) scriptList=')
    log(scriptList)
    // 设置list内容, 添加点击事件
    ui.run(
      function () {
        ui.scripts.setDataSource(scriptList)
        ui.scripts.on("item_click", function (item, i, itemView, listView) {
          var scriptName = itemView.scriptName.text()
          // 点击后,下载后运行该脚本
          threads.start(
            function () {
              console.log('多线程里的scriptName=', scriptName)
              scriptName = scriptName.replace('脚本名字: ', '').trim()
              console.log('点击的脚本scriptName=', scriptName)
              console.log('config=', config)
              var scriptPath = downloadScript(scriptName, config)
              log('开始执行下载的文件')
              engines.execScriptFile(scriptPath);
              log('结束执行下载的文件')
            }
          )
        });
      }
    )
  }
)

function downLoadScriptList(config) {
  var scriptListUrl = 'https://n2y09qsw.api.lncld.net/1.1/classes/_File'
  var url = encodeURI(scriptListUrl)
  var r = http.get(url, {
    headers: {
      "X-LC-Id": config.appId,
      "X-LC-Key": config.appKey,
      "Content-Type": "application/json"
    }
  }).body.json()
  console.log(r)
  if (r.results && r.results.length > 0) {
    log('脚本列表下载成功')
    log('脚本数量=', r.results.length)
    var results = r.results
    var scriptList = []
    // 提取脚本名字
    for (var i = 0; i < results.length; i++) {
      var result = results[i]
      var scriptName = result.name
      scriptList.push({
        scriptName: scriptName
      })
    }
    console.log(scriptList)
    return scriptList
  } else {
    log('脚本列表下载失败')
  }
}

function downloadScript(scriptName, config) {
  // 查找指定名字脚本的下载链接
  var scriptUrl = util.format('https://n2y09qsw.api.lncld.net/1.1/classes/_File?where={"name":"%s"}', scriptName)
  console.log('下载的脚本链接=', scriptUrl)
  var url = encodeURI(scriptUrl)
  var r = http.get(url, {
    headers: {
      "X-LC-Id": config.appId,
      "X-LC-Key": config.appKey,
      "Content-Type": "application/json"
    }
  }).body.json()
  console.log(r)
  if (r.results && r.results.length > 0 && r.results[0].name === scriptName) {
    log('找到了指定名字的脚本')
    console.log(r.results[0].url)
    var scriptPath = downloadScript(r.results[0].url)
    console.log('下载完毕, scriptPath=', scriptPath)
    return scriptPath
  } else {
    log('没找到指定名字的脚本')
  }
  // 这是知道了下载链接,下载脚本
  function downloadScript(scriptUrl) {
    var r = http.get(scriptUrl).body.bytes()
    var scriptPath = './' + scriptName
    files.writeBytes(scriptPath, r)
    return scriptPath
  }
}

你可能感兴趣的:(autojs实现云端脚本(七))