74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
|
/**
|
||
|
* 小报童请求加密逻辑
|
||
|
* decode from: https://static.xiaobot.net/resource/xiaobot-web/js/reader.e060c116.js
|
||
|
*
|
||
|
* @since 2024.06.25
|
||
|
*/
|
||
|
const md5 = require('js-md5');
|
||
|
const request = require('request');
|
||
|
const axios = require('axios');
|
||
|
|
||
|
// 抽离出的方法
|
||
|
function getAccessToken() {
|
||
|
return '填写网页F12中取得的 access_token'
|
||
|
}
|
||
|
|
||
|
function call(method, url, data, callback) {
|
||
|
let timestamp = Date.now() / 1000
|
||
|
let sign = _getSign(data, timestamp);
|
||
|
axios.request({
|
||
|
method: method,
|
||
|
url: url,
|
||
|
params: ["POST", "PUT", "PATCH"].includes(method) ? null : data,
|
||
|
data: ["POST", "PUT", "PATCH"].includes(method) ? data : null,
|
||
|
headers: {
|
||
|
timestamp: timestamp,
|
||
|
"api-key": "xiaobot_web",
|
||
|
"app-version": "0.1",
|
||
|
sign: sign,
|
||
|
Authorization: "Bearer " + getAccessToken() // o.a.state.user.user.access_token
|
||
|
}
|
||
|
}).then(response => {
|
||
|
if (response.data && 411 === response.data.code) {
|
||
|
console.log("请先登录")
|
||
|
// o.a.commit("user/setUser", {})
|
||
|
}
|
||
|
callback(response)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
function _getSign(data, timestamp) {
|
||
|
data = _ksort(data);
|
||
|
let a = "";
|
||
|
for (let t in data) {
|
||
|
let c = data[t];
|
||
|
void 0 !== c && (a += t + "=" + c + "&")
|
||
|
}
|
||
|
return a = a.substring(0, a.length - 1),
|
||
|
md5(a + "dbbc1dd37360b4084c3a69346e0ce2b2." + timestamp)
|
||
|
// i() -> js-md5
|
||
|
// decode from: https://static.xiaobot.net/resource/xiaobot-web/js/chunk-core.25cf2aec.js
|
||
|
/**
|
||
|
* [js-md5]{@link https://github.com/emn178/js-md5}
|
||
|
*
|
||
|
* @namespace md5
|
||
|
* @version 0.7.3
|
||
|
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
||
|
* @copyright Chen, Yi-Cyuan 2014-2017
|
||
|
* @license MIT
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
function _ksort(data) {
|
||
|
var t = Object.keys(data).sort(), a = {};
|
||
|
for (var c in t)
|
||
|
a[t[c]] = data[t[c]];
|
||
|
return a
|
||
|
}
|
||
|
|
||
|
// const sign = _getSign({}, Date.now())
|
||
|
// console.log('sign', sign)
|
||
|
|
||
|
module.exports = {
|
||
|
call,
|
||
|
}
|