Skip to content

Instantly share code, notes, and snippets.

@fanlushuai
Last active February 9, 2025 20:09
Show Gist options
  • Save fanlushuai/fc1c7f335e759fe8fbe1255c6326bb7c to your computer and use it in GitHub Desktop.
Save fanlushuai/fc1c7f335e759fe8fbe1255c6326bb7c to your computer and use it in GitHub Desktop.
神州出行抢单司机端- 老板跑单代码开源-仅供学习交流
"ui";
ui.layout(
<frame w="*" h="*" bg="#F6FFE3" gravity="center">
<vertical>
<appbar>
<toolbar
bg="#4EBFDD"
layout_height="50"
margin="-2"
id="toolbar"
title="奥利给-神州专车-抢单机器人"
/>
</appbar>
<ScrollView>
<vertical marginTop="5">
<card
w="*"
h="auto"
margin="10 5"
cardCornerRadius="2dp"
cardElevation="1dp"
gravity="center_vertical"
>
<vertical padding="18 8" h="auto">
<linear>
<Switch
marginLeft="0"
marginRight="12"
marginTop="0"
marginBottom="0"
layout_weight="1"
id="autoService"
text="无障碍服务"
textSize="15sp"
checked="{{auto.service != null}}"
/>
</linear>
</vertical>
<View bg="#4EBFDD" h="*" w="5" />
</card>
<card
w="*"
h="auto"
margin="10 5"
cardCornerRadius="2dp"
cardElevation="1dp"
gravity="center_vertical"
>
<vertical padding="18 8" h="auto">
<linear>
<text
text="最小刷新时间:"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
<input
id="refreshTime"
text="10"
textSize="14sp"
color="black"
paddingLeft="3"
ems="3"
inputType="number"
gravity="center"
/>
<text
text="毫秒"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
</linear>
</vertical>
<View bg="#4EBFDD" h="*" w="5" />
</card>
<card
w="*"
h="auto"
margin="10 5"
cardCornerRadius="2dp"
cardElevation="1dp"
gravity="center_vertical"
>
<vertical padding="18 8" h="auto">
<linear>
<text
text="距我小于:"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
<input
id="maxDisFromHere"
text="10"
textSize="14sp"
color="black"
paddingLeft="3"
ems="3"
inputType="number"
gravity="center"
/>
<text
text="公里"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
</linear>
<linear>
<text
text="全程介于:"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
<input
id="minAllDis"
text="10"
textSize="14sp"
color="black"
paddingLeft="5"
ems="3"
inputType="number"
gravity="center"
/>
<text
text="-"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
<input
id="maxAllDis"
text="10"
textSize="14sp"
color="black"
paddingLeft="5"
ems="3"
inputType="number"
gravity="center"
/>
<text
text="公里"
textSize="14sp"
textColor="black"
w="auto"
textStyle="bold"
/>
</linear>
</vertical>
<View bg="#4EBFDD" h="*" w="5" />
</card>
<card
w="*"
h="auto"
margin="10 5"
cardCornerRadius="2dp"
cardElevation="1dp"
gravity="center_vertical"
>
<button
id="save"
h="auto"
text="保 存 配 置"
textSize="17"
textStyle="bold"
color="#ffffff"
bg="#4EBFDD"
foreground="?selectableItemBackground"
layout_gravity="bottom"
/>
<View bg="#4EBFDD" h="*" w="5" />
</card>
<card
w="*"
h="auto"
margin="10 5"
cardCornerRadius="2dp"
cardElevation="1dp"
gravity="center_vertical"
>
<button
id="boot"
h="auto"
text="开 始 运 行"
textSize="17"
textStyle="bold"
color="#ffffff"
bg="#4EBFDD"
foreground="?selectableItemBackground"
layout_gravity="bottom"
/>
<View bg="#4EBFDD" h="*" w="5" />
</card>
</vertical>
</ScrollView>
</vertical>
</frame>
);
let ls = storages.create("config-alg12122");
let data = {
ls2ui: function () {
let refreshTime = ls.get("refreshTime", 1000);
ui.refreshTime.setText(parseInt(refreshTime) + "");
let minAllDis = ls.get("minAllDis", 0);
ui.minAllDis.setText(parseFloat(minAllDis) + "");
let maxAllDis = ls.get("maxAllDis", 1000);
ui.maxAllDis.setText(parseFloat(maxAllDis) + "");
let maxDisFromHere = ls.get("maxDisFromHere", 10);
ui.maxDisFromHere.setText(parseFloat(maxDisFromHere) + "");
},
ui2ls: function () {
ls.put("refreshTime", ui.refreshTime.text());
ls.put("minAllDis", ui.minAllDis.text());
ls.put("maxAllDis", ui.maxAllDis.text());
ls.put("maxDisFromHere", ui.maxDisFromHere.text());
},
ui2mm: function () {
let refreshTime = parseFloat(ls.get("refreshTime", 1000));
let minAllDis = parseFloat(ls.get("minAllDis", 0));
let maxAllDis = parseFloat(ls.get("maxAllDis", 1000));
let maxDisFromHere = parseFloat(ls.get("maxDisFromHere", 10));
Config.minAllDis = minAllDis;
Config.maxAllDis = maxAllDis;
Config.maxDisFromHere = maxDisFromHere;
Config.refreshTime = refreshTime;
},
};
ui.autoService.on("check", function (checked) {
// 用户勾选无障碍服务的选项时,跳转到页面让用户去开启
if (checked && auto.service == null) {
app.startActivity({
action: "android.settings.ACCESSIBILITY_SETTINGS",
});
}
if (!checked && auto.service != null) {
auto.service.disableSelf();
}
});
// 当用户回到本界面时,resume事件会被触发
ui.emitter.on("resume", function () {
// 此时根据无障碍服务的开启情况,同步开关的状态
ui.autoService.checked = auto.service != null;
});
data.ls2ui();
ui.save.click(function () {
data.ui2ls();
});
ui.boot.click(function () {
if (auto.service == null) {
toastLog("请先开启无障碍服务!");
return;
}
data.ui2ls();
data.ui2mm();
// 此线程任务,可能在后台一直处于工作中,结束不了
threads.start(function () {
home();
app.launch("com.szzc.ucar.ptdriver");
main();
});
threads.start(() => {
let dw = device.width;
let dh = device.height;
let cw = (dw * 1) / 3;
let ch = (dh * 5) / 10;
console.setTitle("神州专车");
console.show(true);
console.setCanInput(false);
sleep(100); //等待一会,才能设置尺寸成功
console.setSize(cw, ch); //需要前面等待一会
console.setPosition(dw - (cw + 50), 50);
console.setMaxLines(300);
});
toastLog("任务已经启动");
});
let Config = {
minAllDis: 10,
maxAllDis: 10,
maxDisFromHere: 10,
refreshTime: 1000, // 1秒
};
let lastRefreshTime = 0;
function onPage() {
return (
id("com.szzc.ucar.ptdriver:id/titlebar_title")
.text("抢单大厅")
.findOnce() != null
);
}
function refresh() {
let refreshTime = Config.refreshTime || 1000;
function flush() {
//下拉刷新
swipe(
device.width / 2,
(device.height * 1) / 4,
device.width / 2,
(device.height * 3) / 4,
50
);
}
// 必须在目标界面内才能做这个动作
if (new Date().getTime() - lastRefreshTime > refreshTime && onPage()) {
flush();
lastRefreshTime = new Date().getTime();
}
}
function parseOrder() {
let orders = [];
//距您79.1公里
let fromHereDisEles = id(
"com.szzc.ucar.ptdriver:id/tv_order_relative_distance"
).find();
for (let i = 0; i < fromHereDisEles.length; i++) {
let fromHereDisEle = fromHereDisEles[i];
let fromHereDisStr = fromHereDisEle.text().match(/距您(\d+\.\d+)公里/)[1];
if (fromHereDisStr == null) {
continue;
}
let fromHereDis = parseFloat(fromHereDisStr);
//进一步解析。其他内容
let orderEle = fromHereDisEle.parent().parent();
let disAllEle = orderEle.findOne(
id("com.szzc.ucar.ptdriver:id/tv_order_estimate_distance")
);
// 全程1.0公里
let disAllStr = disAllEle.text().match(/全程(\d+\.\d+)公里/)[1];
let allDis = parseFloat(disAllStr);
let swipeSheetEle = orderEle.findOne(
id("com.szzc.ucar.ptdriver:id/tv_grab_status")
);
let order = {
allDis: allDis,
fromHereDis: fromHereDis,
swipeSheetEle: swipeSheetEle,
};
orders.push(order);
}
return orders;
}
function filterOrder(orders) {
let okOrders = [];
for (let i = 0; i < orders.length; i++) {
let order = orders[i];
if (order.allDis < Config.minAllDis) {
continue;
}
if (order.allDis > Config.maxAllDis) {
continue;
}
if (order.fromHereDis > Config.maxDisFromHere) {
continue;
}
if (order.swipeSheetEle != null) {
okOrders.push(order);
}
}
if (okOrders.length == 0) {
return null;
}
if (okOrders.length == 1) {
return okOrders[0];
}
//多订单同时满足,优选最大公里数的订单
let maxAllDis = 0;
let maxAllDisOrderI = null;
for (let i = 0; i < okOrders.length; i++) {
let order = okOrders[i];
if (order.allDis > maxAllDis) {
maxAllDis = order.allDis;
maxAllDisOrderI = i;
}
}
return okOrders[maxAllDisOrderI];
}
function swipeForOrder(order) {
if (order == null || order.swipeSheetEle == null) return;
let b = order.swipeSheetEle.bounds();
let x1 = b.left;
let x2 = b.right;
let y = b.top + (b.bottom - b.top) / 2;
log("滑动抢单" + x1 + " " + y + " -> " + x2 + " " + y);
swipe(x1, y, x2, y, 100);
return true;
}
let lastMsg = "";
function logJustNew(msg) {
if (lastMsg != msg) {
log(msg);
lastMsg = msg;
}
}
function main() {
let ordermsg = "";
log(Config);
while (1) {
sleep(10); // 防止卡死
refresh();
let orders = parseOrder();
let order = filterOrder(orders);
if (order) {
ordermsg = order.allDis + " " + order.fromHereDis;
logJustNew(ordermsg);
}
if (swipeForOrder(order)) {
break;
}
}
console.warn("任务完成,下次再见!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment