这篇文章主要讲解一下Auto.js开发者如何通过对接泡椒云sdk,给你自己写的脚本增加卡密验证功能。
泡椒云在线文档:https://docs.paojiaoyun.com/autojs_sdk.html
对接教程:
1、先将下载到的PJYSDK.js文件用编辑器打开,选中所有复制粘贴到你自己的Autojs代码文件中。下面就是一段引用PJYSDK.js的Auto.js示例程序:
/* 将PJYSDK.js文件中的代码复制粘贴到下面空白处 */
/* 将PJYSDK.js文件中的代码复制粘贴到上面空白处 */
// AppKey 和 AppSecret 在泡椒云开发者后台获取
let pjysdk = new PJYSDK("bnsekoso6itblafodqe6", "m3p0lkODcCUpyf3o6DkktAQSJqqLygeV");
pjysdk._protocol = "https"
pjysdk.debug = true;
pjysdk.SetCard("mrfzBBIFb295RAE8");
// 监听心跳失败事件
pjysdk.event.on("heartbeat_failed", function(hret) {
log("心跳失败,尝试重登...")
sleep(2000);
let login_ret = pjysdk.CardLogin();
if (login_ret.code == 0) {
log("重登成功");
} else {
toastLog(login_ret.message); //重登失败
sleep(200);
exit(); // 退出脚本
}
});
// 当脚本正常或者异常退出时会触发exit事件
events.on("exit", function(){
pjysdk.CardLogout(); // 调用退出登录
log("结束运行");
});
let login_ret = pjysdk.CardLogin();
if (login_ret.code == 0) {
// 登录成功,后面写你的业务代码
} else {
// 登录失败提示
toast(login_ret.message);
}
// 当脚本正常或者异常退出时会触发exit事件
events.on("exit", function(){
pjysdk.CardLogout(); // 调用退出登录
log("结束运行");
});
2、然后根据文档(https://docs.paojiaoyun.com/autojs_sdk.html)初始化,开始使用。
3、下面给出了PJYSDK.js源码,懒得下载的朋友可以直接复制下面的代码。
//下面是PJYSDK.js源码
const PJYSDK = (function(){
function PJYSDK(app_key, app_secret){
http.__okhttp__.setMaxRetries(0);
http.__okhttp__.setTimeout(10*1000);
this.event = events.emitter();
this.debug = true;
this._lib_version = "v1.08";
this._protocol = "https";
this._host = "api.paojiaoyun.com";
this._device_id = this.getDeviceID();
this._retry_count = 9;
this._app_key = app_key;
this._app_secret = app_secret;
this._card = null;
this._username = null;
this._password = null;
this._token = null;
this.is_trial = false; // 是否是试用用户
this.login_result = {
"card_type": "",
"expires": "",
"expires_ts": 0,
"config": "",
};
this._auto_heartbeat = true; // 是否自动开启心跳任务
this._heartbeat_gap = 60 * 1000; // 默认60秒
this._heartbeat_task = null;
this._heartbeat_ret = {"code": -9, "message": "还未开始验证"};
this._prev_nonce = null;
}
PJYSDK.prototype.SetCard = function(card) {
this._card = card.trim();
}
PJYSDK.prototype.SetUser = function(username, password) {
this._username = username.trim();
this._password = password;
}
PJYSDK.prototype.getDeviceID = function() {
let id = device.serial;
if (id == null || id == "" || id == "unknown") {
id = device.getAndroidId();
}
if (id == null || id == "" || id == "unknown") {
id = device.getIMEI();
}
return id;
}
PJYSDK.prototype.MD5 = function(str) {
try {
let digest = java.security.MessageDigest.getInstance("md5");
let result = digest.digest(new java.lang.String(str).getBytes("UTF-8"));
let buffer = new java.lang.StringBuffer();
for (let index = 0; index < result.length; index++) {
let b = result[index];
let number = b & 0xff;
let str = java.lang.Integer.toHexString(number);
if (str.length == 1) {
buffer.append("0");
}
buffer.append(str);
}
return buffer.toString();
} catch (error) {
alert(error);
return "";
}
}
PJYSDK.prototype.getTimestamp = function() {
try {
let res = http.get("http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp");
let data = res.body.json();
return Math.floor(data["data"]["t"]/1000);
} catch (error) {
return Math.floor(new Date().getTime()/1000);
}
}
PJYSDK.prototype.genNonce = function() {
const ascii_str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let tmp = '';
for(let i = 0; i < 20; i++) {
tmp += ascii_str.charAt(Math.round(Math.random()*ascii_str.length));
}
return this.MD5(this.getDeviceID() + tmp);
}
PJYSDK.prototype.joinParams =