在autojs實(shí)現(xiàn)云端腳本(六)中, 我們實(shí)現(xiàn)了下載運(yùn)行指定腳本,
今天,我們把代碼整理一下, 完完整整的.
autojs交流群: 284029554
點(diǎn)擊注冊(cè): leanCloud
以下代碼在前六節(jié)課中都可以找到, 這里只是整理到一塊了, 經(jīng)過測(cè)試可正常運(yùn)行.
云腳本功能完成
'ui';
ui.layout(
<vertical>
<list id='scripts'>
<card w="*" h="70" margin="10 5" cardCornerRadius="2dp" cardElevation="5dp" foreground="?selectableItemBackground">
<horizontal w='*' margin='6 6 6 6' bg='{{this.bg}}'>
<frame layout_weight="1" layout_height='match_parent'>
<vertical bg='#ffff00' layout_weight="1" layout_height='match_parent'>
<text id='scriptName' text='腳本名字: {{this.scriptName}} ' textSize="15sp" textColor='#000000'
bg='{{this.bg}}' layout_weight="1" gravity='left|center'></text>
</vertical>
</frame>
</horizontal>
</card>
</list>
</vertical>
)
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)
// 設(shè)置list內(nèi)容, 添加點(diǎn)擊事件
ui.run(
function () {
ui.scripts.setDataSource(scriptList)
ui.scripts.on("item_click", function (item, i, itemView, listView) {
var scriptName = itemView.scriptName.text()
// 點(diǎn)擊后,下載后運(yùn)行該腳本
threads.start(
function () {
console.log('多線程里的scriptName=', scriptName)
scriptName = scriptName.replace('腳本名字: ', '').trim()
console.log('點(diǎn)擊的腳本scriptName=', scriptName)
console.log('config=', config)
var scriptPath = downloadScript(scriptName, config)
log('開始執(zhí)行下載的文件')
engines.execScriptFile(scriptPath);
log('結(jié)束執(zhí)行下載的文件')
}
)
});
}
)
}
)
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('腳本數(shù)量=', 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
}
}