求助!!!!!!!
微信云开发调用API:接口地址:https://api.weixin.qq.com/cgi-bin/express/intracity/addorder?access_token={ACCESS_TOKEN}
返回值显示失败,errcode啥的信息都没有 大神们帮忙看看,哪里有问题
----------------main 代码----------------------------------------------------------------
exports.main = async (event, context) => {
try {
// 获取调用接口所需的 access_token
const accessToken = event.accessToken;
// 构造请求参数,包括订单数据等
const order = event.order;
// 加密和签名
const { iv, data, authtag } = encrypt(order);
const signature = calculateSignature(data);
// 发起 POST 请求调用微信同城配送接口
const response = await postToWeixinAPI(accessToken, iv, data, authtag, signature, req_ts);
// 返回结果
return response.data;
} catch (error) {
// 错误处理
console.error('调用微信同城配送接口失败:', error);
return { error: '调用微信同城配送接口失败', details: error };
}
};
--------------------- 加密函数--------------------------------------------------------------------------------------------------------------------------------
function encrypt(order) {
// 从上下文对象获取本地对称密钥、对称序列号、应用ID和请求路径
const ctx = getCtx();
const { local_sym_key, local_sym_sn, local_appid, url_path } = ctx;
// 获取当前时间戳和随机生成的16字节的Base64编码的随机数
const local_ts = Math.floor(Date.now() / 1000);
const nonce = crypto.randomBytes(16).toString('base64').replace(/=/g, '');
// 构造请求参数对象
const reqex = {
_n: nonce,
_appid: local_appid,
_timestamp: local_ts
};
const real_req = Object.assign({}, reqex, order);
// 将请求参数对象转换为JSON字符串
const plaintext = JSON.stringify(real_req);
// 构造附加认证数据 (AAD)
const aad = `${url_path}|${local_appid}|${local_ts}|${local_sym_sn}`;
// 将密钥解码为Buffer格式,并生成随机的IV
const real_key = Buffer.from(local_sym_key, "base64");
const real_iv = crypto.randomBytes(12);
// 将AAD、明文和IV输入到AES-256-GCM加密器中进行加密
const cipher = crypto.createCipheriv("aes-256-gcm", real_key, real_iv);
cipher.setAAD(Buffer.from(aad, "utf-8"));
let cipher_update = cipher.update(Buffer.from(plaintext, "utf-8"));
let cipher_final = cipher.final();
const real_ciphertext = Buffer.concat([cipher_update, cipher_final]);
const real_authTag = cipher.getAuthTag();
// 获取加密结果中的密文、认证标签和IV,并转换为Base64格式
const iv = real_iv.toString("base64");
const data = real_ciphertext.toString("base64");
const authtag = real_authTag.toString("base64");
// 构造加密后的请求数据对象
const req_data = {
iv,
data,
authtag,
};
// 返回加密后的请求数据
const new_req = {
req_ts: local_ts,
req_data: JSON.stringify(req_data)
}
return new_req
}
--------------------- 签名函数--------------------------------------------------------------------------------------------------------------------------------
function calculateSignature(data) {
// 从上下文对象获取本地私钥、序列号、应用ID和请求路径
const ctx = getSigCtx();
const { local_private_key, local_sn, local_appid, url_path } = ctx;
// 获取请求数据中的时间戳和加密后的请求数据
const { req_ts, req_data } = data;
// 构造待签名的数据
const payload = `${url_path}\n${local_appid}\n${req_ts}\n${req_data}`;
const data_buffer = Buffer.from(payload, 'utf-8');
// 构造签名使用的密钥对象
const key_obj = {
key: local_private_key,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST // salt长度,需与SHA256结果长度(32)一致
};
// 对数据进行签名
const sig_buffer = crypto.sign(
'RSA-SHA256',
data_buffer,
key_obj
);
// 将签名结果转换为Base64字符串并返回
const signature = sig_buffer.toString('base64');
return signature;
}
---------------------API调用--------------------------------------------------------------------------------------------------------------------------------
function postToWeixinAPI(accessToken, iv, data, authtag, signature, req_ts) {
return axios.post('https://api.weixin.qq.com/cgi-bin/express/intracity/addorder', {
iv,
data,
authtag,
req_ts // 使用传入的时间戳
}, {
headers: {
'Wechatmp-Appid': Appid',
'Wechatmp-TimeStamp': req_ts, // 使用传入的时间戳
'Wechatmp-Signature': signature
},
params: {
access_token: accessToken
}
});
}