//获取历史cookie
let Cookie = await ctx.model("Cookie").query(qb => {
qb.where("userid", 10000);
qb.orderBy("id", "desc");
}).fetch();
//请求头设置
let headers = {
Host: "xxgk.xxxxxxxxx.cn",
Origin: "http://xxgk.xxxxxxxxx.cn",
Referer: "http://xxgk.xxxxxxxxx.cn/xxxx/xxxx.jsp",
"content-type": "application/x-www-form-urlencoded",
};
//如果有Cookie
if (Cookie) {
headers["Cookie"] = Cookie["JSESSIONID"];
};
const agent = new module.https.Agent({
rejectUnauthorized: false
});
let options = {
url: 'http://xxgk.xxxxxxxxx.cn/xxxx/image.jsp?0.228394673249388',
method: 'GET',
headers: headers,
responseType: 'arraybuffer'
};
const res = await module.axios(options).catch(err => {
throw new Error(`请求验证码出错`);
});
let data = res.data;
//将获取到的验证码转base64
let ImageBase64 = data.toString('base64');
//请求ocr识别
const code = await module.axios({
url: 'https://ocr.ooooooyun.com/api.Common_VerificationCode',
method: 'POST',
headers: {
"Content-Type": "application/json",
},
data: {
ImageBase64: ImageBase64
}
}).catch(err => {
throw new Error(`请求验证码识别出错`);
});
const Code = code.data.result;
let c = res.headers["set-cookie"] || "";
if (c != "") {
//如果平台重新生成了Cookie,则保存新的验证码及Cookie
let d = c[0].split(";");
await ctx.model("Cookie").forge({
JSESSIONID: d[0],
"userid": 10000,
VerificationCode: Code
}).save();
} else {
//否则更新验证码
await ctx.model("Cookie").forge({
id: Cookie["id"],
"userid": 10000,
VerificationCode: Code
}).save();
};
ctx.log('自动维护环保平台验证码');
ctx.body={success:true}
版权归属:
Administrator
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区