云函数调用同城配送接口返回签名错误,我是严格按照文档上加密签名的,为什么一直报签名错误
// cloudfunctions/cityDelivery/utils/signature.js
const crypto = require('crypto')
class WechatAPISignature {
constructor(privateKey, keySn, appid) {
// 清理私钥格式
this.privateKey = privateKey.replace(/^ /gm, '').trim()
this.keySn = keySn
this.appid = appid
}
generateSignature(urlPath, timestamp, postData) {
try {
console.log('=== 签名生成详细过程 ===')
// 严格按照官方格式拼接:url_path\nappid\ntimestamp\npostdata
const signString = `${urlPath}\n${this.appid}\n${timestamp}\n${postData}`
console.log('签名组件:')
console.log('1. URL Path:', urlPath)
console.log('2. AppID:', this.appid)
console.log('3. Timestamp:', timestamp)
console.log('4. PostData长度:', postData.length)
console.log('完整签名字符串前200字符:', signString.substring(0, 200))
const data_buffer = Buffer.from(signString, 'utf-8')
// 使用正确的签名参数
const signature = crypto.sign(
'RSA-SHA256',
data_buffer,
{
key: this.privateKey,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: 32 // 明确指定salt长度,与文档一致
}
)
const signatureBase64 = signature.toString('base64')
console.log('签名生成成功,长度:', signatureBase64.length)
return signatureBase64
} catch (error) {
console.error('签名生成失败:', error)
throw error
}
}
}
module.exports = WechatAPISignature

没人吗,找人家客服弄了两天还是让我发帖