侧边栏壁纸
博主头像
个人开发测试博主等级

愿你我背道而驰却没有走远

  • 累计撰写 8 篇文章
  • 累计创建 6 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

云函数-某平台验证码自主维护定时任务,用于其它接口调用

Administrator
2023-12-13 / 0 评论 / 0 点赞 / 93 阅读 / 1583 字 / 正在检测是否收录...
//获取历史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}

0

评论区